Fixed login
Started building page cleanup Modified code pools Backported layout/blocks/templates 3-step view system Reforged Overview page Moved graphics, added topnav template, fixed number render bug Added BCMath optionnal usage Updated BC Math comparison for capped resources Fixed planet resource production updates Various additions Signed-off-by: Gregory PLANCHAT <g.planchat@gmail.com>
This commit is contained in:
parent
3fd8832b45
commit
f2d4b0f7e2
384 changed files with 2727 additions and 1364 deletions
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Block_Overview
|
||||
extends Legacies_Core_Block_Template
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Block_Overview_DestroyPlanet
|
||||
extends Legacies_Core_Block_Html_Form
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Block_Overview_RenamePlanet
|
||||
extends Legacies_Core_Block_Html_Form
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Block_PlanetList
|
||||
extends Legacies_Core_Block_Template
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Block_Topnav
|
||||
extends Legacies_Core_Block_Template
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @return Legacies_Empire_Model_User
|
||||
*/
|
||||
public function getCurrentUser()
|
||||
{
|
||||
return Legacies_Empire_Model_User::getSingleton();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Legacies_Empire_Model_Planet
|
||||
*/
|
||||
public function getCurrentPlanet()
|
||||
{
|
||||
return $this->getCurrentUser()->getCurrentPlanet();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Legacies_Core_Collection
|
||||
*/
|
||||
public function getPlanetCollection()
|
||||
{
|
||||
return $this->getCurrentUser()->getPlanetCollection();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
<?php
|
||||
|
||||
interface Legacies_Empire_Exception extends Legacies_Exception {}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Builder_Break
|
||||
extends Exception
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,221 @@
|
|||
<?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;
|
||||
|
||||
$this->init();
|
||||
}
|
||||
|
||||
abstract public function init();
|
||||
|
||||
abstract protected function _initItem();
|
||||
|
||||
public function enqueue()
|
||||
{
|
||||
$params = func_get_args();
|
||||
$item = call_user_func_array(array($this, '_initItem'), $params);
|
||||
$item->setIndex($this->_index);
|
||||
$this->_queue[$this->_index++] = $item;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function dequeue($item)
|
||||
{
|
||||
unset($this->_queue[(int) $item->getIndex()]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public 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();
|
||||
|
||||
$unserialized = @unserialize($serialized);
|
||||
if ($unserialized === false) {
|
||||
$this->_queue = array();
|
||||
return $this;
|
||||
}
|
||||
|
||||
foreach ($unserialized as $itemData) {
|
||||
call_user_func_array(array($this, 'enqueue'), $itemData);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->_serializeQueue();
|
||||
}
|
||||
|
||||
public function getQueue()
|
||||
{
|
||||
return $this->_queue;
|
||||
}
|
||||
|
||||
public function clearQueue()
|
||||
{
|
||||
$this->_queue = array();
|
||||
}
|
||||
|
||||
abstract public function updateQueue($time);
|
||||
abstract public function appendQueue($typeId, $qty, $time);
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
abstract public function getResourcesNeeded($typeId, $level);
|
||||
abstract public function getBuildingTime($typeId, $level);
|
||||
|
||||
public function serialize()
|
||||
{
|
||||
return $this->_serializeQueue();
|
||||
}
|
||||
|
||||
public function unserialize($serialized)
|
||||
{
|
||||
$this->_unserializeQueue($serialized);
|
||||
}
|
||||
|
||||
public function count()
|
||||
{
|
||||
return count($this->_queue);
|
||||
}
|
||||
|
||||
public function current()
|
||||
{
|
||||
return current($this->_queue);
|
||||
}
|
||||
|
||||
public function next()
|
||||
{
|
||||
return next($this->_queue);
|
||||
}
|
||||
|
||||
public function key()
|
||||
{
|
||||
return key($this->_queue);
|
||||
}
|
||||
|
||||
public function valid()
|
||||
{
|
||||
return current($this->_queue) !== false;
|
||||
}
|
||||
|
||||
public function rewind()
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
*
|
||||
* @uses Legacies_Object
|
||||
* @uses Legacies_Empire
|
||||
*/
|
||||
class Legacies_Empire_Model_Fleet
|
||||
extends Legacies_Core_Entity
|
||||
{
|
||||
protected static $_instances = array();
|
||||
|
||||
protected $_eventPrefix = 'fleet';
|
||||
protected $_eventObject = 'fleet';
|
||||
|
||||
public static function factory($id)
|
||||
{
|
||||
if ($id === null) {
|
||||
return new self();
|
||||
}
|
||||
|
||||
$id = intval($id);
|
||||
if (!isset(self::$_instances[$id])) {
|
||||
$instance = new self();
|
||||
$params = func_get_args();
|
||||
call_user_func_array(array($instance, 'load'), $params);
|
||||
self::$_instances[$id] = $instance;
|
||||
}
|
||||
return self::$_instances[$id];
|
||||
}
|
||||
|
||||
protected function _init()
|
||||
{
|
||||
$this->setIdFieldName('fleet_id');
|
||||
$this->setTableName('fleets');
|
||||
}
|
||||
|
||||
public static function planetListener($eventData)
|
||||
{
|
||||
}
|
||||
|
||||
public function isOwnedBy(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
if ($this->getData('fleet_owner') == $user->getId()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isMission($missionType)
|
||||
{
|
||||
if ($this->getData('fleet_mission') == $missionType) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getRowClass()
|
||||
{
|
||||
if ($this->isMission(Legacies_Empire::ID_MISSION_ATTACK) || $this->isMission(Legacies_Empire::ID_MISSION_GROUP_ATTACK)) {
|
||||
return 'attack';
|
||||
} else if ($this->isMission(Legacies_Empire::ID_MISSION_TRANSPORT)) {
|
||||
return 'transport';
|
||||
} else if ($this->isMission(Legacies_Empire::ID_MISSION_STATION) || $this->isMission(Legacies_Empire::ID_MISSION_STATION_ALLY)) {
|
||||
return 'station';
|
||||
} else if ($this->isMission(Legacies_Empire::ID_MISSION_SETTLE_COLONY)) {
|
||||
return 'settle';
|
||||
} else if ($this->isMission(Legacies_Empire::ID_MISSION_RECYCLE)) {
|
||||
return 'recycle';
|
||||
} else if ($this->isMission(Legacies_Empire::ID_MISSION_DESTROY)) {
|
||||
return 'destroy';
|
||||
} else if ($this->isMission(Legacies_Empire::ID_MISSION_MISSILES)) {
|
||||
return 'missiles';
|
||||
} else if ($this->isMission(Legacies_Empire::ID_MISSION_EXPEDITION)) {
|
||||
return 'expedition';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Galaxy_Position
|
||||
extends Legacies_Core_Entity_SubTable
|
||||
{
|
||||
protected function _init()
|
||||
{
|
||||
$this->_tableName = 'galaxy';
|
||||
$this->_idFieldNames = array('id_planet');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
abstract class Legacies_Empire_Model_Game_Abstract
|
||||
extends Legacies_Core_Model
|
||||
implements Iterator, Countable
|
||||
{
|
||||
public function count()
|
||||
{
|
||||
return count($this->_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);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
*
|
||||
* @uses Legacies_Object
|
||||
* @uses Legacies_Empire
|
||||
*/
|
||||
class Legacies_Empire_Model_Game_Combat
|
||||
extends Legacies_Empire_Model_Game_Abstract
|
||||
implements Legacies_Core_Singleton
|
||||
{
|
||||
private static $_singleton = null;
|
||||
|
||||
public static function getSingleton()
|
||||
{
|
||||
if (self::$_singleton === null) {
|
||||
self::$_singleton = new self();
|
||||
}
|
||||
return self::$_singleton;
|
||||
}
|
||||
|
||||
protected function _init()
|
||||
{
|
||||
foreach (include ROOT_PATH . 'includes/data/combat.php' as $elementId => $fieldName) {
|
||||
$this->setData($elementId, $fieldName);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _load()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _save()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _delete()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
*
|
||||
* @uses Legacies_Object
|
||||
* @uses Legacies_Empire
|
||||
*/
|
||||
class Legacies_Empire_Model_Game_FieldsAlias
|
||||
extends Legacies_Empire_Model_Game_Abstract
|
||||
implements Legacies_Core_Singleton
|
||||
{
|
||||
private static $_singleton = null;
|
||||
|
||||
public static function getSingleton()
|
||||
{
|
||||
if (self::$_singleton === null) {
|
||||
self::$_singleton = new self();
|
||||
}
|
||||
return self::$_singleton;
|
||||
}
|
||||
|
||||
protected function _init()
|
||||
{
|
||||
foreach (include ROOT_PATH . 'includes/data/fields-alias.php' as $elementId => $fieldName) {
|
||||
$this->setData($elementId, $fieldName);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _load()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _save()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _delete()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
*
|
||||
* @uses Legacies_Object
|
||||
* @uses Legacies_Empire
|
||||
*/
|
||||
class Legacies_Empire_Model_Game_Prices
|
||||
extends Legacies_Empire_Model_Game_Abstract
|
||||
implements Legacies_Core_Singleton
|
||||
{
|
||||
private static $_singleton = null;
|
||||
|
||||
public static function getSingleton()
|
||||
{
|
||||
if (self::$_singleton === null) {
|
||||
self::$_singleton = new self();
|
||||
}
|
||||
return self::$_singleton;
|
||||
}
|
||||
|
||||
protected function _init()
|
||||
{
|
||||
foreach (include ROOT_PATH . 'includes/data/prices.php' as $elementId => $fieldName) {
|
||||
$this->setData($elementId, $fieldName);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _load()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _save()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _delete()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
*
|
||||
* @uses Legacies_Object
|
||||
* @uses Legacies_Empire
|
||||
*/
|
||||
class Legacies_Empire_Model_Game_Production
|
||||
extends Legacies_Empire_Model_Game_Abstract
|
||||
implements Legacies_Core_Singleton
|
||||
{
|
||||
private static $_singleton = null;
|
||||
|
||||
public static function getSingleton()
|
||||
{
|
||||
if (self::$_singleton === null) {
|
||||
self::$_singleton = new self();
|
||||
}
|
||||
return self::$_singleton;
|
||||
}
|
||||
|
||||
protected function _init()
|
||||
{
|
||||
foreach (include ROOT_PATH . 'includes/data/production.php' as $elementId => $fieldName) {
|
||||
$this->setData($elementId, $fieldName);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _load()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _save()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _delete()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
*
|
||||
* @uses Legacies_Object
|
||||
* @uses Legacies_Empire
|
||||
*/
|
||||
class Legacies_Empire_Model_Game_Requirements
|
||||
extends Legacies_Empire_Model_Game_Abstract
|
||||
implements Legacies_Core_Singleton
|
||||
{
|
||||
private static $_singleton = null;
|
||||
|
||||
public static function getSingleton()
|
||||
{
|
||||
if (self::$_singleton === null) {
|
||||
self::$_singleton = new self();
|
||||
}
|
||||
return self::$_singleton;
|
||||
}
|
||||
|
||||
protected function _init()
|
||||
{
|
||||
foreach (include ROOT_PATH . 'includes/data/requirements.php' as $elementId => $fieldName) {
|
||||
$this->setData($elementId, $fieldName);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _load()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _save()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _delete()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
*
|
||||
* @uses Legacies_Object
|
||||
* @uses Legacies_Empire
|
||||
*/
|
||||
class Legacies_Empire_Model_Game_Resources
|
||||
extends Legacies_Empire_Model_Game_Abstract
|
||||
implements Legacies_Core_Singleton
|
||||
{
|
||||
private static $_singleton = null;
|
||||
|
||||
public static function getSingleton()
|
||||
{
|
||||
if (self::$_singleton === null) {
|
||||
self::$_singleton = new self();
|
||||
}
|
||||
return self::$_singleton;
|
||||
}
|
||||
|
||||
protected function _init()
|
||||
{
|
||||
foreach (include ROOT_PATH . 'includes/data/resources.php' as $elementId => $fieldName) {
|
||||
$this->setData($elementId, $fieldName);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _load()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _save()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _delete()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
*
|
||||
* @uses Legacies_Object
|
||||
* @uses Legacies_Empire
|
||||
*/
|
||||
class Legacies_Empire_Model_Game_Types
|
||||
extends Legacies_Empire_Model_Game_Abstract
|
||||
implements Legacies_Core_Singleton
|
||||
{
|
||||
private static $_singleton = null;
|
||||
|
||||
public static function getSingleton()
|
||||
{
|
||||
if (self::$_singleton === null) {
|
||||
self::$_singleton = new self();
|
||||
}
|
||||
return self::$_singleton;
|
||||
}
|
||||
|
||||
protected function _init()
|
||||
{
|
||||
foreach (include ROOT_PATH . 'includes/data/types.php' as $elementId => $fieldName) {
|
||||
$this->setData($elementId, $fieldName);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _load()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _save()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _delete()
|
||||
{
|
||||
// NOP
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function is($element, $type)
|
||||
{
|
||||
if (!is_array($this->getData($type))) {
|
||||
return false;
|
||||
}
|
||||
return in_array($element, $this->getData($type));
|
||||
}
|
||||
}
|
||||
894
includes/application/code/core/Legacies/Empire/Model/Planet.php
Normal file
894
includes/application/code/core/Legacies/Empire/Model/Planet.php
Normal file
|
|
@ -0,0 +1,894 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* @uses Legacies_Object
|
||||
* @uses Legacies_Empire
|
||||
* @uses Legacies_Empire_User
|
||||
*/
|
||||
class Legacies_Empire_Model_Planet
|
||||
extends Legacies_Core_Entity
|
||||
{
|
||||
const TYPE_PLANET = 1;
|
||||
const TYPE_DEBRIS = 2;
|
||||
const TYPE_MOON = 3;
|
||||
|
||||
protected $_eventPrefix = 'planet';
|
||||
protected $_eventObject = 'planet';
|
||||
|
||||
/**
|
||||
* @var Legacies_Empire_Model_User
|
||||
*/
|
||||
protected $_user = null;
|
||||
|
||||
/**
|
||||
* @var Legacies_Empire_Model_Planet
|
||||
*/
|
||||
protected $_moon = null;
|
||||
|
||||
/**
|
||||
* @var Legacies_Empire_Model_Planet_Builder
|
||||
*/
|
||||
protected $_builder = null;
|
||||
|
||||
/**
|
||||
* @var Legacies_Empire_Model_Planet_Building_Shipyard
|
||||
*/
|
||||
protected $_shipyard = null;
|
||||
|
||||
/**
|
||||
* @var Legacies_Empire_Model_Planet_Building_Laboratory
|
||||
*/
|
||||
protected $_laboratory = null;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected static $_instances = array();
|
||||
|
||||
protected static $_productionInstances = array();
|
||||
|
||||
public static function factory($id)
|
||||
{
|
||||
if ($id === null) {
|
||||
return new self();
|
||||
}
|
||||
|
||||
$id = intval($id);
|
||||
if (!isset(self::$_instances[$id])) {
|
||||
$instance = new self();
|
||||
$params = func_get_args();
|
||||
call_user_func_array(array($instance, 'load'), $params);
|
||||
self::$_instances[$id] = $instance;
|
||||
}
|
||||
return self::$_instances[$id];
|
||||
}
|
||||
|
||||
public function _init()
|
||||
{
|
||||
$this->_tableName = 'planets';
|
||||
$this->_idFieldName = 'id';
|
||||
}
|
||||
|
||||
public function _afterLoad()
|
||||
{
|
||||
parent::_afterLoad();
|
||||
|
||||
$this->getBuildingQueue()->init();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLastUpdate()
|
||||
{
|
||||
return $this->getData('last_update');
|
||||
}
|
||||
|
||||
public function setLastUpdate($time)
|
||||
{
|
||||
return $this->setData('last_update', $time);
|
||||
}
|
||||
|
||||
public function getBuildingFields()
|
||||
{
|
||||
return $this->getData('field_max');
|
||||
}
|
||||
|
||||
public function getUsedFields()
|
||||
{
|
||||
return $this->getData('field_current');
|
||||
}
|
||||
|
||||
public function getGalaxyData()
|
||||
{
|
||||
$entity = new Legacies_Empire_Model_Galaxy_Position();
|
||||
$entity->load(array('id_planet' => $this->getId()));
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
public function updateStorages($time = null)
|
||||
{
|
||||
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) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//$officerEnhancement = (.5 * $this->getUser()->getData('rpg_stockeur')) + 1;
|
||||
$officerEnhancement = 1;
|
||||
|
||||
$storageEnhancementFactor = Math::floor(Math::pow(1.6, $this[Legacies_Empire::getFieldName($resourceData['storage'])]));
|
||||
$storageEnhancement = Math::mul(BASE_STORAGE_SIZE / 2, $storageEnhancementFactor);
|
||||
|
||||
$value = Math::mul(MAX_OVERFLOW, Math::mul($officerEnhancement, Math::add(BASE_STORAGE_SIZE, $storageEnhancement)));
|
||||
$this->setData($resourceData['storage_field'], Math::floor($value));
|
||||
}
|
||||
Math::setPrecision();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function updateBuildingFields()
|
||||
{
|
||||
$types = Legacies_Empire_Model_Game_Types::getSingleton();
|
||||
|
||||
if ($this->isPlanet()) {
|
||||
$filter = Legacies_Empire::TYPE_BUILDING_PLANET;
|
||||
} else if ($this->isMoon()) {
|
||||
$filter = Legacies_Empire::TYPE_BUILDING_MOON;
|
||||
} else {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$usedFields = 0;
|
||||
foreach ($types[$filter] as $buildingId) {
|
||||
$usedFields += $this->getElement($buildingId);
|
||||
}
|
||||
|
||||
$this->setData('field_current', $usedFields);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function updateResourceProduction($time = null)
|
||||
{
|
||||
$types = Legacies_Empire_Model_Game_Types::getSingleton();
|
||||
$resources = Legacies_Empire_Model_Game_Resources::getSingleton();
|
||||
$production = Legacies_Empire_Model_Game_Production::getSingleton();
|
||||
|
||||
if ($time === null) {
|
||||
$time = Legacies::now();
|
||||
}
|
||||
|
||||
if (!$this->isPlanet()) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute resources consumers and resources producers
|
||||
*/
|
||||
$resourcesTotals = array();
|
||||
$resourcesProductionTotals = array();
|
||||
$resourcesConsumptionTotals = array();
|
||||
$resourcesProducers = array();
|
||||
$resourcesConsumers = array();
|
||||
$productionRatios = array();
|
||||
foreach ($resources->getAllDatas() as $resource => $resourceData) {
|
||||
foreach ($resourceData['production'] as $productionUnit => $ratioField) {
|
||||
if (!$types->is($productionUnit, Legacies_Empire::TYPE_PRODUCTION)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$level = $this->getData(Legacies_Empire::getFieldName($productionUnit));
|
||||
$ratio = $this->getData($ratioField);
|
||||
$element = self::getProductionElementInstance($productionUnit);
|
||||
|
||||
$productionRatios[$productionUnit] = $element->getProductionRatios($level, $ratio, $this, $this->getUser());
|
||||
foreach ($productionRatios[$productionUnit] as $resourceId => $resourceProduction) {
|
||||
$comp = Math::comp($resourceProduction, 0);
|
||||
if ($comp < 0) {
|
||||
if (!isset($resourcesConsumers[$resourceId])) {
|
||||
$resourcesConsumers[$resourceId] = array();
|
||||
}
|
||||
$resourcesConsumers[$resourceId][] = $productionUnit;
|
||||
|
||||
if (!isset($resourcesConsumptionTotals[$resourceId])) {
|
||||
$resourcesConsumptionTotals[$resourceId] = 0;
|
||||
}
|
||||
$resourcesConsumptionTotals[$resourceId] = Math::add($resourcesConsumptionTotals[$resourceId], $resourceProduction);
|
||||
|
||||
if (!isset($resourcesTotals[$resourceId])) {
|
||||
$resourcesTotals[$resourceId] = 0;
|
||||
}
|
||||
$resourcesTotals[$resourceId] = Math::add($resourcesTotals[$resourceId], $resourceProduction);
|
||||
} else if ($comp > 0) {
|
||||
if (!isset($resourcesProducers[$resourceId])) {
|
||||
$resourcesProducers[$resourceId] = array();
|
||||
}
|
||||
$resourcesProducers[$resourceId][] = $productionUnit;
|
||||
|
||||
if (!isset($resourcesProductionTotals[$resourceId])) {
|
||||
$resourcesProductionTotals[$resourceId] = 0;
|
||||
}
|
||||
$resourcesProductionTotals[$resourceId] = Math::add($resourcesProductionTotals[$resourceId], $resourceProduction);
|
||||
|
||||
if (!isset($resourcesTotals[$resourceId])) {
|
||||
$resourcesTotals[$resourceId] = 0;
|
||||
}
|
||||
$resourcesTotals[$resourceId] = Math::add($resourcesTotals[$resourceId], $resourceProduction);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute resource consumption
|
||||
*/
|
||||
$actualProduction = $resourcesTotals;
|
||||
foreach ($resourcesTotals as $resource => $total) {
|
||||
if (Math::isPositiveOrZero($total)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$productionRatio = 0;
|
||||
if (isset($resourcesProductionTotals[$resource]) && Math::isPositive($resourcesProductionTotals[$resource])) {
|
||||
$consumptions = Math::mul(-1, $resourcesConsumptionTotals[$resource]);
|
||||
Math::setPrecision(50);
|
||||
$productionRatio = Math::div($resourcesProductionTotals[$resource], $consumptions);
|
||||
Math::setPrecision();
|
||||
}
|
||||
|
||||
foreach ($resourcesConsumers[$resource] as $consumer) {
|
||||
foreach ($productionRatios[$consumer] as $resourceId => $ratio) {
|
||||
Math::setPrecision(50);
|
||||
$productionOverhead = Math::mul(Math::sub(1, $productionRatio), $ratio);
|
||||
Math::setPrecision();
|
||||
|
||||
$actualProduction[$resourceId] = Math::sub($actualProduction[$resourceId], $productionOverhead);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dispatch 'planet.update-production.before' event
|
||||
Legacies::dispatchEvent($this->_eventPrefix . '.update-production.before', array(
|
||||
$this->_eventObject => $this,
|
||||
'user' => $this->getUser(),
|
||||
'productions' => $actualProduction
|
||||
));
|
||||
|
||||
foreach ($actualProduction as $resource => $production) {
|
||||
if ($resources[$resource]['storage_field']) {
|
||||
$this->setData($resources[$resource]['production_field'], $production);
|
||||
} else if (isset($resourcesProductionTotals[$resource])) {
|
||||
$this->setData($resources[$resource]['field'], $production);
|
||||
$this->setData($resources[$resource]['production_field'], $resourcesProductionTotals[$resource]);
|
||||
}
|
||||
}
|
||||
|
||||
// Dispatch 'planet.update-production.after' event
|
||||
Legacies::dispatchEvent($this->_eventPrefix . '.update-production.after', array(
|
||||
$this->_eventObject => $this,
|
||||
'user' => $this->getUser()
|
||||
));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function updateResources($time = null)
|
||||
{
|
||||
$resources = Legacies_Empire_Model_Game_Resources::getSingleton();
|
||||
|
||||
if ($time === null) {
|
||||
$time = Legacies::now();
|
||||
}
|
||||
|
||||
$timeDiff = ($time - $this->getLastUpdate()) / 3600;
|
||||
foreach ($resources->getAllDatas() as $resourceId => $resourceConfig) {
|
||||
if (!$resourceConfig['storage_field']) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$production = Math::mul($this->getData($resourceConfig['production_field']), $timeDiff);
|
||||
$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;
|
||||
}
|
||||
|
||||
public static function getProductionElementInstance($buildingId)
|
||||
{
|
||||
$production = Legacies_Empire_Model_Game_Production::getSingleton();
|
||||
|
||||
if (!isset(self::$_productionInstances[$buildingId])) {
|
||||
if (!isset($production[$buildingId])) {
|
||||
return null;
|
||||
}
|
||||
$class = $production[$buildingId][Legacies_Empire::RESOURCE_CLASS];
|
||||
$reflection = new ReflectionClass($class);
|
||||
self::$_productionInstances[$buildingId] = $reflection->newInstance();
|
||||
}
|
||||
|
||||
return self::$_productionInstances[$buildingId];
|
||||
}
|
||||
|
||||
public function getFleetCollection($time = null)
|
||||
{
|
||||
$firstCollection = new Legacies_Core_Collection(array('fleet' => 'fleets'));
|
||||
$firstCollection
|
||||
->column('*')
|
||||
->where('fleet.fleet_start_galaxy = :galaxy')
|
||||
->where('fleet.fleet_start_system = :system')
|
||||
->where('fleet.fleet_start_planet = :position')
|
||||
->where('fleet.fleet_start_type = :planet_type')
|
||||
;
|
||||
$backCollection = new Legacies_Core_Collection(array('fleet' => 'fleets'));
|
||||
$backCollection
|
||||
->column('*')
|
||||
->where('fleet.fleet_end_galaxy = :galaxy')
|
||||
->where('fleet.fleet_end_system = :system')
|
||||
->where('fleet.fleet_end_planet = :position')
|
||||
->where('fleet.fleet_end_type = :planet_type')
|
||||
;
|
||||
|
||||
$options = array(
|
||||
'galaxy' => $this->getSystem(),
|
||||
'system' => $this->getSystem(),
|
||||
'position' => $this->getPosition(),
|
||||
'planet_type' => $this->getType()
|
||||
);
|
||||
|
||||
if ($time !== null) {
|
||||
$options['now'] = $time;
|
||||
$firstCollection->where('fleet_start_time <= :now');
|
||||
$backCollection->where('fleet_end_time <= :now');
|
||||
}
|
||||
|
||||
$collection = new Legacies_Core_Collection();
|
||||
$collection
|
||||
->setEntityClassName('Legacies_Empire_Model_Fleet')
|
||||
->union($firstCollection)
|
||||
->union($backCollection)
|
||||
->load($options)
|
||||
;
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
public function getUserId()
|
||||
{
|
||||
return (int) $this->getData('id_owner');
|
||||
}
|
||||
|
||||
public function getUser()
|
||||
{
|
||||
if ($this->_user === null && $this->getUserId()) {
|
||||
$this->_user = Legacies_Empire_Model_User::factory($this->getUserId());
|
||||
}
|
||||
return $this->_user;
|
||||
}
|
||||
|
||||
public function setUser(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
$this->_user = $user;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getShipyard()
|
||||
{
|
||||
if ($this->_shipyard === null) {
|
||||
$this->_shipyard = new Legacies_Empire_Model_Planet_Building_Shipyard($this, $this->getUser());
|
||||
}
|
||||
return $this->_shipyard;
|
||||
}
|
||||
|
||||
public function setShipyard(Legacies_Empire_Model_Planet_Building_Shipyard $shipyard)
|
||||
{
|
||||
$this->_shipyard = $shipyard;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLaboratory()
|
||||
{
|
||||
if ($this->_laboratory === null) {
|
||||
$this->_laboratory = new Legacies_Empire_Model_Planet_Building_Laboratory($this, $this->getUser());
|
||||
}
|
||||
return $this->_laboratory;
|
||||
}
|
||||
|
||||
public function setLaboratory(Legacies_Empire_Model_Planet_Building_Laboratory $laboratory)
|
||||
{
|
||||
$this->_laboratory = $laboratory;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isPlanet()
|
||||
{
|
||||
return (bool) ($this->getData('planet_type') == self::TYPE_PLANET);
|
||||
}
|
||||
|
||||
public function isDebris()
|
||||
{
|
||||
return (bool) ($this->getData('planet_type') == self::TYPE_DEBRIS);
|
||||
}
|
||||
|
||||
public function isMoon()
|
||||
{
|
||||
return (bool) ($this->getData('planet_type') == self::TYPE_MOON);
|
||||
}
|
||||
|
||||
public function getMoon()
|
||||
{
|
||||
static $statement = null;
|
||||
|
||||
if ($this->isMoon()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->_moon === null) {
|
||||
if ($statement === null) {
|
||||
$statement = new Legacies_Core_Collection(array('planet' => 'planets'), get_class($this));
|
||||
$statement
|
||||
->where('galaxy=:galaxy')
|
||||
->where('system=:system')
|
||||
->where('planet=:position')
|
||||
->where('planet_type=' . strval(self::TYPE_MOON))
|
||||
;
|
||||
}
|
||||
$statement->load(array(
|
||||
'galaxy' => $this->getGalaxy(),
|
||||
'system' => $this->getSystem(),
|
||||
'position' => $this->getPosition()
|
||||
));
|
||||
|
||||
$this->_moon = $statement->current();
|
||||
}
|
||||
return $this->_moon;
|
||||
}
|
||||
|
||||
public function setGalaxy($galaxy)
|
||||
{
|
||||
$this->setData('galaxy', $galaxy);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getGalaxy()
|
||||
{
|
||||
return (int) $this->getData('galaxy');
|
||||
}
|
||||
|
||||
public function setSystem($system)
|
||||
{
|
||||
$this->setData('system', $system);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSystem()
|
||||
{
|
||||
return (int) $this->getData('system');
|
||||
}
|
||||
|
||||
public function setPosition($position)
|
||||
{
|
||||
$this->setData('planet', $position);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPosition()
|
||||
{
|
||||
return (int) $this->getData('planet');
|
||||
}
|
||||
|
||||
public function getCoords()
|
||||
{
|
||||
return sprintf('%d:%d:%d', $this->getGalaxy(), $this->getSystem(), $this->getPosition());
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return $this->getData('name');
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->setData('name', $name);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getImage()
|
||||
{
|
||||
return 'graphics/planeten/' . $this->getData('image') . '.jpg';
|
||||
}
|
||||
|
||||
public function getSmallImage()
|
||||
{
|
||||
return 'graphics/planeten/small/s_' . $this->getData('image') . '.jpg';
|
||||
}
|
||||
|
||||
public function setType($type)
|
||||
{
|
||||
$this->setData('planet_type', $type);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getType()
|
||||
{
|
||||
return (int) $this->getData('planet_type');
|
||||
}
|
||||
|
||||
public function isDestroyed()
|
||||
{
|
||||
return (bool) $this->getData('destruyed');
|
||||
}
|
||||
|
||||
public function destroy()
|
||||
{
|
||||
if ($this->isDebris()) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
if ($this->isDestroyed()) {
|
||||
throw new Legacies_Empire_Model_Planet_Exception(Legacies::__('Planet is already destroyed.'));
|
||||
}
|
||||
|
||||
if ($this->getFleetCollection()->count() > 0) {
|
||||
throw new Legacies_Empire_Model_Planet_Exception(Legacies::__("Could not delete planet until fleets aten't retuned."));
|
||||
}
|
||||
|
||||
$user = $this->getUser();
|
||||
if ($user->getHomePlanet()->getId() == $this->getId()) {
|
||||
throw new Legacies_Empire_Model_Planet_Exception(Legacies::__("You can't destroy your home planet."));
|
||||
}
|
||||
|
||||
if ($user->getCurrentPlanet()->getId() == $this->getId()) {
|
||||
$homePlanet = $user->getHomePlanet();
|
||||
|
||||
if ($homePlanet !== null) {
|
||||
$user->updateCurrentPlanet($homePlanet);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->isPlanet() && ($moon = $this->getMoon())) {
|
||||
$moon->destroy();
|
||||
}
|
||||
|
||||
$this
|
||||
->setData('destruyed', true)
|
||||
->setData('id_owner', 0)
|
||||
->save();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getElement($elementId)
|
||||
{
|
||||
$fields = Legacies_Empire_Model_Game_FieldsAlias::getSingleton();
|
||||
|
||||
return $this->getData($fields[$elementId]);
|
||||
}
|
||||
|
||||
public function setElement($elementId, $level)
|
||||
{
|
||||
$fields = Legacies_Empire_Model_Game_FieldsAlias::getSingleton();
|
||||
|
||||
return $this->setData($fields[$elementId], $level);
|
||||
}
|
||||
|
||||
public function hasElement($elementId, $levelRequired = 0)
|
||||
{
|
||||
$fields = Legacies_Empire_Model_Game_FieldsAlias::getSingleton();
|
||||
|
||||
return $this->hasData($fields[$elementId]) && Math::comp($this->getElement($elementId), $levelRequired) > 0;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if ($destroy === false && !$this->checkAvailability($buildingId)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Dispatch 'planet.building.append-queue.before' event
|
||||
Legacies::dispatchEvent($this->_eventPrefix . '.building.append-queue.before', array(
|
||||
'bulding_id' => $buildingId,
|
||||
'level' => $this->getElement($buildingId),
|
||||
$this->_eventObject => $this,
|
||||
'user' => $this->getUser(),
|
||||
'destroy' => $destroy
|
||||
));
|
||||
|
||||
if ($destroy === false) {
|
||||
$level = $this->getElement($buildingId) + 1;
|
||||
} else {
|
||||
$level = max($this->getElement($buildingId) - 1, 0);
|
||||
}
|
||||
|
||||
$this->getBuildingQueue()->appendQueue($buildingId, $level, $time);
|
||||
|
||||
// Dispatch 'planet.shipyard.append-queue.after' event
|
||||
Legacies::dispatchEvent($this->_eventPrefix . '.building.append-queue.after', array(
|
||||
'bulding_id' => $buildingId,
|
||||
'level' => $this->getElement($buildingId),
|
||||
$this->_eventObject => $this,
|
||||
'user' => $this->getUser(),
|
||||
'destroy' => $destroy
|
||||
));
|
||||
|
||||
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,
|
||||
$this->_eventObject => $this->_currentPlanet,
|
||||
'user' => $this->_currentUser
|
||||
));
|
||||
|
||||
$this->getBuildingQueue()->updateQueue($time);
|
||||
|
||||
// Dispatch event
|
||||
Legacies::dispatchEvent($this->_eventPrefix . '.update-queue.after', array(
|
||||
'time' => $time,
|
||||
'shipyard' => $this,
|
||||
$this->_eventObject => $this->_currentPlanet,
|
||||
'user' => $this->_currentUser
|
||||
));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Legacies_Empire_Model_Planet_Builder
|
||||
*/
|
||||
public function getBuildingQueue()
|
||||
{
|
||||
if ($this->_builder === null) {
|
||||
$this->_builder = new Legacies_Empire_Model_Planet_Builder($this, $this->getUser());
|
||||
}
|
||||
return $this->_builder;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
* @return Legacies_Empire_Model_Planet
|
||||
*/
|
||||
public function dequeueBuilding()
|
||||
{
|
||||
$this->getBuildingQueue()->rewind();
|
||||
$item = $this->getBuildingQueue()->current();
|
||||
|
||||
$this->getBuildingQueue()->dequeueFirstItem(Legacies::now());
|
||||
|
||||
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)
|
||||
{
|
||||
$this->getBuildingQueue()->checkAvailability($buildingId);
|
||||
|
||||
try {
|
||||
// Dispatch event. Throw an exception to break the avaliability.
|
||||
Legacies::dispatchEvent($this->_eventPrefix . '.check-availability', array(
|
||||
'ship_id' => $buildingId,
|
||||
'shipyard' => $this,
|
||||
$this->_eventObject => $this->_currentPlanet,
|
||||
'user' => $this->_currentUser
|
||||
));
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the resources needed to build a specific building
|
||||
*
|
||||
* @param int $buildingId
|
||||
* @return bool
|
||||
*/
|
||||
public function getResourcesNeeded($buildingId, $level)
|
||||
{
|
||||
return $this->getBuildingQueue()->getResourcesNeeded($buildingId, $level);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the time needed to build a specific building
|
||||
*
|
||||
* @param int $buildingId
|
||||
* @return bool
|
||||
*/
|
||||
public function getBuildingTime($buildingId, $level)
|
||||
{
|
||||
return $this->getBuildingQueue()->getBuildingTime($buildingId, $level);
|
||||
}
|
||||
|
||||
public static function registrationListener($eventData)
|
||||
{
|
||||
if (isset($eventData['user'])) {
|
||||
$user = $eventData['user'];
|
||||
$request = $eventData['request'];
|
||||
|
||||
if ($user === null || !$user instanceof Legacies_Empire_Model_User || $user->getId()) {
|
||||
return;
|
||||
}
|
||||
if ($request === null || !$request instanceof Legacies_Core_Controller_Request) {
|
||||
return;
|
||||
}
|
||||
|
||||
$collection = new Legacies_Core_Collection('planets');
|
||||
$collection
|
||||
->column(array(
|
||||
'galaxy' => 'planet.galaxy',
|
||||
'system' => 'planet.system',
|
||||
'count' => 'COUNT(planet.id)'
|
||||
))
|
||||
->group('planet.galaxy')
|
||||
->group('planet.system')
|
||||
->where('planet.planet_type=1')
|
||||
->order('COUNT(planet.id)', 'ASC')
|
||||
->order('RAND()', 'ASC')
|
||||
->limit(1)
|
||||
;
|
||||
|
||||
$params = array();
|
||||
$galaxy = $request->getParam('system');
|
||||
if ($galaxy !== null) {
|
||||
$collection->where('planet.galaxy=:galaxy');
|
||||
$params['galaxy'] = $galaxy;
|
||||
|
||||
$systems = explode(',', $request->getParam('system'));
|
||||
if (is_array($systems) && count($systems) == 2 && is_int($systems[0]) && is_int($systems[1])) {
|
||||
$collection->where('planet.system IN(' . implode(', ', range($systems[0], $systems[1])) . ')');
|
||||
}
|
||||
}
|
||||
$collection->load($params);
|
||||
|
||||
if ($collection->count() == 0) {
|
||||
throw new Exception('No planet to colonize there!'); // FIXME
|
||||
}
|
||||
|
||||
$systemInfo = $collection->current();
|
||||
if ($systemInfo->getData('count') >= MAX_PLANET_IN_SYSTEM) {
|
||||
throw new Exception('No planet to colonize there!'); // FIXME
|
||||
}
|
||||
$system = $systemInfo->getData('system');
|
||||
$galaxy = $systemInfo->getData('galaxy');
|
||||
|
||||
$collection = new Legacies_Core_Collection('planets');
|
||||
$collection
|
||||
->column(array('position' => 'planet.position'))
|
||||
->where('planet.planet_type=1')
|
||||
->where('planet.planet_type=:system')
|
||||
->load()
|
||||
;
|
||||
$positions = range(1, MAX_PLANET_IN_SYSTEM);
|
||||
foreach ($collection as $planet) {
|
||||
$key = array_search($planet->getData('position'), $positions);
|
||||
if ($key !== false) {
|
||||
unset($positions[$key]);
|
||||
}
|
||||
}
|
||||
$key = array_rand($positions, 1);
|
||||
$finalPosition = $positions[$key];
|
||||
|
||||
$planet = new self();
|
||||
$planet
|
||||
->setData('id_owner', $user->getId())
|
||||
->setData('name', $request->getParam('planet'))
|
||||
->setData('galaxy', $galaxy)
|
||||
->setData('system', $system)
|
||||
->setData('position', $finalPosition)
|
||||
->setData('planet_type', 1)
|
||||
;
|
||||
|
||||
Legacies::dispatchEvent('planet.init', array(
|
||||
'planet' => $planet,
|
||||
'user' => $user
|
||||
));
|
||||
|
||||
$planet
|
||||
->setData('field_max', 163)
|
||||
->setData('field_current', 0)
|
||||
->save()
|
||||
;
|
||||
|
||||
$user
|
||||
->setData('id_planet', $planet->getId())
|
||||
->setData('current_planet', $planet->getId())
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
public static function planetUpdateListener($eventData)
|
||||
{
|
||||
if (isset($eventData['planet'])) {
|
||||
$planet = $eventData['planet'];
|
||||
|
||||
if (isset($eventData['time'])) {
|
||||
$time = $eventData['time'];
|
||||
} else {
|
||||
$time = time();
|
||||
}
|
||||
|
||||
if ($planet === null || !$planet instanceof Legacies_Empire_Model_Planet) {
|
||||
return;
|
||||
}
|
||||
|
||||
$planet->updateBuildingQueue($time);
|
||||
$planet->updateResources($time);
|
||||
$planet->getShipyard()->updateQueue($time);
|
||||
$planet->setLastUpdate($time);
|
||||
}
|
||||
}
|
||||
|
||||
public function getResourceAmount($resourceType)
|
||||
{
|
||||
$resources = Legacies_Empire_Model_Game_Resources::getSingleton();
|
||||
|
||||
$resourceConfig = $resources->getData($resourceType);
|
||||
if ($resourceConfig === null) {
|
||||
return null;
|
||||
}
|
||||
return $this->getData($resourceConfig['field']);
|
||||
}
|
||||
|
||||
public function getResourceCap($resourceType)
|
||||
{
|
||||
$resources = Legacies_Empire_Model_Game_Resources::getSingleton();
|
||||
|
||||
$resourceConfig = $resources->getData($resourceType);
|
||||
if ($resourceConfig === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($resourceConfig['storage_field'] === null) {
|
||||
return $this->getData($resourceConfig['production_field']);
|
||||
}
|
||||
return $this->getData($resourceConfig['storage_field']);
|
||||
}
|
||||
|
||||
public function isResourceCapped($resourceType)
|
||||
{
|
||||
if (Math::comp($this->getResourceAmount($resourceType), $this->getResourceCap($resourceType)) >= 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,192 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Planet_Builder
|
||||
extends Legacies_Empire_Model_BuilderAbstract
|
||||
{
|
||||
public function init()
|
||||
{
|
||||
$this->_unserializeQueue($this->_currentPlanet->getData('b_building_id'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $buildingId
|
||||
* @param int $level
|
||||
* @param int $time
|
||||
*/
|
||||
public function _initItem()
|
||||
{
|
||||
$buildingId = func_get_arg(0);
|
||||
$level = func_get_arg(1);
|
||||
$time = func_get_arg(2);
|
||||
|
||||
return new Legacies_Empire_Model_Planet_Builder_Item(array(
|
||||
'building_id' => $buildingId,
|
||||
'level' => $level,
|
||||
'created_at' => $time,
|
||||
'updated_at' => $time
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
if ($types->is($buildingId, Legacies_Empire::TYPE_BUILDING)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->checkAvailability($buildingId);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
$fields = Legacies_Empire_Model_Game_FieldsAlias::getSingleton();
|
||||
|
||||
$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->_currentPlanet->updateBuildingFields();
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append items to build to the construction list
|
||||
*
|
||||
* @param int $buildingId
|
||||
* @param int|string $level
|
||||
* @return Legacies_Empire_Model_Planet_Building_Shipyard
|
||||
*/
|
||||
public function dequeueFirstItem($time)
|
||||
{
|
||||
$types = Legacies_Empire_Model_Game_Types::getSingleton();
|
||||
$resources = Legacies_Empire_Model_Game_Resources::getSingleton();
|
||||
|
||||
if (!Math::isPositive($level)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
// FIXME
|
||||
|
||||
$this->_currentPlanet->setData('b_building_id', $this->serialize());
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Planet_Builder_Item
|
||||
extends Legacies_Empire_Model_Builder_Item
|
||||
{
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Planet_Building_CristalMine
|
||||
implements Legacies_Empire_Model_Planet_ResourceProductionInterface, Legacies_Empire_Model_Planet_BuildingInterface
|
||||
{
|
||||
public function getProductionRatios($level, $produtionRatio, $planet, $user)
|
||||
{
|
||||
return array(
|
||||
Legacies_Empire::RESOURCE_CRISTAL => 10 + 20 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio),
|
||||
Legacies_Empire::RESOURCE_ENERGY => -10 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Planet_Building_DeuteriumSynthetiser
|
||||
implements Legacies_Empire_Model_Planet_ResourceProductionInterface, Legacies_Empire_Model_Planet_BuildingInterface
|
||||
{
|
||||
public function getProductionRatios($level, $produtionRatio, $planet, $user)
|
||||
{
|
||||
return array(
|
||||
Legacies_Empire::RESOURCE_DEUTERIUM => 10 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio),
|
||||
Legacies_Empire::RESOURCE_ENERGY => -30 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Planet_Building_FusionReactor
|
||||
implements Legacies_Empire_Model_Planet_ResourceProductionInterface, Legacies_Empire_Model_Planet_BuildingInterface
|
||||
{
|
||||
public function getProductionRatios($level, $produtionRatio, $planet, $user)
|
||||
{
|
||||
return array(
|
||||
Legacies_Empire::RESOURCE_DEUTERIUM => -10 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio),
|
||||
Legacies_Empire::RESOURCE_ENERGY => 50 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Planet_Building_MetalMine
|
||||
implements Legacies_Empire_Model_Planet_ResourceProductionInterface, Legacies_Empire_Model_Planet_BuildingInterface
|
||||
{
|
||||
public function getProductionRatios($level, $produtionRatio, $planet, $user)
|
||||
{
|
||||
return array(
|
||||
Legacies_Empire::RESOURCE_METAL => 20 + 30 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio),
|
||||
Legacies_Empire::RESOURCE_ENERGY => -10 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,285 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is part of XNova:Legacies
|
||||
*
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.txt
|
||||
* @see http://www.xnova-ng.org/
|
||||
*
|
||||
* Copyright (c) 2009-Present, XNova Support Team <http://www.xnova-ng.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* --> NOTICE <--
|
||||
* This file is part of the core development branch, changing its contents will
|
||||
* make you unable to use the automatic updates manager. Please refer to the
|
||||
* documentation for further information about customizing XNova.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Shipyard building, manages ship and defenses building queue on each planet
|
||||
*
|
||||
* @access public
|
||||
* @category Empire
|
||||
* @category Planet
|
||||
* @package Legacies
|
||||
* @subpackage Legacies_Empire
|
||||
*/
|
||||
class Legacies_Empire_Model_Planet_Building_Shipyard
|
||||
implements Legacies_Empire_Model_Planet_BuildingInterface
|
||||
{
|
||||
private $_eventPrefix = 'planet.shipyard.';
|
||||
|
||||
/**
|
||||
* Planet instance
|
||||
* @var Legacies_Empire_Model_Planet
|
||||
*/
|
||||
protected $_currentPlanet = null;
|
||||
|
||||
/**
|
||||
* User instance
|
||||
* @var Legacies_Empire_Model_User
|
||||
*/
|
||||
protected $_currentUser = null;
|
||||
|
||||
/**
|
||||
* construction queue
|
||||
* @var array
|
||||
*/
|
||||
protected $_builder = null;
|
||||
|
||||
/**
|
||||
* Resource list
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_resourcesTypes = array(
|
||||
Legacies_Empire::RESOURCE_METAL,
|
||||
Legacies_Empire::RESOURCE_CRISTAL,
|
||||
Legacies_Empire::RESOURCE_DEUTERIUM,
|
||||
Legacies_Empire::RESOURCE_ENERGY
|
||||
);
|
||||
|
||||
/**
|
||||
* Multiton instances
|
||||
* @var array
|
||||
*/
|
||||
protected static $_instances = array();
|
||||
|
||||
/**
|
||||
* Multiton factory. Retruns the planet's shipyard instance or created it if
|
||||
* it doesn't yet exist.
|
||||
*
|
||||
* @param Legacies_Empire_Model_Planet $currentPlanet
|
||||
* @param Legacies_Empire_Model_User $currentUser
|
||||
* @return Legacies_Empire_Model_Planet_Building_Shipyard
|
||||
*/
|
||||
public static function factory($currentPlanet, $currentUser)
|
||||
{
|
||||
if ($currentPlanet->getId()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!isset(self::$_instances[$currentPlanet->getId()])) {
|
||||
self::$_instances[$currentPlanet->getId()] = new self($currentPlanet, $currentUser);
|
||||
}
|
||||
return self::$_instances[$currentPlanet->getId()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor. Used for specific usage, use the factory for standard usage.
|
||||
*
|
||||
* @see Legacies_Empire_Model_Planet_Building_Shipyard::factory()
|
||||
*
|
||||
* @param Legacies_Empire_Model_Planet $currentPlanet
|
||||
* @param Legacies_Empire_Model_User $currentUser
|
||||
*/
|
||||
public function __construct($currentPlanet, $currentUser)
|
||||
{
|
||||
$this->_currentPlanet = $currentPlanet;
|
||||
$this->_currentUser = $currentUser;
|
||||
|
||||
$this->_builder = new Legacies_Empire_Model_Planet_Building_Shipyard_Builder($currentPlanet, $currentUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the timestamp at the instance creation.
|
||||
*
|
||||
* @deprecated
|
||||
* @return int
|
||||
*/
|
||||
protected function _now()
|
||||
{
|
||||
return Legacies::now();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* @return Legacies_Empire_Model_Planet_Building_Shipyard
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
$this->_currentPlanet->save();
|
||||
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 = null)
|
||||
{
|
||||
if ($time === null) {
|
||||
$time = Legacies::now();
|
||||
}
|
||||
|
||||
// Dispatch event
|
||||
Legacies::dispatchEvent($this->_eventPrefix . 'append-queue.before', array(
|
||||
'ship_id' => $shipId,
|
||||
'qty' => &$qty,
|
||||
'time' => &$time,
|
||||
'shipyard' => $this,
|
||||
'planet' => $this->_currentPlanet,
|
||||
'user' => $this->_currentUser
|
||||
));
|
||||
|
||||
$this->_builder->appendQueue($shipId, $qty, $time);
|
||||
|
||||
// Dispatch event
|
||||
Legacies::dispatchEvent($this->_eventPrefix . 'append-queue.before', array(
|
||||
'ship_id' => $shipId,
|
||||
'qty' => $qty,
|
||||
'time' => $time,
|
||||
'shipyard' => $this,
|
||||
'planet' => $this->_currentPlanet,
|
||||
'user' => $this->_currentUser
|
||||
));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the contruction queue.
|
||||
*
|
||||
* @return 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,
|
||||
'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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the construction queue
|
||||
* @see Legacies_Empire_Model_Planet_Building_Shipyard_Builder
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getBuilder()
|
||||
{
|
||||
return $this->_builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a ship or defense type is actually buildable on the current
|
||||
* planet, depending on the technology and buildings requirements.
|
||||
*
|
||||
* @param int $shipId
|
||||
* @return bool
|
||||
*/
|
||||
public function 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;
|
||||
}
|
||||
|
||||
return $this->_builder->checkAvailability($shipId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum quantity of elements that are possible to build on
|
||||
* the current planet.
|
||||
*
|
||||
* @param int $shipId
|
||||
* @return int|string
|
||||
*/
|
||||
public function getMaximumBuildableElementsCount($shipId)
|
||||
{
|
||||
return $this->_builder->getMaximumBuildableElementsCount($shipId);
|
||||
}
|
||||
|
||||
public function getResourcesNeeded($shipId, $qty)
|
||||
{
|
||||
return $this->_builder->getResourcesNeeded($shipId, $qty);
|
||||
}
|
||||
|
||||
public function getBuildingTime($shipId, $qty)
|
||||
{
|
||||
$this->_builder->getBuildingTime($shipId, $qty);
|
||||
}
|
||||
|
||||
public static function planetUpdateListener($eventData)
|
||||
{
|
||||
if (isset($eventData['planet'])) {
|
||||
$planet = $eventData['planet'];
|
||||
|
||||
if ($planet === null || !$planet instanceof Legacies_Empire_Model_Planet || !$planet->getId()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$time = null;
|
||||
if (isset($eventData['time'])) {
|
||||
$time = $eventData['time'];
|
||||
}
|
||||
|
||||
$shipyard = self::factory($planet, $planet->getUser());
|
||||
if ($shipyard !== null) {
|
||||
$shipyard->updateQueue();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,294 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Planet_Building_Shipyard_Builder
|
||||
extends Legacies_Empire_Model_BuilderAbstract
|
||||
{
|
||||
public function init()
|
||||
{
|
||||
$this->_unserializeQueue($this->_currentPlanet->getData('b_hangar_id'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $buildingId
|
||||
* @param int $qty
|
||||
* @param int $time
|
||||
*/
|
||||
public function _initItem()
|
||||
{
|
||||
$shipId = func_get_arg(0);
|
||||
$qty = func_get_arg(1);
|
||||
$time = func_get_arg(2);
|
||||
|
||||
return new Legacies_Empire_Model_Planet_Building_Shipyard_Item(array(
|
||||
'ship_id' => $shipId,
|
||||
'qty' => $qty,
|
||||
'created_at' => $time,
|
||||
'updated_at' => $time
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a ship or defense type is actually buildable on the current
|
||||
* planet, depending on the technology and buildings requirements.
|
||||
*
|
||||
* @param int $shipId
|
||||
* @return bool
|
||||
*/
|
||||
public function checkAvailability($shipId)
|
||||
{
|
||||
$types = Legacies_Empire_Model_Game_Types::getSingleton();
|
||||
|
||||
if (!$types->is($shipId, Legacies_Empire::TYPE_SHIP) && !$types->is($shipId, Legacies_Empire::TYPE_DEFENSE)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
parent::checkAvailability($shipId);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum quantity of elements that are possible to build on
|
||||
* the current planet.
|
||||
*
|
||||
* @param int $shipId
|
||||
* @return int|string
|
||||
*/
|
||||
public function getMaximumBuildableElementsCount($shipId)
|
||||
{
|
||||
$prices = Legacies_Empire_Model_Game_Prices::getSingleton();
|
||||
$fields = Legacies_Empire_Model_Game_FieldsAlias::getSingleton();
|
||||
$resources = Legacies_Empire_Model_Game_Resources::getSingleton();
|
||||
|
||||
$qty = 0;
|
||||
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]));
|
||||
|
||||
if ($maxQty == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ($qty == 0 || Math::comp($maxQty, $qty) < 0) {
|
||||
$qty = $maxQty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($qty == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$limitedElementsQty = array(
|
||||
Legacies_Empire::ID_DEFENSE_SMALL_SHIELD_DOME => array(
|
||||
'current' => $this->_currentPlanet[$fields[Legacies_Empire::ID_DEFENSE_SMALL_SHIELD_DOME]],
|
||||
'requested' => $this->_currentPlanet[$fields[Legacies_Empire::ID_DEFENSE_SMALL_SHIELD_DOME]],
|
||||
'limit' => 1
|
||||
),
|
||||
Legacies_Empire::ID_DEFENSE_LARGE_SHIELD_DOME => array(
|
||||
'current' => $this->_currentPlanet[$fields[Legacies_Empire::ID_DEFENSE_LARGE_SHIELD_DOME]],
|
||||
'requested' => $this->_currentPlanet[$fields[Legacies_Empire::ID_DEFENSE_LARGE_SHIELD_DOME]],
|
||||
'limit' => 1
|
||||
),
|
||||
Legacies_Empire::ID_SPECIAL_ANTIBALLISTIC_MISSILE => array(
|
||||
'current' => $this->_currentPlanet[$fields[Legacies_Empire::ID_SPECIAL_ANTIBALLISTIC_MISSILE]],
|
||||
'requested' => $this->_currentPlanet[$fields[Legacies_Empire::ID_SPECIAL_ANTIBALLISTIC_MISSILE]],
|
||||
'limit' => $this->_currentPlanet[$fields[Legacies_Empire::ID_BUILDING_MISSILE_SILO]] * 10
|
||||
),
|
||||
Legacies_Empire::ID_SPECIAL_INTERPLANETARY_MISSILE => array(
|
||||
'current' => $this->_currentPlanet[$fields[Legacies_Empire::ID_SPECIAL_INTERPLANETARY_MISSILE]],
|
||||
'requested' => $this->_currentPlanet[$fields[Legacies_Empire::ID_SPECIAL_INTERPLANETARY_MISSILE]],
|
||||
'limit' => $this->_currentPlanet[$fields[Legacies_Empire::ID_BUILDING_MISSILE_SILO]] * 5
|
||||
)
|
||||
);
|
||||
|
||||
if (in_array($shipId, array_keys($limitedElementsQty))) {
|
||||
foreach ($this->_queue as $element) {
|
||||
if ($element['ship_id'] != $shipId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$limitedElementsQty[$shipId]['requested'] = Math::add($limitedElementsQty[$shipId]['requested'], $element['qty']);
|
||||
if (Math::comp($limitedElementsQty[$shipId]['requested'], $limitedElementsQty[$shipId]['limit']) >= 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (Math::comp($limitedElementsQty[$shipId]['current'], $limitedElementsQty[$shipId]['limit']) >= 0) {
|
||||
return 0;
|
||||
}
|
||||
if (Math::comp($qty, $limitedElementsQty[$shipId]['limit']) >= 0) {
|
||||
return $limitedElementsQty[$shipId]['limit'];
|
||||
}
|
||||
}
|
||||
|
||||
return $qty;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the time needed to build $qty of $shipId
|
||||
*
|
||||
* @param int $shipId
|
||||
* @param int $qty
|
||||
*/
|
||||
public function getBuildingTime($shipId, $qty)
|
||||
{
|
||||
$prices = Legacies_Empire_Model_Game_Prices::getSingleton();
|
||||
$fields = Legacies_Empire_Model_Game_FieldsAlias::getSingleton();
|
||||
$types = Legacies_Empire_Model_Game_Types::getSingleton();
|
||||
$gameConfig = Legacies_Core_Model_Config::getSingleton();
|
||||
|
||||
Math::setPrecision(50);
|
||||
|
||||
// FIXME: Resource dependency
|
||||
$totalCost = Math::mul(Math::add($prices[$shipId][Legacies_Empire::RESOURCE_METAL], $prices[$shipId][Legacies_Empire::RESOURCE_CRISTAL]), $qty);
|
||||
$speedFactor = $gameConfig->getData('game_speed');
|
||||
|
||||
// FIXME: Building dependency
|
||||
$shipyardSpeedup = Math::div(1, Math::add($this->_currentPlanet[$fields[Legacies_Empire::ID_BUILDING_SHIPYARD]], 1));
|
||||
$naniteSpeedup = Math::pow(.5, $this->_currentPlanet[$fields[Legacies_Empire::ID_BUILDING_NANITE_FACTORY]]);
|
||||
$structuresSpeedup = Math::mul($shipyardSpeedup, $naniteSpeedup);
|
||||
|
||||
// FIXME: officers
|
||||
$officerSpeedup = 1;
|
||||
if ($types->is($shipId, Legacies_Empire::TYPE_SHIP)) {
|
||||
$officerSpeedup = 1 - ($this->_currentUser['rpg_technocrate'] * .05);
|
||||
} else if ($types->is($shipId, Legacies_Empire::TYPE_SPECIAL)) {
|
||||
$officerSpeedup = 1 - ($this->_currentUser['rpg_technocrate'] * .05);
|
||||
} else if ($types->is($shipId, Legacies_Empire::TYPE_DEFENSE)) {
|
||||
$officerSpeedup = 1 - ($this->_currentUser['rpg_defenseur'] * .375);
|
||||
}
|
||||
|
||||
Math::setPrecision();
|
||||
|
||||
$baseTime = ($totalCost / $speedFactor) * $structuresSpeedup;
|
||||
|
||||
return (int) Math::floor($baseTime * $officerSpeedup * 3600);
|
||||
}
|
||||
|
||||
/**
|
||||
* (non-PHPdoc)
|
||||
* @see Legacies_Empire_Model_BuilderAbstract::getResourcesNeeded()
|
||||
*/
|
||||
public function getResourcesNeeded($shipId, $qty)
|
||||
{
|
||||
$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][$resourceId])) {
|
||||
continue;
|
||||
}
|
||||
if (Math::isPositive($prices[$shipId][$resourceId])) {
|
||||
$resourcesNeeded[$resourceId] = Math::mul($prices[$shipId][$resourceId], $qty);
|
||||
}
|
||||
}
|
||||
|
||||
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.
|
||||
*
|
||||
* @return Legacies_Empire_Model_Planet_Building_Shipyard
|
||||
*/
|
||||
public function updateQueue($time)
|
||||
{
|
||||
$fields = Legacies_Empire_Model_Game_FieldsAlias::getSingleton();
|
||||
|
||||
$elapsedTime = $time - $this->_currentPlanet->getData('b_hangar');
|
||||
|
||||
foreach ($this->getQueue() as $element) {
|
||||
$shipId = $element->getData('ship_id');
|
||||
$qty = $element->getData('qty');
|
||||
$buildTime = $this->getBuildingTime($shipId, $qty);
|
||||
|
||||
if ($elapsedTime >= $buildTime) {
|
||||
$this->_currentPlanet[$fields[$shipId]] = Math::add($this->_currentPlanet[$fields[$shipId]], $qty);
|
||||
$elapsedTime -= $buildTime;
|
||||
$this->dequeue($element);
|
||||
continue;
|
||||
}
|
||||
|
||||
$timeRatio = $elapsedTime / $buildTime;
|
||||
$itemsBuilt = Math::mul($timeRatio, $qty);
|
||||
|
||||
$element->setData('updated_at', $time);
|
||||
$element->setData('qty', Math::sub($qty, $itemsBuilt));
|
||||
$this->_currentPlanet->setData($fields[$shipId], Math::add($this->_currentPlanet->getData($fields[$shipId]), $itemsBuilt));
|
||||
break;
|
||||
}
|
||||
|
||||
$this->_currentPlanet->setData('b_hangar_id', $this->serialize());
|
||||
$this->_currentPlanet->setData('b_hangar', $time);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
$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;
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Planet_Building_Shipyard_Item
|
||||
extends Legacies_Empire_Model_Builder_Item
|
||||
{
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Planet_Building_SolarPlant
|
||||
implements Legacies_Empire_Model_Planet_ResourceProductionInterface, Legacies_Empire_Model_Planet_BuildingInterface
|
||||
{
|
||||
public function getProductionRatios($level, $produtionRatio, $planet, $user)
|
||||
{
|
||||
return array(
|
||||
Legacies_Empire::RESOURCE_ENERGY => 20 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
|
||||
interface Legacies_Empire_Model_Planet_BuildingInterface
|
||||
{
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Planet_Exception implements Legacies_Core_Exception {}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
|
||||
interface Legacies_Empire_Model_Planet_ResourceProductionInterface
|
||||
{
|
||||
public function getProductionRatios($level, $produtionRatio, $planet, $user);
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Planet_Ship_LargeTransport
|
||||
extends Legacies_Empire_Model_Planet_ShipAbstract
|
||||
{
|
||||
const REQUIREMENT_TO_UPGRADE_CELERITY__RESEARCH_IMPULSE_DRIVE = 4;
|
||||
|
||||
public function getBaseSpeed(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
$prices = Legacies_Empire_Model_Game_Prices::getSingleton();
|
||||
|
||||
return $prices[Legacies_Empire::ID_SHIP_LARGE_TRANSPORT][Legacies_Empire::SHIPS_CELERITY_PRIMARY];
|
||||
}
|
||||
|
||||
public function getSpeedMultiplier(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
return pow(1.1, $user->getElement(Legacies_Empire::ID_RESEARCH_COMBUSTION_DRIVE));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Planet_Ship_LightTransport
|
||||
extends Legacies_Empire_Model_Planet_ShipAbstract
|
||||
{
|
||||
const REQUIREMENT_TO_UPGRADE_CELERITY__RESEARCH_IMPULSE_DRIVE = 4;
|
||||
|
||||
public function getBaseSpeed(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
$prices = Legacies_Empire_Model_Game_Prices::getSingleton();
|
||||
|
||||
if ($user->getElement(Legacies_Empire::ID_RESEARCH_IMPULSE_DRIVE) < self::REQUIREMENT_TO_UPGRADE_CELERITY__RESEARCH_IMPULSE_DRIVE) {
|
||||
return $prices[Legacies_Empire::ID_SHIP_LIGHT_TRANSPORT][Legacies_Empire::SHIPS_CELERITY_PRIMARY];
|
||||
} else {
|
||||
return $prices[Legacies_Empire::ID_SHIP_LIGHT_TRANSPORT][Legacies_Empire::SHIPS_CELERITY_SECONDARY];
|
||||
}
|
||||
}
|
||||
|
||||
public function getSpeedMultiplier(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
if ($user->getElement(Legacies_Empire::ID_RESEARCH_HYPERSPACE_DRIVE) < self::REQUIREMENT_TO_UPGRADE_CELERITY__RESEARCH_IMPULSE_DRIVE) {
|
||||
return pow(1.1, $user->getElement(Legacies_Empire::ID_RESEARCH_COMBUSTION_DRIVE));
|
||||
} else {
|
||||
return pow(1.2, $user->getElement(Legacies_Empire::ID_RESEARCH_IMPULSE_DRIVE));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Planet_Ship_SolarSatellite
|
||||
extends Legacies_Empire_Model_Planet_ShipAbstract
|
||||
implements Legacies_Empire_Model_Planet_ResourceProductionInterface
|
||||
{
|
||||
public function getProductionRatios($quantity, $produtionRatio, $planet, $user)
|
||||
{
|
||||
return array(
|
||||
Legacies_Empire::RESOURCE_ENERGY => ((floatval($planet->getData('temp_max')) / 4) + 20) * (0.1 * $produtionRatio) * floatval($quantity)
|
||||
);
|
||||
}
|
||||
|
||||
public function getBaseSpeed(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function getSpeedMultiplier(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function getActualSpeed(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function getBaseConsumption(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_Planet_Ship_Supernova
|
||||
extends Legacies_Empire_Model_Planet_ShipAbstract
|
||||
implements Legacies_Empire_Model_Planet_ResourceProductionInterface
|
||||
{
|
||||
const REQUIREMENT_TO_UPGRADE_CELERITY__RESEARCH_HYPERSPACE_DRIVE = 20;
|
||||
const REQUIREMENT_TO_UPGRADE_CELERITY__RESEARCH_HYPERSPACE_TECHNOLOGY = 15;
|
||||
|
||||
public function getProductionRatios($quantity, $produtionRatio, $planet, $user)
|
||||
{
|
||||
return array(
|
||||
Legacies_Empire::RESOURCE_ENERGY => ((floatval($planet->getData('temp_max')) / 4) + 20) * (0.1 * $produtionRatio) * floatval($quantity) * -1250
|
||||
);
|
||||
}
|
||||
|
||||
public function getBaseSpeed(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
$prices = Legacies_Empire_Model_Game_Prices::getSingleton();
|
||||
|
||||
if ($user->getElement(Legacies_Empire::ID_RESEARCH_HYPERSPACE_DRIVE) < self::REQUIREMENT_TO_UPGRADE_CELERITY__RESEARCH_HYPERSPACE_DRIVE &&
|
||||
$user->getElement(Legacies_Empire::ID_RESEARCH_HYPERSPACE_TECHNOLOGY) < self::REQUIREMENT_TO_UPGRADE_CELERITY__RESEARCH_HYPERSPACE_TECHNOLOGY) {
|
||||
return $prices[Legacies_Empire::ID_SHIP_SUPERNOVA][Legacies_Empire::SHIPS_CELERITY_PRIMARY];
|
||||
} else {
|
||||
return $prices[Legacies_Empire::ID_SHIP_SUPERNOVA][Legacies_Empire::SHIPS_CELERITY_SECONDARY];
|
||||
}
|
||||
}
|
||||
|
||||
public function getSpeedMultiplier(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
if ($user->getElement(Legacies_Empire::ID_RESEARCH_HYPERSPACE_DRIVE) < self::REQUIREMENT_TO_UPGRADE_CELERITY__RESEARCH_HYPERSPACE_DRIVE &&
|
||||
$user->getElement(Legacies_Empire::ID_RESEARCH_HYPERSPACE_TECHNOLOGY) < self::REQUIREMENT_TO_UPGRADE_CELERITY__RESEARCH_HYPERSPACE_TECHNOLOGY) {
|
||||
return pow(1.2, $user->getElement(Legacies_Empire::ID_RESEARCH_IMPULSE_DRIVE));
|
||||
} else {
|
||||
return pow(1.3, $user->getElement(Legacies_Empire::ID_RESEARCH_HYPERSPACE_DRIVE)) *
|
||||
pow(1.1, $user->getElement(Legacies_Empire::ID_RESEARCH_HYPERSPACE_TECHNOLOGY));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
abstract class Legacies_Empire_Model_Planet_ShipAbstract
|
||||
implements Legacies_Empire_Model_Planet_ShipInterface
|
||||
{
|
||||
public function getBaseSpeed(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
$prices = Legacies_Empire_Model_Game_Prices::getSingleton();
|
||||
|
||||
return $prices[Legacies_Empire::ID_SHIP_SUPERNOVA][Legacies_Empire::SHIPS_CELERITY_PRIMARY];
|
||||
}
|
||||
|
||||
public function getSpeedMultiplier(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
return pow(1.1, $user->getElement(Legacies_Empire::ID_RESEARCH_COMBUSTION_DRIVE));
|
||||
}
|
||||
|
||||
public function getActualSpeed(Legacies_Empire_Model_User $user)
|
||||
{
|
||||
return $this->getBaseSpeed($user) * $this->getSpeedMultiplier($user);
|
||||
}
|
||||
|
||||
public function getActualConsumption(Legacies_Empire_Model_User $user, $speed)
|
||||
{
|
||||
return $this->getBaseConsumption($user) * ($speed / 100) * $this->getSpeedMultiplier($user);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
interface Legacies_Empire_Model_Planet_ShipInterface
|
||||
{
|
||||
public function getBaseSpeed(Legacies_Empire_Model_User $user);
|
||||
public function getSpeedMultiplier(Legacies_Empire_Model_User $user);
|
||||
public function getActualSpeed(Legacies_Empire_Model_User $user);
|
||||
|
||||
public function getBaseConsumption(Legacies_Empire_Model_User $user);
|
||||
public function getActualConsumption(Legacies_Empire_Model_User $user, $speed);
|
||||
}
|
||||
487
includes/application/code/core/Legacies/Empire/Model/User.php
Normal file
487
includes/application/code/core/Legacies/Empire/Model/User.php
Normal file
|
|
@ -0,0 +1,487 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
*
|
||||
* @uses Legacies_Object
|
||||
* @uses Legacies_Empire
|
||||
*/
|
||||
class Legacies_Empire_Model_User
|
||||
extends Legacies_Core_Entity
|
||||
{
|
||||
protected static $_instances = array();
|
||||
|
||||
protected static $_singleton = null;
|
||||
|
||||
protected $_eventPrefix = 'user';
|
||||
protected $_eventObject = 'user';
|
||||
|
||||
public static $hashCallback = 'md5';
|
||||
|
||||
const SESSION_KEY = 'user';
|
||||
const COOKIE_NAME = 'legacies';
|
||||
const COOKIE_LIFETIME = 2592000;
|
||||
|
||||
const PLANET_SORT_DATE = 0;
|
||||
const PLANET_SORT_POSITION = 1;
|
||||
const PLANET_SORT_NAME = 2;
|
||||
|
||||
protected static $_cookieName = self::COOKIE_NAME;
|
||||
|
||||
public static function setCookieName($name)
|
||||
{
|
||||
self::$_cookieName = $name;
|
||||
}
|
||||
|
||||
public static function factory($id)
|
||||
{
|
||||
if ($id === null) {
|
||||
return new self();
|
||||
}
|
||||
|
||||
$id = intval($id);
|
||||
if (!isset(self::$_instances[$id])) {
|
||||
$instance = new self();
|
||||
$params = func_get_args();
|
||||
call_user_func_array(array($instance, 'load'), $params);
|
||||
self::$_instances[$id] = $instance;
|
||||
}
|
||||
return self::$_instances[$id];
|
||||
}
|
||||
|
||||
public static function getSingleton()
|
||||
{
|
||||
if (self::$_singleton === null) {
|
||||
$session = Legacies::getSession(self::SESSION_KEY);
|
||||
if ($session->hasData('user_id')) {
|
||||
$id = intval($session->getData('user_id'));
|
||||
} else if (Legacies::$request !== null && ($cookie = Legacies::$request->getCookie(self::$_cookieName)) !== null) {
|
||||
$cookieData = unserialize(stripslashes($cookie));
|
||||
if (is_array($cookieData)) {
|
||||
$collection = new Legacies_Core_Collection(array('user' => 'users'));
|
||||
$cookieData = array(
|
||||
'id' => (isset($cookieData['id']) ? intval($cookieData['id']) : 0),
|
||||
'key' => (isset($cookieData['key']) ? $collection->quote($cookieData['key']) : null)
|
||||
);
|
||||
|
||||
$collection
|
||||
->column('id')
|
||||
->where('user.id=:id')
|
||||
->where(':key=CONCAT((@salt:=MID(:key, 0, 4)), SHA1(CONCAT(user.username, user.password, @salt)))')
|
||||
->load($cookieData)
|
||||
;
|
||||
|
||||
if ($collection->count() > 0) {
|
||||
$session->setData(self::SESSION_KEY, $cookieData['id']);
|
||||
} else {
|
||||
$session->addError('Your session has expired, please login.');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$session->addError('Your session has expired, please login.');
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
self::$_singleton = self::factory($id);
|
||||
} catch (Legacies_Core_Model_Exception $e) {
|
||||
$session->addError('Session error.');
|
||||
return null;
|
||||
}
|
||||
self::$_singleton->_updateActivity();
|
||||
}
|
||||
return self::$_singleton;
|
||||
}
|
||||
|
||||
public static function hash($password, $salt = null)
|
||||
{
|
||||
if ($salt === null) {
|
||||
return call_user_func(self::$hashCallback, ($password));
|
||||
}
|
||||
|
||||
return call_user_func(self::$hashCallback, ($password . $salt));
|
||||
}
|
||||
|
||||
protected function _init()
|
||||
{
|
||||
$this->setIdFieldName('id');
|
||||
$this->setTableName('users');
|
||||
}
|
||||
|
||||
protected function _updateActivity()
|
||||
{
|
||||
$this
|
||||
->setData('current_page', $_SERVER['REQUEST_URI'])
|
||||
->setData('user_lastip', $_SERVER['REMOTE_ADDR'])
|
||||
->setData('user_agent', $_SERVER['HTTP_USER_AGENT'])
|
||||
->setData('onlinetime', time())
|
||||
->save()
|
||||
;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function logout()
|
||||
{
|
||||
if (Legacies::$response !== null) {
|
||||
Legacies::$response->unsetCookie(self::$_cookieName);
|
||||
}
|
||||
Legacies_Core_Model_Session::destroy();
|
||||
}
|
||||
|
||||
public static function login($username, $password, $remember = false)
|
||||
{
|
||||
$session = Legacies::getSession(self::SESSION_KEY);
|
||||
|
||||
try {
|
||||
$collection = new Legacies_Core_Collection(array('user' => 'users'));
|
||||
|
||||
$passwordHash = md5($password);
|
||||
$collection
|
||||
->column('user.id')
|
||||
->column('user.username')
|
||||
->column('user.password')
|
||||
->column('user.banaday')
|
||||
->column('CONCAT((@salt:=MID(MD5(RAND()), 0, 4)), SHA1(CONCAT(user.username, user.password, @salt))) AS login_rememberme')
|
||||
->column('(CASE WHEN user.password="' . $passwordHash . '" THEN 1 ELSE 0 END) AS login_success')
|
||||
->where('user.username=:username')
|
||||
->load(array(
|
||||
'username' => $username
|
||||
))
|
||||
;
|
||||
} catch (Legacies_Core_Model_Exception $e) {
|
||||
Legacies_Core_ErrorProfiler::getSingleton()->exceptionManager($e);
|
||||
$session->addError('No such user.');
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($collection->count() <= 0) {
|
||||
$session->addError('No such user.');
|
||||
return null;
|
||||
}
|
||||
$login = $collection->current();
|
||||
|
||||
if (intval($login['login_success']) == 1) {
|
||||
if ($login['banaday'] != 0) {
|
||||
if($login['banaday'] <= time() && $login['banaday'] != '0') {
|
||||
$user->setData('banaday', 0)
|
||||
->setData('bana', 0)
|
||||
->setData('urlaubs_modus', 0)
|
||||
->save()
|
||||
;
|
||||
} else {
|
||||
$session->addError('You were banned, please contact admin for more information.');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($_POST["rememberme"]) && Legacies::$request !== null) {
|
||||
Legacies::$response->setCookie(self::$_cookieName, array('id' => $login['id'], 'key' => $login['login_rememberme']), self::COOKIE_LIFETIME);
|
||||
}
|
||||
|
||||
self::$_singleton = self::factory($login['id']);
|
||||
self::$_singleton->_updateActivity();
|
||||
|
||||
$session->setData('user_id', intval($login['id']));
|
||||
|
||||
return self::$_singleton;
|
||||
}
|
||||
|
||||
$session->addError('Your username or credential is invalid, please check your input.');
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function register($username, $email, $password)
|
||||
{
|
||||
try {
|
||||
$user = new self(array(
|
||||
'username' => $username,
|
||||
'password' => md5($password),
|
||||
'email' => $email,
|
||||
'email_2' => $email
|
||||
));
|
||||
|
||||
$user->save();
|
||||
|
||||
Legacies::dispatchEvent('user.init', array(
|
||||
'user' => $user
|
||||
));
|
||||
|
||||
$user->save();
|
||||
} catch (Legacies_Core_Model_Exception $e) {
|
||||
$session->addError($e->getMessage());
|
||||
return null;
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
* @param int|Legacies_Empire_Model_Planet $planet
|
||||
*/
|
||||
public function updateCurrentPlanet($planet)
|
||||
{
|
||||
if (!$planetId instanceof Legacies_Empire_Model_Planet) {
|
||||
$planetColelction = $this->_preparePlanetCollection()->where('id=:id');
|
||||
|
||||
$planetCollection->load(array(
|
||||
'id' => $planet,
|
||||
'user' => $this->getId()
|
||||
));
|
||||
|
||||
if ($planetCollection->count() !== 1) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$planet = $planetCollection->current();
|
||||
}
|
||||
|
||||
if ($planet->getUserId() != $this->getId() || $planet->isDestroyed()) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->setData('current_planet', $planet->getId())->save();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
* @param Legacies_Empire_Model_Planet $planet
|
||||
*/
|
||||
public function setCurrentPlanet(Legacies_Empire_Model_Planet $planet)
|
||||
{
|
||||
if ($planet->getUserId() != $this->getId() || $planet->isDestroyed()) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->setData('current_planet', $planet->getId());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
* @return Legacies_Empire_Model_Planet
|
||||
*/
|
||||
public function getHomePlanet()
|
||||
{
|
||||
$planetId = $this->getData('id_planet');
|
||||
if (!$planetId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$planet = Legacies_Empire_Model_Planet::factory($planetId);
|
||||
|
||||
if ($planet->getUserId() != $this->getId() || $planet->isDestroyed()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $planet;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Enter description here ...
|
||||
* @return Legacies_Empire_Model_Planet
|
||||
*/
|
||||
public function getCurrentPlanet()
|
||||
{
|
||||
$planetId = $this->getData('current_planet');
|
||||
|
||||
if (!$planetId) {
|
||||
$planet = $this->getHomePlanet();
|
||||
$this->setData('current_planet', $planet->getId())->save();
|
||||
|
||||
return $planet;
|
||||
}
|
||||
|
||||
$planet = Legacies_Empire_Model_Planet::factory($planetId);
|
||||
|
||||
if ($planet->getUserId() != $this->getId() || $planet->isDestroyed()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $planet;
|
||||
}
|
||||
|
||||
protected function _preparePlanetCollection()
|
||||
{
|
||||
$planetCollection = new Legacies_Core_Collection(array('planet' => 'planets'), 'Legacies_Empire_Model_Planet');
|
||||
$planetCollection->where('id_owner=:user');
|
||||
|
||||
$order = ($this->getData('planet_sort_order') == 1) ? 'DESC' : 'ASC';
|
||||
|
||||
switch ($this->getData('planet_sort')) {
|
||||
case self::PLANET_SORT_POSITION:
|
||||
$planetCollection
|
||||
->order('planet.galaxy', $order)
|
||||
->order('planet.system', $order)
|
||||
->order('planet.planet', $order)
|
||||
->order('planet.planet_type', $order)
|
||||
;
|
||||
break;
|
||||
|
||||
case self::PLANET_SORT_NAME:
|
||||
$planetCollection->order('planet.name', $order);
|
||||
break;
|
||||
|
||||
case self::PLANET_SORT_DATE:
|
||||
default:
|
||||
$planetCollection->order('planet.id', $order);
|
||||
break;
|
||||
}
|
||||
return $planetCollection;
|
||||
}
|
||||
|
||||
public function getPlanetCollection()
|
||||
{
|
||||
$planetCollection = $this->_preparePlanetCollection();
|
||||
|
||||
$planetCollection->load(array(
|
||||
'user' => $this->getId()
|
||||
));
|
||||
|
||||
return $planetCollection;
|
||||
}
|
||||
|
||||
public function getFleets()
|
||||
{
|
||||
$collection = new Legacies_Core_Collection(array('fleet' => 'fleets'));
|
||||
$collection
|
||||
->setEntityClassName('Legacies_Empire_Model_Fleet')
|
||||
->where('fleet_owner <= :user_id')
|
||||
->load(array('user_id' => $this->getId()))
|
||||
;
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
public function getVisibleFleets()
|
||||
{
|
||||
|
||||
$user = Legacies_Empire_Model_User::getSingleton();
|
||||
$firstCollection = new Legacies_Core_Collection(array('fleet' => 'fleets'));
|
||||
$firstCollection
|
||||
// ->column('*')
|
||||
->column('fleet.fleet_start_galaxy', 'galaxy')
|
||||
->column('fleet.fleet_start_system', 'system')
|
||||
->column('fleet.fleet_start_planet', 'planet')
|
||||
->column('fleet.fleet_start_type', 'planet_type')
|
||||
->where('fleet_end_time <= :now')
|
||||
;
|
||||
$backCollection = new Legacies_Core_Collection(array('fleet' => 'fleets'));
|
||||
$backCollection
|
||||
// ->column('*')
|
||||
->column('fleet.fleet_end_galaxy', 'galaxy')
|
||||
->column('fleet.fleet_end_system', 'system')
|
||||
->column('fleet.fleet_end_planet', 'planet')
|
||||
->column('fleet.fleet_end_type', 'planet_type')
|
||||
->where('fleet_end_time <= :now')
|
||||
;
|
||||
$collection = new Legacies_Core_Collection();
|
||||
$collection
|
||||
->setEntityClassName('Legacies_Empire_Model_Fleet')
|
||||
->union($firstCollection)
|
||||
->union($backCollection)
|
||||
->load(array('now' => time()));
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
public function getElement($elementId)
|
||||
{
|
||||
$fields = Legacies_Empire_Model_Game_FieldsAlias::getSingleton();
|
||||
|
||||
return $this->getData($fields[$elementId]);
|
||||
}
|
||||
|
||||
public function setElement($elementId, $level)
|
||||
{
|
||||
$fields = Legacies_Empire_Model_Game_FieldsAlias::getSingleton();
|
||||
|
||||
return $this->setData($fields[$elementId], $level);
|
||||
}
|
||||
|
||||
public function hasElement($elementId, $levelRequired = 0)
|
||||
{
|
||||
$fields = Legacies_Empire_Model_Game_FieldsAlias::getSingleton();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public function checkPassword($password)
|
||||
{
|
||||
if (self::hash($password) == $this->getData('password')) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getFleetCollection($time = null)
|
||||
{
|
||||
$firstCollection = new Legacies_Core_Collection(array('fleet' => 'fleets'));
|
||||
$firstCollection
|
||||
->column('*')
|
||||
->where('fleet.fleet_owner = :user')
|
||||
;
|
||||
$backCollection = new Legacies_Core_Collection(array('fleet' => 'fleets'));
|
||||
$backCollection
|
||||
->column('*')
|
||||
->where('fleet.fleet_target_owner = :user')
|
||||
;
|
||||
|
||||
$options = array(
|
||||
'user' => $this->getId()
|
||||
);
|
||||
|
||||
if ($time !== null) {
|
||||
$options['now'] = $time;
|
||||
$firstCollection->where('fleet_start_time <= :now');
|
||||
$backCollection->where('fleet_end_time <= :now');
|
||||
}
|
||||
|
||||
$collection = new Legacies_Core_Collection();
|
||||
$collection
|
||||
->setEntityClassName('Legacies_Empire_Model_Fleet')
|
||||
->union($firstCollection)
|
||||
->union($backCollection)
|
||||
->load($options)
|
||||
;
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
public function getNewMessagesCount()
|
||||
{
|
||||
$messageCollection = new Legacies_Core_Collection('messages');
|
||||
$messageCollection->where('message_owner=:user');
|
||||
$messageCollection->where('message_read_at<=0');
|
||||
|
||||
$newMessages = null;
|
||||
if (($count = $messageCollection->getSize(array('user' => $this->getId()))) > 0) {
|
||||
return $count;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
class Legacies_Empire_Model_User_Message
|
||||
extends Legacies_Core_Entity_SubTable
|
||||
{
|
||||
protected function _init()
|
||||
{
|
||||
$this->_tableName = 'messages';
|
||||
$this->_idFieldNames = array('message_id', 'message_owner');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue