diff --git a/includes/application/code/community/Legacies/Core/Event/Break.php b/includes/application/code/community/Legacies/Core/Event/Break.php new file mode 100644 index 0000000..ea0cf19 --- /dev/null +++ b/includes/application/code/community/Legacies/Core/Event/Break.php @@ -0,0 +1,7 @@ +_queue[$itemIndex]); + unset($this->_queue[(int) $item->getIndex()]); return $this; } @@ -117,6 +117,8 @@ abstract class Legacies_Empire_Model_BuilderAbstract $this->_queue = array(); } + abstract public function updateQueue($time = null); + abstract public function appendQueue($typeId, $qty, $time); /** * Check if an item type is actually buildable on the current @@ -147,18 +149,6 @@ abstract class Legacies_Empire_Model_BuilderAbstract return false; } - try { - // Dispatch event. Throw an exception to break the avaliability. - Legacies::dispatchEvent('builder.check-availability', array( - 'type_id' => $typeId, - 'builder' => $this, - 'planet' => $this->_currentPlanet, - 'user' => $this->_currentUser - )); - } catch (Legacies_Empire_Model_Builder_Break $e) { - return false; - } - return true; } @@ -187,25 +177,21 @@ abstract class Legacies_Empire_Model_BuilderAbstract public function next() { - $element = next($this->_queue); - $this->_index = key($this->_queue); - - return $element; + return next($this->_queue); } public function key() { - return $this->_index; + return key($this->_queue); } public function valid() { - return $this->current() !== false; + return current($this->_queue) !== false; } public function rewind() { reset($this->_queue); - $this->_index = 0; } } \ No newline at end of file diff --git a/includes/application/code/community/Legacies/Empire/Model/Game/Abstract.php b/includes/application/code/community/Legacies/Empire/Model/Game/Abstract.php new file mode 100644 index 0000000..b288971 --- /dev/null +++ b/includes/application/code/community/Legacies/Empire/Model/Game/Abstract.php @@ -0,0 +1,36 @@ +_data); + } + + public function current() + { + return current($this->_data); + } + + public function next() + { + return next($this->_data); + } + + public function key() + { + return key($this->_data); + } + + public function valid() + { + return $this->current() !== false; + } + + public function rewind() + { + reset($this->_data); + } +} \ No newline at end of file diff --git a/includes/application/code/community/Legacies/Empire/Model/Game/Combat.php b/includes/application/code/community/Legacies/Empire/Model/Game/Combat.php index 688d08d..306699a 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Game/Combat.php +++ b/includes/application/code/community/Legacies/Empire/Model/Game/Combat.php @@ -8,7 +8,7 @@ * @uses Legacies_Empire */ class Legacies_Empire_Model_Game_Combat - extends Legacies_Core_Model + extends Legacies_Empire_Model_Game_Abstract implements Legacies_Core_Singleton { private static $_singleton = null; diff --git a/includes/application/code/community/Legacies/Empire/Model/Game/FieldsAlias.php b/includes/application/code/community/Legacies/Empire/Model/Game/FieldsAlias.php index 8b3bac6..fcdd2a2 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Game/FieldsAlias.php +++ b/includes/application/code/community/Legacies/Empire/Model/Game/FieldsAlias.php @@ -8,7 +8,7 @@ * @uses Legacies_Empire */ class Legacies_Empire_Model_Game_FieldsAlias - extends Legacies_Core_Model + extends Legacies_Empire_Model_Game_Abstract implements Legacies_Core_Singleton { private static $_singleton = null; diff --git a/includes/application/code/community/Legacies/Empire/Model/Game/Prices.php b/includes/application/code/community/Legacies/Empire/Model/Game/Prices.php index 25147ed..a2b0c20 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Game/Prices.php +++ b/includes/application/code/community/Legacies/Empire/Model/Game/Prices.php @@ -8,7 +8,7 @@ * @uses Legacies_Empire */ class Legacies_Empire_Model_Game_Prices - extends Legacies_Core_Model + extends Legacies_Empire_Model_Game_Abstract implements Legacies_Core_Singleton { private static $_singleton = null; diff --git a/includes/application/code/community/Legacies/Empire/Model/Game/Production.php b/includes/application/code/community/Legacies/Empire/Model/Game/Production.php index bc561ae..38eb98f 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Game/Production.php +++ b/includes/application/code/community/Legacies/Empire/Model/Game/Production.php @@ -8,7 +8,7 @@ * @uses Legacies_Empire */ class Legacies_Empire_Model_Game_Production - extends Legacies_Core_Model + extends Legacies_Empire_Model_Game_Abstract implements Legacies_Core_Singleton { private static $_singleton = null; diff --git a/includes/application/code/community/Legacies/Empire/Model/Game/Requirements.php b/includes/application/code/community/Legacies/Empire/Model/Game/Requirements.php index 358240c..b2fa004 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Game/Requirements.php +++ b/includes/application/code/community/Legacies/Empire/Model/Game/Requirements.php @@ -8,7 +8,7 @@ * @uses Legacies_Empire */ class Legacies_Empire_Model_Game_Requirements - extends Legacies_Core_Model + extends Legacies_Empire_Model_Game_Abstract implements Legacies_Core_Singleton { private static $_singleton = null; diff --git a/includes/application/code/community/Legacies/Empire/Model/Game/Resources.php b/includes/application/code/community/Legacies/Empire/Model/Game/Resources.php index dd68d2a..ec3cab5 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Game/Resources.php +++ b/includes/application/code/community/Legacies/Empire/Model/Game/Resources.php @@ -8,7 +8,7 @@ * @uses Legacies_Empire */ class Legacies_Empire_Model_Game_Resources - extends Legacies_Core_Model + extends Legacies_Empire_Model_Game_Abstract implements Legacies_Core_Singleton { private static $_singleton = null; diff --git a/includes/application/code/community/Legacies/Empire/Model/Game/Types.php b/includes/application/code/community/Legacies/Empire/Model/Game/Types.php index 594d540..40e9ed7 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Game/Types.php +++ b/includes/application/code/community/Legacies/Empire/Model/Game/Types.php @@ -8,7 +8,7 @@ * @uses Legacies_Empire */ class Legacies_Empire_Model_Game_Types - extends Legacies_Core_Model + extends Legacies_Empire_Model_Game_Abstract implements Legacies_Core_Singleton { private static $_singleton = null; diff --git a/includes/application/code/community/Legacies/Empire/Model/Planet.php b/includes/application/code/community/Legacies/Empire/Model/Planet.php index aa9ea68..1fcae72 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Planet.php +++ b/includes/application/code/community/Legacies/Empire/Model/Planet.php @@ -80,11 +80,11 @@ class Legacies_Empire_Model_Planet Math::setPrecision(50); $resources = Legacies_Empire_Model_Game_Resources::getSingleton(); foreach ($resources->getAllDatas() as $resource => $resourceData) { - if (!isset($resourceData['storage_field']) || $resourceData['storage_field'] === null && $this['rpg_stockeur'] === null) { + if (!isset($resourceData['storage_field']) || $resourceData['storage_field'] === null) { continue; } - $officerEnhancement = (.5 * $this->getData('rpg_stockeur')) + 1; + $officerEnhancement = (.5 * $this->getUser()->getData('rpg_stockeur')) + 1; $storageEnhancementFactor = Math::floor(Math::pow(1.6, $this[Legacies_Empire::getFieldName($resourceData['storage'])])); $storageEnhancement = Math::mul(BASE_STORAGE_SIZE / 2, $storageEnhancementFactor); diff --git a/includes/application/code/community/Legacies/Empire/Model/Planet/Building/Shipyard.php b/includes/application/code/community/Legacies/Empire/Model/Planet/Building/Shipyard.php index f1afa51..c340588 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Planet/Building/Shipyard.php +++ b/includes/application/code/community/Legacies/Empire/Model/Planet/Building/Shipyard.php @@ -40,6 +40,8 @@ class Legacies_Empire_Model_Planet_Building_Shipyard implements Legacies_Empire_Model_Planet_BuildingInterface { + private $_eventPrefix = 'planet.shipyard.'; + /** * Planet instance * @var Legacies_Empire_Model_Planet @@ -140,50 +142,29 @@ class Legacies_Empire_Model_Planet_Building_Shipyard * @param int|string $qty * @return Legacies_Empire_Model_Planet_Building_Shipyard */ - public function appendQueue($shipId, $qty) + public function appendQueue($shipId, $qty, $time = null) { - $types = Legacies_Empire_Model_Game_Types::getSingleton(); - - if (Math::isPositive($qty)) { - return $this; - } - - if (!$types->is($shipId, Legacies_Empire::TYPE_SHIP) && !$types->is($shipId, Legacies_Empire::TYPE_DEFENSE)) { - return $this; - } - - if (!$this->checkAvailability($shipId)) { - return $this; - } - - $qty = $this->_checkMaximumQuantity($shipId, $qty); - - if (MAX_FLEET_OR_DEFS_PER_ROW > 0 && Math::comp($qty, MAX_FLEET_OR_DEFS_PER_ROW) > 0) { - $qty = MAX_FLEET_OR_DEFS_PER_ROW; + if ($time === null) { + $time = Legacies::now(); } // Dispatch event - Legacies::dispatchEvent('planet.shipyard.append-queue.before', array( + Legacies::dispatchEvent($this->_eventPrefix . 'append-queue.before', array( 'ship_id' => $shipId, - 'qty' => $qty, + 'qty' => &$qty, + 'time' => &$time, 'shipyard' => $this, 'planet' => $this->_currentPlanet, 'user' => $this->_currentUser )); - $resourcesNeeded = $this->getResourcesNeeded($shipId, $qty); - $buildTime = $this->getBuildTime($shipId, $qty); - - $this->_queue->enqueue($shipId, $qty, now()); - - foreach ($this->_resourcesTypes as $resourceType) { - $this->_currentPlanet[$resourceType] = Math::sub($this->_currentPlanet[$resourceType], $resourcesNeeded[$resourceType]); - } + $this->_builder->appendQueue($shipId, $qty, $time); // Dispatch event - Legacies::dispatchEvent('planet.shipyard.append-queue.after', array( + Legacies::dispatchEvent($this->_eventPrefix . 'append-queue.before', array( 'ship_id' => $shipId, 'qty' => $qty, + 'time' => $time, 'shipyard' => $this, 'planet' => $this->_currentPlanet, 'user' => $this->_currentUser @@ -199,8 +180,24 @@ class Legacies_Empire_Model_Planet_Building_Shipyard */ public function updateQueue($time = null) { + // Dispatch event + Legacies::dispatchEvent($this->_eventPrefix . 'update-queue.before', array( + 'time' => &$time, + 'shipyard' => $this, + 'planet' => $this->_currentPlanet, + 'user' => $this->_currentUser + )); + $this->_builder->updateQueue($time); + // Dispatch event + Legacies::dispatchEvent($this->_eventPrefix . 'update-queue.after', array( + 'time' => $time, + 'shipyard' => $this, + 'planet' => $this->_currentPlanet, + 'user' => $this->_currentUser + )); + return $this; } @@ -224,20 +221,19 @@ class Legacies_Empire_Model_Planet_Building_Shipyard */ public function checkAvailability($shipId) { - return $this->_builder->checkAvailability($shipId); - } + try { + // Dispatch event + Legacies::dispatchEvent($this->_eventPrefix . 'check-availability', array( + 'ship_id' => $shipId, + 'shipyard' => $this, + 'planet' => $this->_currentPlanet, + 'user' => $this->_currentUser + )); + } catch (Legacies_Core_Event_Break $e) { + return false; + } - /** - * Returns the quantity set in parameter or the maximum buildable elements - * if the quantity requested exeeds this number. - * - * @param int $shipId - * @param int|string $qty - * @return int|stirng - */ - protected function _checkMaximumQuantity($shipId, $qty) - { - return Math::min($qty, $this->getMaximumBuildableElementsCount($shipId)); + return $this->_builder->checkAvailability($shipId); } /** @@ -254,12 +250,12 @@ class Legacies_Empire_Model_Planet_Building_Shipyard public function getResourcesNeeded($shipId, $qty) { - return $this->_builder->getResourcesNeeded(); + return $this->_builder->getResourcesNeeded($shipId, $qty); } - public function getBuildTime($shipId, $qty) + public function getBuildingTime($shipId, $qty) { - $this->_builder->getBuildTime($shipId, $qty); + $this->_builder->getBuildingTime($shipId, $qty); } public static function planetUpdateListener($eventData) diff --git a/includes/application/code/community/Legacies/Empire/Model/Planet/Building/Shipyard/Builder.php b/includes/application/code/community/Legacies/Empire/Model/Planet/Building/Shipyard/Builder.php index 101d232..47974fb 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Planet/Building/Shipyard/Builder.php +++ b/includes/application/code/community/Legacies/Empire/Model/Planet/Building/Shipyard/Builder.php @@ -37,36 +37,12 @@ class Legacies_Empire_Model_Planet_Building_Shipyard_Builder public function checkAvailability($shipId) { $types = Legacies_Empire_Model_Game_Types::getSingleton(); - $requirements = Legacies_Empire_Model_Game_Requirements::getSingleton(); - if (!isset($requirements[$shipId]) || empty($requirements[$shipId])) { - return true; - } - - foreach ($requirements[$shipId] as $requirement => $level) { - if ($types->is($requirement, Legacies_Empire::TYPE_BUILDING) && $this->_currentPlanet->hasElement($requirement, $level)) { - continue; - } else if ($types->is($requirement, Legacies_Empire::TYPE_RESEARCH) && $this->_currentUser->hasElement($requirement, $level)) { - continue; - } else if ($types->is($requirement, Legacies_Empire::TYPE_DEFENSE) && $this->_currentPlanet->hasElement($requirement, $level)) { - continue; - } else if ($types->is($requirement, Legacies_Empire::TYPE_SHIP) && $this->_currentPlanet->hasElement($requirement, $level)) { - continue; - } + if (!$types->is($shipId, Legacies_Empire::TYPE_SHIP) && !$types->is($shipId, Legacies_Empire::TYPE_DEFENSE)) { return false; } - try { - // Dispatch event. Throw an exception to break the avaliability. - Legacies::dispatchEvent('planet.shipyard.check-availability', array( - 'ship_id' => $shipId, - 'shipyard' => $this, - 'planet' => $this->_currentPlanet, - 'user' => $this->_currentUser - )); - } catch (Exception $e) { - return false; - } + parent::checkAvailability($shipId); return true; } @@ -202,10 +178,17 @@ class Legacies_Empire_Model_Planet_Building_Shipyard_Builder public function getResourcesNeeded($shipId, $qty) { $prices = Legacies_Empire_Model_Game_Prices::getSingleton(); + $resources = Legacies_Empire_Model_Game_Resources::getSingleton(); $resourcesNeeded = array(); - foreach ($this->_resourcesTypes as $resourceId) { - if (isset($prices[$shipId]) && isset($prices[$shipId][$resourceId]) && $prices[$shipId][$resourceId] > 0) { + foreach ($resources as $resourceId => $resourceConfig) { + if (!isset($prices[$shipId])) { + continue; + } + if (!isset($prices[$shipId][$resourceId])) { + continue; + } + if (Math::isPositive($prices[$shipId][$resourceId])) { $resourcesNeeded[$resourceId] = Math::mul($prices[$shipId][$resourceId], $qty); } } @@ -213,6 +196,19 @@ class Legacies_Empire_Model_Planet_Building_Shipyard_Builder return $resourcesNeeded; } + /** + * Returns the quantity set in parameter or the maximum buildable elements + * if the quantity requested exeeds this number. + * + * @param int $shipId + * @param int|string $qty + * @return int|stirng + */ + protected function _checkMaximumQuantity($shipId, $qty) + { + return Math::min($qty, $this->getMaximumBuildableElementsCount($shipId)); + } + /** * Update the contruction queue. * @@ -227,15 +223,7 @@ class Legacies_Empire_Model_Planet_Building_Shipyard_Builder } $elapsedTime = $time - $this->_currentPlanet->getLastUpdate(); - // Dispatch event - Legacies::dispatchEvent('planet.shipyard.update-queue.before', array( - 'shipyard' => $this, - 'planet' => $this->_currentPlanet, - 'user' => $this->_currentUser - )); - - - foreach ($this as $element) { + foreach ($this->getQueue() as $element) { $shipId = $element->getData('ship_id'); $qty = $element->getData('qty'); $buildTime = $this->getBuildingTime($shipId, $qty); @@ -243,7 +231,7 @@ class Legacies_Empire_Model_Planet_Building_Shipyard_Builder if ($elapsedTime >= $buildTime) { $this->_currentPlanet[$fields[$shipId]] = Math::add($this->_currentPlanet[$fields[$shipId]], $qty); $elapsedTime -= $buildTime; - $this->dequeue($element->getIndex()); + $this->dequeue($element); continue; } @@ -258,12 +246,48 @@ class Legacies_Empire_Model_Planet_Building_Shipyard_Builder $this->_currentPlanet->setData('b_hangar_id', $this->serialize()); - // Dispatch event - Legacies::dispatchEvent('planet.shipyard.update-queue.after', array( - 'shipyard' => $this, - 'planet' => $this->_currentPlanet, - 'user' => $this->_currentUser - )); + return $this; + } + + /** + * Append items to build to the construction list + * + * @param int $shipId + * @param int|string $qty + * @return Legacies_Empire_Model_Planet_Building_Shipyard + */ + public function appendQueue($shipId, $qty, $time) + { + $types = Legacies_Empire_Model_Game_Types::getSingleton(); + $resources = Legacies_Empire_Model_Game_Resources::getSingleton(); + + if (!Math::isPositive($qty)) { + return $this; + } + + if (!$types->is($shipId, Legacies_Empire::TYPE_SHIP) && !$types->is($shipId, Legacies_Empire::TYPE_DEFENSE)) { + return $this; + } + + if (!$this->checkAvailability($shipId)) { + return $this; + } + + $qty = Math::min($this->_checkMaximumQuantity($shipId, $qty), MAX_FLEET_OR_DEFS_PER_ROW); + + $resourcesNeeded = $this->getResourcesNeeded($shipId, $qty); + $buildTime = $this->getBuildingTime($shipId, $qty); + + $this->enqueue($shipId, $qty, $time); + + foreach ($resources as $resourceId => $resourceConfig) { + if (!isset($resourcesNeeded[$resourceId])) { + continue; + } + $this->_currentPlanet[$resourceId] = Math::sub($this->_currentPlanet[$resourceId], $resourcesNeeded[$resourceId]); + } + + $this->_currentPlanet->setData('b_hangar_id', $this->serialize()); return $this; } diff --git a/includes/application/test/UpdatePlanet.php b/includes/application/test/UpdatePlanet.php index fa5e8bc..ee0ca01 100644 --- a/includes/application/test/UpdatePlanet.php +++ b/includes/application/test/UpdatePlanet.php @@ -6,20 +6,51 @@ if (!extension_loaded('xdebug')) { header('Content-Type: text/plain'); } +$user = new Legacies_Empire_Model_User(array( + 'rpg_stockeur' => 0, + + "spy_tech" => 12, + "computer_tech" => 8, + "military_tech" => 10, + "defence_tech" => 10, + "shield_tech" => 10, + "energy_tech" => 8, + "hyperspace_tech" => 8, + "combustion_tech" => 6, + "impulse_motor_tech" => 6, + "hyperspace_motor_tech" => 6, + "laser_tech" => 6, + "ionic_tech" => 6, + "buster_tech" => 6, + "intergalactic_tech" => 0, + "expedition_tech" => 0, + "graviton_tech" => 0, + )); + $planet = new Legacies_Empire_Model_Planet(array( 'last_update' => 0, 'planet_type' => 1, 'b_hangar_id' => serialize(array(array( 'ship_id' => Legacies_Empire::ID_SHIP_LIGHT_TRANSPORT, - 'qty' => 1, + 'qty' => 10, 'created_at' => 0, 'updated_at' => 0 ), array( 'ship_id' => Legacies_Empire::ID_SHIP_LARGE_TRANSPORT, - 'qty' => 100, + 'qty' => 20, 'created_at' => 600, 'updated_at' => 600 + ), array( + 'ship_id' => Legacies_Empire::ID_SHIP_LARGE_TRANSPORT, + 'qty' => 30, + 'created_at' => 650, + 'updated_at' => 650 + ), array( + 'ship_id' => Legacies_Empire::ID_SHIP_LARGE_TRANSPORT, + 'qty' => 40, + 'created_at' => 700, + 'updated_at' => 700 ))), 'metal' => 0, @@ -39,13 +70,13 @@ $planet = new Legacies_Empire_Model_Planet(array( 'deuterium_sintetizer_porcent' => 10, 'metal_mine' => 20, - 'crystal_mine' => 0, + 'crystal_mine' => 19, 'deuterium_sintetizer' => 0, 'solar_plant' => 20, 'fusion_plant' => 0, 'robot_factory' => 0, 'nano_factory' => 0, - 'hangar' => 6, + 'hangar' => 12, 'metal_store' => 5, 'crystal_store' => 5, 'deuterium_store' => 5, @@ -53,6 +84,8 @@ $planet = new Legacies_Empire_Model_Planet(array( 'rpg_stockeur' => 0 )); +$planet->setUser($user); + var_dump($planet->getAllDatas()); $planet->updateStorages(3600); @@ -65,14 +98,31 @@ $planet->updateResourceProduction(3600); Legacies::dispatchEvent('planet.update', array( 'planet' => $planet, - 'time' => 3600 + 'time' => 7200 + )); + +//var_dump($planet->getAllDatas()); +$planet->getShipyard()->appendQueue(Legacies_Empire::ID_SHIP_LIGHT_FIGHTER, 10, 3600); + +var_dump($planet->getAllDatas()); + +Legacies::dispatchEvent('planet.update', array( + 'planet' => $planet, + 'time' => (3600 * 20) )); var_dump($planet->getAllDatas()); Legacies::dispatchEvent('planet.update', array( 'planet' => $planet, - 'time' => (3600 + (3600 * 200)) + 'time' => (3600 * 30) + )); + +var_dump($planet->getAllDatas()); + +Legacies::dispatchEvent('planet.update', array( + 'planet' => $planet, + 'time' => (3600 * 50) )); var_dump($planet->getAllDatas()); \ No newline at end of file