Example #1
0
 /**
  * Responsible for the initial loading of a territory into the game (not
  * the match)
  **/
 public function loadTerritories(array $objs)
 {
     //print_r($objs);
     $ts = array();
     foreach ($objs as $obj) {
         $t = TerritoryTemplate::create($obj->name, $obj->type, $obj->has_supply ? true : false);
         //global $config; $config->system->db->useDebug(true);
         $empire = EmpireQuery::create()->filterByGame($this)->filterByAbbr($obj->empire_start)->findOne();
         if ($empire instanceof Empire) {
             $t->setInitialOccupation($empire, new Unit($obj->starting_forces));
         }
         //$config->system->db->useDebug(false);
         $this->addGameTerritory($t);
         $t->save();
         $ts[$obj->id] = $t;
         // Use the 'given' ID, rather than the new DB ID
     }
     unset($obj);
     // Safety
     // Second pass, set up neighbours
     foreach ($objs as $obj) {
         $t = $ts[$obj->id];
         foreach ($obj->neighbours as $nid) {
             $n = $ts[$nid];
             // again, using the spreadsheet IDs (nid) here
             // This doubles the size of the map, but makes it easier to query.
             // Re-evaluate this later.  Maybe I could modify getNeighbours() to
             // query territory_a or territory_b.
             $t->addNeighbour($n);
             $n->addNeighbour($t);
             // Now that neighbours are in, lets work on converting some of these
             // 'land's to 'coast's
             if ($t->getType() === 'land' && $n->getType() === 'water') {
                 $t->setType('coast');
             }
             $n->save();
         }
         unset($n);
         unset($nid);
         // safety
         $t->save();
     }
     unset($obj);
     unset($t);
     // safety
     $this->save();
 }
 /**
  * Main workhorse behind doAddOrder and doValidate
  *
  * @param int $match_id Match
  * @param int $empire_id empire
  * @param string $order_str Order we're creating/validating
  * @param Response& $resp (intent(INOUT)) Response for this request
  * @param Match& $match (intent(OUT)) Will populate the match variable
  * @param Empire& $empire (intent(OUT)) Will populate the empire variable
  * @return Order
  * @throws Exception Passes along any exception it hits
  */
 protected function validateOrder($match_id, $empire_id, $order_str, Response &$resp, &$match, &$empire)
 {
     $order = null;
     do {
         $match = $this->getMatch($match_id, $resp);
         if (is_null($match)) {
             break;
         }
         $empire = EmpireQuery::create()->filterByGame($match->getGame())->filterByPrimaryKey($empire_id)->findOne();
         if (!$empire instanceof Empire) {
             $resp->fail(Response::INVALID_EMPIRE, "Invalid empire {$empire_id}");
             break;
         }
         $order = Order::interpretText($order_str, $match, $empire);
         // Order is not yet saved
         $order->validate(false);
         // false for light validation
         if ($order->failed()) {
             $resp->fail(Response::INVALID_ORDER, $order->getTranscript());
             break;
         }
         $resp->data = true;
     } while (false);
     return $order;
 }