예제 #1
0
 public function __toString()
 {
     $str = '';
     $str .= $this->getName() . ": ";
     $str .= $this->getCurrentTurn() . "\n";
     $states = StateQuery::create()->filterByTurn($this->getCurrentTurn());
     $str .= str_pad('Territory', 30) . str_pad('Empire', 12) . str_pad('Unit', 10) . "\n";
     $str .= str_pad('', 29, '-') . ' ' . str_pad('', 11, '-') . ' ' . str_pad('', 10, '-') . "\n";
     foreach ($states as $s) {
         $str .= str_pad($s->getTerritory(), 30) . str_pad($s->getOccupier(), 12) . new Unit($s->getUnitType()) . "\n";
     }
     return $str;
 }
예제 #2
0
 /**
  * Returns a multidimentional array of State where the top level are 
  * Country names and the bottom level are State objects
  * 
  * e.g.
  * 
  * array(
  *   'Canada' => array(
  *     0 => State    // Alberta
  *     1 => State    // British Columbia
  * ...
  * 
  * @param StateQuery $query  // optional StateQuery to filter results
  * @return array
  */
 public static function retrieveStatesGroupByCountry($query = null)
 {
     if (!is_null($query)) {
         \Altumo\Validation\Objects::assertObjectClass($query, 'StateQuery');
     } else {
         $query = StateQuery::create();
     }
     $result = array();
     $all_states = $query->useCountryQuery()->addAscendingOrderByColumn(CountryPeer::NAME)->endUse()->addAscendingOrderByColumn(StatePeer::NAME)->find();
     foreach ($all_states as $state) {
         $result[$state->getCountry()->getName()][] = $state;
     }
     return $result;
 }
