protected function checkCurrentPhase(array &$data, $id_phase)
 {
     $game = ModelGame::getCurrentGame();
     if ($game->getIdPhase() !== (int) $id_phase) {
         $data['notCurrentPhase'] = true;
         return true;
     }
     return false;
 }
 public function run(array &$data)
 {
     $data['template'] = $this->getTemplate();
     $this->addCurrentGameInfo($data);
     $this->moveController = new SetShipsController(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId());
     // update moves
     if (isset($_POST['setship'])) {
         $this->setShip($data);
     }
     if (isset($_POST['fixate_start'])) {
         $this->fixateMove($data);
     }
     if (isset($_POST['delete'])) {
         $this->deleteMove($data);
     }
     // show already set ships
     $data['currentShips'] = array();
     $iterator = ModelSetShipsMove::getSetShipMovesForUser(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId());
     while ($iterator->hasNext()) {
         /** @var ModelSetShipsMove $move */
         $move = $iterator->next();
         $zShip = ModelInGameShip::getShipById(ModelGame::getCurrentGame()->getId(), $move->getIdZunit());
         $id_ship = $zShip->getIdUnit();
         $ship = ModelShip::getModelById($id_ship);
         $zAreaInPort = ModelGameArea::getGameArea(ModelGame::getCurrentGame()->getId(), $move->getIdZareaInPort());
         $zAreaAtSea = ModelGameArea::getGameArea(ModelGame::getCurrentGame()->getId(), $move->getIdZarea());
         $data['currentShips'][] = array('id' => $move->getId(), 'ship_type' => $ship->getName(), 'ship_name' => $zShip->getName(), 'zarea_in_port' => $zAreaInPort->getName() . ' ' . $zAreaInPort->getNumber(), 'zarea_at_sea' => $zAreaAtSea->getName() . ' ' . $zAreaAtSea->getNumber());
     }
     // show still available ships
     $data['availableShips'] = array();
     $stillAvailableShips = $this->moveController->getStillAvailableShips();
     foreach ($stillAvailableShips as $id_unit => $count) {
         if ($count <= 0) {
             continue;
         }
         $data['availableShips'][] = array('id' => $id_unit, 'count' => $count, 'name' => ModelShip::getModelById($id_unit)->getName());
     }
     // show available countries
     $data['availableZAreasInPort'] = array();
     $data['availableZAreasAtSea'] = array();
     $iterator = ModelGameArea::iterator(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId());
     while ($iterator->hasNext()) {
         /** @var ModelGameArea $zArea */
         $zArea = $iterator->next();
         $data['availableZAreasInPort'][] = array('id_zarea_in_port' => $zArea->getId(), 'name' => $zArea->getName(), 'number' => $zArea->getNumber());
     }
     $iterator = ModelArea::iterator(TYPE_SEA);
     while ($iterator->hasNext()) {
         /** @var ModelArea $area */
         $area = $iterator->next();
         $data['availableZAreasAtSea'][] = array('id_zarea_at_sea' => ModelGameArea::getGameAreaForArea(ModelGame::getCurrentGame()->getId(), $area->getId())->getId(), 'name' => $area->getName(), 'number' => $area->getNumber());
     }
     $this->checkFixate($data, PHASE_SETSHIPS);
     $this->checkCurrentPhase($data, PHASE_SETSHIPS);
 }
 protected function checkAuth($required_session)
 {
     $current_user = Model\User\ModelUser::getCurrentUser();
     $status = $current_user->getStatus();
     $current_game = Model\Game\ModelGame::getCurrentGame();
     switch ($required_session) {
         case CHECK_SESSION_NONE:
             break;
         case CHECK_SESSION_USER:
             if ($status !== STATUS_USER_ACTIVE && $status !== STATUS_USER_ADMIN && $status !== STATUS_USER_MODERATOR) {
                 throw new SessionException('No active user.');
             }
             break;
         case CHECK_SESSION_ADMIN:
             if ($status !== STATUS_USER_ADMIN) {
                 throw new SessionException('Non-admin tried to login at content: ' . $this->getName() . '.');
             }
             break;
         case CHECK_SESSION_MOD:
             if ($status !== STATUS_USER_ADMIN && $status !== STATUS_USER_MODERATOR) {
                 throw new SessionException('Non-moderator tried to login at content: ' . $this->getName() . '.');
             }
             break;
         case CHECK_SESSION_GAME:
             if ($current_game === null) {
                 throw new SessionException('Choose game first');
             }
             break;
         case CHECK_SESSION_GAME_START:
             if ($current_game === null) {
                 throw new SessionException('Choose game first');
             }
             if ($current_game->getStatus() !== GAME_STATUS_STARTED) {
                 throw new SessionException('Game is not in the starting phase.');
             }
             break;
         case CHECK_SESSION_GAME_RUNNING:
             if ($current_game === null) {
                 throw new SessionException('Choose game first');
             }
             if ($current_game->getStatus() !== GAME_STATUS_RUNNING) {
                 throw new SessionException('Game is not running.');
             }
             break;
         default:
             throw new SessionException('Invalid Session Type.');
     }
 }
 public function getUserInfo($ingame)
 {
     $user = ModelUser::getUser($ingame->getIdUser());
     // money on bank
     $money = $ingame->getMoney();
     // money from resources
     $resproduction = 0;
     $combos = array();
     $combos[RESOURCE_OIL] = 0;
     $combos[RESOURCE_TRANSPORT] = 0;
     $combos[RESOURCE_INDUSTRY] = 0;
     $combos[RESOURCE_MINERALS] = 0;
     $combos[RESOURCE_POPULATION] = 0;
     $iter = ModelGameArea::iterator($user->getId(), ModelGame::getCurrentGame()->getId());
     while ($iter->hasNext()) {
         $area = $iter->next();
         $resproduction += $area->getProductivity();
         $combos[$area->getIdResource()]++;
     }
     // money from traderoutes
     $traderoutes = 0;
     // money from combos
     $combo_count = $combos[RESOURCE_OIL];
     foreach ($combos as $res) {
         if ($res < $combo_count) {
             $combo_count = $res;
         }
     }
     $combo_money = $combo_count * 4;
     // sum
     $sum = $money + $resproduction + $traderoutes + $combo_money;
     $userData = array();
     $userData['login'] = $user->getLogin();
     $userData['money'] = $money;
     $userData['resproduction'] = $resproduction;
     $userData['trproduction'] = $traderoutes;
     $userData['comboproduction'] = $combo_money;
     $userData['sum'] = $sum;
     return $userData;
 }
 public static function parseCurrentUser(array &$data)
 {
     $user = ModelUser::getCurrentUser()->getViewData();
     $currGame = ModelGame::getCurrentGame();
     $user['games'] = array();
     $iter = ModelIsInGameInfo::iterator(ModelUser::getCurrentUser()->getId());
     while ($iter->hasNext()) {
         $gameModel = ModelGame::getGame($iter->next()->getIdGame());
         if ($gameModel->getStatus() !== GAME_STATUS_STARTED && $gameModel->getStatus() !== GAME_STATUS_RUNNING) {
             continue;
         }
         $game = array('id' => $gameModel->getId(), 'name' => $gameModel->getName());
         if ($currGame !== null && $currGame === $gameModel) {
             $game['selected'] = true;
         }
         $user['games'][] = $game;
     }
     if ($currGame === null) {
         $user['noGameSelected'] = true;
     } else {
         $user['currGame'] = $currGame->getViewData();
     }
     $data['user'] = $user;
 }
 private function handleInput(array &$data)
 {
     if (empty($_POST)) {
         return;
     }
     $controller = new LandMoveController(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId());
     // fixating land move
     if (isset($_POST['fixate_land_move'])) {
         $controller->finishMove();
         return;
     }
     // deleting land move
     if (isset($_POST['delete'])) {
         try {
             $controller->deleteLandMove(intval($_POST['delete']));
             $data['status'] = array('message' => 'Landzug gelöscht.');
         } catch (NullPointerException $ex) {
             $data['errors'] = array('message' => $ex->getMessage());
         } catch (ControllerException $ex) {
             $data['errors'] = array('message' => $ex->getMessage());
         } finally {
             return;
         }
     }
     // creating new land move
     if (isset($_POST['newmove'])) {
         try {
             if (!isset($_POST['start']) || !isset($_POST['destination'])) {
                 $data['errors'] = array('message' => 'Missing parameter!');
                 return;
             }
             $units = array();
             $iter = ModelLandUnit::iterator();
             while ($iter->hasNext()) {
                 /* @var $unit ModelLandUnit */
                 $unit = $iter->next();
                 $abbr = $unit->getAbbreviation();
                 $id_unit = $unit->getId();
                 $units[$id_unit] = isset($_POST[$abbr]) ? intval($_POST[$abbr]) : 0;
             }
             $controller->createLandMove(intval($_POST['start']), intval($_POST['destination']), $units);
             $data['status'] = array('message' => 'Landzug erstellt.');
         } catch (NullPointerException $ex) {
             $data['errors'] = array('message' => $ex->getMessage());
         } catch (ControllerException $ex) {
             $data['errors'] = array('message' => $ex->getMessage());
         }
     }
 }
 private function parseOptions(array &$data)
 {
     $viewData = array();
     foreach ($this->possibleStartRegions as $id_option_type => $option_types) {
         $optionType = ModelOptionType::getOptionType($id_option_type);
         foreach ($option_types as $option_number => $options) {
             $optionViewData = array();
             $optionViewData['number'] = $option_number;
             $optionViewData['countrySelectUnitCount'] = $optionType->getUnits();
             $optionViewData['countrySelectCount'] = $optionType->getCountries();
             $optionViewData['areas'] = array();
             foreach ($options as $id_area => $startRegion) {
                 // get area infos
                 $areaModel = ModelArea::getArea($id_area);
                 $area = array();
                 $area['id_area'] = $id_area;
                 $area['number'] = $areaModel->getNumber();
                 $area['name'] = $areaModel->getName();
                 // check if country already selected
                 $gameArea = ModelGameArea::getGameAreaForArea(ModelGame::getCurrentGame()->getId(), $id_area);
                 $id_zarea = $gameArea->getId();
                 $modelMove = ModelSelectStartMove::getSelectStartMoveForUser(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId());
                 if ($modelMove->checkIfAreaIsSelected($option_number, $id_zarea)) {
                     $area['checked'] = true;
                 }
                 $optionViewData['areas'][] = $area;
             }
             $viewData[] = $optionViewData;
         }
     }
     $data['options'] = $viewData;
 }
 private function showNewMove(array &$data)
 {
     $areasViewData = array();
     $areas = ModelArea::iterator(TYPE_LAND);
     while ($areas->hasNext()) {
         /* @var $area ModelArea */
         $area = $areas->next();
         $zArea = ModelGameArea::getGameAreaForArea(ModelGame::getCurrentGame()->getId(), $area->getId());
         $areaViewData = array();
         $areaViewData['id_zarea'] = $zArea->getId();
         $areaViewData['number'] = $area->getNumber();
         $areaViewData['name'] = $area->getName();
         if ($zArea->getIdUser() === ModelUser::getCurrentUser()->getId()) {
             $areasViewData[] = $areaViewData;
         }
     }
     $data['areas'] = $areasViewData;
     $unitsViewData = array();
     $unit_iter = ModelLandUnit::iterator();
     while ($unit_iter->hasNext()) {
         /* @var $unit ModelLandUnit */
         $unit = $unit_iter->next();
         $unitsViewData[] = array('id' => $unit->getId(), 'name' => $unit->getName());
     }
     $data['units'] = $unitsViewData;
 }
示例#9
0
    $data['template'] = 'home';
    $app->render('main.twig', $data);
});
$app->get('/login/', function () use($app, $debug) {
    UserActions::logout();
    $data['template'] = 'login';
    $app->render('main.twig', $data);
});
$app->get('/logout/', function () use($app, $debug) {
    UserActions::logout();
    $app->redirect(ABS_REF_PREFIX);
});
$app->get('/map/', function () use($app, $debug) {
    $data = array();
    HeaderViewHelper::parseCurrentUser($data);
    if (ModelGame::getCurrentGame() === null) {
        $data['errors'] = array('message' => 'select a game first');
        $app->render('error.twig', $data);
        return;
    }
    try {
        $map = new Map();
        $map->run($data);
    } catch (MapException $ex) {
        $data['errors'] = array('message' => $ex->getMessage());
        $app->render('error.twig', $data);
        return;
    }
    $app->render('map.twig', $data);
});
$app->get('/cron(/:id_game)(/)', function ($id_game = null) use($app, $debug, $logger) {
示例#10
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;
 }