/**
  * returns the specific model
  *
  * @param $id_game int
  * @param $id_game_area int
  * @throws NullPointerException
  * @return ModelGameArea
  */
 public static function getGameArea($id_game, $id_game_area)
 {
     $id_game = intval($id_game);
     $id_game_area = intval($id_game_area);
     if (isset(self::$game_areas[$id_game][$id_game_area])) {
         return self::$game_areas[$id_game][$id_game_area];
     }
     $area = new ModelGameArea($id_game, $id_game_area);
     self::$game_areas[$id_game][$id_game_area] = $area;
     self::$game_areas_for_area[$id_game][$area->getIdArea()] = $area;
     return $area;
 }
 /**
  * user selected areas
  *
  * @param $id_set int
  * @param $option_number int
  * @param $areas array(int id_area)
  * @throws ControllerException
  * @return void
  */
 public function selectStartAreas($id_set, $option_number, $areas)
 {
     if ($this->error) {
         return;
     }
     // check if user has already finished this move
     if ($this->checkIfDone()) {
         $this->error = true;
         throw new ControllerException('You have already finished this move.');
     }
     // check if regions are in the set option
     $supported_areas = ModelStartRegion::getRegionsForSetAndOption($id_set, $option_number);
     foreach ($areas as $id_area) {
         if (!isset($supported_areas[$id_area])) {
             $this->error = true;
             throw new ControllerException('The area ' . $id_area . ' is not in your set and option-number.');
         }
     }
     // check if correct number of areas selected
     $startRegion = array_shift($supported_areas);
     $id_option_type = $startRegion->getIdOptionType();
     $optionType = ModelOptionType::getOptionType($id_option_type);
     if ($optionType->getCountries() !== count($areas)) {
         $this->error = true;
         throw new ControllerException('Please choose the correct amount of areas.');
     }
     // insert
     $move = ModelSelectStartMove::getSelectStartMoveForUser($this->id_user, $this->id_game);
     $zareas = array();
     foreach ($areas as $id_area) {
         $gameArea = ModelGameArea::getGameAreaForArea($this->id_game, $id_area);
         $zareas[] = $gameArea->getId();
     }
     $move->setRegions($option_number, $zareas);
 }
 /**
  * creates new ship and corresponding move
  *
  * @param $id_unit int
  * @param $name string
  * @param $zarea_in_port int
  * @param $zarea int
  * @throws ControllerException
  * @throws ModelException
  */
 public function setNewShip($id_unit, $name, $zarea_in_port, $zarea)
 {
     // 1. regex name
     $name = trim($name);
     if (!preg_match("/^([a-zA-Z0-9]+[a-zA-Z0-9' -]+[a-zA-Z0-9']+){1,}?\$/", $name)) {
         throw new ControllerException('Invalid ship name. Only letters, numbers, spaces and -\' allowed');
     }
     // 2. check if id_zarea_in_port belongs to user
     $port_area = ModelGameArea::getGameArea($this->id_game, (int) $zarea_in_port);
     if ($port_area->getIdUser() !== $this->id_user) {
         throw new ControllerException('Area doesn\'t belong to user.');
     }
     // 3. check if zarea and id_zarea_in_port are adjacent
     if (!in_array((int) $zarea, $port_area->getAdjecents())) {
         throw new ControllerException('Area not adjacent do port area.');
     }
     // 4. check if ship id is in still available ships
     $stillAvailableShips = $this->getStillAvailableShips();
     if (!isset($stillAvailableShips[$id_unit])) {
         throw new ControllerException('No ships of this type available.');
     } else {
         if ($stillAvailableShips[$id_unit] <= 0) {
             throw new ControllerException('No ships of this type available anymore.');
         }
     }
     // 5. create new move
     ModelSetShipsMove::createSetShipsMove($this->id_user, $this->id_game, (int) $zarea_in_port, (int) $zarea, (int) $id_unit, $name);
 }
 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);
 }
 /**
  * @param $id_user int
  * @param $id_game int
  * @return array (money => int, countries => int, resproduction => int, traderoutes => int, trproduction => int, combos => int, comboproduction => int, sum => int)
  * @throws NullPointerException
  */
 public static function getCurrentProductionForUserInGame($id_user, $id_game)
 {
     $id_user = (int) $id_user;
     $id_game = (int) $id_game;
     $output = array();
     // money on bank
     $ingame = ModelIsInGameInfo::getIsInGameInfo($id_user, $id_game);
     $output['money'] = $ingame->getMoney();
     $output['moneySpendable'] = min($output['money'], MAX_MONEY_SPENDABLE);
     // money from resources
     $output['countries'] = 0;
     $output['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($id_user, $id_game);
     while ($iter->hasNext()) {
         $area = $iter->next();
         ++$output['countries'];
         $output['resproduction'] += $area->getProductivity();
         ++$combos[$area->getIdResource()];
     }
     // money from traderoutes
     $output['traderoutes'] = 0;
     $output['trproduction'] = 0;
     // money from combos
     $combo_count = $combos[RESOURCE_OIL];
     foreach ($combos as $res_count) {
         if ($res_count < $combo_count) {
             $combo_count = $res_count;
         }
     }
     $output['combos'] = $combo_count;
     $output['comboproduction'] = $combo_count * 4;
     // sum
     $output['sum'] = $output['moneySpendable'] + $output['resproduction'] + $output['trproduction'] + $output['comboproduction'];
     return $output;
 }
 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;
 }
 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;
 }
 private function validateNewProductionMove($round, $id_zarea, array $units)
 {
     // 1. check if zarea belongs to user
     $gameArea = ModelGameArea::getGameArea($this->id_game, $id_zarea);
     if ($this->id_user !== $gameArea->getIdUser()) {
         throw new ControllerException('Unable to create production move. Area doesn\'t belong to the current user.');
     }
     // 2. check if user has enough res left
     // 2.a check cost of previous productions
     $current_costs = 0;
     $moves = ModelProductionMove::iterator($this->id_user, $this->id_game, $round);
     while ($moves->hasNext()) {
         /* @var $move ModelProductionMove */
         $move = $moves->next();
         $current_costs += $move->getCost();
     }
     // 2.b get available res
     $current_production = UserViewHelper::getCurrentProductionForUserInGame($this->id_user, $this->id_game);
     // 2.c get cost of new move
     $new_cost = 0;
     $unit_iter = ModelLandUnit::iterator();
     while ($unit_iter->hasNext()) {
         /* @var $unit ModelLandUnit */
         $unit = $unit_iter->next();
         $id_unit = (int) $unit->getId();
         if (!isset($units[$id_unit]) || $units[$id_unit] <= 0) {
             continue;
         }
         $new_cost += (int) $unit->getPrice() * $units[$id_unit];
     }
     if ($current_production['sum'] - $current_costs < $new_cost) {
         throw new ControllerException('Insufficient funds!');
     }
 }
 private function showNewMove(array &$data)
 {
     $startAreas = array();
     $destinationAreas = 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()) {
             $startAreas[] = $areaViewData;
         }
         $destinationAreas[] = $areaViewData;
     }
     $data['startAreas'] = $startAreas;
     $data['destinationAreas'] = $destinationAreas;
 }
 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;
 }
 /**
  * @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;
 }
 private function isAreaPassable($id_zarea, $move_type)
 {
     $zArea = ModelGameArea::getGameArea($this->id_game, $id_zarea);
     $id_owner = (int) $zArea->getIdUser();
     $area_type = (int) $zArea->getIdType();
     if ($id_owner === $this->id_user) {
         if ($move_type === TYPE_AIR || $move_type === TYPE_LAND) {
             return true;
         }
         if ($area_type === $move_type) {
             return true;
         }
     }
     if ($id_owner === NEUTRAL_COUNTRY) {
         if ($move_type === TYPE_AIR) {
             return true;
         }
         if ($area_type === TYPE_SEA && $move_type === TYPE_SEA) {
             return true;
         }
     }
     return false;
 }
示例#13
0
 /**
  * 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);
 }
 /**
  * run the game logic
  *
  * @throws LogicException
  * @return void
  */
 public function run()
 {
     if (!$this->checkIfValid()) {
         throw new LogicException('Game ' . $this->id_game . ' not valid for processing.');
     }
     $this->startProcessing();
     try {
         // run through moves for each user
         $iter = ModelSelectStartMove::iterator($this->id_game);
         while ($iter->hasNext()) {
             // areas to select for user
             $selectStartMove = $iter->next();
             $regions_selected = $selectStartMove->getRegions();
             // array(int option_number => array(int id_zarea))
             $id_user = $selectStartMove->getIdUser();
             /* @var $iigi ModelIsInGameInfo */
             $iigi = ModelIsInGameInfo::getIsInGameInfo($id_user, $this->id_game);
             $id_set = $iigi->getIdStartingSet();
             foreach ($regions_selected as $option_number => $areas) {
                 $regions = ModelStartRegion::getRegionsForSetAndOption($id_set, $option_number);
                 // array(int id_area => ModelStartRegion)
                 foreach ($areas as $id_zarea) {
                     $gameArea = ModelGameArea::getGameArea($this->id_game, $id_zarea);
                     $id_area = $gameArea->getIdArea();
                     /* @var $region ModelStartRegion */
                     $region = $regions[$id_area];
                     $id_option = $region->getIdOptionType();
                     $unit_count = ModelOptionType::getOptionType($id_option)->getUnits();
                     // set user for game area
                     $gameArea->setIdUser($id_user);
                     // create units for user
                     $iterUnits = ModelLandUnit::iterator();
                     while ($iterUnits->hasNext()) {
                         $landUnit = $iterUnits->next();
                         $id_unit = $landUnit->getId();
                         $inGameLandUnit = ModelInGameLandUnit::getModelByIdZAreaUserUnit($this->id_game, $id_zarea, $id_user, $id_unit);
                         $inGameLandUnit->setCount($unit_count);
                     }
                 }
             }
         }
         // add units to all empty game-areas
         $iter = ModelGameArea::iterator(NEUTRAL_COUNTRY, $this->id_game);
         while ($iter->hasNext()) {
             /* @var $gameArea ModelGameArea */
             $gameArea = $iter->next();
             if ($gameArea->getIdType() !== TYPE_LAND) {
                 continue;
             }
             $count = $gameArea->getProductivity();
             if ($gameArea->getIdResource() === RESOURCE_OIL) {
                 ++$count;
             }
             $iterUnits = ModelLandUnit::iterator();
             while ($iterUnits->hasNext()) {
                 $landUnit = $iterUnits->next();
                 $id_unit = $landUnit->getId();
                 $inGameLandUnit = ModelInGameLandUnit::getModelByIdZAreaUserUnit($this->id_game, $gameArea->getId(), NEUTRAL_COUNTRY, $id_unit);
                 $inGameLandUnit->setCount($count);
             }
         }
         $this->finishProcessing();
     } catch (\Exception $ex) {
         $this->logger->fatal($ex);
         $this->rollback();
     }
 }
