private static function load_option_types() { $query = 'get_option_types'; $result = DataSource::getInstance()->epp($query); foreach ($result as $type) { self::$types[$type['id']] = new ModelOptionType($type['id'], $type['units'], $type['countries']); } }
/** * returns the model to the selected ship * * @param $id_unit int * @return ModelShip * @throws NullPointerException */ public static function getModelById($id_unit) { $id_unit = intval($id_unit); if (isset(self::$units[$id_unit])) { return self::$units[$id_unit]; } $query = 'get_ship'; $dict = array(':id_unit' => $id_unit); $result = DataSource::getInstance()->epp($query, $dict); if (empty($result)) { throw new NullPointerException("Ship with id {$id_unit} not found."); } $unit = $result[0]; return self::$units[$id_unit] = new ModelShip($unit['id'], $unit['name'], $unit['abbreviation'], $unit['price'], $unit['speed'], $unit['id_type'], $unit['tanksize'], $unit['hitpoints']); }
/** * returns the model to the selected land unit * * @param $id_unit int * @throws NullPointerException * @return ModelLandUnit */ public static function getModelById($id_unit) { $id_unit = intval($id_unit); if (isset(self::$units[$id_unit])) { return self::$units[$id_unit]; } $query = 'get_land_unit'; $dict = array(':id_unit' => $id_unit); $result = DataSource::getInstance()->epp($query, $dict); if (empty($result)) { throw new NullPointerException('Unit not found.'); } $unit = $result[0]; self::$units[$id_unit] = new ModelLandUnit($unit['id'], $unit['name'], $unit['abbreviation'], $unit['price'], $unit['speed'], $unit['id_type'], $unit['killing_sequence'], $unit['kill_sequence_offset'], $unit['ship_takeover']); return self::$units[$id_unit]; }
/** * returns all startregions for the given set * * @param $id_set int * @throws NullPointerException * @return array(int id_opttype => array(int option_number => array(int id_area => ModelStartRegion))) */ public static function getRegionsForSet($id_set) { $id_set = intval($id_set); if (isset(self::$regions[$id_set])) { return self::$regions[$id_set]; } $query = 'get_startregions_for_set'; $dict[':id_set'] = $id_set; $result = DataSource::getInstance()->epp($query, $dict); if (empty($result)) { throw new NullPointerException('Set not found.'); } foreach ($result as $region) { self::$regions[$id_set][$region['id_optiontype']][$region['options']][$region['id_area']] = new ModelStartRegion($region['id'], $region['id_area'], $region['id_optiontype'], $region['id_set'], $region['options']); } return self::$regions[$id_set]; }
/** * creates a game-area or updates all infos if already created * * @param $id_game int * @param $tank int * @param $id_user int * @param $id_area int * @param $id_resource * @param $productivity int * @return ModelGameArea */ public static function setGameArea($id_game, $tank, $id_user, $id_area, $id_resource, $productivity) { $id_game = intval($id_game); $tank = intval($tank); $id_user = intval($id_user); $id_area = intval($id_area); $id_resource = intval($id_resource); $productivity = intval($productivity); if (self::checkArea($id_game, $id_area)) { $area = self::getGameAreaForArea($id_game, $id_area); $area->setTank($tank); $area->setIdUser($id_user); $area->setIdResource($id_resource); $area->setProductivity($productivity); return $area; } else { SQLCommands::init($id_game); $query = 'create_zarea'; $dict = array(); $dict[':tank'] = $tank; $dict[':id_user'] = $id_user; $dict[':id_area'] = $id_area; $dict[':id_resource'] = $id_resource; $dict[':productivity'] = $productivity; DataSource::getInstance()->epp($query, $dict); return self::getGameAreaForArea($id_game, $id_area); } }
/** * @return array(int $id_adjacent_area) */ public function getAdjecents() { if (!empty($this->adjecents)) { return $this->adjecents; } // load a2a $query = 'get_a2a'; $dict = array(':id_area' => $this->id); $result = DataSource::getInstance()->epp($query, $dict); foreach ($result as $line) { $this->adjecents[] = $line['id_adjacent_area']; } return $this->adjecents; }
/** * creates land move for user * * @param $id_user int * @param $id_game int * @param $round int * @param $steps array(int step_nr => int id_zarea) -> step_nr counting from 1 to x * @param $units array(int id_unit => count) * @throws NullPointerException * @throws ModelException * @return ModelLandMove */ public static function createLandMove($id_user, $id_game, $round, $steps, $units) { SQLCommands::init(intval($id_game)); // CREATE MOVE $query = 'create_move'; $dict = array(); $dict[':id_user'] = intval($id_user); $dict[':id_phase'] = PHASE_LANDMOVE; $dict[':round'] = $round; DataSource::Singleton()->epp($query, $dict); $id_move = DataSource::getInstance()->getLastInsertId(); try { // INSERT MOVE STEPS $x = 0; foreach ($steps as $step => $id_zarea) { ++$x; ModelGameArea::getGameArea((int) $id_game, (int) $id_zarea); if (!isset($steps[$x])) { throw new ModelException('Cannot create landmove, steps not consistent.'); } $query = 'insert_area_for_move'; $dict = array(); $dict[':id_move'] = intval($id_move); $dict[':step'] = intval($step); $dict[':id_zarea'] = intval($id_zarea); DataSource::Singleton()->epp($query, $dict); } $id_zarea_start = (int) $steps[1]; // INSERT UNITS foreach ($units as $id_unit => $count) { ModelLandUnit::getModelById($id_unit); $zUnit = ModelInGameLandUnit::getModelByIdZAreaUserUnit((int) $id_game, $id_zarea_start, (int) $id_user, (int) $id_unit); $query = 'insert_land_units_for_move'; $dict = array(); $dict[':id_zunit'] = $zUnit->getId(); $dict[':id_move'] = intval($id_move); $dict[':count'] = intval($count); DataSource::Singleton()->epp($query, $dict); } } catch (ModelException $ex) { self::flagMoveDeleted(); throw $ex; } catch (NullPointerException $ex) { self::flagMoveDeleted(); throw $ex; } return self::$moves[$id_game][$id_move] = new ModelLandMove((int) $id_user, (int) $id_game, PHASE_LANDMOVE, (int) $id_move, (int) $round, false, $steps, $units); }
/** * updates selected areas if necessary * * @param $option_number int * @param $zareas array(int id_zarea) * @return void */ public function setRegions($option_number, $zareas) { $option_number = intval($option_number); // check if areas are already set if (isset($this->regions[$option_number])) { $found_all = true; foreach ($zareas as $id_zarea) { if (!in_array($id_zarea, $this->regions[$option_number])) { $found_all = false; } } if ($found_all) { return; } } $dict = array(); $dict[':id_move'] = $this->id; $dict[':step'] = $option_number; if (isset($this->regions[$option_number])) { unset($this->regions[$option_number]); $query = 'delete_move_areas_for_step'; DataSource::Singleton()->epp($query, $dict); } // insert areas $query = 'insert_area_for_move'; foreach ($zareas as $id_zarea) { $dict[':id_zarea'] = $id_zarea; DataSource::getInstance()->epp($query, $dict); } $this->regions[$option_number] = $zareas; }
private static function createModel($id_game, $id_zarea, $id_user, $id_unit) { SQLCommands::init($id_game); $query = 'create_unit_for_zarea_user'; $dict = array(); $dict[':id_user'] = $id_user; $dict[':id_zarea'] = $id_zarea; $dict[':id_unit'] = $id_unit; $dict[':count'] = 0; DataSource::getInstance()->epp($query, $dict); $id = DataSource::getInstance()->getLastInsertId(); self::$units[$id_game][$id_zarea][$id_user][$id_unit] = new ModelInGameLandUnit($id, $id_unit, $id_user, $id_zarea, 0, $id_game); }
private function create_info($id_phase) { $query = 'set_new_game_phase_info'; $dict = array(); $dict[':id_user'] = $this->id_user; $dict[':id_game'] = $this->id_game; $dict[':id_phase'] = $id_phase; if ($this->id_game === 0) { $dict[':rule'] = true; $dict[':is_ready'] = false; } else { $_Rule = ModelInGamePhaseInfo::getInGamePhaseInfo($this->id_user); $dict[':rule'] = $_Rule->getNotificationRuleForPhase($id_phase); $dict[':is_ready'] = $_Rule->getIsReadyForPhase($id_phase); } DataSource::getInstance()->epp($query, $dict); $this->notification_rules[$id_phase] = $dict[':rule']; $this->is_ready[$id_phase] = $dict[':is_ready']; }
/** * @param $id_user int * @param $id_game int * @param $id_zarea_in_port int * @param $id_zarea int * @param $id_unit int * @param $name string * @return ModelSetShipsMove * @throws ModelException * @throws DataSourceException */ public static function createSetShipsMove($id_user, $id_game, $id_zarea_in_port, $id_zarea, $id_unit, $name) { $id_user = (int) $id_user; $id_game = (int) $id_game; $id_zarea_in_port = (int) $id_zarea_in_port; $id_zarea = (int) $id_zarea; $id_unit = (int) $id_unit; SQLCommands::init($id_game); // 1. check if name is available $query = 'get_ingame_ship_by_name'; $dict = array(); $dict[':name'] = $name; $result = DataSource::Singleton()->epp($query, $dict); if (!empty($result)) { throw new ModelException('Name already taken.'); } // 2. create ship $ship = ModelInGameShip::createShip($id_user, $id_game, $id_unit, 0, $name, 0); $id_zunit = $ship->getId(); // 3. create new move $query = 'create_move'; $dict = array(); $dict[':id_user'] = $id_user; $dict[':id_phase'] = PHASE_SETSHIPS; $dict[':round'] = 0; DataSource::Singleton()->epp($query, $dict); $id_move = (int) DataSource::getInstance()->getLastInsertId(); // 4. set areas $query = 'insert_area_for_move'; $dict = array(); $dict[':id_move'] = $id_move; $dict[':step'] = 0; $dict[':id_zarea'] = $id_zarea_in_port; DataSource::Singleton()->epp($query, $dict); $dict[':step'] = 1; $dict[':id_zarea'] = $id_zarea; DataSource::Singleton()->epp($query, $dict); // 5. add new ship to move $query = 'insert_ship_for_move'; $dict = array(); $dict[':id_move'] = $id_move; $dict[':id_zunit'] = $id_zunit; DataSource::Singleton()->epp($query, $dict); return self::getSetShipsMove($id_game, $id_move); }
private function nextRound() { ++$this->round; $query = 'set_game_round'; $dict = array(); $dict[':id_game'] = $this->id; $dict[':round'] = $this->round; DataSource::getInstance()->epp($query, $dict); }
public function run(array &$data) { $game = ModelGame::getCurrentGame(); $id_game = $game->getId(); SQLCommands::init($id_game); // running game (or newly started but countries are already picked) if ($game->getStatus() === GAME_STATUS_RUNNING || $game->getStatus() === GAME_STATUS_STARTED && $game->getIdPhase() === PHASE_SETSHIPS) { $query = 'get_map_for_running_game'; } else { if ($game->getStatus() === GAME_STATUS_STARTED && $game->getIdPhase() === PHASE_SELECTSTART) { $query = 'get_map_for_new_game'; } else { throw new MapException('invalid game selected: ' . $id_game); } } $result = DataSource::getInstance()->epp($query); $countryData = array(); foreach ($result as $country) { // newly started game countries have to be picked -> no landunits/ships available if (array_key_exists('countrySelectOption', $country)) { $countryData[] = $country; continue; } // running game (or newly started but countries are already picked) // check landunits $unitCount = 0; $id_user = (int) $country['id_user']; if ($id_user <= 0) { $id_user = NEUTRAL_COUNTRY; } $units = ModelInGameLandUnit::getUnitsByIdZAreaUser($id_game, (int) $country['id'], $id_user); $unitsViewData = array(); /* @var $unit ModelInGameLandUnit */ foreach ($units as $unit) { $unitCount += $unit->getCount(); $landUnit = ModelLandUnit::getModelById($unit->getIdUnit()); $unitViewData = array('name' => $landUnit->getName(), 'count' => $unit->getCount()); $unitsViewData[] = $unitViewData; } if ($unitCount > 0) { $country['units'] = $unitsViewData; } $country['unitCount'] = $unitCount; // check ships $shipCount = 0; $shipViewData = array(); if ((int) $country['area_type'] === TYPE_LAND) { $ships = ModelInGameShip::getShipsInPort($id_game, (int) $country['id_zarea']); } else { $ships = ModelInGameShip::getShipsInAreaNotInPort($id_game, (int) $country['id_zarea']); } while ($ships->hasNext()) { /* @var $ship ModelInGameShip */ $ship = $ships->next(); $id_ship_owner = $ship->getIdUser(); if (!isset($shipViewData[$id_ship_owner])) { $shipViewData[$id_ship_owner] = array('username' => ModelUser::getUser($id_ship_owner)->getLogin(), 'ships' => array()); } $shipType = ModelShip::getModelById($ship->getIdUnit()); $currShipViewData = array('name' => $ship->getName(), 'type' => $shipType->getName(), 'diveStatus' => $ship->getDiveStatus(), 'experience' => $ship->getExperience()); if ((int) $country['area_type'] === TYPE_LAND) { $portToArea = ModelGameArea::getGameArea($id_game, $ship->getIdZArea()); $currShipViewData['port'] = $portToArea->getName(); $currShipViewData['portNumber'] = $portToArea->getNumber(); } $shipViewData[$id_ship_owner]['ships'][] = $currShipViewData; ++$shipCount; } if ($shipCount > 0) { $country['ships'] = $shipViewData; } $country['shipCount'] = $shipCount; $countryData[] = $country; } $data['countryData'] = $countryData; return $data; }
/** * creates land move for user * * @param $id_user int * @param $id_game int * @param $round int * @param $id_zarea int id_zarea * @param $units array(int id_unit => count) * @throws NullPointerException * @throws ModelException * @return ModelProductionMove */ public static function createProductionMove($id_user, $id_game, $round, $id_zarea, $units) { SQLCommands::init(intval($id_game)); // CREATE MOVE $query = 'create_move'; $dict = array(); $dict[':id_user'] = intval($id_user); $dict[':id_phase'] = PHASE_PRODUCTION; $dict[':round'] = $round; DataSource::Singleton()->epp($query, $dict); $id_move = DataSource::getInstance()->getLastInsertId(); try { // INSERT MOVE STEPS ModelGameArea::getGameArea((int) $id_game, (int) $id_zarea); $query = 'insert_area_for_move'; $dict = array(); $dict[':id_move'] = intval($id_move); $dict[':step'] = intval(1); $dict[':id_zarea'] = intval($id_zarea); DataSource::Singleton()->epp($query, $dict); // INSERT UNITS foreach ($units as $id_unit => $count) { ModelLandUnit::getModelById($id_unit); $zUnit = ModelInGameLandUnit::getModelByIdZAreaUserUnit((int) $id_game, $id_zarea, (int) $id_user, (int) $id_unit); $query = 'insert_land_units_for_move'; $dict = array(); $dict[':id_zunit'] = $zUnit->getId(); $dict[':id_move'] = intval($id_move); $dict[':count'] = intval($count); DataSource::Singleton()->epp($query, $dict); } } catch (ModelException $ex) { self::flagMoveDeleted(); throw $ex; } catch (NullPointerException $ex) { self::flagMoveDeleted(); throw $ex; } return self::$moves[$id_game][$id_move] = new ModelProductionMove((int) $id_user, (int) $id_game, PHASE_PRODUCTION, (int) $id_move, (int) $round, false, $id_zarea, $units); }
/** * @param int $id_zarea_in_port * @throws ModelException */ public function setIdZareaInPort($id_zarea_in_port = null) { if ($id_zarea_in_port !== null) { $id_zarea_in_port = intval($id_zarea_in_port); $port_zarea = ModelGameArea::getGameArea($this->id_game, $id_zarea_in_port); $sea_zarea = ModelGameArea::getGameArea($this->id_game, $this->id_zarea); if (!in_array($port_zarea->getIdArea(), $sea_zarea->getAdjecents())) { throw new ModelException('Invalid port -> not adjacent to current area.'); } } SQLCommands::init($this->id_game); $query = 'set_ship_in_port'; $dict = array(':id_zunit' => $this->id, ':id_zarea_in_port' => $id_zarea_in_port); DataSource::getInstance()->epp($query, $dict); $this->id_zarea_in_port = $id_zarea_in_port; }
/** * flag move as deleted * @return void */ public function flagMoveDeleted() { SQLCommands::init(intval($this->id_game)); $query = 'flag_move_deleted'; $dict = array(':id_move' => $this->id); DataSource::getInstance()->epp($query, $dict); $this->deleted = true; }
protected function rollback() { // something went wrong, rollback DataSource::getInstance()->rollBack(); }
private function fill_member_vars() { $query = 'get_iig_info_for_user_in_game'; $dict = array(); $dict[':id_user'] = $this->id_user; $dict[':id_game'] = $this->id_game; $result = DataSource::getInstance()->epp($query, $dict); if (empty($result)) { throw new NullPointerException('User is not in this game!'); } $data = $result[0]; $this->id_color = $data['id_color']; $this->money = $data['money']; $this->id_set = $data['id_set']; }