Пример #1
0
 public static function checkCurrentGame()
 {
     // check if a user is logged in
     if (ModelUser::getCurrentUser()->getId() <= 0) {
         unset($_SESSION['id_game']);
         return;
     }
     // check if user selected another game
     if (isset($_POST['select_game'])) {
         $_SESSION['id_game'] = (int) $_POST['select_game'];
     }
     if (!isset($_SESSION['id_game'])) {
         return;
     }
     // check if game exists and is running/started
     try {
         $game = ModelGame::getGame($_SESSION['id_game']);
     } catch (NullPointerException $ex) {
         unset($_SESSION['id_game']);
         return;
     }
     if ($game->getStatus() !== GAME_STATUS_STARTED && $game->getStatus() !== GAME_STATUS_RUNNING) {
         unset($_SESSION['id_game']);
         return;
     }
     // check if user is in this game
     try {
         ModelIsInGameInfo::getIsInGameInfo(ModelUser::getCurrentUser()->getId(), $_SESSION['id_game']);
     } catch (NullPointerException $ex) {
         unset($_SESSION['id_game']);
         return;
     }
     // set current game
     ModelGame::setCurrentGame($_SESSION['id_game']);
 }
Пример #2
0
 /**
  * try to create a new game, returns the game as model on success
  *
  * @param string $game_name
  * @param int $players
  * @param string $password1
  * @param string $password2
  * @param bool $creator_joins
  * @param int $id_color
  * @throws GameCreationException
  * @return ModelGame
  * @throws JoinUserException
  */
 public function create($game_name, $players, $password1, $password2, $creator_joins, $id_color)
 {
     $id_color = intval($id_color);
     $players = intval($players);
     if (empty($game_name) || empty($players)) {
         throw new GameCreationException('Fill in name and players.');
     }
     if (!preg_match("/^([a-zA-Z0-9]+[a-zA-Z0-9' -]+[a-zA-Z0-9']+)?\$/", $game_name)) {
         throw new GameCreationException('Invalid name, only use those letters: a-Z 0-9 \'-');
     }
     if (!preg_match("/[2-6]{1}/", $players)) {
         throw new GameCreationException('Invalid number of players.');
     }
     if ($password1 !== $password2) {
         throw new GameCreationException('Passwords have to match.');
     }
     if (!preg_match("/^([a-zA-Z0-9\$%'-]{5,})?\$/", $password1)) {
         throw new GameCreationException('Invalid password. At least 5 of the following letters: a-Z 0-9 $%\'-');
     }
     $game = ModelGame::createGame($game_name, $players, ModelUser::getCurrentUser()->getId(), $password1);
     // join user
     if ($creator_joins) {
         ModelIsInGameInfo::joinGame(ModelUser::getCurrentUser()->getId(), $game->getId(), $id_color);
     }
     return $game;
 }
Пример #3
0
 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);
 }
 /**
  * returns the specific model, if no id_game given, default rules are loaded (id_game == 0)
  *
  * @param $id_user int
  * @param $id_game int
  * @throws NullPointerException
  * @return ModelInGamePhaseInfo
  */
 private function __construct($id_user, $id_game = null)
 {
     if ($id_game === null) {
         $id_game = 0;
     }
     $this->id_user = intval($id_user);
     $this->id_game = intval($id_game);
     // check if user exists
     ModelUser::getUser($id_user);
     $this->fill_member_vars();
 }
 /**
  * trys to leave the given game, returns true on success
  *
  * @param int $id_game
  * @throws ControllerException, NullPointerException
  * @return boolean
  */
 public function leaveGame($id_game)
 {
     $game = ModelGame::getGame($id_game);
     if ($game->getStatus() !== GAME_STATUS_NEW) {
         throw new ControllerException('Can\'t leave game. It has allready started.');
     }
     if ($game->checkProcessing()) {
         throw new ControllerException('Can\'t leave game. It has allready started.');
     }
     ModelIsInGameInfo::leaveGame(ModelUser::getCurrentUser()->getId(), $id_game);
     return true;
 }
Пример #6
0
 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.');
     }
 }