示例#15
0
 /**
  * sets game status of a new game to GAME_STATUS_STARTED
  *
  * @return bool
  * @throws GameAdministrationException
  * @throws ModelException
  */
 public function startGame()
 {
     if ($this->status !== GAME_STATUS_NEW) {
         throw new GameAdministrationException('Only new games can be started.');
     }
     // allocate starting sets to users
     $iter_player = ModelIsInGameInfo::iterator(null, $this->id);
     $players = $iter_player->size();
     $iter_sets = ModelStartingSet::iterator($players, true);
     while ($iter_player->hasNext()) {
         if (!$iter_sets->hasNext()) {
             throw new GameAdministrationException('Not enough starting sets found!');
         }
         $iter_player->next()->setStartingSet($iter_sets->next()->getId());
     }
     // allocate resources
     $iter_poor = ModelEconomy::iterator(ECONOMY_POOR);
     $iter_weak = ModelEconomy::iterator(ECONOMY_WEAK);
     $iter_normal = ModelEconomy::iterator(ECONOMY_NORMAL);
     $iter_strong = ModelEconomy::iterator(ECONOMY_STRONG);
     $iter_areas = ModelArea::iterator(TYPE_LAND);
     while ($iter_areas->hasNext()) {
         $_Area = $iter_areas->next();
         switch ($_Area->getEconomy()) {
             case ECONOMY_POOR:
                 $_Eco = $iter_poor->next();
                 break;
             case ECONOMY_WEAK:
                 $_Eco = $iter_weak->next();
                 break;
             case ECONOMY_NORMAL:
                 $_Eco = $iter_normal->next();
                 break;
             case ECONOMY_STRONG:
                 $_Eco = $iter_strong->next();
                 break;
             default:
                 throw new ModelException('Area with invalid eco type found: ' . $_Area->getId());
                 break;
         }
         ModelGameArea::setGameArea($this->id, 0, NEUTRAL_COUNTRY, $_Area->getId(), $_Eco->getIdResource(), $_Eco->getResPower());
     }
     // create sea areas
     $iter_sea = ModelArea::iterator(TYPE_SEA);
     while ($iter_sea->hasNext()) {
         $_Area = $iter_sea->next();
         ModelGameArea::setGameArea($this->id, 0, NEUTRAL_COUNTRY, $_Area->getId(), RESOURCE_NONE, 0);
     }
     // set game to started
     $this->setStatus(GAME_STATUS_STARTED);
     return true;
 }
