Created generic building queue

Updated ORM

Signed-off-by: Gregory PLANCHAT <g.planchat@gmail.com>
This commit is contained in:
Gregory PLANCHAT 2011-06-19 11:34:43 +02:00
commit 66875791e6
19 changed files with 575 additions and 110 deletions

View file

@ -35,8 +35,9 @@ class Legacies
return;
}
$eventObject = new Legacies_Core_Event($params);
foreach (self::$_listeners[$event] as $listener) {
call_user_func($listener, $params);
call_user_func($listener, $eventObject);
}
}

View file

@ -0,0 +1,129 @@
<?php
/**
*
* @uses Legacies_Object
* @uses Legacies_Empire
* @uses Legacies_Empire_User
*/
abstract class Legacies_Core_Entity_SubTable
extends Legacies_Core_Model
{
private $_isLoaded = false;
protected $_tableName = null;
protected $_idFieldNames = array();
protected function _save()
{
if ($this->_isLoaded !== false) {
$fields = array();
$values = array();
$idFields = array();
foreach ($this->getAllDatas() as $field => $value) {
if (in_array($field, $this->_idFieldNames)) {
$idFields[] = "{$field}=:{$field}";
} else {
$fields[] = "{$field}=:{$field}";
}
$values[$field] = $value;
}
$idFields = '(' . implode(') AND (', $idFields) . ')';
$fields = implode(', ', $fields);
$database = Legacies_Database::getSingleton();
$sql =<<<SQL_EOF
UPDATE {$database->getTable(self::getTableName())}
SET {$fields}
WHERE {$idFields}
SQL_EOF;
$statement = $database->prepare($sql);
$statement->execute($values);
} else {
$datas = $this->getAllDatas();
$fields = implode(', ', array_keys($datas));
$tokens = array();
foreach ($datas as $field => $value) {
$tokens[] = ":{$field}";
}
$tokens = implode(', ', $tokens);
$database = Legacies_Database::getSingleton();
$sql =<<<SQL_EOF
INSERT INTO {$database->getTable(self::getTableName())} ($fields)
VALUES ({$tokens})
SQL_EOF;
$statement = $database->prepare($sql);
$statement->execute($datas);
}
return $this;
}
protected function _load()
{
$idValues = func_get_arg(0);
$database = Legacies_Database::getSingleton();
$idFields = array();
foreach ($this->_idFieldNames as $field) {
$idFields[] = "{$field}=:{$field}";
}
$idFields = '(' . implode(') AND (', $idFields) . ')';
$sql =<<<SQL_EOF
SELECT * FROM {$database->getTable(self::getTableName())}
WHERE $idFields
LIMIT 1
SQL_EOF;
$statement = $database->prepare($sql);
$statement->execute($idValues);
$datas = $statement->fetch(PDO::FETCH_ASSOC);
if (!is_array($datas) || empty($datas)) {
throw new Legacies_Core_Model_Exception('Could not load data: this id combination could not be found.');
}
$this->_data = $datas;
$this->_isLoaded = true;
return $this;
}
protected function _delete()
{
$database = Legacies_Database::getSingleton();
$idFields = array();
foreach ($this->_idFieldNames as $field) {
$idFields[] = "{$field}=:{$field}";
}
$idFields = '(' . implode(') AND (', $idFields) . ')';
$sql =<<<SQL_EOF
DELETE {$database->getTable(self::getTableName())}
WHERE $idFields
LIMIT 1
SQL_EOF;
$statement = $database->prepare($sql);
$statement->execute($idValues);
$this->_data = array();
$this->_isLoaded = false;
return $this;
}
public function setTableName($tableName)
{
$this->_tableName = $tableName;
return $this;
}
public function getTableName()
{
return $this->_tableName;
}
}

View file

@ -0,0 +1,6 @@
<?php
class Legacies_Core_Event
extends Legacies_Object
{
}

View file