Пример #7
0
 private function parseGame(array &$data)
 {
     $gameinfo = array();
     $gameinfo['name'] = $this->game->getName();
     $gameinfo['creator'] = $this->game->getCreator()->getLogin();
     $gameinfo['id'] = $this->game->getId();
     $gameinfo['passwordProtected'] = $this->game->checkPasswordProtection();
     $players = array();
     $iter_players = ModelUser::iterator(STATUS_USER_ALL, $this->id_game);
     while ($iter_players->hasNext()) {
         $players[] = $iter_players->next()->getLogin();
     }
     $gameinfo['players'] = $players;
     $gameinfo['availColors'] = $this->game->getFreeColors();
     $data['game'] = $gameinfo;
 }
 /**
  * returns the corresponding model -> creates it if necessary
  *
  * @param $id_game int
  * @param $id_user int
  * @return ModelIterator
  * @throws NullPointerException
  */
 public static function getSetShipMovesForUser($id_user, $id_game)
 {
     SQLCommands::init(intval($id_game));
     $query = 'get_specific_moves';
     $dict = array();
     $dict[':id_user'] = intval($id_user);
     $dict[':id_phase'] = PHASE_SETSHIPS;
     $dict[':round'] = 0;
     $result = DataSource::getInstance()->epp($query, $dict);
     ModelUser::getUser($id_user);
     if (empty($result)) {
         return new ModelIterator(array());
     }
     $moves = array();
     foreach ($result as $move) {
         $id_move = $move['id'];
         $moves[] = self::getSetShipsMove($id_game, $id_move);
     }
     return new ModelIterator($moves);
 }
Пример #9
0
 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;
 }
Пример #10
0
 private function createGame(array &$data)
 {
     if (!isset($_POST['name']) || empty($_POST['name'])) {
         throw new GameCreationException('Missing game name.');
     }
     if (!isset($_POST['players'])) {
         throw new GameCreationException('Missing players.');
     }
     if (!isset($_POST['password1'])) {
         $_POST['password1'] = '';
     }
     if (!isset($_POST['password2'])) {
         $_POST['password2'] = '';
     }
     if (!isset($_POST['color'])) {
         throw new GameCreationException('Missing color.');
     }
     $creator_joins = isset($_POST['play']);
     $gamesModeration = new GamesModeration(ModelUser::getCurrentUser()->getId());
     $gamesModeration->create($_POST['name'], $_POST['players'], $_POST['password1'], $_POST['password2'], $creator_joins, $_POST['color']);
     $data['status'] = array('message' => 'Game successfully created.');
     return $data;
 }
Пример #11
0
 private function do_creator_action(array &$data)
 {
     $gamesModeration = new GamesModeration(ModelUser::getCurrentUser()->getId());
     if (isset($_POST['kick'])) {
         $gamesModeration->kickUser(intval($_POST['kick']), $this->id_game);
         $data['status'] = array('message' => 'User successfully kicked.');
         return;
     }
     if (isset($_POST['change_pw'])) {
         $gamesModeration->changePassword($_POST['password1'], $_POST['password2'], $this->id_game);
         $data['status'] = array('message' => 'Password successfully changed.');
         return;
     }
     if (isset($_POST['start'])) {
         $gamesModeration->startGame($this->id_game);
         $data['status'] = array('message' => 'Game successfully started.');
         return;
     }
     if (isset($_POST['delete_affirmed'])) {
         $gamesModeration->deleteGame($this->id_game);
         $data['status'] = array('message' => 'Game successfully deleted.');
         return;
     }
 }
Пример #12
0
 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 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;
 }
Пример #14
0
<?php

namespace AttOn;

use Logger;
use Slim\Slim;
/* @var $app Slim */
/* @var $debug bool */
global $app, $debug;
$app->post('/login/', function () use($app, $debug) {
    // logout user if logged in
    Controller\User\UserActions::logout();
    try {
        $username = isset($_POST['username']) ? $_POST['username'] : '';
        $password = isset($_POST['password']) ? $_POST['password'] : '';
        $remember = isset($_POST['remember']) ? true : false;
        Controller\User\UserActions::login($username, $password, $remember);
        // successfully logged in, redirect to main route
        $app->redirect(ABS_REF_PREFIX);
    } catch (Exceptions\LoginException $ex) {
        $user = Model\User\ModelUser::getCurrentUser();
        $data = array();
        $data['user'] = $user->getViewData();
        $data['errors'] = array('login' => $ex->getMessage());
        if (isset($_POST['username']) && !empty($_POST['username'])) {
            $data['user']['username'] = $_POST['username'];
        }
        $data['template'] = 'login';
        $app->render('main.twig', $data);
    }
});
 /**
  * returns the corresponding model -> creates it if necessary
  *
  * @param $id_game int
  * @param $id_user int
  * @throws NullPointerException
  * @return ModelSelectStartMove
  */
 public static function getSelectStartMoveForUser($id_user, $id_game)
 {
     SQLCommands::init(intval($id_game));
     $query = 'get_start_move_for_user';
     $dict = array();
     $dict[':id_user'] = intval($id_user);
     $dict[':id_phase'] = PHASE_SELECTSTART;
     $dict[':round'] = 0;
     $result = DataSource::getInstance()->epp($query, $dict);
     ModelUser::getUser($id_user);
     if (empty($result)) {
         $id_move = self::createSelectStartMove($id_user, $id_game);
     } else {
         $id_move = $result[0]['id'];
     }
     return self::getSelectStartMove($id_game, $id_move);
 }
