Initial commit

This commit is contained in:
Gregory PLANCHAT 2011-06-07 09:23:48 +02:00
commit d7619777af
915 changed files with 59386 additions and 0 deletions

View file

@ -0,0 +1,39 @@
<?php
/**
*
* Enter description here ...
*
* @uses Legacies_Object
* @uses Legacies_Empire
*/
class Legacies_Empire_Model_Fleet
extends Legacies_Core_Entity
{
protected static $_instances = 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];
}
protected function _init()
{
$this->setIdFieldName('fleet_id');
$this->setTableName('fleets');
}
public static function planetListener($eventData)
{
}
}

View file

@ -0,0 +1,49 @@
<?php
/**
*
* Enter description here ...
*
* @uses Legacies_Object
* @uses Legacies_Empire
*/
class Legacies_Empire_Model_Game_Combat
extends Legacies_Core_Model
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;
}
}

View file

@ -0,0 +1,49 @@
<?php
/**
*
* Enter description here ...
*
* @uses Legacies_Object
* @uses Legacies_Empire
*/
class Legacies_Empire_Model_Game_FieldsAlias
extends Legacies_Core_Model
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;
}
}

View file

@ -0,0 +1,49 @@
<?php
/**
*
* Enter description here ...
*
* @uses Legacies_Object
* @uses Legacies_Empire
*/
class Legacies_Empire_Model_Game_Prices
extends Legacies_Core_Model
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;
}
}

View file

@ -0,0 +1,49 @@
<?php
/**
*
* Enter description here ...
*
* @uses Legacies_Object
* @uses Legacies_Empire
*/
class Legacies_Empire_Model_Game_Production
extends Legacies_Core_Model
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;
}
}

View file

@ -0,0 +1,49 @@
<?php
/**
*
* Enter description here ...
*
* @uses Legacies_Object
* @uses Legacies_Empire
*/
class Legacies_Empire_Model_Game_Requirements
extends Legacies_Core_Model
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;
}
}

View file

@ -0,0 +1,49 @@
<?php
/**
*
* Enter description here ...
*
* @uses Legacies_Object
* @uses Legacies_Empire
*/
class Legacies_Empire_Model_Game_Resources
extends Legacies_Core_Model
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;
}
}

View file

@ -0,0 +1,54 @@
<?php
/**
*
* Enter description here ...
*
* @uses Legacies_Object
* @uses Legacies_Empire
*/
class Legacies_Empire_Model_Game_Types
extends Legacies_Core_Model
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)
{
return in_array($element, $this->getData($type));
}
}

View file