@ -4,7 +4,6 @@ abstract class Legacies_Core_Model
extends Legacies_Object
{
protected $_originalData = array();
protected $_data = array();
public function __construct(Array $data = array())
{

View file

@ -46,11 +46,13 @@ class Legacies_Empire
const TYPE_PRODUCTION = 'prod';
const RESOURCE_METAL = 'metal';
const RESOURCE_CRISTAL = 'crystal';
const RESOURCE_CRISTAL = 'cristal';
const RESOURCE_DEUTERIUM = 'deuterium';
const RESOURCE_ENERGY = 'energy';
const RESOURCE_MULTIPLIER = 'factor';
const RESOURCE_FORMULA = 'formule';
const RESOURCE_CLASS = 'class';
const SHIPS_CONSUMPTION_PRIMARY = 'consumption';
const SHIPS_CELERITY_PRIMARY = 'speed';

View file

@ -0,0 +1,7 @@
<?php
class Legacies_Empire_Model_Builder_Break
extends Exception
{
}

View file

@ -0,0 +1,19 @@
<?php
class Legacies_Empire_Model_Builder_Item
extends Legacies_Object
{
protected $_index = null;
public function getIndex()
{
return $this->_index;
}
public function setIndex($index)
{
$this->_index = $index;
return $this;
}
}

View file

@ -0,0 +1,161 @@
<?php
abstract class Legacies_Empire_Model_BuilderAbstract
implements Countable, Serializable, Iterator
{
/**
* Planet instance
* @var Legacies_Empire_Model_Planet
*/
protected $_currentPlanet = null;
/**
* User instance
* @var Legacies_Empire_Model_User
*/
protected $_currentUser = null;
/**
* construction queue
* @var array
*/
private $_queue = null;
/**
* construction queue
* @var array
*/
protected $_itemClass = 'Legacies_Empire_Model_Builder_Item';
/**
* construction queue
* @var array
*/
protected $_index = 0;
/**
*
* @param Legacies_Empire_Model_Planet $currentPlanet
* @param Legacies_Empire_Model_User $currentUser
*/
public function __construct(Legacies_Empire_Model_Planet $currentPlanet, Legacies_Empire_Model_User $currentUser)
{
$this->_currentPlanet = $currentPlanet;
$this->_currentUser = $currentUser;
}
abstract protected function _initItem();
public function enqueue()
{
$params = func_get_args();
$item = call_user_func_array(array($this, '_initItem'), $params);
$item->setIndex($this->_index);
$queue[$this->_index++] = $item;
return $this;
}
public function dequeue($itemIndex)
{
unset($itemIndex);
return $this;
}
protected function getItem($itemIndex)
{
if (isset($this->_queue[$itemIndex])) {
return $this->_queue[$itemIndex];
}
return null;
}
protected function _serializeQueue()
{
$serialize = array();
foreach ($this->_queue as $item) {
$serialize[] = $item->getAllDatas();
}
return serialize($serialize);
}
protected function _unserializeQueue($serialized)
{
$this->clearQueue();
$reflection = new ReflectionClass($this->_itemClass);
$unserialized = unserialize($serialized);
foreach ($unserialized as $itemData) {
$this->_enqueue($reflection->newInstance($itemData));
}
return $this;
}
public function __toString()
{
return $this->_serializeQueue();
}
public function getQueue()
{
return $this->_queue;
}
abstract public function enqueue($item);
abstract public function dequeue($item);
public function clearQueue()
{
$this->_queue = array();
}
/**
* Check if an item type is actually buildable on the current
* planet, depending on the technology and buildings requirements.
*
* @param int $typeId
* @return bool
*/
public function checkAvailability($typeId)
{
$types = Legacies_Empire_Model_Game_Types::getSingleton();
$requirements = Legacies_Empire_Model_Game_Requirements::getSingleton();
if (!isset($requirements[$typeId]) || empty($requirements[$typeId])) {
return true;
}
foreach ($requirements[$typeId] 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;
}
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;
}
abstract public function getResourcesNeeded($typeId, $level);
abstract public function getBuildingTime($typeId, $level);
}

View file

@ -19,48 +19,13 @@ class Legacies_Empire_Model_Planet
protected static $_instances = array();
protected static $_productionConfig = array(
Legacies_Empire::RESOURCE_METAL => array(
'field' => Legacies_Empire::RESOURCE_METAL,
'production_field' => 'metal_perhour',
'ratio_field' => 'metal_porcent',
'storage_field' => 'metal_max',
'production' => array(
Legacies_Empire::ID_BUILDING_METAL_MINE => 'metal_mine_porcent'
),
'storage' => Legacies_Empire::ID_BUILDING_METAL_STORAGE
),
Legacies_Empire::RESOURCE_CRISTAL => array(
'field' => Legacies_Empire::RESOURCE_CRISTAL,
'production_field' => 'crystal_perhour',
'ratio_field' => 'crystal_porcent',
'storage_field' => 'crystal_max',
'production' => array(
Legacies_Empire::ID_BUILDING_CRISTAL_MINE => 'crystal_mine_porcent'
),
'storage' => Legacies_Empire::ID_BUILDING_CRISTAL_STORAGE
),
Legacies_Empire::RESOURCE_DEUTERIUM => array(
'field' => Legacies_Empire::RESOURCE_DEUTERIUM,
'production_field' => 'deuterium_perhour',
'ratio_field' => 'deuterium_porcent',
'storage_field' => 'deuterium_max',
'production' => array(
Legacies_Empire::ID_BUILDING_DEUTERIUM_SYNTHETISER => 'deuterium_sintetizer_porcent'
),
'storage' => Legacies_Empire::ID_BUILDING_DEUTERIUM_TANK
),
Legacies_Empire::RESOURCE_ENERGY => array(
'field' => 'energy_used',
'production_field' => 'energy_max',
'storage_field' => null,
'production' => array(
Legacies_Empire::ID_BUILDING_SOLAR_PLANT => 'solar_plant_porcent',
Legacies_Empire::ID_BUILDING_FUSION_REACTOR => 'fusion_plant_porcent',
Legacies_Empire::ID_SHIP_SOLAR_SATELLITE => 'solar_satelit_porcent'
),
'storage' => null
)
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();
@ -111,14 +76,14 @@ class Legacies_Empire_Model_Planet
$resourcesProductions = array();
foreach (self::$_productionConfig as $resource => $resourceData) {
if ($resourceData['storage_field'] !== null && $resourceData['storage_field'] !== null) {
$officerEnhancement = Math::add(Math::mul(.5, $this->getUser('rpg_stockeur')), 1);
$officerEnhancement = Math::add(Math::mul(.5, $this->getData('rpg_stockeur')), 1);
$storageCapacity = Math::pow(1.5, $this->getData(Legacies_Empire::getFieldName($resourceData['storage'])));
$value = Math::mul(MAX_OVERFLOW, Math::mul($officerEnhancement, Math::add(BASE_STORAGE_SIZE, $storageCapacity)));
$this->setData($resourceData['storage_field'], $value);
}
foreach ($resourceData[production] as $productionUnit => $ratioField) {
foreach ($resourceData['production'] as $productionUnit => $ratioField) {
if (!in_array($productionUnit, $types['prod'])) {
continue;
}
@ -127,7 +92,7 @@ class Legacies_Empire_Model_Planet
$ratio = $this->getData($ratioField);
$element = self::getProducitonElementInstance($productionUnit);
foreach ($element->getRatios($level, $ratio, $this, $this->getUser()) as $resourceId => $resourceProduction) {
foreach ($element->getProductionRatios($level, $ratio, $this, $this->getUser()) as $resourceId => $resourceProduction) {
if (!isset($resourcesProductions[$resourceId])) {
$resourcesProductions[$resourceId] = $resourceProduction;
} else {
@ -138,14 +103,15 @@ class Legacies_Empire_Model_Planet
}
$timeDiff = ($time - $this->getData('last_update')) / 3600;
foreach ($resourcesProductions as $resourceId => $productionPerHour) {
foreach ($resourcesProductions as $resource => $productionPerHour) {
if (!isset(self::$_productionConfig[$resource])) {
continue;
}
$this->setData(self::$_productionConfig[$resource]['production_field'], $productionPerHour);
$production = Math::add($this->getData(self::$_productionConfig[$resource]['field']), Math::mul($timeDiff, $productionPerHour));
if (Math::diff($production, $this->getData(self::$_productionConfig[$resource]['storage_field'])) > 0) {
if (Math::comp($production, $this->getData(self::$_productionConfig[$resource]['storage_field'])) > 0) {
$production = $this->getData(self::$_productionConfig[$resource]['storage_field']);
}
$this->setData(self::$_productionConfig[$resource]['field'], $production);
@ -156,14 +122,15 @@ class Legacies_Empire_Model_Planet
public static function getProducitonElementInstance($buildingId)
{
global $ProdGrid; // FIXME
$production = Legacies_Empire_Model_Game_Production::getSingleton();
if (!isset(self::$_productionInstances[$buildingId])) {
if (!isset($ProdGrid[$buildingId])) {
if (!isset($production[$buildingId])) {
return null;
}
$class = $ProdGrid[$buildingId][Legacies_Empire::RESOURCE_FORMULA];
self::$_productionInstances[$buildingId] = new $class;
$class = $production[$buildingId][Legacies_Empire::RESOURCE_CLASS];
$reflection = new ReflectionClass($class);
self::$_productionInstances[$buildingId] = $reflection->newInstance();
}
return self::$_productionInstances[$buildingId];
@ -290,6 +257,87 @@ class Legacies_Empire_Model_Planet
return $this->hasData($fields[$elementId]) && Math::comp($this->getElement($elementId), $levelRequired) > 0;
}
public function appendQueue($buildingId)
{
$types = Legacies_Empire_Model_Game_Types::getSingleton();
if (!$types->is($buildingId, Legacies_Empire::TYPE_BUILDING)) {
return $this;
}
if (!$this->checkAvailability($buildingId)) {
return $this;
}
// Dispatch event
Legacies::dispatchEvent('planet.building.append-queue.before', array(
'ship_id' => $shipId,
'qty' => $qty,
'shipyard' => $this,
'planet' => $this->_currentPlanet,
'user' => $this->_currentUser
));
foreach ($this->_resourcesTypes as $resourceType) {
$this->setData($resourceType, Math::sub($this->getData($resourceType), $resourcesNeeded[$resourceType]));
}
// Dispatch event
Legacies::dispatchEvent('planet.shipyard.append-queue.after', array(
'ship_id' => $shipId,
'qty' => $qty,
'shipyard' => $this,
'planet' => $this->_currentPlanet,
'user' => $this->_currentUser
));
return $this;
}
/**
* 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)
{
$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;
}
try {
// Dispatch event. Throw an exception to break the avaliability.
Legacies::dispatchEvent('planet.shipyard.check-availability', array(
'ship_id' => $buildingId,
'shipyard' => $this,
'planet' => $this->_currentPlanet,
'user' => $this->_currentUser
));
} catch (Exception $e) {
return false;
}
return true;
}
public static function registrationListener($eventData)
{
if (isset($eventData['user'])) {
@ -392,9 +440,10 @@ class Legacies_Empire_Model_Planet
if (isset($eventData['planet'])) {
$planet = $eventData['planet'];
$time = null;
if (isset($eventData['time'])) {
$time = $eventData['time'];
} else {
$time = time();
}
if ($planet === null || !$planet instanceof Legacies_Empire_Model_Planet || !$planet->getId()) {
@ -402,10 +451,10 @@ class Legacies_Empire_Model_Planet
}
$user = $planet->getUser();
if (($queue = $this->getData('b_building_id')) != '') {
if (($queue = $planet->getData('b_building_id')) != '0') { //FIXME: refactor buildings construciton list
$explodedQueue = explode(';', $queue);
foreach ($explodedQueue as $item) {
$partialTime = $this->getData('b_building');
$partialTime = $planet->getData('b_building');
if ($partialTime < $time) {
$planet->updateResources($partialTime);
@ -420,6 +469,7 @@ class Legacies_Empire_Model_Planet
} else {
$planet->updateResources($time);
}
$planet->setData('last_update', $time);
}
}
}

View file

@ -75,7 +75,7 @@ class Legacies_Empire_Model_Planet_Building_Shipyard
Legacies_Empire::RESOURCE_METAL,
Legacies_Empire::RESOURCE_CRISTAL,
Legacies_Empire::RESOURCE_DEUTERIUM,
//Legacies_Empire::RESOURCE_ENERGY
Legacies_Empire::RESOURCE_ENERGY
);
/**
@ -95,7 +95,7 @@ class Legacies_Empire_Model_Planet_Building_Shipyard
public static function factory($currentPlanet, $currentUser)
{
if ($currentPlanet->getId()) {
return false;
return null;
}
if (!isset(self::$_instances[$currentPlanet->getId()])) {
@ -117,10 +117,7 @@ class Legacies_Empire_Model_Planet_Building_Shipyard
$this->_currentPlanet = $currentPlanet;
$this->_currentUser = $currentUser;
$this->_queue = unserialize($this->_currentPlanet->getData('b_hangar_id'));
if (!is_array($this->_queue)) {
$this->_queue = array();
}
$this->_queue = new Legacies_Empire_Model_Planet_Building_Shipyard_Builder($currentPlanet, $currentUser);
$this->_now = time();
}
@ -187,12 +184,7 @@ class Legacies_Empire_Model_Planet_Building_Shipyard
$resourcesNeeded = $this->_getResourcesNeeded($shipId, $qty);
$buildTime = $this->getBuildTime($shipId, $qty);
$this->_queue[] = new Legacies_Empire_Model_Planet_Building_Shipyard_Item(array(
'ship_id' => $shipId,
'qty' => $qty,
'created_at' => $this->_now(),
'updated_at' => $this->_now()
));
$this->_queue->enqueue($shipId, $qty);
foreach ($this->_resourcesTypes as $resourceType) {
$this->_currentPlanet[$resourceType] = Math::sub($this->_currentPlanet[$resourceType], $resourcesNeeded[$resourceType]);

View file

@ -0,0 +1,22 @@
<?php
class Legacies_Empire_Model_Planet_Building_Shipyard_Builder
extends Legacies_Empire_Model_BuilderAbstract
{
public function __construct($currentPlanet, $currentUser)
{
parent::__construct($currentPlanet, $currentUser);
$this->_unserializeQueue($currentPlanet->getData('b_hangar_id'));
}
public function _initItem($shipId, $qty)
{
return new Legacies_Empire_Model_Planet_Building_Shipyard_Item(array(
'ship_id' => $shipId,
'qty' => $qty,
'created_at' => time(),
'updated_at' => time()
));
}
}

View file

@ -1,6 +1,6 @@
<?php
class Legacies_Empire_Model_Planet_Building_Shipyard_Item
extends Legacies_Object
extends Legacies_Empire_Model_Builder_Item
{
}

View file

@ -0,0 +1,20 @@
<?php
/**
*
* @uses Legacies_Object
* @uses Legacies_Empire
* @uses Legacies_Empire_User
*/
class Legacies_Empire_Model_Planet_Production
extends Legacies_Core_Entity_SubTable
{
public function _init()
{
$this->_tableName = 'planet_production';
$this->_idFieldNames = array(
'production_code',
'planet_id'
);
}
}

View file

@ -0,0 +1,20 @@
<?php
/**
*
* @uses Legacies_Object
* @uses Legacies_Empire
* @uses Legacies_Empire_User
*/
class Legacies_Empire_Model_Planet_Resource
extends Legacies_Core_Entity_SubTable
{
public function _init()
{
$this->_tableName = 'planet_resource';
$this->_idFieldNames = array(
'resource_code',
'planet_id'
);
}
}

View file

@ -205,7 +205,7 @@ class Legacies_Empire_Model_User
$planetCollection = new Legacies_Core_Collection(array('planet' => 'planets'), 'Legacies_Empire_Model_Planet');
$planetCollection->where('id_owner=:user');
$order = ($user['planet_sort_order'] == 1) ? 'DESC' : 'ASC';
$order = ($this->getData('planet_sort_order') == 1) ? 'DESC' : 'ASC';
switch ($this->getData('planet_sort')) {
case self::PLANET_SORT_POSITION:
@ -298,4 +298,19 @@ class Legacies_Empire_Model_User
return $this->hasData($fields[$elementId]) && Math::comp($this->getElement($elementId), $levelRequired) > 0;
}
public function getSkinPath($default = null)
{
if ($path = $this->getData('dpath')) {
return $path;
}
return $default;
}
public function setSkinPath($path)
{
$this->setData('dpath', $path);
return $this;
}
}