Finished install script

Updated configuration data loading
Added Website and Game models

Signed-off-by: Gregory PLANCHAT <g.planchat@gmail.com>
This commit is contained in:
Gregory PLANCHAT 2011-12-27 19:37:28 +01:00
commit dfa3dfb086
34 changed files with 362 additions and 510 deletions

View file

@ -142,19 +142,6 @@ SQL_EOF;
$this->query($sql);
$sql = <<<SQL_EOF
CREATE TABLE IF NOT EXISTS {$this->getTableName('errors')} (
`error_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`error_sender` VARCHAR(32) NOT NULL,
`error_time` TIMESTAMP NOT NULL,
`error_type` VARCHAR(32) NOT NULL DEFAULT 'unknown',
`error_text` TEXT,
PRIMARY KEY (`error_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SQL_EOF;
$this->query($sql);
$sql = <<<SQL_EOF
CREATE TABLE IF NOT EXISTS {$this->getTableName('fleets')} (
`fleet_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,

View file

@ -84,12 +84,6 @@ SQL_EOF;
$this->query($sql);
$sql = <<<SQL_EOF
DROP TABLE {$this->getTableName('errors')};
SQL_EOF;
$this->query($sql);
$sql = <<<SQL_EOF
DROP TABLE {$this->getTableName('fleets')};
SQL_EOF;

View file

@ -400,7 +400,7 @@ class Wootook
self::$_websitesById[$websiteId] = $website;
self::$_websitesByCode[$websiteCode] = $website;
} catch (Wootook_Core_Exception_DataAccessException $e) {
throw new Wootook_Core_Exception_RuntimeException('Could not load website entity.', null, $e);
throw new Wootook_Core_Exception_WebsiteError('Could not load website entity.', null, $e);
}
return $website;
@ -417,7 +417,7 @@ class Wootook
self::$_websitesById[$websiteId] = $website;
self::$_websitesByCode[$websiteCode] = $website;
} catch (Wootook_Core_Exception_DataAccessException $e) {
throw new Wootook_Core_Exception_RuntimeException('Could not load website entity.', null, $e);
throw new Wootook_Core_Exception_WebsiteError('Could not load website entity.', null, $e);
}
return $website;
@ -438,7 +438,7 @@ class Wootook
self::$_gamesById[$gameId] = $game;
self::$_gamesByCode[$gameCode] = $game;
} catch (Wootook_Core_Exception_DataAccessException $e) {
throw new Wootook_Core_Exception_RuntimeException('Could not load game entity.', null, $e);
throw new Wootook_Core_Exception_GameError('Could not load game entity.', null, $e);
}
return $game;
@ -455,7 +455,7 @@ class Wootook
self::$_gamesById[$gameId] = $game;
self::$_gamesByCode[$gameCode] = $game;
} catch (Wootook_Core_Exception_DataAccessException $e) {
throw new Wootook_Core_Exception_RuntimeException('Could not load game entity.', null, $e);
throw new Wootook_Core_Exception_GameError('Could not load game entity.', null, $e);
}
return $game;
@ -472,8 +472,8 @@ class Wootook
public static function setGame($gameId, $game)
{
$gameKey = $game->getData('code');
self::$_gameById[$gameId] = $game;
self::$_gameByCode[$gameKey] = $game;
self::$_gamesById[$gameId] = $game;
self::$_gamesByCode[$gameKey] = $game;
}
protected static function _initWebsiteConfig($websiteId)

View file

@ -69,6 +69,8 @@ SQL_EOF;
protected function _save()
{
$database = $this->getWriteConnection();
if ($this->getId() !== null) {
$fields = array();
$values = array();
@ -76,7 +78,7 @@ SQL_EOF;
if ($field == self::getIdFieldName()) {
continue;
}
$fields[] = "{$field}=:{$field}";
$fields[] = "{$database->quoteIdentifier($field)}=:{$field}";
$values[$field] = $value;
}
@ -84,7 +86,6 @@ SQL_EOF;
$idFieldName = self::getIdFieldName();
$values[$idFieldName] = $this->getId();
$database = $this->getWriteConnection();
if ($database === null) {
throw new Wootook_Core_Exception_DataAccessException('Could not load data: no write connection configured.');
}
@ -99,8 +100,8 @@ SQL_EOF;
$statement->execute($values);
} else {
$datas = $this->getAllDatas();
$fieldsImploded = implode(', ', array_keys($datas));
$fields = array();
$tokens = array();
$values = array();
foreach ($datas as $field => $value) {
@ -108,24 +109,27 @@ SQL_EOF;
continue;
}
$tokens[] = ":{$field}";
$values[$field] = $value;
$fields[] = $database->quoteIdentifier($field);
$values[$field] = strval($value);
}
$tokensImploded = implode(', ', $tokens);
$fieldsImploded = implode(', ', $fields);
$database = $this->getWriteConnection();
if ($database === null) {
throw new Wootook_Core_Exception_DataAccessException('Could not load data: no write connection configured.');
}
$table = $database->getTable($this->getTableName());
$sql =<<<SQL_EOF
INSERT INTO {$database->getTable($this->getTableName())} ($fieldsImploded)
VALUES ({$tokensImploded})
INSERT INTO {$database->quoteIdentifier($table)} ({$database->quoteIdentifier($this->getIdFieldName())}, $fieldsImploded)
VALUES (NULL, {$tokensImploded})
SQL_EOF;
$statement = $database->prepare($sql);
$statement->execute($values);
$this->setId($database->lastInsertId());
$id = $database->lastInsertId($table);
$this->setId($id);
}
return $this;

View file

@ -90,6 +90,11 @@ class Wootook_Core_ErrorProfiler
return true;
}
public function addException($exception)
{
$this->_exceptions[] = $exception;
}
public function exceptionManager($exception)
{
if (!$this->_listen) {

View file

@ -0,0 +1,7 @@
<?php
class Wootook_Core_Exception_Deprecated
extends Wootook_Core_Exception_RuntimeException
{
}

View file

@ -0,0 +1,7 @@
<?php
class Wootook_Core_Exception_GameError
extends Wootook_Core_Exception_RuntimeException
{
}

View file

@ -0,0 +1,7 @@
<?php
class Wootook_Core_Exception_WebsiteError
extends Wootook_Core_Exception_RuntimeException
{
}

View file

@ -21,6 +21,10 @@ class Wootook_Core_Model_Session
protected static $_levels = null;
const COOKIE_LIFETIME_CONFIG_KEY = 'web/session/time';
const COOKIE_DOMAIN_CONFIG_KEY = 'web/session/domain';
const COOKIE_PATH_CONFIG_KEY = 'web/session/path';
public static function factory($namespace)
{
$namespace = (string) $namespace;
@ -43,6 +47,7 @@ class Wootook_Core_Model_Session
public function __construct($namespace)
{
if (session_id() == '') {
session_set_cookie_params($this->getCookieLifetime(), $this->getCookiePath(), $this->getCookieDomain(), false, true);
session_start();
}
@ -56,6 +61,26 @@ class Wootook_Core_Model_Session
}
}
public function getCookieLifetime()
{
$lifetime = Wootook::getWebsiteConfig(self::COOKIE_LIFETIME_CONFIG_KEY);
if ($lifetime <= 0) {
return 900;
}
return $lifetime;
}
public function getCookieDomain()
{
return Wootook::getWebsiteConfig(self::COOKIE_DOMAIN_CONFIG_KEY);
}
public function getCookiePath()
{
return Wootook::getWebsiteConfig(self::COOKIE_PATH_CONFIG_KEY);
}
public function getMessages($clear = true)
{
$messages = $this->_data['messages'];

View file

@ -166,6 +166,12 @@ INSERT IGNORE INTO {$this->getTableName('core_config')} (`website_id`, `game_id`
(0, 0, 'web/cookie/domain', ''),
(0, 0, 'web/cookie/path', ''),
(1, 1, 'web/cookie/name', '__wtk_1_1'),
(0, 0, 'web/session/time', '900'),
(0, 0, 'web/session/domain', '.wootook.org'),
(0, 0, 'web/session/path', '/'),
(0, 0, 'engine/options/bbcode', '1'),
(0, 0, 'engine/options/ga', '1'),
(0, 0, 'engine/options/announces', '0'),
@ -184,12 +190,18 @@ INSERT IGNORE INTO {$this->getTableName('core_config')} (`website_id`, `game_id`
(0, 0, 'engine/bot/name', 'Woot'),
(0, 0, 'engine/bot/email', 'contact@wootook.org');
SQL_EOF;
/**
* TODO: change these paths
* (0, 0, 'BuildLabWhileRun', '0')
* (0, 0, 'ExtCopyFrame', '0')
* (0, 0, 'ExtCopyOwner', '')
* (0, 0, 'ExtCopyFunct', '')
*/
$this->query($sql);
$sql = <<<SQL_EOF
CREATE TABLE IF NOT EXISTS {$this->getTableName('errors')} (
`error_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`error_sender` VARCHAR(32) NOT NULL,
`error_time` TIMESTAMP NOT NULL,
`error_type` VARCHAR(32) NOT NULL DEFAULT 'unknown',
`error_text` TEXT,
PRIMARY KEY (`error_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SQL_EOF;
$this->query($sql);

View file

@ -40,7 +40,7 @@
*
*/
$this->setSetupConnection('legacies_setup');
$this->setSetupConnection('core_setup');
$sql = <<<SQL_EOF
DROP TABLE {$this->getTableName('core_website')};
@ -65,3 +65,9 @@ DROP TABLE {$this->getTableName('core_config')};
SQL_EOF;
$this->query($sql);
$sql = <<<SQL_EOF
DROP TABLE {$this->getTableName('errors')};
SQL_EOF;
$this->query($sql);

View file

@ -22,6 +22,11 @@ class Wootook_Database
return self::getConnection(self::DEFAULT_CONNECTION_NAME);
}
public function quoteIdentifier($identifier)
{
return "`$identifier`";
}
public static function getConnection($connectionName)
{
if (empty($connectionName) || $connectionName === null) {

View file

@ -918,67 +918,6 @@ class Wootook_Empire_Model_Planet
return $this->getBuildingQueue()->getBuildingTime($buildingId, $level);
}
public static function registrationListener($eventData)
{
if (isset($eventData['user'])) {
$user = $eventData['user'];
if ($user === null || !$user instanceof Wootook_Empire_Model_User || !$user->getId()) {
return;
}
$collection = self::searchMostFreeSystems();
$collection->limit(1)->load();
if ($collection->count() == 0) {
throw new Exception('No more planet to colonize!'); // Oops, no more free place
}
$systemInfo = $collection->current();
if ($systemInfo->getData('count') >= MAX_PLANET_IN_SYSTEM) {
throw new Exception('No more planet to colonize!'); // Oops, no more free place
}
$collection = new Wootook_Core_Collection(array('planet' => 'planets'));
$collection
->column(array('position' => 'planet.planet'))
->where('planet.planet_type=1')
->where('planet.galaxy=:galaxy')
->where('planet.system=:system')
->load(array(
'galaxy' => $systemInfo->getData('galaxy'),
'system' => $systemInfo->getData('system'),
))
;
$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 = $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())
->setData('current_planet', $planet->getId())
->setData('galaxy', $planet->getGalaxy())
->setData('system', $planet->getSystem())
->setData('planet', $planet->getPosition())
;
}
}
public static function searchMostFreeSystems($galaxyList = null, $systemList = null)
{
$collection = new Wootook_Core_Collection(array('galaxy' => 'galaxy'));
@ -1011,15 +950,15 @@ class Wootook_Empire_Model_Planet
}
$orders = array(
"COUNT(*) / {$collection->quote(MAX_PLANET_IN_SYSTEM)}",
"1 + ABS(galaxy.galaxy - CEIL({$collection->quote(MAX_GALAXY_IN_WORLD)} / 2))",
"1 + 2 * ABS(galaxy.system - CEIL({$collection->quote(MAX_SYSTEM_IN_GALAXY)} / 2))",
"COUNT(*) / {$collection->quote(Wootook::getGameConfig('engine/universe/positions'))}",
"1 + ABS(galaxy.galaxy - CEIL({$collection->quote(Wootook::getGameConfig('engine/universe/galaxies'))} / 2))",
"1 + 2 * ABS(galaxy.system - CEIL({$collection->quote(Wootook::getGameConfig('engine/universe/systems'))} / 2))",
"RAND() / 1000",
);
$collection
->order('((' . implode(') * (', $orders) . '))', 'ASC')
//->order("ABS(galaxy.system - CEIL({$collection->quote(MAX_SYSTEM_IN_GALAXY)} / 2))", 'ASC')
//->order("ABS(galaxy.galaxy - CEIL({$collection->quote(MAX_GALAXY_IN_WORLD)} / 2))", 'ASC')
//->order("ABS(galaxy.system - CEIL({$collection->quote(Wootook::getGameConfig('engine/universe/systems'))} / 2))", 'ASC')
//->order("ABS(galaxy.galaxy - CEIL({$collection->quote(Wootook::getGameConfig('engine/universe/galaxies'))} / 2))", 'ASC')
//->order("1.5 / COUNT(*)", 'ASC')
->order('RAND()', 'ASC');

View file

@ -23,18 +23,48 @@ class Wootook_Empire_Model_User
protected $_currentPlanet = null;
const SESSION_KEY = 'user';
const COOKIE_NAME = 'Wootook';
const COOKIE_NAME = '__wtk';
const COOKIE_LIFETIME = 2592000;
const COOKIE_NAME_CONFIG_KEY = 'web/cookie/name';
const COOKIE_LIFETIME_CONFIG_KEY = 'web/cookie/time';
const COOKIE_DOMAIN_CONFIG_KEY = 'web/cookie/domain';
const COOKIE_PATH_CONFIG_KEY = 'web/cookie/path';
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)
public static function getCookieName()
{
self::$_cookieName = $name;
$cookieName = Wootook::getWebsiteConfig(self::COOKIE_NAME_CONFIG_KEY);
if (is_null($cookieName)) {
return self::COOKIE_NAME;
}
return $cookieName;
}
public static function getCookieLifetime()
{
$cookieLifetime = Wootook::getWebsiteConfig(self::COOKIE_LIFETIME_CONFIG_KEY);
if (is_null($cookieLifetime)) {
return self::COOKIE_LIFETIME;
}
return $cookieLifetime;
}
public static function getCookieDomain()
{
return Wootook::getWebsiteConfig(self::COOKIE_DOMAIN_CONFIG_KEY);
}
public static function getCookiePath()
{
return Wootook::getWebsiteConfig(self::COOKIE_PATH_CONFIG_KEY);
}
public static function factory($id)
@ -64,8 +94,7 @@ class Wootook_Empire_Model_User
$session = Wootook::getSession(self::SESSION_KEY);
if ($session->hasData('user_id')) {
$id = intval($session->getData('user_id'));
} else if (Wootook::getRequest() !== null && ($cookieData = Wootook::getRequest()->getCookie(self::$_cookieName)) !== null) {
//$cookieData = unserialize(stripslashes($cookie));
} else if (Wootook::getRequest() !== null && ($cookieData = Wootook::getRequest()->getCookie(self::getCookieName())) !== null) {
if (is_array($cookieData)) {
$collection = new Wootook_Core_Collection(array('user' => 'users'));
$cookieData = array(
@ -86,6 +115,9 @@ class Wootook_Empire_Model_User
$session->addError('Your session has expired, please login.');
return null;
}
} else {
$session->addError('Your session has expired, please login.');
return null;
}
} else {
$session->addError('Your session has expired, please login.');
@ -132,7 +164,7 @@ class Wootook_Empire_Model_User
public function logout()
{
Wootook::getResponse()->unsetCookie(self::$_cookieName);
Wootook::getResponse()->unsetCookie(self::getCookieName());
Wootook_Core_Model_Session::destroy();
}
@ -184,7 +216,13 @@ class Wootook_Empire_Model_User
}
if (isset($_POST["rememberme"]) && Wootook::getResponse() !== null) {
Wootook::getResponse()->setCookie(self::$_cookieName, array('id' => $login['id'], 'key' => $login['login_rememberme']), self::COOKIE_LIFETIME);
Wootook::getResponse()->setCookie(
self::getCookieName(),
array('id' => $login['id'], 'key' => $login['login_rememberme']),
self::getCookieLifetime(),
self::getCookiePath(),
self::getCookieDomain()
);
}
return self::setLoggedIn(self::factory($login['id']));
@ -223,20 +261,81 @@ class Wootook_Empire_Model_User
'user_agent' => $request->getServer('HTTP_USER_AGENT')
));
$user->getWriteConnection()->beginTransaction();
$user->save();
$collection = Wootook_Empire_Model_Planet::searchMostFreeSystems();
$collection->limit(1)->load();
if ($collection->count() == 0) {
throw new Wootook_Empire_Exception_RuntimeException('No more planet to colonize!'); // Oops, no more free place
}
$systemInfo = $collection->current();
if ($systemInfo->getData('count') >= Wootook::getGameConfig('engine/universe/positions')) {
throw new Wootook_Empire_Exception_RuntimeException('No more planet to colonize!'); // Oops, no more free place
}
$collection = new Wootook_Core_Collection(array('planet' => 'planets'));
$collection
->column(array('position' => 'planet.planet'))
->where('planet.planet_type=1')
->where('planet.galaxy=:galaxy')
->where('planet.system=:system')
->load(array(
'galaxy' => $systemInfo->getData('galaxy'),
'system' => $systemInfo->getData('system'),
))
;
$positions = range(1, Wootook::getGameConfig('engine/universe/positions'));
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 = $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())
->setData('current_planet', $planet->getId())
->setData('galaxy', $planet->getGalaxy())
->setData('system', $planet->getSystem())
->setData('planet', $planet->getPosition())
;
Wootook::dispatchEvent('user.init', array(
'user' => $user
));
$user->save();
} catch (Wootook_Core_Exception_DataAccessException $e) {
$user->getWriteConnection()->rollback();
$session = Wootook_Core_Model_Session::factory(Wootook_Empire_Model_User::SESSION_KEY);
Wootook_Core_ErrorProfiler::getSingleton()->exceptionManager($e);
$session->addError($e->getMessage());
return null;
} catch (Wootook_Empire_Exception_RuntimeException $e) {
$user->getWriteConnection()->rollback();
$session = Wootook_Core_Model_Session::factory(Wootook_Empire_Model_User::SESSION_KEY);
Wootook_Core_ErrorProfiler::getSingleton()->exceptionManager($e);
$session->addError($e->getMessage());
return null;
}
$user->getWriteConnection()->commit();
return $user;
}
@ -265,17 +364,18 @@ class Wootook_Empire_Model_User
->setData('field_current', 0)
;
$resourceConfig = Wootook::getGameConfig('resource/initial');
$resourceList = Wootook_Empire_Model_Game_Resources::getSingleton();
$resourceConfig = Wootook::getGameConfig('resource/initial');
foreach ($resourceList as $resource => $resourceData) {
$planet->setData($resourceData['storage_field'], $resourceConfig[$resource]);
}
$resourceConfig = Wootook::getGameConfig('resource/base-income');
foreach ($resourceList as $resource => $resourceData) {
$planet->setData($resourceData['production_field'], $resourceConfig[$resource]);
}
$planet->save();
var_dump($planet->getId());
die();
Wootook::dispatchEvent('planet.init', array(
'planet' => $planet,
'user' => $this
@ -608,6 +708,10 @@ class Wootook_Empire_Model_User
$layout = $eventData['layout'];
$navigation = $layout->getBlock('navigation');
if ($navigation === null) {
return;
}
if (!defined('IN_ADMIN')) {
$navigation->addLink('tools/admin', 'Admin Panel', 'Admin Panel', 'admin/overview.php', array(), array('admin'));
} else {