예제 #3
0
 /**
  * Returns the number of related State objects.
  *
  * @param      Criteria $criteria
  * @param      boolean $distinct
  * @param      PropelPDO $con
  * @return int             Count of related State objects.
  * @throws PropelException
  */
 public function countStates(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
 {
     if (null === $this->collStates || null !== $criteria) {
         if ($this->isNew() && null === $this->collStates) {
             return 0;
         } else {
             $query = StateQuery::create(null, $criteria);
             if ($distinct) {
                 $query->distinct();
             }
             return $query->filterByCountry($this)->count($con);
         }
     } else {
         return count($this->collStates);
     }
 }
예제 #4
0
 /**
  * Looks through the turn, and determines how many units an
  * empire has, how many supply territories they have, and
  * home many home supply territories they have.
  */
 public function run()
 {
     $ret = array();
     $empires = $this->turn->getMatch()->getGame()->getEmpires();
     foreach ($empires as $empire) {
         $empire_ret = array();
         $territories_owned = StateQuery::create()->filterByTurn($this->turn)->filterByOccupier($empire)->count();
         $territories_occupied_armies = StateQuery::create()->filterByTurn($this->turn)->filterByOccupier($empire)->filterByUnitType('army')->count();
         $territories_occupied_fleets = StateQuery::create()->filterByTurn($this->turn)->filterByOccupier($empire)->filterByUnitType('fleet')->count();
         $territories_supplies = StateQuery::create()->filterByTurn($this->turn)->filterByOccupier($empire)->useTerritoryQuery()->filterByIsSupply(1)->endUse()->count();
         // territory home supplies?
         // Adjustments required?
         $adjustments_required = $territories_owned != $territories_supplies;
         $empire_ret = array('empire' => $empire->__toArray(), 'owned' => $territories_owned, 'occupied-army' => $territories_occupied_armies, 'occupied-fleet' => $territories_occupied_fleets, 'supplies' => $territories_supplies, 'adjustment-required' => $adjustments_required);
         $ret[] = $empire_ret;
         if ($adjustments_required) {
             $this->required_adjustments++;
         }
     }
     $this->unit_supply = $ret;
 }
예제 #5
0
 /**
  * Try to look up a game territory given a string.  This is intended
  * to be as loose as possible to match what people type.  Only return
  * if one result is found.
  *
  * @param $str Territory name, or abbreviation, or anything we put in
  *             the system that a user may use
  * @param Match Requires a match such that it can use the current turn,
  *              without this several results (one per turn) will be returned
  *              everytime
  * @param Empire Sometimes we can limit what territories might be
  *               being searched for by the empire.  In this case,
  *               use the game state to filter the results.
  * @return State
  * @throws TerritoryMatchException
  */
 public function lookupTerritory($str, Match $match, Empire $empire = null)
 {
     //global $config; $config->system->db->useDebug(true);
     $query = StateQuery::create()->filterByTurn($match->getCurrentTurn())->_if($empire instanceof Empire)->filterByOccupier($empire)->_endif()->join('State.Territory')->useTerritoryQuery()->filterByGame($this)->filterByName($str . '%', Criteria::LIKE)->_or()->filterByPrimaryKey($str)->endUse();
     $ts = $query->find();
     //$config->system->db->useDebug(false);
     if (count($ts) == 1) {
         return $ts[0];
     } elseif (count($ts) > 1) {
         $match_names = [];
         foreach ($ts as $t) {
             $match_names[] = $t->getTerritory()->getName();
         }
         throw new MultiTerritoryMatchException("Multiple matches for {$str}: '" . join("', '", $match_names) . "'");
     } else {
         $msg = "No match for {$str}";
         if (!is_null($empire)) {
             $msg .= ". Purhaps {$empire} does not own {$str}?";
         }
         throw new NoTerritoryMatchException($msg);
     }
 }
예제 #6
0
 /**
  * Get the associated State object
  *
  * @param      PropelPDO $con Optional Connection object.
  * @return                 State The associated State object.
  * @throws PropelException
  */
 public function getState(PropelPDO $con = null)
 {
     if ($this->aState === null && $this->state_id !== null) {
         $this->aState = StateQuery::create()->findPk($this->state_id, $con);
         /* The following can be used additionally to
               guarantee the related object contains a reference
               to this object.  This level of coupling may, however, be
               undesirable since it could result in an only partially populated collection
               in the referenced object.
               $this->aState->addContacts($this);
            */
     }
     return $this->aState;
 }
예제 #7
0
 /**
  * Removes this object from datastore and sets delete attribute.
  *
  * @param      PropelPDO $con
  * @return void
  * @throws PropelException
  * @throws Exception
  * @see        BaseObject::setDeleted()
  * @see        BaseObject::isDeleted()
  */
 public function delete(PropelPDO $con = null)
 {
     if ($this->isDeleted()) {
         throw new PropelException("This object has already been deleted.");
     }
     if ($con === null) {
         $con = Propel::getConnection(StatePeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
     }
     $con->beginTransaction();
     try {
         $deleteQuery = StateQuery::create()->filterByPrimaryKey($this->getPrimaryKey());
         $ret = $this->preDelete($con);
         // symfony_behaviors behavior
         foreach (sfMixer::getCallables('BaseState:delete:pre') as $callable) {
             if (call_user_func($callable, $this, $con)) {
                 $con->commit();
                 return;
             }
         }
         if ($ret) {
             $deleteQuery->delete($con);
             $this->postDelete($con);
             // symfony_behaviors behavior
             foreach (sfMixer::getCallables('BaseState:delete:post') as $callable) {
                 call_user_func($callable, $this, $con);
             }
             $con->commit();
             $this->setDeleted(true);
         } else {
             $con->commit();
         }
     } catch (Exception $e) {
         $con->rollBack();
         throw $e;
     }
 }
예제 #8
0
 /**
  * Mostly a debug function.
  * Print out all the units for a given turn.
  */
 public static function printUnitTable(Turn $turn)
 {
     $states = StateQuery::create()->filterByTurn($turn)->find();
     $str = '';
     $str .= "State of units:\n";
     if (count($states) == 0) {
         $str .= "Warning! No units!\n";
         return $str;
     }
     $str .= str_pad('Empire', 12) . str_pad('Unit', 7) . str_pad('Territory', 30) . "\n";
     $str .= str_pad('', 11, '-') . ' ' . str_pad('', 6, '-') . ' ' . str_pad('', 29, '-') . "\n";
     foreach ($states as $state) {
         $str .= str_pad($state->getOccupier(), 12);
         $str .= str_pad($state->getUnitType(), 7);
         if (is_object($state)) {
             $currentTerritory = $state->getTerritory()->__toString();
         } else {
             // Retreated..
             $currentTerritory = '<stateless>';
         }
         $str .= str_pad($currentTerritory, 30) . "\n";
     }
     return $str;
 }
예제 #9
0
 /**
  * Fetch the list of territories owned by an empire.
  *
  * @param int $match_id Match ID
  * @param int $empire_id Empire ID issuing the order
  * @return array array(TerritoryTemplate, ...)
  */
 public function doGetEmpireTerritoryMap($match_id, $empire_id)
 {
     $match = $this->getMatch($match_id, $resp);
     if (is_null($match)) {
         return $resp;
     }
     $empire = $this->getEmpire($empire_id, $match, $resp);
     if (is_null($empire)) {
         return $resp;
     }
     $states = StateQuery::create()->filterByTurn($match->getCurrentTurn())->filterByOccupier($empire)->find();
     $territories = array();
     foreach ($states as $state) {
         $territories[] = array('territory' => $state->getTerritory()->__toArray(), 'unit' => $state->getUnitType());
     }
     $resp->data = $territories;
     return $resp->__toArray();
 }