Updated config management

Updated install

Signed-off-by: Gregory PLANCHAT <g.planchat@gmail.com>
This commit is contained in:
Gregory PLANCHAT 2011-12-14 23:45:09 +01:00
commit fcd1f5b740
58 changed files with 1579 additions and 1695 deletions

View file

@ -14,10 +14,14 @@ class Wootook_Core_Block_Messages
{
$messages = array();
var_dump($this->_storages);
foreach ($this->_storages as $namespace) {
$session = Wootook::getSession($namespace);
var_dump($session);
foreach ($session->getMessages() as $messageLevel => $messageList) {
var_dump(array($messageLevel => $messageList));
foreach ($session->getData('messages') as $messageLevel => $messageList) {
if (!isset($messages[$messageLevel])) {
$messages[$messageLevel] = $messageList;
} else {

View file

@ -1,7 +1,7 @@
<?php
class Wootook_Core_Collection
extends Wootook_Core_Model
extends Wootook_Core_Database_Resource
implements Iterator, Countable
{
protected $_tableName = null;
@ -254,7 +254,7 @@ SQL_EOF;
{
$sql = $this->_prepareSql();
$database = Wootook_Database::getSingleton();
$database = $this->getReadConnection();
$statement = $database->prepare($sql);
$args = func_get_args();

View file

@ -0,0 +1,6 @@
<?php
abstract class Wootook_Core_Config_Adapter_Abstract
extends Wootook_Core_Config_Node
{
}

View file

@ -0,0 +1,33 @@
<?php
class Wootook_Core_Config_Adapter_Array
extends Wootook_Core_Config_Adapter_Abstract
{
public function __construct($filename = null)
{
if ($filename !== null) {
$this->load($filename);
}
}
public function load($filename)
{
$data = include $filename;
if (!is_array($data)) {
throw new Wootook_Core_Exception_DataAccessException(
Wootook::__('Configuration file could not be loaded.'));
}
$this->_init($data);
return $this;
}
public function save($filename)
{
file_put_contents($filename, '<' . '?p' . 'hp return ' . var_export($this->toArray(), true) . ';');
return $this;
}
}

View file

@ -0,0 +1,204 @@
<?php
class Wootook_Core_Config_Node
implements ArrayAccess
{
protected $_children = array();
protected $_parent = null;
protected static $_attributeNameCache = array();
public function __construct(Array $data = array(), $parent = null)
{
$this->_init($data, $parent);
}
public function setData(Array $data = array())
{
$this->_init($data);
return $this;
}
protected function _init(Array $data, $parent = null)
{
if ($parent !== null) {
$this->_parent = $parent;
}
$this->_children = array();
foreach ($data as $key => $value) {
if (is_array($value)) {
$this->offsetSet($key, new self($value, $this));
} else {
$this->offsetSet($key, $value);
}
}
return $this;
}
public function setConfig($path, $value)
{
$explodedPath = explode('/', $path);
$length = count($explodedPath);
$currentNode = $this;
for ($i = 0; $i < $length; $i++) {
if (!$currentNode->offsetExists($explodedPath[$i])) {
$newNode = new Wootook_Core_Config_Node(array(), $currentNode);
$currentNode->offsetSet($explodedPath[$i], $newNode);
}
$currentNode = $currentNode->offsetGet($explodedPath[$i]);
if ($i >= ($length - 1)) {
if (is_array($value)) {
$currentNode->offsetSet($explodedPath[$i], new self($value, $this));
} else {
$currentNode->offsetSet($explodedPath[$i], $value);
}
break;
} else if (!$currentNode instanceof Wootook_Core_Config_Node) {
throw new Wootook_Core_Exception_RuntimeException();
}
}
return $this;
}
public function getConfig($path)
{
$explodedPath = explode('/', $path);
$length = count($explodedPath);
$currentNode = $this;
for ($i = 0; $i < $length; $i++) {
if (!$currentNode instanceof Wootook_Core_Config_Node) {
throw new Wootook_Core_Exception_RuntimeException();
}
if (!$currentNode->offsetExists($explodedPath[$i])) {
return null;
}
$currentNode = $currentNode->offsetGet($explodedPath[$i]);
}
return $currentNode;
}
public function toArray()
{
$result = array();
foreach ($this->_children as $key => $child) {
if ($child instanceof self) {
$result[$key] = $child->toArray();
} else if ($child instanceof Wootook_Core_Config_Leaf) {
$result[$key] = $child->getValue();
} else {
$result[$key] = $child;
}
}
return $result;
}
public function reset()
{
$this->_children = array();
$this->_parent = null;
return $this;
}
public function merge(self $node)
{
foreach ($node->_children as $offset => $value) {
if ($value instanceof self) {
if (!$this->offsetExists($offset)) {
$this->offsetSet($offset, new self(array(), $this));
}
$this->offsetGet($offset)->merge($value);
} else {
$this->offsetSet($offset, $value);
}
}
return $this;
}
private function _resolveAttributeName($key)
{
if (!isset(self::$_attributeNameCache[$key])) {
self::$_attributeNameCache[$key] = str_replace(' ', '-', strtolower(preg_replace('#[A-Z]#', ' \\1', $key)));
}
return self::$_attributeNameCache[$key];
}
public function __get($offset)
{
return $this->offsetGet($this->_resolveAttributeName($offset));
}
public function __set($offset, $value)
{
return $this->offsetSet($this->_resolveAttributeName($offset), $value);
}
public function __isset($offset)
{
return $this->offsetExists($this->_resolveAttributeName($offset));
}
public function __unset($offset)
{
return $this->offsetUnset($this->_resolveAttributeName($offset));
}
public function __clone()
{
$clone = new self();
foreach ($this->_children as $childName => $childNode) {
if ($childNode instanceof self) {
$childNode = clone $childNode;
$childNode->_parent = $clone;
}
$clone->offsetSet($childName, $childNode);
}
return $clone;
}
public function offsetGet($offset)
{
if ($this->offsetExists($offset)) {
return $this->_children[$offset];
}
return null;
}
public function offsetSet($offset, $value)
{
$this->_children[$offset] = $value;
return $value;
}
public function offsetExists($offset)
{
if (isset($this->_children[$offset])) {
return true;
}
return false;
}
public function offsetUnset($offset)
{
if ($this->offsetExists($offset)) {
unset($this->_children[$offset]);
}
}
}

View file

@ -0,0 +1,67 @@
<?php
abstract class Wootook_Core_Database_Resource
extends Wootook_Core_Model
{
protected $_readConnection = null;
protected $_writeConnection = null;
protected $_tableName = null;
public function setTableName($tableName)
{
$this->_tableName = $tableName;
return $this;
}
public function getTableName()
{
return $this->_tableName;
}
public function getReadConnection()
{
if ($this->_readConnection === null) {
$this->_readConnection = Wootook_Database::getConnection('core_read');
}
return $this->_readConnection;
}
public function setReadConnection($connection)
{
if ($connection instanceof Wootook_Database) {
$this->_readConnection = $connection;
} else if (is_string($connection)) {
$this->_readConnection = Wootook_Database::getConnection($connection);
} else {
throw new Wootook_Core_Exception_RuntimeException(
'First parameter should be either a database connection object or a string identifier.');
}
return $this;
}
public function getWriteConnection()
{
if ($this->_writeConnection === null) {
$this->_writeConnection = Wootook_Database::getConnection('core_write');
}
return $this->_writeConnection;
}
public function setWriteConnection($connection)
{
if ($connection instanceof Wootook_Database) {
$this->_readConnection = $connection;
} else if (is_string($connection)) {
$this->_readConnection = Wootook_Database::getConnection($connection);
} else {
throw new Wootook_Core_Exception_RuntimeException(
'First parameter should be either a database connection object or a string identifier.');
}
return $this;
}
}

View file

@ -1,11 +1,10 @@
<?php
abstract class Wootook_Core_Entity
extends Wootook_Core_Model
extends Wootook_Core_Database_Resource
implements Wootook_Core_EntityInterface
{
protected $_idFieldName = null;
protected $_tableName = null;
public function setIdFieldName($fieldName)
{
@ -19,18 +18,6 @@ abstract class Wootook_Core_Entity
return $this->_idFieldName;
}
public function setTableName($tableName)
{
$this->_tableName = $tableName;
return $this;
}
public function getTableName()
{
return $this->_tableName;
}
public function setId($id)
{
$this->setData($this->getIdFieldName(), $id);
@ -48,10 +35,10 @@ abstract class Wootook_Core_Entity
$id = func_get_arg(0);
$idFieldName = self::getIdFieldName();
$database = Wootook_Database::getSingleton();
$database = $this->getReadConnection();
$sql =<<<SQL_EOF
SELECT * FROM {$database->getTable(self::getTableName())}
SELECT * FROM {$database->getTable($this->getTableName())}
WHERE {$idFieldName}=:id
LIMIT 1
SQL_EOF;
@ -89,9 +76,9 @@ SQL_EOF;
$idFieldName = self::getIdFieldName();
$values[$idFieldName] = $this->getId();
$database = Wootook_Database::getSingleton();
$database = $this->getWriteConnection();
$sql =<<<SQL_EOF
UPDATE {$database->getTable(self::getTableName())}
UPDATE {$database->getTable($this->getTableName())}
SET {$fieldsImploded}
WHERE {$idFieldName}=:{$idFieldName}
SQL_EOF;
@ -113,9 +100,9 @@ SQL_EOF;
}
$tokensImploded = implode(', ', $tokens);
$database = Wootook_Database::getSingleton();
$database = $this->getWriteConnection();
$sql =<<<SQL_EOF
INSERT INTO {$database->getTable(self::getTableName())} ($fieldsImploded)
INSERT INTO {$database->getTable($this->getTableName())} ($fieldsImploded)
VALUES ({$tokensImploded})
SQL_EOF;
$statement = $database->prepare($sql);
@ -140,9 +127,9 @@ SQL_EOF;
$fieldsImploded = implod(', ', $fields);
$idFieldName = self::getIdFieldName();
$database = Wootook_Database::getSingleton();
$database = $this->getWriteConnection();
$sql =<<<SQL_EOF
DELETE {$database->getTable(self::getTableName())}
DELETE {$database->getTable($this->getTableName())}
WHERE {$idFieldName}=:{$idFieldName}
SQL_EOF;

View file

@ -7,10 +7,9 @@
* @uses Wootook_Empire_Model_User
*/
abstract class Wootook_Core_Entity_SubTable
extends Wootook_Core_Model
extends Wootook_Core_Database_Resource
{
private $_isLoaded = false;
protected $_tableName = null;
protected $_idFieldNames = array();
protected $_eventPrefix = 'entity.sub-table';
@ -118,16 +117,4 @@ SQL_EOF;
return $this;
}
public function setTableName($tableName)
{
$this->_tableName = $tableName;
return $this;
}
public function getTableName()
{
return $this->_tableName;
}
}

View file

@ -16,6 +16,10 @@ class Wootook_Core_ErrorProfiler
private $_mute = false;
/**
*
* @return Wootook_Core_ErrorProfiler
*/
public static function getSingleton()
{
if (self::$_singleton === null) {

View file

@ -8,81 +8,56 @@
* @uses Legacies_Empire
*/
class Wootook_Core_Model_Config
extends Wootook_Core_Model
implements Wootook_Core_Singleton
extends Wootook_Core_Entity_SubTable
{
private static $_singleton = null;
protected $_eventPrefix = 'core.config';
protected $_eventObject = 'config';
public static function getSingleton()
{
if (self::$_singleton === null) {
self::$_singleton = new self();
}
return self::$_singleton;
}
protected function _init()
{
$this->load();
$this->_tableName = 'core_config';
$this->_idFieldNames = array('config_path', 'website_id', 'game_id');
}
protected function _load()
public function setWebsiteId($websiteId)
{
$database = Wootook_Database::getSingleton();
$sql =<<<SQL_EOF
SELECT config_name AS attribute, config_value AS value
FROM {$database->getTable('config')} AS config
SQL_EOF;
$attributeName = null;
$attributeValue = null;
$statement = $database->prepare($sql);
$statement->execute();
$statement->bindColumn('attribute', $attributeName, PDO::PARAM_STR);
$statement->bindColumn('value', $attributeValue, PDO::PARAM_STR);
while ($statement->fetch(PDO::FETCH_BOUND)) {
$this->setData($attributeName, $attributeValue);
}
return $this;
return $this->setData('website_id', $websiteId);
}
protected function _save()
public function getWebsiteId()
{
$database = Wootook_Database::getSingleton();
$fields = array();
$sql =<<<SQL_EOF
UPDATE {{table}}
SET config_value=:value
WHERE config_name=:name
SQL_EOF;
$statement = $database->prepare($sql);
foreach ($this->getAllDatas() as $attributeName => $attributeValue) {
$statement->execute(array(
'name' => $attributeName,
'value' => $attributeValue
));
}
return $this;
return $this->getData('website_id');
}
protected function _delete()
public function setGameId($gameId)
{
// NOP
return $this;
return $this->setData('game_id', $gameId);
}
public function isEnabled()
public function getGameId()
{
return (bool) $this->getData('game_disable');
return $this->getData('game_id');
}
public function setPath($path)
{
return $this->setData('config_path', $path);
}
public function getPath()
{
return $this->getData('config_path');
}
public function setValue($value)
{
return $this->setData('config_value', $value);
}
public function getValue()
{
return $this->getData('config_value');
}
}

View file

@ -58,7 +58,8 @@ class Wootook_Core_Model_Session
{
$messages = $this->_data['messages'];
if ($clear == true) {
$this->_data['messages'] = array();
var_dump($this->_data['messages']);
//$this->_data['messages'] = array();
}
return $messages;
}

View file

@ -33,8 +33,10 @@ class Wootook_Database
return self::$_connectionAliases[$connectionName];
}
if ($alias = Wootook::getConfig("global/database/{$connectionName}/use")) {
if ($alias = Wootook::getConfig("database/{$connectionName}/use")) {
self::$_connectionAliases[$connectionName] = self::getConnection($alias);
return self::$_connectionAliases[$connectionName];
}
self::$_connections[$connectionName] = self::_initConnection($connectionName);
@ -45,23 +47,23 @@ class Wootook_Database
private static function _initConnection($connectionName)
{
$hostname = Wootook::getConfig("global/database/{$connectionName}/params/hostname");
$username = Wootook::getConfig("global/database/{$connectionName}/params/username");
$password = Wootook::getConfig("global/database/{$connectionName}/params/password");
$database = Wootook::getConfig("global/database/{$connectionName}/params/database");
$hostname = Wootook::getConfig("database/{$connectionName}/params/hostname");
$username = Wootook::getConfig("database/{$connectionName}/params/username");
$password = Wootook::getConfig("database/{$connectionName}/params/password");
$database = Wootook::getConfig("database/{$connectionName}/params/database");
if (empty($hostname) || empty($username) || empty($database)) {
return null;
}
$dsn = "mysql:dbname={$database};host={$hostname}";
if (($port = Wootook::getConfig("global/database/{$connectionName}/params/port")) && is_numeric($port)) {
if (($port = Wootook::getConfig("database/{$connectionName}/params/port")) && is_numeric($port)) {
$dsn .= ";port={$port}";
}
$event = Wootook::dispatchEvent('database.prepare-options', array(
'name' => $connectionName,
'options' => array_merge(self::$options, Wootook::getConfig("global/database/{$connectionName}/options"))
'options' => array_merge(self::$options, Wootook::getConfig("database/{$connectionName}/options")->toArray())
));
$options = $event->getData('options');
@ -71,7 +73,7 @@ class Wootook_Database
$connection->_username = $username;
$connection->_password = $password;
if (($prefix = Wootook::getConfig("global/database/{$connectionName}/table_prefix")) !== null) {
if (($prefix = Wootook::getConfig("database/{$connectionName}/table_prefix")) !== null) {
$connection->setTablePrefix($prefix);
}

View file

@ -46,6 +46,10 @@ abstract class Wootook_Empire_Model_BuilderAbstract
public function enqueue($params, $index = null)
{
if ($this->_currentUser->getVacation()) {
return $this;
}
$item = $this->_initItem($params);
if ($item === null) {
return $this;

View file

@ -260,6 +260,14 @@ class Wootook_Empire_Model_Planet
return $this;
}
if ($this->getUser()->isVacation()) {
$resourceConfig = Wootook::getGameConfig('resource/base-income');
foreach ($resources->getAllDatas() as $resource => $resourceData) {
$this->setData($resourceData['production_field'], $resourceConfig[$resource]);
}
return $this;
}
/*
* Compute resources consumers and resources producers
*/
@ -952,22 +960,14 @@ class Wootook_Empire_Model_Planet
$key = array_rand($positions, 1);
$finalPosition = $positions[$key];
$config = Wootook_Core_Model_Config::getSingleton();
$planet = new self();
$planet
->setData('id_owner', $user->getId())
->setData('name', Wootook::getRequest()->getParam('planet'))
->setData('galaxy', $systemInfo->getData('galaxy'))
->setData('system', $systemInfo->getData('system'))
->setData('planet', $finalPosition)
->setData('planet_type', 1)
->setData('field_max', $config->getData('initial_fields'))
->setData('field_current', 0)
->setData('metal', 500) // TODO: use config
->setData('cristal', 500) // TODO: use config
;
$planet->save();
$user->createNewPlanet(
$systemInfo->getData('galaxy'),
$systemInfo->getData('system'),
$finalPosition,
Wootook_Empire_Model_Planet::TYPE_PLANET,
Wootook::getRequest()->getParam('planet'),
Wootook::getGameConfig('resource/initial/fields')
);
$user
->setData('id_planet', $planet->getId())
@ -976,13 +976,6 @@ class Wootook_Empire_Model_Planet
->setData('system', $planet->getSystem())
->setData('planet', $planet->getPosition())
;
Wootook::dispatchEvent('planet.init', array(
'planet' => $planet,
'user' => $user
));
$planet->save();
}
}

View file

@ -68,14 +68,13 @@ class Wootook_Empire_Model_Planet_Builder
public function getBuildingTime($buildingId, $level)
{
$prices = Wootook_Empire_Model_Game_Prices::getSingleton();
$gameConfig = Wootook_Core_Model_Config::getSingleton();
Math::setPrecision(50);
$firstLevelTime = $prices[$buildingId][Legacies_Empire::BASE_BUILDING_TIME];
$partialLevelTime = Math::mul($firstLevelTime, Math::pow($prices[$buildingId][Legacies_Empire::RESOURCE_MULTIPLIER], $level));
$levelTime = Math::sub($partialLevelTime, $firstLevelTime);
$speedFactor = $gameConfig->getData('game_speed');
$speedFactor = Wootook::getGameConfig('game/speed/general');
$baseTime = $levelTime / $speedFactor * 3600;
Math::setPrecision();

View file

@ -174,6 +174,7 @@ class Wootook_Empire_Model_User
$user->setData('banaday', 0)
->setData('bana', 0)
->setData('urlaubs_modus', 0)
->setData('urlaubs_until', null)
->save()
;
} else {
@ -186,18 +187,25 @@ class Wootook_Empire_Model_User
Wootook::getResponse()->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;
return self::setLoggedIn(self::factory($login['id']));
}
$session->addError('Your username or credential is invalid, please check your input.');
return null;
}
public static function setLoggedIn(self $user)
{
$session = Wootook::getSession(self::SESSION_KEY);
self::$_singleton = $user;
self::$_singleton->_updateActivity();
$session->setData('user_id', intval(self::$_singleton->getId()));
return self::$_singleton;
}
public static function register($username, $email, $password)
{
try {
@ -225,7 +233,7 @@ class Wootook_Empire_Model_User
} catch (Wootook_Core_Exception_DataAccessException $e) {
$session = Wootook_Core_Model_Session::factory(Wootook_Empire_Model_User::SESSION_KEY);
trigger_error($e->getMessage(), E_USER_ERROR);
Wootook_Core_ErrorProfiler::getSingleton()->exceptionManager($e);
$session->addError($e->getMessage());
return null;
}
@ -233,6 +241,44 @@ class Wootook_Empire_Model_User
return $user;
}
public function createNewPlanet($galaxy, $system, $position, $type, $name, $size = null)
{
if ($size === null) {
$baseSize = Wootook::getGameConfig('resource/initial/fields');
$factor = $position * 10 / (1 + log($position * 10));
$fuzz = 2 * $factor * pow(sin($factor), 2) / 2 + $factor / 4;
$size = mt_rand(floor($factor / 10), ceil($factor * 5 / 4)) + mt_rand(0, $fuzz);
}
$planet = new self();
$planet
->setData('id_owner', $user->getId())
->setData('name', $name)
->setData('galaxy', $galaxy)
->setData('system', $system)
->setData('planet', $position)
->setData('planet_type', $type)
->setData('field_max', $size)
->setData('diameter', pow($size, 2) + mt_rand(0, $size * $position))
->setData('field_current', 0)
;
$resourceConfig = Wootook::getGameConfig('resource/initial');
$resourceList = Wootook_Empire_Model_Game_Resources::getSingleton();
foreach ($resourceList as $resource => $resourceData) {
$planet->setData($resourceData['production_field'], $resourceConfig[$resource]);
}
Wootook::dispatchEvent('planet.init', array(
'planet' => $planet,
'user' => $user
));
$planet->save();
}
/**
*
* Enter description here ...
@ -561,4 +607,34 @@ class Wootook_Empire_Model_User
$navigation->addLink('tools/back', 'Go back to the game', 'Go back to the game', 'overview.php', array(), array('admin'));
}
}
public function getVacation()
{
return $this->getData('urlaubs_modus') ? true : false;
}
public function getVacationEndDate()
{
return $this->getData('urlaubs_until');
}
public function setVacation($active = true)
{
$this->setData('urlaubs_modus', $active);
foreach ($this->getPlanetCollection() as $planet) {
$planet->updateResources();
$planet->updateResourceProduction();
$planet->save();
}
if ($active) {
$this->setData('urlaubs_until', time() + Wootook::getConfig('engine/options/vacation-min-time'));
} else {
$this->setData('urlaubs_until', null);
}
$this->save();
return $this;
}
}