@ -0,0 +1,425 @@
<?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 $_user = null;
protected $_moon = null;
protected $_now = null;
protected static $_instances = array();
protected static $_productionConfig = array(
Legacies_Empire::RESOURCE_METAL => array(
'field' => Legacies_Empire::RESOURCE_METAL,
'production_field' => 'metal_perhour',
'ratio_field' => 'metal_porcent',
'storage_field' => 'metal_max',
'production' => array(
Legacies_Empire::ID_BUILDING_METAL_MINE => 'metal_mine_porcent'
),
'storage' => Legacies_Empire::ID_BUILDING_METAL_STORAGE
),
Legacies_Empire::RESOURCE_CRISTAL => array(
'field' => Legacies_Empire::RESOURCE_CRISTAL,
'production_field' => 'crystal_perhour',
'ratio_field' => 'crystal_porcent',
'storage_field' => 'crystal_max',
'production' => array(
Legacies_Empire::ID_BUILDING_CRISTAL_MINE => 'crystal_mine_porcent'
),
'storage' => Legacies_Empire::ID_BUILDING_CRISTAL_STORAGE
),
Legacies_Empire::RESOURCE_DEUTERIUM => array(
'field' => Legacies_Empire::RESOURCE_DEUTERIUM,
'production_field' => 'deuterium_perhour',
'ratio_field' => 'deuterium_porcent',
'storage_field' => 'deuterium_max',
'production' => array(
Legacies_Empire::ID_BUILDING_DEUTERIUM_SYNTHETISER => 'deuterium_sintetizer_porcent'
),
'storage' => Legacies_Empire::ID_BUILDING_DEUTERIUM_TANK
),
Legacies_Empire::RESOURCE_ENERGY => array(
'field' => 'energy_used',
'production_field' => 'energy_max',
'storage_field' => null,
'production' => array(
Legacies_Empire::ID_BUILDING_SOLAR_PLANT => 'solar_plant_porcent',
Legacies_Empire::ID_BUILDING_FUSION_REACTOR => 'fusion_plant_porcent',
Legacies_Empire::ID_SHIP_SOLAR_SATELLITE => 'solar_satelit_porcent'
),
'storage' => null
)
);
protected static $_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->_now = time();
$this->_tableName = 'planets';
$this->_idFieldName = 'id';
}
/**
* @deprecated
*/
protected function _now()
{
return $this->_now;
}
public function updateResources($time = null)
{
$types = Legacies_Empire_Model_Game_Types::getSingleton();
if ($time === null) {
$time = $this->_now();
}
if ($this->getData('planet_type') != 1) {
return $this;
}
$resourcesProductions = array();
foreach (self::$_productionConfig as $resource => $resourceData) {
if ($resourceData['storage_field'] !== null && $resourceData['storage_field'] !== null) {
$officerEnhancement = Math::add(Math::mul(.5, $this->getUser('rpg_stockeur')), 1);
$storageCapacity = Math::pow(1.5, $this->getData(Legacies_Empire::getFieldName($resourceData['storage'])));
$value = Math::mul(MAX_OVERFLOW, Math::mul($officerEnhancement, Math::add(BASE_STORAGE_SIZE, $storageCapacity)));
$this->setData($resourceData['storage_field'], $value);
}
foreach ($resourceData[production] as $productionUnit => $ratioField) {
if (!in_array($productionUnit, $types['prod'])) {
continue;
}
$level = $this->getData(Legacies_Empire::getFieldName($productionUnit));
$ratio = $this->getData($ratioField);
$element = self::getProducitonElementInstance($productionUnit);
foreach ($element->getRatios($level, $ratio, $this, $this->getUser()) as $resourceId => $resourceProduction) {
if (!isset($resourcesProductions[$resourceId])) {
$resourcesProductions[$resourceId] = $resourceProduction;
} else {
$resourcesProductions[$resourceId] = Math::add($resourcesProductions[$resourceId], $resourceProduction);
}
}
}
}
$timeDiff = ($time - $this->getData('last_update')) / 3600;
foreach ($resourcesProductions as $resourceId => $productionPerHour) {
if (!isset(self::$_productionConfig[$resource])) {
continue;
}
$this->setData(self::$_productionConfig[$resource]['production_field'], $productionPerHour);
$production = Math::add($this->getData(self::$_productionConfig[$resource]['field']), Math::mul($timeDiff, $productionPerHour));
if (Math::diff($production, $this->getData(self::$_productionConfig[$resource]['storage_field'])) > 0) {
$production = $this->getData(self::$_productionConfig[$resource]['storage_field']);
}
$this->setData(self::$_productionConfig[$resource]['field'], $production);
}
return $this;
}
public static function getProducitonElementInstance($buildingId)
{
global $ProdGrid; // FIXME
if (!isset(self::$_productionInstances[$buildingId])) {
if (!isset($ProdGrid[$buildingId])) {
return null;
}
$class = $ProdGrid[$buildingId][Legacies_Empire::RESOURCE_FORMULA];
self::$_productionInstances[$buildingId] = new $class;
}
return self::$_productionInstances[$buildingId];
}
public function getUser()
{
if ($this->_user === null) {
$this->_user = Legacies_Empire_Model_User::factory($this->getData('id_owner'));
}
return $this->_user;
}
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 setType($type)
{
$this->setData('planet_type', $type);
return $this;
}
public function getType()
{
return (int) $this->getData('planet_type');
}
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 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'];
$time = null;
if (isset($eventData['time'])) {
$time = $eventData['time'];
}
if ($planet === null || !$planet instanceof Legacies_Empire_Model_Planet || !$planet->getId()) {
return;
}
$user = $planet->getUser();
if (($queue = $this->getData('b_building_id')) != '') {
$explodedQueue = explode(';', $queue);
foreach ($explodedQueue as $item) {
$partialTime = $this->getData('b_building');
if ($partialTime < $time) {
$planet->updateResources($partialTime);
if (CheckPlanetBuildingQueue($planet, $user)) {
SetNextQueueElementOnTop($planet, $user);
}
} else {
$planet->updateResources($time);
break;
}
}
} else {
$planet->updateResources($time);
}
}
}
}

