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 iterator for all land units
  *
  * @return ModelIterator
  */
 public static function iterator()
 {
     $models = array();
     $query = 'get_all_land_units';
     // query units
     $result = DataSource::Singleton()->epp($query);
     foreach ($result as $unit) {
         $id_unit = $unit['id'];
         $models[] = self::getModelById($id_unit);
     }
     return new ModelIterator($models);
 }
 /**
  * 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];
 }
 /**
  * returns the specific model
  *
  * @param $players int
  * @return ModelStartShips
  * @throws NullPointerException
  */
 public static function getStartShipsForPlayers($players)
 {
     $players = (int) $players;
     if (isset(self::$models[$players])) {
         return self::$models[$players];
     }
     // check if there is a game
     $query = 'get_start_ships_for_players';
     $dict = array(':players' => $players);
     $result = DataSource::Singleton()->epp($query, $dict);
     if (empty($result)) {
         throw new NullPointerException('No startships for ' . $players . ' players found.');
     }
     $ships = array();
     foreach ($result as $line) {
         $ships[(int) $line['id_unit']] = (int) $line['numberof'];
     }
     return self::$models[$players] = new ModelStartShips($players, $ships);
 }
 /**
  * returns an iterator for economy models
  * @param $economy_type string/enum
  * @param $random_order boolean - set true to get random order
  * @throws DataSourceException
  * @return ModelIterator
  */
 public static function iterator($economy_type, $random_order = true)
 {
     $models = array();
     $query = 'get_resource_allocation';
     $dict = array();
     $dict[':economy'] = $economy_type;
     // query phases
     $result = DataSource::Singleton()->epp($query, $dict);
     foreach ($result as $alloc) {
         $id_res = $alloc['id_resource'];
         $res_pow = $alloc['res_power'];
         $count = $alloc['count'];
         for ($x = 0; $x < $count; $x++) {
             $models[] = new ModelEconomy($id_res, $res_pow);
         }
     }
     if ($random_order) {
         shuffle($models);
     }
     return new ModelIterator($models);
 }
 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'];
 }
 /**
  * permenantly remove move (and corresponding ship) from database
  *
  * @param ModelSetShipsMove $move
  * @throws NullPointerException
  */
 public static function deleteSetShipsMove(ModelSetShipsMove $move)
 {
     $id_game = $move->getIdGame();
     $id_move = $move->getId();
     SQLCommands::init($id_game);
     // 1. delete ship from DB
     ModelInGameShip::deleteShip($id_game, $move->getIdZunit());
     // 2. delete ship for move
     $query = 'delete_units_for_move';
     $dict = array();
     $dict[':id_move'] = $id_move;
     DataSource::Singleton()->epp($query, $dict);
     // 3. delete areas for move
     $query = 'delete_move_areas_for_move';
     DataSource::Singleton()->epp($query, $dict);
     // 4. delete move
     $query = 'delete_move';
     DataSource::Singleton()->epp($query, $dict);
     unset(self::$moves[$id_game][$id_move]);
 }
Beispiel #9
0
 private function fill_member_vars()
 {
     // check if there is a game
     $result = DataSource::Singleton()->epp('get_all_area_info', array(':id_area' => $this->id));
     if (empty($result)) {
         return false;
     }
     $data = $result[0];
     $this->name = $data['name'];
     $this->number = $data['number'];
     $this->coords_small = $data['coords_small'];
     $this->x = (int) $data['x'];
     $this->y = (int) $data['y'];
     $this->x2 = (int) $data['x2'];
     $this->y2 = (int) $data['y2'];
     $this->xres = (int) $data['xres'];
     $this->yres = (int) $data['yres'];
     $this->height = (int) $data['height'];
     $this->width = (int) $data['width'];
     $this->tanksize = (int) $data['tanksize'];
     $this->id_type = (int) $data['id_type'];
     $this->zone = (int) $data['zone'];
     $this->economy = $data['economy'];
     return true;
 }
Beispiel #10
0
 private function fill_member_vars()
 {
     // check if there is a game
     $result = DataSource::Singleton()->epp('get_color', array(':id_color' => $this->id));
     if (empty($result)) {
         return false;
     }
     $data = $result[0];
     $this->name = $data['name'];
     $this->color = $data['color'];
     return true;
 }
Beispiel #11
0
 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);
 }
Beispiel #13
0
 private function fill_member_vars()
 {
     $result = DataSource::Singleton()->epp('get_all_user_data', array(':id_user' => $this->id));
     if (empty($result)) {
         return false;
     }
     $data = $result[0];
     $this->given_name = $data['name'];
     $this->last_name = $data['lastname'];
     $this->login = $data['login'];
     $this->email = $data['email'];
     $this->status = $data['status'];
     $this->verify = $data['verify'];
     $this->token = $data['token'];
     return true;
 }
 /**
  * @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;
 }
Beispiel #15
0
 /**
  * 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;
 }
 /**
  * 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;
 }
 /**
  * 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);
 }
Beispiel #18
0
 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);
 }
 private function fill_member_vars()
 {
     SQLCommands::init($this->id_game);
     // check if there is a game
     $result = DataSource::Singleton()->epp('get_all_zarea_info', array(':id_zarea' => $this->id));
     if (empty($result)) {
         return false;
     }
     $data = $result[0];
     $this->tank = (int) $data['tank'];
     $this->id_user = (int) $data['id_user'];
     $this->id_area = (int) $data['id_area'];
     $this->id_resource = (int) $data['id_resource'];
     $this->productivity = (int) $data['productivity'];
     return true;
 }
Beispiel #20
0
 private function fill_member_vars()
 {
     // check if there is a game
     $result = DataSource::Singleton()->epp('get_phase_info', array(':id_phase' => $this->id));
     if (empty($result)) {
         return false;
     }
     $data = $result[0];
     $this->name = $data['name'];
     $this->label = $data['label'];
     $this->id_type = $data['id_type'];
     return true;
 }
Beispiel #21
0
 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'];
 }