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 #2
0
$env['basepath'] = __DIR__;
// DEFINE SLIM-ERROR HANDLING
$app->error(function (\Exception $e) use($app, $logger) {
    $logger->error($e->getMessage());
    $data = array();
    HeaderViewHelper::parseCurrentUser($data);
    $app->render('error.twig', $data);
});
$app->notFound(function () use($app) {
    $data = array();
    HeaderViewHelper::parseCurrentUser($data);
    $app->render('404.twig', $data);
});
// INIT DB-CONNECTION
try {
    Model\DataBase\SQLCommands::init();
} catch (Exceptions\DataSourceException $e) {
    $logger->fatal($e->getMessage());
    $app->render('error.twig');
    die;
}
// DEFINE GET ROUTES
include_once __DIR__ . DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'get.php';
// DEFINE POST ROUTES
include_once __DIR__ . DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'post.php';
// DEFINE CONTENT ROUTES
include_once __DIR__ . DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'content.php';
// SET GLOBAL VARS
$app->view->setData(array('debug' => $debug, 'nomerge' => $nomerge, 'absRefPrefix' => ABS_REF_PREFIX));
Tools\CheckSessions::checkCookies();
Tools\CheckSessions::checkCurrentGame();
 /**
  * creates the start move for user, and returns the id of the new move
  *
  * @param $id_game int
  * @param $id_user int
  * @return int
  */
 private static function createSelectStartMove($id_user, $id_game)
 {
     SQLCommands::init(intval($id_game));
     $query = 'create_move';
     $dict = array();
     $dict[':id_user'] = intval($id_user);
     $dict[':id_phase'] = PHASE_SELECTSTART;
     $dict[':round'] = 0;
     DataSource::Singleton()->epp($query, $dict);
     $id_move = DataSource::getInstance()->getLastInsertId();
     return (int) $id_move;
 }
 /**
  * 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 #5
0
 private static function setGameSpecificQueries($id_game)
 {
     SQLCommands::init($id_game);
 }
 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);
 }
Beispiel #7
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);
 }
 /**
  * @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 #10
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;
 }
 /**
  * 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]);
 }