diff --git a/includes/application/code/community/Legacies/Empire/Model/BuilderAbstract.php b/includes/application/code/community/Legacies/Empire/Model/BuilderAbstract.php index 9a0e05e..e2748df 100644 --- a/includes/application/code/community/Legacies/Empire/Model/BuilderAbstract.php +++ b/includes/application/code/community/Legacies/Empire/Model/BuilderAbstract.php @@ -117,7 +117,7 @@ abstract class Legacies_Empire_Model_BuilderAbstract $this->_queue = array(); } - abstract public function updateQueue($time = null); + abstract public function updateQueue($time); abstract public function appendQueue($typeId, $qty, $time); /** @@ -194,4 +194,28 @@ abstract class Legacies_Empire_Model_BuilderAbstract { reset($this->_queue); } + + public function getCurrentPlanet() + { + return $this->_currentPlanet; + } + + public function getCurrentUser() + { + return $this->_currentUser; + } + + public function setCurrentPlanet(Legacies_Empire_Model_Planet $planet) + { + $this->_currentPlanet = $planet; + + return $this; + } + + public function setCurrentUser(Legacies_Empire_Model_User $user) + { + $this->_currentUser = $user; + + return $this; + } } \ No newline at end of file diff --git a/includes/application/code/community/Legacies/Empire/Model/Planet.php b/includes/application/code/community/Legacies/Empire/Model/Planet.php index 1fcae72..dd00f7d 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Planet.php +++ b/includes/application/code/community/Legacies/Empire/Model/Planet.php @@ -13,6 +13,8 @@ class Legacies_Empire_Model_Planet const TYPE_DEBRIS = 2; const TYPE_MOON = 3; + protected $_eventPrefix = 'planet.'; + protected $_user = null; protected $_moon = null; @@ -21,15 +23,6 @@ class Legacies_Empire_Model_Planet protected static $_instances = array(); - protected static $_productionCodes = array( - Legacies_Empire::ID_BUILDING_METAL_MINE => 'metal_mine', - Legacies_Empire::ID_BUILDING_CRISTAL_MINE => 'cristal_mine', - Legacies_Empire::ID_BUILDING_DEUTERIUM_SYNTHETISER => 'deuterium_synthetiser', - Legacies_Empire::ID_BUILDING_SOLAR_PLANT => 'solar_plant', - Legacies_Empire::ID_BUILDING_FUSION_REACTOR => 'fusion_reactor', - Legacies_Empire::ID_SHIP_SOLAR_SATELLITE => 'solar_satelite' - ); - protected static $_productionInstances = array(); public static function factory($id) @@ -215,8 +208,10 @@ class Legacies_Empire_Model_Planet $timeDiff = ($time - $this->getLastUpdate()) / 3600; foreach ($resources->getAllDatas() as $resourceId => $resourceConfig) { $production = Math::mul($this->getData($resourceConfig['production_field']), $timeDiff); - $this->setData($resourceConfig['field'], Math::min($production, $this->getData($resourceConfig['storage_field']))); + $resourceValue = Math::add($this->getData($resourceConfig['field']), $production); + $this->setData($resourceConfig['field'], Math::min($resourceValue, $this->getData($resourceConfig['storage_field']))); } + $this->setLastUpdate($time); return $this; } @@ -380,11 +375,15 @@ class Legacies_Empire_Model_Planet return $this->hasData($fields[$elementId]) && Math::comp($this->getElement($elementId), $levelRequired) > 0; } - public function appendQueue($buildingId, $time = null, $destroy = false) + public function appendBuildingQueue($buildingId, $destroy = false, $time = null) { $types = Legacies_Empire_Model_Game_Types::getSingleton(); $prices = Legacies_Empire_Model_Game_Prices::getSingleton(); + if ($time === null) { + $time = Legacies::now(); + } + if (!$types->is($buildingId, Legacies_Empire::TYPE_BUILDING)) { return $this; } @@ -394,7 +393,7 @@ class Legacies_Empire_Model_Planet } // Dispatch 'planet.building.append-queue.before' event - Legacies::dispatchEvent('planet.building.append-queue.before', array( + Legacies::dispatchEvent($this->_eventPrefix . 'building.append-queue.before', array( 'bulding_id' => $buildingId, 'level' => $this->getElement($buildingId), 'planet' => $this, @@ -402,17 +401,16 @@ class Legacies_Empire_Model_Planet 'destroy' => $destroy )); - $this->_builder->enqueue($buildingId, $level, $time); - - //$elementBasePrice = $prices; - /* - foreach ($this->_resourcesTypes as $resourceType) { - $this->setData($resourceType, Math::sub($this->getData($resourceType), $resourcesNeeded[$resourceType])); + if ($destroy === false) { + $level = $this->getElement($buildingId) + 1; + } else { + $level = max($this->getElement($buildingId) - 1, 0); } - */ + + $this->_builder->appendQueue($buildingId, $level, $time); // Dispatch 'planet.shipyard.append-queue.after' event - Legacies::dispatchEvent('planet.shipyard.append-queue.after', array( + Legacies::dispatchEvent($this->_eventPrefix . 'building.append-queue.after', array( 'bulding_id' => $buildingId, 'level' => $this->getElement($buildingId), 'planet' => $this, @@ -423,6 +421,30 @@ class Legacies_Empire_Model_Planet return $this; } + public function updateBuildingQueue($time = null) + { + if ($time === null) { + $time = Legacies::now(); + } + + // Dispatch event + Legacies::dispatchEvent($this->_eventPrefix . 'update-queue.before', array( + 'time' => &$time, + 'planet' => $this->_currentPlanet, + 'user' => $this->_currentUser + )); + + $this->_builder->updateQueue(); + + // Dispatch event + Legacies::dispatchEvent($this->_eventPrefix . 'update-queue.after', array( + 'time' => $time, + 'shipyard' => $this, + 'planet' => $this->_currentPlanet, + 'user' => $this->_currentUser + )); + } + /** * Check if a building is actually buildable on the current planet, * depending on the technology and buildings requirements. @@ -432,29 +454,11 @@ class Legacies_Empire_Model_Planet */ public function checkAvailability($buildingId) { - $types = Legacies_Empire_Model_Game_Types::getSingleton(); - $requirements = Legacies_Empire_Model_Game_Requirements::getSingleton(); - - if (!isset($requirements[$buildingId]) || empty($requirements[$buildingId])) { - return true; - } - - foreach ($requirements[$buildingId] 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; - } - return false; - } + $this->_builder->checkAvailability($buildingId); try { // Dispatch event. Throw an exception to break the avaliability. - Legacies::dispatchEvent('planet.shipyard.check-availability', array( + Legacies::dispatchEvent($this->_eventPrefix . 'check-availability', array( 'ship_id' => $buildingId, 'shipyard' => $this, 'planet' => $this->_currentPlanet, @@ -579,26 +583,7 @@ class Legacies_Empire_Model_Planet return; } - $user = $planet->getUser(); - /* - if (($queue = $planet->getData('b_building_id')) != '0') { //FIXME: refactor buildings construciton list - $explodedQueue = explode(';', $queue); - foreach ($explodedQueue as $item) { - $partialTime = $planet->getData('b_building'); - - if ($partialTime < $time) { - $planet->updateResources($partialTime); - if (CheckPlanetBuildingQueue($planet, $user)) { - SetNextQueueElementOnTop($planet, $user); - } - } else { - $planet->updateResources($time); - break; - } - } - } else { - $planet->updateResources($time); - }*/ + $planet->updateBuildingQueue($time); $planet->updateResources($time); $planet->getShipyard()->updateQueue($time); $planet->setLastUpdate($time); diff --git a/includes/application/code/community/Legacies/Empire/Model/Planet/Builder.php b/includes/application/code/community/Legacies/Empire/Model/Planet/Builder.php index 4da968b..39ebd9b 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Planet/Builder.php +++ b/includes/application/code/community/Legacies/Empire/Model/Planet/Builder.php @@ -27,13 +27,145 @@ class Legacies_Empire_Model_Planet_Builder )); } - public function getResourcesNeeded($typeId, $level) + /** + * Check if a building is actually buildable on the current planet, + * depending on the technology and buildings requirements. + * + * @param int $buildingId + * @return bool + */ + public function checkAvailability($buildingId) { - return array(); + $types = Legacies_Empire_Model_Game_Types::getSingleton(); + + if ($types->is($buildingId, Legacies_Empire::TYPE_BUILDING)) { + return true; + } + + return $this->checkAvailability($buildingId); } - public function getBuildingTime($typeId, $level) + public function getResourcesNeeded($buildingId, $level) + { + $prices = Legacies_Empire_Model_Game_Prices::getSingleton(); + $resources = Legacies_Empire_Model_Game_Resources::getSingleton(); + + if (!isset($prices[$buildingId])) { + return array(); + } + $resourcesNeeded = array(); + foreach ($resources as $resourceId => $resourceConfig) { + if (!isset($prices[$buildingId][$resourceId])) { + continue; + } + if (Math::isPositive($prices[$buildingId][$resourceId])) { + $firstLevelCost = $prices[$buildingId][$resourceId]; + $partialLevelCost = Math::mul($firstLevelCost, Math::pow($prices[$buildingId][Legacies_Empire::RESOURCE_MULTIPLIER], $level)); + $resourcesNeeded[$resourceId] = Math::sub($partialLevelCost, $firstLevelCost); + } + } + + return $resourcesNeeded; + } + + public function getBuildingTime($buildingId, $level) { return 0; } + + /** + * Update the contruction queue. + * + * @return Legacies_Empire_Model_Planet_Building_Shipyard + */ + public function updateQueue($time = null) + { + $fields = Legacies_Empire_Model_Game_FieldsAlias::getSingleton(); + + if ($time === null) { + $time = Legacies::now(); + } + $elapsedTime = $time - $this->_currentPlanet->getData('b_building'); + + foreach ($this->getQueue() as $element) { + $buildingId = $element->getData('building_id'); + $level = $element->getData('level'); + $buildTime = $this->getBuildingTime($buildingId, $level); // FIXME: consider total time, not only the construction time + + if ($elapsedTime >= $buildTime) { + $this->_currentPlanet->updateResources($time - $elapsedTime); + $this->_currentPlanet->setElement($buildingId, $level); + $this->_currentPlanet->updateResourceProduction($time - $elapsedTime); + $this->_currentPlanet->updateStorages($time - $elapsedTime); + $this->dequeue($element); + + Legacies::dispatchEvent('planet.building.level-update', array( + 'time' => $time - $elapsedTime, + 'planet' => $this->_currentPlanet, + 'user' => $this->_currentUser, + 'building_id' => $buildingId, + 'level' => $level + )); + + $elapsedTime -= $buildTime; + continue; + } + $element->setData('updated_at', $time); + break; + } + + $this->_currentPlanet->setData('b_building_id', $this->serialize()); + $this->_currentPlanet->setData('b_building', $time); + + return $this; + } + + /** + * Append items to build to the construction list + * + * @param int $buildingId + * @param int|string $level + * @return Legacies_Empire_Model_Planet_Building_Shipyard + */ + public function appendQueue($buildingId, $level, $time) + { + $types = Legacies_Empire_Model_Game_Types::getSingleton(); + $resources = Legacies_Empire_Model_Game_Resources::getSingleton(); + + if (!Math::isPositive($level)) { + return $this; + } + + if (!$types->is($buildingId, Legacies_Empire::TYPE_BUILDING)) { + return $this; + } + + if (!$this->checkAvailability($buildingId)) { + return $this; + } + + $resourcesNeeded = $this->getResourcesNeeded($buildingId, $level); + + $amounts = array(); + foreach ($resourcesNeeded as $resourceId => $resourceCost) { + $amount[$resourceId] = Math::sub($this->_currentPlanet[$resourceId], $resourceCost); + + if (Math::isNegative($amount[$resourceId])) { + return $this; + } + } + + $this->enqueue($buildingId, $level, $time); + + foreach ($resourcesNeeded as $resourceId => $resourceCost) { + if (!isset($resources[$resourceId]) || !Math::isPositive($resourceCost)) { + continue; + } + $this->_currentPlanet->setData($resourceId, $resourceId[$resourceId]); + } + + $this->_currentPlanet->setData('b_building_id', $this->serialize()); + + return $this; + } } \ No newline at end of file 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 c340588..9107908 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 @@ -180,6 +180,10 @@ class Legacies_Empire_Model_Planet_Building_Shipyard */ public function updateQueue($time = null) { + if ($time === null) { + $time = Legacies::now(); + } + // Dispatch event Legacies::dispatchEvent($this->_eventPrefix . 'update-queue.before', array( 'time' => &$time, 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 47974fb..d2723ac 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 @@ -58,16 +58,10 @@ class Legacies_Empire_Model_Planet_Building_Shipyard_Builder { $prices = Legacies_Empire_Model_Game_Prices::getSingleton(); $fields = Legacies_Empire_Model_Game_FieldsAlias::getSingleton(); - - $resources = array( - Legacies_Empire::RESOURCE_METAL, - Legacies_Empire::RESOURCE_CRISTAL, - Legacies_Empire::RESOURCE_DEUTERIUM, - Legacies_Empire::RESOURCE_ENERGY - ); + $resources = Legacies_Empire_Model_Game_Resources::getSingleton(); $qty = 0; - foreach ($resources as $resourceId) { + foreach ($resources as $resourceId => $_) { if (isset($prices[$shipId]) && isset($prices[$shipId][$resourceId]) && Math::comp($prices[$shipId][$resourceId], 0) > 0) { $maxQty = Math::floor(Math::div($this->_currentPlanet->getData($resourceId), $prices[$shipId][$resourceId])); @@ -180,11 +174,11 @@ class Legacies_Empire_Model_Planet_Building_Shipyard_Builder $prices = Legacies_Empire_Model_Game_Prices::getSingleton(); $resources = Legacies_Empire_Model_Game_Resources::getSingleton(); + if (!isset($prices[$shipId])) { + return array(); + } $resourcesNeeded = array(); foreach ($resources as $resourceId => $resourceConfig) { - if (!isset($prices[$shipId])) { - continue; - } if (!isset($prices[$shipId][$resourceId])) { continue; } @@ -214,14 +208,11 @@ class Legacies_Empire_Model_Planet_Building_Shipyard_Builder * * @return Legacies_Empire_Model_Planet_Building_Shipyard */ - public function updateQueue($time = null) + public function updateQueue($time) { $fields = Legacies_Empire_Model_Game_FieldsAlias::getSingleton(); - if ($time === null) { - $time = Legacies::now(); - } - $elapsedTime = $time - $this->_currentPlanet->getLastUpdate(); + $elapsedTime = $time - $this->_currentPlanet->getData('b_hangar'); foreach ($this->getQueue() as $element) { $shipId = $element->getData('ship_id'); @@ -245,6 +236,7 @@ class Legacies_Empire_Model_Planet_Building_Shipyard_Builder } $this->_currentPlanet->setData('b_hangar_id', $this->serialize()); + $this->_currentPlanet->setData('b_hangar', $time); return $this; } @@ -273,7 +265,15 @@ class Legacies_Empire_Model_Planet_Building_Shipyard_Builder return $this; } - $qty = Math::min($this->_checkMaximumQuantity($shipId, $qty), MAX_FLEET_OR_DEFS_PER_ROW); + if (MAX_FLEET_OR_DEFS_PER_ROW > 0) { + $qty = Math::min($this->_checkMaximumQuantity($shipId, $qty), MAX_FLEET_OR_DEFS_PER_ROW); + } else { + $qty = $this->_checkMaximumQuantity($shipId, $qty); + } + + if (!Math::isPositive($qty)) { + return $this; + } $resourcesNeeded = $this->getResourcesNeeded($shipId, $qty); $buildTime = $this->getBuildingTime($shipId, $qty); diff --git a/includes/application/code/community/Legacies/Object.php b/includes/application/code/community/Legacies/Object.php index 0534bff..ff5712c 100644 --- a/includes/application/code/community/Legacies/Object.php +++ b/includes/application/code/community/Legacies/Object.php @@ -87,7 +87,7 @@ class Legacies_Object public function __get($key) { - return $this->getData($key, $value); + return $this->getData($key); } public function __unset($key) diff --git a/includes/application/test/UpdatePlanet.php b/includes/application/test/UpdatePlanet.php index ee0ca01..995fbb6 100644 --- a/includes/application/test/UpdatePlanet.php +++ b/includes/application/test/UpdatePlanet.php @@ -27,11 +27,12 @@ $user = new Legacies_Empire_Model_User(array( "graviton_tech" => 0, )); -$planet = new Legacies_Empire_Model_Planet(array( +$data = array( 'last_update' => 0, 'planet_type' => 1, - 'b_hangar_id' => serialize(array(array( + 'b_building_id' => '', + 'b_hangar_id' => serialize(array(/*array( 'ship_id' => Legacies_Empire::ID_SHIP_LIGHT_TRANSPORT, 'qty' => 10, 'created_at' => 0, @@ -51,78 +52,92 @@ $planet = new Legacies_Empire_Model_Planet(array( 'qty' => 40, 'created_at' => 700, 'updated_at' => 700 - ))), + )*/)), - 'metal' => 0, + 'metal' => 100000, 'metal_perhour' => 20, - 'crystal' => 0, - 'crystal_perhour' => 10, + 'cristal' => 100000, + 'cristal_perhour' => 10, 'deuterium' => 0, 'deuterium_perhour' => 0, 'energy_max' => 0, 'energy_used' => 0, 'metal_mine_porcent' => 10, - 'crystal_mine_porcent' => 10, + 'cristal_mine_porcent' => 10, 'solar_plant_porcent' => 10, 'fusion_plant_porcent' => 10, 'solar_satelit_porcent' => 10, 'deuterium_sintetizer_porcent' => 10, 'metal_mine' => 20, - 'crystal_mine' => 19, + 'cristal_mine' => 19, 'deuterium_sintetizer' => 0, 'solar_plant' => 20, 'fusion_plant' => 0, 'robot_factory' => 0, 'nano_factory' => 0, 'hangar' => 12, - 'metal_store' => 5, - 'crystal_store' => 5, - 'deuterium_store' => 5, + 'metal_store' => 10, + 'cristal_store' => 10, + 'deuterium_store' => 10, 'rpg_stockeur' => 0 - )); + ); +$planet = new Legacies_Empire_Model_Planet($data); $planet->setUser($user); -var_dump($planet->getAllDatas()); +$planet->updateStorages(0); +$planet->updateResourceProduction(0); -$planet->updateStorages(3600); +$planet->getShipyard()->appendQueue(Legacies_Empire::ID_SHIP_LIGHT_FIGHTER, 10, 0); +$planet->appendBuildingQueue(Legacies_Empire::ID_BUILDING_CRISTAL_MINE, false, 0); -//var_dump($planet->getAllDatas()); - -$planet->updateResourceProduction(3600); - -//var_dump($planet->getAllDatas()); +var_dump(array_diff($planet->getAllDatas(), $data)); +$data = $planet->getAllDatas(); Legacies::dispatchEvent('planet.update', array( 'planet' => $planet, - 'time' => 7200 + 'time' => 5400 )); -//var_dump($planet->getAllDatas()); -$planet->getShipyard()->appendQueue(Legacies_Empire::ID_SHIP_LIGHT_FIGHTER, 10, 3600); - -var_dump($planet->getAllDatas()); +var_dump(array_diff($planet->getAllDatas(), $data)); +$data = $planet->getAllDatas(); Legacies::dispatchEvent('planet.update', array( 'planet' => $planet, 'time' => (3600 * 20) )); -var_dump($planet->getAllDatas()); +$planet->appendBuildingQueue(Legacies_Empire::ID_BUILDING_CRISTAL_MINE, false, 3600); + +var_dump(array_diff($planet->getAllDatas(), $data)); +$data = $planet->getAllDatas(); Legacies::dispatchEvent('planet.update', array( 'planet' => $planet, 'time' => (3600 * 30) )); -var_dump($planet->getAllDatas()); +var_dump(array_diff($planet->getAllDatas(), $data)); +$data = $planet->getAllDatas(); Legacies::dispatchEvent('planet.update', array( 'planet' => $planet, - 'time' => (3600 * 50) + 'time' => (3600 * 5000) )); -var_dump($planet->getAllDatas()); \ No newline at end of file +var_dump(array_diff($planet->getAllDatas(), $data)); +$data = $planet->getAllDatas(); + +$planet->appendBuildingQueue(Legacies_Empire::ID_BUILDING_CRISTAL_MINE, false, 0); +$planet->appendBuildingQueue(Legacies_Empire::ID_BUILDING_SOLAR_PLANT, false, 0); + +Legacies::dispatchEvent('planet.update', array( + 'planet' => $planet, + 'time' => (3600 * 10000) + )); + +var_dump(array_diff($planet->getAllDatas(), $data)); +$data = $planet->getAllDatas(); \ No newline at end of file diff --git a/includes/data/fields-alias.php b/includes/data/fields-alias.php index 78e310c..2a56eb1 100644 --- a/includes/data/fields-alias.php +++ b/includes/data/fields-alias.php @@ -3,7 +3,7 @@ // Buildings // {{{ Legacies_Empire::ID_BUILDING_METAL_MINE => "metal_mine", - Legacies_Empire::ID_BUILDING_CRISTAL_MINE => "crystal_mine", + Legacies_Empire::ID_BUILDING_CRISTAL_MINE => "cristal_mine", Legacies_Empire::ID_BUILDING_DEUTERIUM_SYNTHETISER => "deuterium_sintetizer", Legacies_Empire::ID_BUILDING_SOLAR_PLANT => "solar_plant", Legacies_Empire::ID_BUILDING_FUSION_REACTOR => "fusion_plant", @@ -11,7 +11,7 @@ Legacies_Empire::ID_BUILDING_NANITE_FACTORY => "nano_factory", Legacies_Empire::ID_BUILDING_SHIPYARD => "hangar", Legacies_Empire::ID_BUILDING_METAL_STORAGE => "metal_store", - Legacies_Empire::ID_BUILDING_CRISTAL_STORAGE => "crystal_store", + Legacies_Empire::ID_BUILDING_CRISTAL_STORAGE => "cristal_store", Legacies_Empire::ID_BUILDING_DEUTERIUM_TANK => "deuterium_store", Legacies_Empire::ID_BUILDING_RESEARCH_LAB => "laboratory", Legacies_Empire::ID_BUILDING_TERRAFORMER => "terraformer", diff --git a/includes/data/resources.php b/includes/data/resources.php index be77618..4bea873 100644 --- a/includes/data/resources.php +++ b/includes/data/resources.php @@ -16,10 +16,10 @@ 'renderer' => 'Legacies_Empire_Block_Resource_Default', 'field' => Legacies_Empire::RESOURCE_CRISTAL, - 'production_field' => 'crystal_perhour', - 'storage_field' => 'crystal_max', + 'production_field' => 'cristal_perhour', + 'storage_field' => 'cristal_max', 'production' => array( - Legacies_Empire::ID_BUILDING_CRISTAL_MINE => 'crystal_mine_porcent' + Legacies_Empire::ID_BUILDING_CRISTAL_MINE => 'cristal_mine_porcent' ), 'storage' => Legacies_Empire::ID_BUILDING_CRISTAL_STORAGE ),