Пример #16
0
 /**
  * @param int $id_user
  */
 public function setIdUser($id_user)
 {
     $id_user = intval($id_user);
     ModelUser::getUser($id_user);
     SQLCommands::init($this->id_game);
     $query = 'set_ship_user';
     $dict = array(':id_zunit' => $this->id, ':id_user' => $id_user);
     DataSource::getInstance()->epp($query, $dict);
     $this->id_user = $id_user;
 }
Пример #17
0
 /**
  * checks if the user is in the given game
  *
  * @param int $id_game
  * @return bool
  */
 public function checkIfUserIsInThisGame($id_game)
 {
     $iter = ModelUser::iterator($id_game);
     while ($iter->hasNext()) {
         if ($iter->next() == $this) {
             return true;
         }
     }
     return false;
 }
Пример #18
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;
 }
Пример #19
0
 /**
  * tries to active a new user, returns a state if error or successfull
  *
  * @param int $id_user
  * @param string $verification_code
  * @return int state
  * @throws ControllerException
  * 1: user successfully created
  * 2: at least one empty entry
  * 3: verification doe preg_mismatch
  * 4: user not found
  * 5: verification code wrong
  *
  */
 public static function verifyAccount($id_user, $verification_code)
 {
     // check input validity
     if (empty($id_user) || empty($verification_code)) {
         throw new ControllerException('Missing parameters');
     }
     $id_user = intval($id_user);
     if (!preg_match('/^([a-zA-Z0-9]+)?$/', $verification_code)) {
         throw new ControllerException('Invalid parameters');
     }
     // create user model
     try {
         $user = ModelUser::getUser($id_user);
     } catch (\Exception $ex) {
         throw new ControllerException('Invalid parameters');
     }
     // check verification code
     if ($verification_code != $user->getVerify()) {
         throw new ControllerException('Invalid parameters');
     }
     // activate user (if inactive)
     if ($user->getStatus() !== STATUS_USER_INACTIVE) {
         throw new ControllerException('Invalid parameters');
     }
     // return success
     $user->setUserToActive();
 }
 protected function checkInGame($id_game)
 {
     return ModelIsInGameInfo::isUserInGame(ModelUser::getCurrentUser()->getId(), $id_game);
 }
Пример #21
0
 /**
  * @return ModelUser
  */
 public function getCreator()
 {
     return ModelUser::getUser($this->id_creator);
 }
Пример #22
0
 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());
         }
     }
 }
Пример #23
0
 protected function checkFixate(array &$data, $id_phase)
 {
     $igpi = ModelInGamePhaseInfo::getInGamePhaseInfo(ModelUser::getCurrentUser()->getId(), ModelGame::getCurrentGame()->getId());
     return $data['turnFixated'] = $igpi->getIsReadyForPhase((int) $id_phase) ? true : false;
 }
Пример #24
0
 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;
 }
Пример #25
0
 /**
  * joins the user in given game
  *
  * @param $id_user int
  * @param $id_game int
  * @param $id_color int
  * @throws NullPointerException
  * @throws JoinUserException
  * @return ModelIsInGameInfo
  */
 public static function joinGame($id_user, $id_game, $id_color = null)
 {
     $id_user = intval($id_user);
     $id_game = intval($id_game);
     if ($id_color !== null) {
         $id_color = intval($id_color);
     }
     // check if user and game exist
     $user = ModelUser::getUser($id_user);
     $game = ModelGame::getGame($id_game);
     if ($game->getFreeSlots() <= 0) {
         throw new JoinUserException('Game is full.');
     }
     // check if user is in the game
     $iter = ModelUser::iterator($id_game);
     while ($iter->hasNext()) {
         if ($iter->next() === $user) {
             throw new JoinUserException('User allready in this game.');
         }
     }
     // check if color is free
     if ($id_color !== null && !$game->checkIfColorIsFree($id_color)) {
         $id_color = null;
     }
     // get first free color
     if ($id_color === null) {
         $color_iter = ModelColor::iterator();
         while ($color_iter->hasNext()) {
             $color = $color_iter->next();
             if (!$game->checkIfColorIsFree($color->getId())) {
                 continue;
             }
             $id_color = $color->getId();
             break;
         }
     }
     if ($id_color === null) {
         throw new JoinUserException('No free color found.');
     }
     // insert player
     $dict = array();
     $dict[':id_user'] = $id_user;
     $dict[':id_game'] = $id_game;
     $dict[':id_color'] = $id_color;
     DataSource::Singleton()->epp('join_game', $dict);
     // set user notification rules
     ModelInGamePhaseInfo::getInGamePhaseInfo($id_user, $id_game);
     return self::getIsInGameInfo($id_user, $id_game);
 }