示例#16
0
 private function executeAttack($id_target_area, array $moves)
 {
     // 0. init empty arrays for attacker/defender units
     $units_attacker = array();
     $units_defender = array();
     // 1. get units for defender
     $target_area = ModelGameArea::getGameArea($this->id_game, $id_target_area);
     $id_defender = $target_area->getIdUser();
     $iter = ModelLandUnit::iterator();
     while ($iter->hasNext()) {
         /* @var ModelLandUnit $unit */
         $unit = $iter->next();
         $units_attacker[$unit->getId()] = 0;
         $landUnit_defender = ModelInGameLandUnit::getModelByIdZAreaUserUnit($this->id_game, $id_target_area, $id_defender, $unit->getId());
         $units_defender[$unit->getId()] = $landUnit_defender->getCount();
     }
     // 2. add up all units for attacker (multiple moves possible, check already finished moves from NML-fights)
     // 2.a subtract units from originating country
     $id_attacker = 0;
     foreach ($moves as $id_move) {
         $move = ModelLandMove::getLandMove($this->id_game, $id_move);
         $id_user = $move->getIdUser();
         $id_attacker = $id_user;
         $steps = $move->getSteps();
         $from = reset($steps);
         $units = $move->getUnits();
         foreach ($units as $id_unit => $count) {
             $landUnit_from = ModelInGameLandUnit::getModelByIdZAreaUserUnit($this->id_game, $from, $id_user, $id_unit);
             $landUnit_from->addCount($count * -1);
             $units_attacker[$id_unit] += $count;
         }
     }
     // 3. calculate winner and remaining units
     $attacker_wins = $this->calculateFight($units_attacker, $units_defender);
     // 3.a update defender units
     $iter = ModelLandUnit::iterator();
     while ($iter->hasNext()) {
         /* @var ModelLandUnit $unit */
         $unit = $iter->next();
         $landUnit_defender = ModelInGameLandUnit::getModelByIdZAreaUserUnit($this->id_game, $id_target_area, $id_defender, $unit->getId());
         $landUnit_defender->setCount($units_defender[$unit->getId()]);
     }
     // 4. update target country units (and user if attacker won)
     if ($attacker_wins) {
         // 4.a update attacker units
         $iter = ModelLandUnit::iterator();
         while ($iter->hasNext()) {
             /* @var ModelLandUnit $unit */
             $unit = $iter->next();
             $landUnit_attacker = ModelInGameLandUnit::getModelByIdZAreaUserUnit($this->id_game, $id_target_area, $id_attacker, $unit->getId());
             $landUnit_attacker->setCount($units_attacker[$unit->getId()]);
         }
         // 4.b update country owner
         $area = ModelGameArea::getGameArea($this->id_game, $id_target_area);
         $area->setIdUser($id_attacker);
         // 4.c check ships
         // TODO : implement ship takeover or ship destruction
     }
     // 4. flag all moves as finished
     foreach ($moves as $id_move) {
         $this->finished_moves[] = $id_move;
     }
 }
 /**
  * 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);
 }
示例#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;
 }