View file

@ -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 => 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)
);
}
}

View file

@ -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)
);
}
}

View file

@ -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)
);
}
}

View file

@ -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 => 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)
);
}
}

View file

@ -0,0 +1,487 @@
<?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
{
/**
* 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 $_queue = null;
/**
* Current timestamp
*
* @var int
* @deprecated
*/
private $_now = 0;
/**
* 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 false;
}
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->_queue = unserialize($this->_currentPlanet->getData('b_hangar_id'));
if (!is_array($this->_queue)) {
$this->_queue = array();
}
$this->_now = time();
}
/**
* Returns the timestamp at the instance creation.
*
* @deprecated
* @return int
*/
protected function _now()
{
return $this->_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)
{
$types = Legacies_Empire_Model_Game_Types::getSingleton();
if (Math::comp($qty, 0) <= 0) {
return $this;
}
if (!$types->is($shipId, Legacies_Empire::TYPE_SHIP) && !$types->is($shipId, Legacies_Empire::TYPE_DEFENSE)) {
return $this;
}
if (!$this->checkAvailability($shipId)) {
return $this;
}
$qty = $this->_checkMaximumQuantity($shipId, $qty);
if (MAX_FLEET_OR_DEFS_PER_ROW > 0 && Math::comp($qty, MAX_FLEET_OR_DEFS_PER_ROW) > 0) {
$qty = MAX_FLEET_OR_DEFS_PER_ROW;
}
// Dispatch event
Legacies::dispatchEvent('planet.shipyard.append-queue.before', array(
'ship_id' => $shipId,
'qty' => $qty,
'shipyard' => $this,
'planet' => $this->_currentPlanet,
'user' => $this->_currentUser
));
$resourcesNeeded = $this->_getResourcesNeeded($shipId, $qty);
$buildTime = $this->getBuildTime($shipId, $qty);
$this->_queue[] = new Legacies_Empire_Model_Planet_Building_Shipyard_Item(array(
'ship_id' => $shipId,
'qty' => $qty,
'created_at' => $this->_now(),
'updated_at' => $this->_now()
));
foreach ($this->_resourcesTypes as $resourceType) {
$this->_currentPlanet[$resourceType] = Math::sub($this->_currentPlanet[$resourceType], $resourcesNeeded[$resourceType]);
}
// Dispatch event
Legacies::dispatchEvent('planet.shipyard.append-queue.after', array(
'ship_id' => $shipId,
'qty' => $qty,
'shipyard' => $this,
'planet' => $this->_currentPlanet,
'user' => $this->_currentUser
));
return $this;
}
/**
* Update the contruction queue.
*
* @return Legacies_Empire_Model_Planet_Building_Shipyard
*/
public function updateQueue($time = null)
{
$fields = Legacies_Empire_Model_Game_FieldsAlias::getSingleton();
if ($time === null) {
$time = $this->_now();
}
$elapsedTime = $time - $this->_currentPlanet['b_hangar'];
// Dispatch event
Legacies::dispatchEvent('planet.shipyard.update-queue.before', array(
'shipyard' => $this,
'planet' => $this->_currentPlanet,
'user' => $this->_currentUser
));
foreach ($this->_queue as $id => &$element) {
$shipId = $element->getData('ship_id');
$qty = $element->getData('qty');
$buildTime = $this->getBuildTime($shipId, $qty);
if ($elapsedTime >= $buildTime) {
$this->_currentPlanet[$fields[$shipId]] = Math::add($this->_currentPlanet[$fields[$shipId]], $qty);
$elapsedTime -= $buildTime;
unset($this->_queue[$id]);
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;
}
unset($element);
// Dispatch event
Legacies::dispatchEvent('planet.shipyard.update-queue.after', array(
'shipyard' => $this,
'planet' => $this->_currentPlanet,
'user' => $this->_currentUser
));
return $this;
}
/**
* Return the construction queue
* @see Legacies_Empire_Model_Planet_Building_Shipyard_Item
*
* @return array
*/
public function getQueue()
{
return $this->_queue;
}
/**
* 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();
$requirements = Legacies_Empire_Model_Game_Requirements::getSingleton();
if (!isset($requirements[$shipId]) || empty($requirements[$shipId])) {
return true;
}
foreach ($requirements[$shipId] as $requirement => $level) {
if ($types->is($requirement, Legacies_Empire::TYPE_BUILDING) && $this->_currentPlanet->hasElement($requirement, $level)) {
continue;
} else if ($types->is($requirement, Legacies_Empire::TYPE_RESEARCH) && $this->_currentUser->hasElement($requirement, $level)) {
continue;
} else if ($types->is($requirement, Legacies_Empire::TYPE_DEFENSE) && $this->_currentPlanet->hasElement($requirement, $level)) {
continue;
} else if ($types->is($requirement, Legacies_Empire::TYPE_SHIP) && $this->_currentPlanet->hasElement($requirement, $level)) {
continue;
}
return false;
}
try {
// Dispatch event. Throw an exception to break the avaliability.
Legacies::dispatchEvent('planet.shipyard.check-availability', array(
'ship_id' => $shipId,
'shipyard' => $this,
'planet' => $this->_currentPlanet,
'user' => $this->_currentUser
));
} catch (Exception $e) {
return false;
}
return true;
}
/**
* 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)
{
$max = $this->getMaximumBuildableElementsCount($shipId);
if (Math::comp($qty, $max) > 0) {
return $max;
}
return $qty;
}
/**
* 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 = array(
Legacies_Empire::RESOURCE_METAL,
Legacies_Empire::RESOURCE_CRISTAL,
Legacies_Empire::RESOURCE_DEUTERIUM,
Legacies_Empire::RESOURCE_ENERGY
);
$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;
}
protected function _getResourcesNeeded($shipId, $qty)
{
$prices = Legacies_Empire_Model_Game_Prices::getSingleton();
$resourcesNeeded = array();
foreach ($this->_resourcesTypes as $resourceId) {
if (isset($prices[$shipId]) && isset($prices[$shipId][$resourceId]) && $prices[$shipId][$resourceId] > 0) {
$resourcesNeeded[$resourceId] = Math::mul($prices[$shipId][$resourceId], $qty);
}
}
return $resourcesNeeded;
}
public function getBuildTime($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();
$scale = 30;
$totalCost = Math::mul(Math::add($prices[$shipId][Legacies_Empire::RESOURCE_METAL], $prices[$shipId][Legacies_Empire::RESOURCE_CRISTAL]), $qty, $scale);
$speedFactor = $gameConfig->getData('game_speed');
$shipyardSpeedup = Math::div(1, Math::add($this->_currentPlanet[$fields[Legacies_Empire::ID_BUILDING_SHIPYARD]], 1, $scale), $scale);
$naniteSpeedup = Math::pow(.5, $this->_currentPlanet[$fields[Legacies_Empire::ID_BUILDING_NANITE_FACTORY]], $scale);
$structuresSpeedup = Math::mul($shipyardSpeedup, $naniteSpeedup, $scale);
$officerSpeedup = 1;
if (in_array($shipId, $types[Legacies_Empire::TYPE_SHIP])) {
$officerSpeedup = 1 - ($this->_currentUser['rpg_technocrate'] * .05);
} else if (in_array($shipId, $types[Legacies_Empire::TYPE_SPECIAL])) {
$officerSpeedup = 1 - ($this->_currentUser['rpg_technocrate'] * .05);
} else if (in_array($shipId, $types[Legacies_Empire::TYPE_DEFENSE])) {
$officerSpeedup = 1 - ($this->_currentUser['rpg_defenseur'] * .375);
}
$baseTime = ($totalCost / $speedFactor) * $structuresSpeedup;
return $baseTime * $officerSpeedup * 3600;
}
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();
}
}
}
}

View file

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

View file

@ -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)
);
}
}

View file

@ -0,0 +1,5 @@
<?php
interface Legacies_Empire_Model_Planet_BuildingInterface
{
}

View file

@ -0,0 +1,6 @@
<?php
interface Legacies_Empire_Model_Planet_ResourceProductionInterface
{
public function getProductionRatios($level, $produtionRatio, $planet, $user);
}

View file

@ -0,0 +1,12 @@
<?php
class Legacies_Empire_Model_Planet_Ship_SolarSatellite
implements Legacies_Empire_Model_Planet_ResourceProductionInterface, Legacies_Empire_Model_Planet_ShipInterface
{
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)
);
}
}

View file

@ -0,0 +1,12 @@
<?php
class Legacies_Empire_Model_Planet_Ship_Supernova
implements Legacies_Empire_Model_Planet_ResourceProductionInterface, Legacies_Empire_Model_Planet_ShipInterface
{
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
);
}
}

View file

@ -0,0 +1,5 @@
<?php
interface Legacies_Empire_Model_Planet_ShipInterface
{
}

View file

@ -0,0 +1,301 @@
<?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;
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;
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::COOKIE_NAME)) !== 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;
}
protected function _init()
{
$this->setIdFieldName('id');
$this->setTableName('users');
}
protected function _updateActivity()
{
$this
->setData('request_uri', $_SERVER['REQUEST_URI'])
->setData('remote_addr', $_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::COOKIE_NAME);
}
Legacies_Core_Model_Session::destroy();
}
public static function login($username, $password, $remember = false)
{
static $statement = null;
$session = Legacies::getSession(self::SESSION_KEY);
$collection = new Legacies_Core_Collection(array('user' => 'users'));
$collection
->column('user.id')
->column('user.username')
->column('user.banaday')
->column('(CASE WHEN MD5(:password)=user.password THEN 1 ELSE 0 END) AS login_success')
->column('CONCAT((@salt:=MID(MD5(RAND()), 0, 4)), SHA1(CONCAT(user.username, user.password, @salt))) AS login_rememberme')
->where('user.username=:username')
->load(array(
'username' => $username,
'password' => $password
))
;
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::COOKIE_NAME, 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;
}
public function getCurrentPlanet()
{
$planetId = $this->getData('current_planet');
if (!$planetId) {
$planetId = $this->getData('id_planet');
$this->setData('current_planet', $planetId)->save();
}
return Legacies_Empire_Model_Planet::factory($planetId);
}
public function getPlanetCollection()
{
$planetCollection = new Legacies_Core_Collection(array('planet' => 'planets'), 'Legacies_Empire_Model_Planet');
$planetCollection->where('id_owner=:user');
$order = ($user['planet_sort_order'] == 1) ? 'DESC' : 'ASC';
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;
}
$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;
}
}