/** * 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(); }
/** * 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); }
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; }
/** * 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(); }
/** * 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); }
/** * @return ModelUser */ public function getCreator() { return ModelUser::getUser($this->id_creator); }
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; }
/** * tries to log a user in, if successfull loads userdata * * @param string $token * @throws LoginException * @return ModelUser */ public static function loginWithToken($token) { $result = DataSource::Singleton()->epp('check_user_token', array(':token' => $token)); if (empty($result)) { throw new LoginException('Invalid token.'); } self::$current_user = ModelUser::getUser($result[0]['id']); return self::$current_user; }
/** * @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; }
/** * 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); }