* cache $db->hasTable and $db->hasColumn functions

* changed tableExist and fieldExist to $db->hasTable(table) +
$db->hasColumn(table, column)
* new configurable: database_log - can log database queries and show in
website source when logged as super admin
* removed debug_level configurable, enabled by default when logged on
super admin
* added OTS_Account:getCountry()
* added posibility to load OTS_Player partially, and without skills
* (internal) changed depracated $ots->createObject() functions with
their OTS_ equivalents
* (internal) removed unused code
This commit is contained in:
slawkens1
2018-01-06 05:44:33 +01:00
parent 31537687c1
commit 1025fad0e6
65 changed files with 446 additions and 417 deletions

View File

@@ -29,50 +29,6 @@
*/
class POT
{
/**
* MySQL driver.
*/
const DB_MYSQL = 1;
/**
* SQLite driver.
*/
const DB_SQLITE = 2;
/**
* PostgreSQL driver.
*
* @version 0.0.4
* @since 0.0.4
*/
const DB_PGSQL = 3;
/**
* ODBC driver.
*
* @version 0.0.4
* @since 0.0.4
*/
const DB_ODBC = 4;
/**
* @deprecated 0.0.5 Vocations are now loaded dynamicly from vocations.xml file.
*/
const VOCATION_NONE = 0;
/**
* @deprecated 0.0.5 Vocations are now loaded dynamicly from vocations.xml file.
*/
const VOCATION_SORCERER = 1;
/**
* @deprecated 0.0.5 Vocations are now loaded dynamicly from vocations.xml file.
*/
const VOCATION_DRUID = 2;
/**
* @deprecated 0.0.5 Vocations are now loaded dynamicly from vocations.xml file.
*/
const VOCATION_PALADIN = 3;
/**
* @deprecated 0.0.5 Vocations are now loaded dynamicly from vocations.xml file.
*/
const VOCATION_KNIGHT = 4;
/**
* North.
*/
@@ -401,15 +357,13 @@ class POT
* </p>
*
* @version 0.1.3
* @param int|null $driver Database driver type.
* @param array $params Connection info.
* @throws E_OTS_Generic When driver is not supported or not supported.
* @throws LogicException When PDO extension is not loaded.
* @throws PDOException On PDO operation error.
* @example examples/quickstart.php quickstart.php
* @tutorial POT/Basics.pkg#basics.database
*/
public function connect($driver, $params)
public function connect($params)
{
// checks if PDO extension is loaded
if( !extension_loaded('PDO') )
@@ -417,47 +371,7 @@ class POT
throw new LogicException();
}
// $params['driver'] option instead of $driver
if( !isset($driver) )
{
if( isset($params['driver']) )
{
$driver = $params['driver'];
}
else
{
throw new E_OTS_Generic(E_OTS_Generic::CONNECT_NO_DRIVER);
}
}
unset($params['driver']);
// switch() structure provides us further flexibility
switch($driver)
{
// MySQL database
case self::DB_MYSQL:
$this->db = new OTS_DB_MySQL($params);
break;
// SQLite database
case self::DB_SQLITE:
$this->db = new OTS_DB_SQLite($params);
break;
// SQLite database
case self::DB_PGSQL:
$this->db = new OTS_DB_PostgreSQL($params);
break;
// SQLite database
case self::DB_ODBC:
$this->db = new OTS_DB_ODBC($params);
break;
// unsupported driver
default:
throw new E_OTS_Generic(E_OTS_Generic::CONNECT_INVALID_DRIVER);
}
$this->db = new OTS_DB_MySQL($params);
$this->db->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);
// $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

View File

@@ -41,6 +41,7 @@ class OTS_Account extends OTS_Row_DAO implements IteratorAggregate, Countable
*/
private $data = array('email' => '', 'blocked' => false, 'rlname' => '','location' => '','web_flags' => 0, 'lastday' => 0, 'premdays' => 0, 'created' => 0);
public static $cache = array();
/**
* Creates new account.
*
@@ -167,10 +168,16 @@ class OTS_Account extends OTS_Row_DAO implements IteratorAggregate, Countable
* @param int $id Account number.
* @throws PDOException On PDO operation error.
*/
public function load($id)
public function load($id, $fresh = false)
{
if(!$fresh && isset(self::$cache[$id])) {
$this->data = self::$cache[$id];
return;
}
// SELECT query on database
$this->data = $this->db->query('SELECT `id`, ' . (fieldExist('name', 'accounts') ? '`name`,' : '') . '`password`, `email`, ' . $this->db->fieldName('blocked') . ', ' . $this->db->fieldName('rlname') . ', ' . $this->db->fieldName('location') . ', ' . $this->db->fieldName('web_flags') . ', ' . (fieldExist('premdays', 'accounts') ? $this->db->fieldName('premdays') . ',' : '') . (fieldExist('lastday', 'accounts') ? $this->db->fieldName('lastday') . ',' : (fieldExist('premend', 'accounts') ? $this->db->fieldName('premend') . ',' : '')) . $this->db->fieldName('created') . ' FROM ' . $this->db->tableName('accounts') . ' WHERE ' . $this->db->fieldName('id') . ' = ' . (int) $id)->fetch();
$this->data = $this->db->query('SELECT `id`, ' . ($this->db->hasColumn('accounts', 'name') ? '`name`,' : '') . '`password`, `email`, `blocked`, `rlname`, `location`, `country`, `web_flags`, ' . ($this->db->hasColumn('accounts', 'premdays') ? '`premdays`, ' : '') . ($this->db->hasColumn('accounts', 'lastday') ? '`lastday`, ' : ($this->db->hasColumn('accounts', 'premend') ? '`premend`,' : '')) . '`created` FROM `accounts` WHERE `id` = ' . (int) $id)->fetch();
self::$cache[$id] = $this->data;
}
/**
@@ -188,7 +195,7 @@ class OTS_Account extends OTS_Row_DAO implements IteratorAggregate, Countable
public function find($name)
{
// finds player's ID
$id = $this->db->query('SELECT ' . $this->db->fieldName('id') . ' FROM ' . $this->db->tableName('accounts') . ' WHERE ' . $this->db->fieldName('name') . ' = ' . $this->db->quote($name) )->fetch();
$id = $this->db->query('SELECT `id` FROM `accounts` WHERE `name` = ' . $this->db->quote($name) )->fetch();
// if anything was found
if( isset($id['id']) )
@@ -208,7 +215,7 @@ class OTS_Account extends OTS_Row_DAO implements IteratorAggregate, Countable
public function findByEMail($email)
{
// finds player's ID
$id = $this->db->query('SELECT ' . $this->db->fieldName('id') . ' FROM ' . $this->db->tableName('accounts') . ' WHERE ' . $this->db->fieldName('email') . ' = ' . $this->db->quote($email) )->fetch();
$id = $this->db->query('SELECT `id` FROM `accounts` WHERE `email` = ' . $this->db->quote($email) )->fetch();
// if anything was found
if( isset($id['id']) )
@@ -250,7 +257,7 @@ class OTS_Account extends OTS_Row_DAO implements IteratorAggregate, Countable
}
$field = 'lastday';
if(fieldExist('premend', 'accounts')) { // othire
if($this->db->hasColumn('accounts', 'premend')) { // othire
$field = 'premend';
if(!isset($this->data['premend'])) {
$this->data['premend'] = 0;
@@ -258,7 +265,7 @@ class OTS_Account extends OTS_Row_DAO implements IteratorAggregate, Countable
}
// UPDATE query on database
$this->db->query('UPDATE `accounts` SET ' . (fieldExist('name', 'accounts') ? '`name` = ' . $this->db->quote($this->data['name']) . ',' : '') . '`password` = ' . $this->db->quote($this->data['password']) . ', `email` = ' . $this->db->quote($this->data['email']) . ', `blocked` = ' . (int) $this->data['blocked'] . ', `rlname` = ' . $this->db->quote($this->data['rlname']) . ', `location` = ' . $this->db->quote($this->data['location']) . ', `web_flags` = ' . (int) $this->data['web_flags'] . ', ' . (fieldExist('premdays', 'accounts') ? '`premdays` = ' . (int) $this->data['premdays'] . ',' : '') . '`' . $field . '` = ' . (int) $this->data[$field] . ' WHERE `id` = ' . $this->data['id']);
$this->db->query('UPDATE `accounts` SET ' . ($this->db->hasColumn('accounts', 'name') ? '`name` = ' . $this->db->quote($this->data['name']) . ',' : '') . '`password` = ' . $this->db->quote($this->data['password']) . ', `email` = ' . $this->db->quote($this->data['email']) . ', `blocked` = ' . (int) $this->data['blocked'] . ', `rlname` = ' . $this->db->quote($this->data['rlname']) . ', `location` = ' . $this->db->quote($this->data['location']) . ', `web_flags` = ' . (int) $this->data['web_flags'] . ', ' . ($this->db->hasColumn('accounts', 'premdays') ? '`premdays` = ' . (int) $this->data['premdays'] . ',' : '') . '`' . $field . '` = ' . (int) $this->data[$field] . ' WHERE `id` = ' . $this->data['id']);
}
/**
@@ -302,6 +309,16 @@ class OTS_Account extends OTS_Row_DAO implements IteratorAggregate, Countable
return $this->data['location'];
}
public function getCountry()
{
if( !isset($this->data['country']) )
{
throw new E_OTS_NotLoaded();
}
return $this->data['country'];
}
public function getWebFlags()
{
if( !isset($this->data['web_flags']) )
@@ -375,34 +392,6 @@ class OTS_Account extends OTS_Row_DAO implements IteratorAggregate, Countable
return $this->data['created'];
}
/**
* @version 0.1.0
* @since 0.0.4
* @return OTS_Group Group of which current account is member (currently random group).
* @throws E_OTS_NotLoaded If account is not loaded.
* @deprecated 0.0.6 There is no more group_id field in database.
*/
public function getGroup()
{
if( !isset($this->data['id']) )
{
throw new E_OTS_NotLoaded();
}
// loads default group
$groups = new OTS_Groups_List();
$groups->rewind();
return $groups->current();
}
/**
* @version 0.0.6
* @param OTS_Group $group Group to be a member.
* @deprecated 0.0.6 There is no more group_id field in database.
*/
public function setGroup(OTS_Group $group)
{
}
/**
* Name.
@@ -772,13 +761,13 @@ class OTS_Account extends OTS_Row_DAO implements IteratorAggregate, Countable
throw new E_OTS_NotLoaded();
}
if(tableExist('account_bans')) {
if($this->db->hasTable('account_bans')) {
$ban = $this->db->query('SELECT `expires_at` FROM `account_bans` WHERE `account_id` = ' . $this->data['id'] . ' AND (`expires_at` > ' . time() .' OR `expires_at` = -1) ORDER BY `expires_at` DESC')->fetch();
$this->data['banned'] = isset($ban['expires_at']);
$this->data['banned_time'] = $ban['expires_at'];
}
else if(tableExist('bans')) {
if(fieldExist('active', 'bans')) {
else if($this->db->hasTable('bans')) {
if($this->db->hasColumn('bans', 'active')) {
$ban = $this->db->query('SELECT `active`, `expires` FROM `bans` WHERE (`type` = 3 OR `type` = 5) AND `active` = 1 AND `value` = ' . $this->data['id'] . ' AND (`expires` > ' . time() .' OR `expires` = -1) ORDER BY `expires` DESC')->fetch();
$this->data['banned'] = $ban['active'];
$this->data['banned_time'] = $ban['expires'];
@@ -830,20 +819,28 @@ class OTS_Account extends OTS_Row_DAO implements IteratorAggregate, Countable
public function getGroupId()
{
global $db;;
if(fieldExist('group_id', 'accounts')) {
if(isset($this->data['group_id'])) {
return $this->data['group_id'];
}
global $db;
if($db->hasColumn('accounts', 'group_id')) {
$query = $this->db->query('SELECT `group_id` FROM `accounts` WHERE `id` = ' . (int) $this->getId())->fetch();
// if anything was found
if(isset($query['group_id']))
if(isset($query['group_id'])) {
$this->data['group_id'] = $query['group_id'];
return $query['group_id'];
}
}
$query = $this->db->query('SELECT `group_id` FROM `players` WHERE `account_id` = ' . (int) $this->getId() . ' ORDER BY `group_id` DESC LIMIT 1');
if($query->rowCount() == 1)
{
$query = $query->fetch();
$this->data['group_id'] = $query['group_id'];
return $query['group_id'];
}
return 0;
}

View File

@@ -37,6 +37,9 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
*/
private $queries = 0;
protected $logged = false;
private $log = '';
/**
* Query-quoted field name.
*
@@ -96,6 +99,10 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
public function query($query)
{
if($this->logged) {
$this->log .= $query . PHP_EOL;
}
$this->queries++;
//echo $query . PHP_EOL;
return parent::query($query);
@@ -235,6 +242,10 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
public function queries() {
return $this->queries;
}
public function getLog() {
return $this->log;
}
}
/**#@-*/

View File

@@ -24,6 +24,8 @@
*/
class OTS_DB_MySQL extends OTS_Base_DB
{
private $has_table_cache = array();
private $has_column_cache = array();
/**
* Creates database connection.
*
@@ -92,9 +94,48 @@ class OTS_DB_MySQL extends OTS_Base_DB
$this->prefix = $params['prefix'];
}
if( isset($params['log']) )
{
$this->logged = true;
}
global $cache, $config;
if($cache->enabled()) {
$tmp = null;
$need_revalidation = true;
if($cache->fetch('database_checksum', $tmp) && $tmp) {
$tmp = unserialize($tmp);
if(sha1($config['database_host'] . '.' . $config['database_name']) == $tmp) {
$need_revalidation = false;
}
}
if(!$need_revalidation) {
$tmp = null;
if($cache->fetch('database_tables', $tmp) && $tmp) {
$this->has_table_cache = unserialize($tmp);
}
$tmp = null;
if($cache->fetch('database_columns', $tmp) && $tmp) {
$this->has_column_cache = unserialize($tmp);
}
}
}
parent::__construct('mysql:' . implode(';', $dns), $user, $password);
}
public function __destruct()
{
global $cache, $config;
if($cache->enabled()) {
$cache->set('database_tables', serialize($this->has_table_cache));
$cache->set('database_columns', serialize($this->has_column_cache));
$cache->set('database_checksum', serialize(sha1($config['database_host'] . '.' . $config['database_name'])));
}
}
/**
* Query-quoted field name.
*
@@ -133,6 +174,23 @@ class OTS_DB_MySQL extends OTS_Base_DB
return $sql;
}
public function hasTable($name) {
if(isset($this->has_table_cache[$name])) {
return $this->has_table_cache[$name];
}
global $config;
return ($this->has_table_cache[$name] = $this->query("SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = " . $this->quote($config['database_name']) . " AND `TABLE_NAME` = " . $this->quote($name) . " LIMIT 1;")->rowCount() > 0);
}
public function hasColumn($table, $column) {
if(isset($this->has_column_cache[$table . '.' . $column])) {
return $this->has_column_cache[$table . '.' . $column];
}
return ($this->has_column_cache[$table . '.' . $column] = count($this->query("SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "'")->fetchAll()) > 0);
}
}
/**#@-*/

View File

@@ -490,7 +490,7 @@ class OTS_Group extends OTS_Row_DAO implements IteratorAggregate, Countable
// creates filter
$filter = new OTS_SQLFilter();
$filter->compareField('group_id', (int) $this->data['id']);
if(fieldExist('deletion', 'players'))
if($this->db->hasColumn('players', 'deletion'))
$filter->compareField('deletion', 0);
else
$filter->compareField('deleted', 0);

View File

@@ -31,9 +31,8 @@ class OTS_Groups_List implements IteratorAggregate, Countable
*/
public function __construct($file = '')
{
if(tableExist('groups')) { // read groups from database
global $db;
global $db;
if($db->hasTable('groups')) { // read groups from database
foreach($db->query('SELECT `id`, `name`, `access` FROM `groups`;') as $group)
{
$info = array();

View File

@@ -122,13 +122,13 @@ class OTS_Guild extends OTS_Row_DAO implements IteratorAggregate, Countable
public function load($id)
{
$ownerid = 'ownerid';
if(fieldExist('owner_id', 'guilds'))
if($this->db->hasColumn('guilds', 'owner_id'))
$ownerid = 'owner_id';
$creationdata = 'creationdata';
if(fieldExist('creationdate', 'guilds'))
if($this->db->hasColumn('guilds', 'creationdate'))
$creationdata = 'creationdate';
else if(fieldExist('creation_time', 'guilds'))
else if($this->db->hasColumn('guilds', 'creation_time'))
$creationdata = 'creation_time';
// SELECT query on database
@@ -177,13 +177,13 @@ class OTS_Guild extends OTS_Row_DAO implements IteratorAggregate, Countable
public function save()
{
$ownerid = 'ownerid';
if(fieldExist('owner_id', 'guilds'))
if($this->db->hasColumn('guilds', 'owner_id'))
$ownerid = 'owner_id';
$creationdata = 'creationdata';
if(fieldExist('creationdate', 'guilds'))
if($this->db->hasColumn('guilds', 'creationdate'))
$creationdata = 'creationdate';
else if(fieldExist('creation_time', 'guilds'))
else if($this->db->hasColumn('guilds', 'creation_time'))
$creationdata = 'creation_time';
// updates existing guild

View File

@@ -344,7 +344,7 @@ class OTS_GuildRank extends OTS_Row_DAO implements IteratorAggregate, Countable
}
// creates filter
if(fieldExist('rank_id', 'players')) {
if($this->db->hasColumn('players', 'rank_id')) {
$filter = new OTS_SQLFilter();
$filter->compareField('rank_id', (int) $this->data['id']);
}

View File

@@ -129,14 +129,14 @@ class OTS_Player extends OTS_Row_DAO
* @param int $id Player's ID.
* @throws PDOException On PDO operation error.
*/
public function load($id)
public function load($id, $fields = null, $load_skills = true)
{
global $__load;
if(!isset($__load['loss_experience']))
{
$loss = '';
if(fieldExist('loss_experience', 'players')) {
if($this->db->hasColumn('players', 'loss_experience')) {
$loss = ', `loss_experience`, `loss_mana`, `loss_skills`';
}
@@ -146,7 +146,7 @@ class OTS_Player extends OTS_Row_DAO
if(!isset($__load['loss_items']))
{
$loss_items = '';
if(fieldExist('loss_items', 'players')) {
if($this->db->hasColumn('players', 'loss_items')) {
$loss_items = ', `loss_items`, `loss_containers`';
}
@@ -156,7 +156,7 @@ class OTS_Player extends OTS_Row_DAO
if(!isset($__load['guild_info']))
{
$guild_info = '';
if(!tableExist('guild_members') && fieldExist('guildnick', 'players')) {
if(!$this->db->hasTable('guild_members') && $this->db->hasColumn('players', 'guildnick')) {
$guild_info = ', `guildnick`, `rank_id`';
}
@@ -166,7 +166,7 @@ class OTS_Player extends OTS_Row_DAO
if(!isset($__load['skull_type']))
{
$skull_type = 'skull';
if(fieldExist('skull_type', 'players')) {
if($this->db->hasColumn('players', 'skull_type')) {
$skull_type = 'skull_type';
}
@@ -176,7 +176,7 @@ class OTS_Player extends OTS_Row_DAO
if(!isset($__load['skull_time']))
{
$skull_time = 'skulltime';
if(fieldExist('skull_time', 'players')) {
if($this->db->hasColumn('players', 'skull_time')) {
$skull_time = 'skull_time';
}
@@ -184,31 +184,56 @@ class OTS_Player extends OTS_Row_DAO
}
if(!isset($__load['blessings'])) {
$__load['blessings'] = fieldExist('blessings', 'players');
$__load['blessings'] = $this->db->hasColumn('players', 'blessings');
}
if(!isset($__load['direction'])) {
$__load['direction'] = fieldExist('direction', 'players');
$__load['direction'] = $this->db->hasColumn('players', 'direction');
}
if(!isset($__load['stamina'])) {
$__load['stamina'] = fieldExist('stamina', 'players');
$__load['stamina'] = $this->db->hasColumn('players', 'stamina');
}
if(!isset($__load['world_id'])) {
$__load['world_id'] = fieldExist('world_id', 'players');
$__load['world_id'] = $this->db->hasColumn('players', 'world_id');
}
if(!isset($__load['online'])) {
$__load['online'] = fieldExist('online', 'players');
$__load['online'] = $this->db->hasColumn('players', 'online');
}
if(!isset($__load['deletion'])) {
$__load['deletion'] = fieldExist('deletion', 'players');
$__load['deletion'] = $this->db->hasColumn('players', 'deletion');
}
if(!isset($__load['promotion'])) {
$__load['promotion'] = fieldExist('promotion', 'players');
$__load['promotion'] = $this->db->hasColumn('players', 'promotion');
}
if(!isset($__load['marriage'])) {
$__load['marriage'] = fieldExist('marriage', 'players');
$__load['marriage'] = $this->db->hasColumn('players', 'marriage');
}
global $db;
if(isset($fields)) { // load only what we wish
if(in_array('promotion', $fields)) {
if(!$this->db->hasColumn('players', 'promotion')) {
unset($fields[array_search('promotion')]);
}
}
if(in_array('deleted', $fields)) {
if($this->db->hasColumn('players', 'deletion')) {
unset($fields[array_search('deleted')]);
$fields[] = 'deletion';
}
}
if(in_array('online', $fields)) {
if(!$this->db->hasColumn('players', 'online')) {
unset($fields[array_search('online')]);
}
}
$this->data = $this->db->query('SELECT ' . implode(', ', $fields) . ' FROM `players` WHERE `id` = ' . (int)$id)->fetch();
}
else {
// SELECT query on database
$this->data = $this->db->query('SELECT `id`, `name`, `account_id`, `group_id`, `sex`, `vocation`, `experience`, `level`, `maglevel`, `health`, `healthmax`, `mana`, `manamax`, `manaspent`, `soul`, `lookbody`, `lookfeet`, `lookhead`, `looklegs`, `looktype`' . ($this->db->hasColumn('players', 'lookaddons') ? ', `lookaddons`' : '') . ', `posx`, `posy`, `posz`, `cap`, `lastlogin`, `lastlogout`, `lastip`, `save`, `conditions`, `' . $__load['skull_time'] . '` as `skulltime`, `' . $__load['skull_type'] . '` as `skull`' . $__load['guild_info'] . ', `town_id`' . $__load['loss_experience'] . $__load['loss_items'] . ', `balance`' . ($__load['blessings'] ? ', `blessings`' : '') . ($__load['direction'] ? ', `direction`' : '') . ($__load['stamina'] ? ', `stamina`' : '') . ($__load['world_id'] ? ', `world_id`' : '') . ($__load['online'] ? ', `online`' : '') . ', `' . ($__load['deletion'] ? 'deletion' : 'deleted') . '`' . ($__load['promotion'] ? ', `promotion`' : '') . ($__load['marriage'] ? ', `marriage`' : '') . ', `comment`, `created`, `hidden` FROM `players` WHERE `id` = ' . (int)$id)->fetch();
}
// SELECT query on database
$this->data = $this->db->query('SELECT `id`, `name`, `account_id`, `group_id`, `sex`, `vocation`, `experience`, `level`, `maglevel`, `health`, `healthmax`, `mana`, `manamax`, `manaspent`, `soul`, `lookbody`, `lookfeet`, `lookhead`, `looklegs`, `looktype`' . (fieldExist('lookaddons', 'players') ? ', `lookaddons`' : '') . ', `posx`, `posy`, `posz`, `cap`, `lastlogin`, `lastlogout`, `lastip`, `save`, `conditions`, `' . $__load['skull_time'] . '` as `skulltime`, `' . $__load['skull_type'] . '` as `skull`' . $__load['guild_info'] . ', `town_id`' . $__load['loss_experience'] . $__load['loss_items'] . ', `balance`' . ($__load['blessings'] ? ', `blessings`' : '') . ($__load['direction'] ? ', `direction`' : '') . ($__load['stamina'] ? ', `stamina`' : '') . ($__load['world_id'] ? ', `world_id`' : '') . ($__load['online'] ? ', `online`' : '') . ', `' . ($__load['deletion'] ? 'deletion' : 'deleted') . '`' . ($__load['promotion'] ? ', `promotion`' : '') . ($__load['marriage'] ? ', `marriage`' : '') . ', `comment`, `created`, `hidden` FROM `players` WHERE `id` = ' . (int)$id)->fetch();
if(!isset($this->data['guildnick']) || $this->data['guildnick'])
$this->data['guildnick'] = '';
@@ -222,9 +247,9 @@ class OTS_Player extends OTS_Row_DAO
$this->data['vocation'] += ($this->data['promotion'] * $config['vocations_amount']);
}
// loads skills
if( $this->isLoaded() )
if( $this->isLoaded() && $load_skills)
{
if(fieldExist('skill_fist', 'players')) {
if($this->db->hasColumn('players', 'skill_fist')) {
$skill_ids = array(
'skill_fist' => POT::SKILL_FIST,
@@ -252,7 +277,7 @@ class OTS_Player extends OTS_Row_DAO
$this->skills[$id] = array('value' => $skills[$name], 'tries' => $skills[$name . '_tries']);
}
}
else if(tableExist('player_skills')) {
else if($this->db->hasTable('player_skills')) {
foreach( $this->db->query('SELECT ' . $this->db->fieldName('skillid') . ', ' . $this->db->fieldName('value') . ', ' . $this->db->fieldName('count') . ' FROM ' . $this->db->tableName('player_skills') . ' WHERE ' . $this->db->fieldName('player_id') . ' = ' . $this->data['id'])->fetchAll() as $skill)
{
$this->skills[ $skill['skillid'] ] = array('value' => $skill['value'], 'tries' => $skill['count']);
@@ -272,7 +297,7 @@ class OTS_Player extends OTS_Row_DAO
public function find($name)
{
// finds player's ID
$id = $this->db->query('SELECT ' . $this->db->fieldName('id') . ' FROM ' . $this->db->tableName('players') . ' WHERE ' . $this->db->fieldName('name') . ' = ' . $this->db->quote($name) )->fetch();
$id = $this->db->query('SELECT `id` FROM `players` WHERE `name` = ' . $this->db->quote($name) )->fetch();
// if anything was found
if( isset($id['id']) )
@@ -304,12 +329,12 @@ class OTS_Player extends OTS_Row_DAO
public function save()
{
$skull_type = 'skull';
if(fieldExist('skull_type', 'players')) {
if($this->db->hasColumn('players', 'skull_type')) {
$skull_type = 'skull_type';
}
$skull_time = 'skulltime';
if(fieldExist('skull_time', 'players')) {
if($this->db->hasColumn('players', 'skull_time')) {
$skull_time = 'skull_time';
}
@@ -350,37 +375,37 @@ class OTS_Player extends OTS_Row_DAO
if( isset($this->data['id']) )
{
$loss = '';
if(fieldExist('loss_experience', 'players')) {
if($this->db->hasColumn('players', 'loss_experience')) {
$loss = ', `loss_experience` = ' . $this->data['loss_experience'] . ', `loss_mana` = ' . $this->data['loss_mana'] . ', `loss_skills` = ' . $this->data['loss_skills'];
}
$loss_items = '';
if(fieldExist('loss_items', 'players')) {
if($this->db->hasColumn('players', 'loss_items')) {
$loss_items = ', `loss_items` = ' . $this->data['loss_items'] . ', `loss_containers` = ' . $this->data['loss_containers'];
}
$guild_info = '';
if(!tableExist('guild_members') && fieldExist('guildnick', 'players')) {
if(!$this->db->hasTable('guild_members') && $this->db->hasColumn('players', 'guildnick')) {
$guild_info = ', `guildnick` = ' . $this->db->quote($this->data['guildnick']) . ', ' . $this->db->fieldName('rank_id') . ' = ' . $this->data['rank_id'];
}
$direction = '';
if(fieldExist('direction', 'players')) {
if($this->db->hasColumn('players', 'direction')) {
$direction = ', `direction` = ' . $this->db->quote($this->data['direction']);
}
$blessings = '';
if(fieldExist('blessings', 'players')) {
if($this->db->hasColumn('players', 'blessings')) {
$blessings = ', `blessings` = ' . $this->db->quote($this->data['blessings']);
}
$stamina = '';
if(fieldExist('stamina', 'players')) {
if($this->db->hasColumn('players', 'stamina')) {
$stamina = ', `stamina` = ' . $this->db->quote($this->data['stamina']);
}
$lookaddons = '';
if(fieldExist('lookaddons', 'players')) {
if($this->db->hasColumn('players', 'lookaddons')) {
$lookaddons = ', `lookaddons` = ' . $this->db->quote($this->data['lookaddons']);
}
@@ -392,56 +417,56 @@ class OTS_Player extends OTS_Row_DAO
{
$loss = '';
$loss_data = '';
if(fieldExist('loss_experience', 'players')) {
if($this->db->hasColumn('players', 'loss_experience')) {
$loss = ', `loss_experience`, `loss_mana`, `loss_skills`';
$loss_data = ', ' . $this->data['loss_experience'] . ', ' . $this->data['loss_mana'] . ', ' . $this->data['loss_skills'];
}
$loss_items = '';
$loss_items_data = '';
if(fieldExist('loss_items', 'players')) {
if($this->db->hasColumn('players', 'loss_items')) {
$loss_items = ', `loss_items`, `loss_containers`';
$loss_items_data = ', ' . $this->data['loss_items'] . ', ' . $this->data['loss_containers'];
}
$guild_info = '';
$guild_info_data = '';
if(!tableExist('guild_members') && fieldExist('guildnick', 'players')) {
if(!$this->db->hasTable('guild_members') && $this->db->hasColumn('players', 'guildnick')) {
$guild_info = ', `guildnick`, `rank_id`';
$guild_info_data = ', ' . $this->db->quote($this->data['guildnick']) . ', ' . $this->data['rank_id'];
}
$promotion = '';
$promotion_data = '';
if(fieldExist('promotion', 'players')) {
if($this->db->hasColumn('players', 'promotion')) {
$promotion = ', `promotion`';
$promotion_data = ', ' . $this->data['promotion'];
}
$direction = '';
$direction_data = '';
if(fieldExist('direction', 'players')) {
if($this->db->hasColumn('players', 'direction')) {
$direction = ', `direction`';
$direction_data = ', ' . $this->data['direction'];
}
$blessings = '';
$blessings_data = '';
if(fieldExist('blessings', 'players')) {
if($this->db->hasColumn('players', 'blessings')) {
$blessings = ', `blessings`';
$blessings_data = ', ' . $this->data['blessings'];
}
$stamina = '';
$stamina_data = '';
if(fieldExist('stamina', 'players')) {
if($this->db->hasColumn('players', 'stamina')) {
$stamina = ', `stamina`';
$stamina_data = ', ' . $this->data['stamina'];
}
$lookaddons = '';
$lookaddons_data = '';
if(fieldExist('lookaddons', 'players')) {
if($this->db->hasColumn('players', 'lookaddons')) {
$lookaddons = ', `lookaddons`';
$lookaddons_data = ', ' . $this->data['lookaddons'];
}
@@ -453,7 +478,7 @@ class OTS_Player extends OTS_Row_DAO
}
// updates skills - doesn't matter if we have just created character - trigger inserts new skills
if(fieldExist('skill_fist', 'players')) { // tfs 1.0
if($this->db->hasColumn('players', 'skill_fist')) { // tfs 1.0
$set = '';
$skill_ids = array(
@@ -477,7 +502,7 @@ class OTS_Player extends OTS_Row_DAO
$skills = $this->db->query('UPDATE `players` SET ' . $set . ' WHERE `id` = ' . $this->data['id']);
}
else if(tableExist('player_skills')) {
else if($this->db->hasTable('player_skills')) {
foreach($this->skills as $id => $skill)
{
$this->db->query('UPDATE ' . $this->db->tableName('player_skills') . ' SET ' . $this->db->fieldName('value') . ' = ' . $skill['value'] . ', ' . $this->db->fieldName('count') . ' = ' . $skill['tries'] . ' WHERE ' . $this->db->fieldName('player_id') . ' = ' . $this->data['id'] . ' AND ' . $this->db->fieldName('skillid') . ' = ' . $id);
@@ -513,7 +538,7 @@ class OTS_Player extends OTS_Row_DAO
throw new E_OTS_NotLoaded();
}
return $this->data['hidden'];
return $this->data['hidden'] == 1;
}
public function setHidden($hidden)
@@ -523,7 +548,7 @@ class OTS_Player extends OTS_Row_DAO
public function getMarriage()
{
if(!fieldExist('marriage', 'players'))
if(!$this->db->hasColumn('players', 'marriage'))
return '';
if( !isset($this->data['marriage']) )
@@ -725,7 +750,7 @@ class OTS_Player extends OTS_Row_DAO
public function isDeleted()
{
$field = 'deleted';
if(fieldExist('deletion', 'players'))
if($this->db->hasColumn('players', 'deletion'))
$field = 'deletion';
if( !isset($this->data[$field]) )
@@ -743,7 +768,7 @@ class OTS_Player extends OTS_Row_DAO
public function isOnline()
{
if(tableExist('players_online')) // tfs 1.0
if($this->db->hasTable('players_online')) // tfs 1.0
{
$query = $this->db->query('SELECT `player_id` FROM `players_online` WHERE `player_id` = ' . $this->data['id']);
return $query->rowCount() > 0;
@@ -1833,7 +1858,7 @@ class OTS_Player extends OTS_Row_DAO
public function loadRank()
{
$table = 'guild_membership';
if(tableExist('guild_members'))
if($this->db->hasTable('guild_members'))
$table = 'guild_members';
$ranks = $this->db->query('SELECT `rank_id`, `nick` FROM `' . $table . '` WHERE `player_id` = ' . $this->db->quote($this->getID()))->fetch();
@@ -1869,11 +1894,11 @@ class OTS_Player extends OTS_Row_DAO
public function setGuildNick($guildnick)
{
$this->data['guildnick'] = (string) $guildnick;
if(tableExist('guild_members'))
if($this->db->hasTable('guild_members'))
$this->db->query('UPDATE `guild_members` SET `nick` = ' . $this->db->quote($this->data['guildnick']) . ' WHERE `player_id` = ' . $this->getId());
else if(tableExist('guild_membership'))
else if($this->db->hasTable('guild_membership'))
$this->db->query('UPDATE `guild_membership` SET `nick` = ' . $this->db->quote($this->data['guildnick']) . ' WHERE `player_id` = ' . $this->getId());
else if(fieldExist('guildnick', 'players'))
else if($this->db->hasColumn('players', 'guildnick'))
$this->db->query('UPDATE `players` SET `guildnick` = ' . $this->db->quote($this->data['guildnick']) . ' WHERE `id` = ' . $this->getId());
}
@@ -1908,21 +1933,21 @@ class OTS_Player extends OTS_Row_DAO
public function getRank()
{
$rank_id = 0;
if(tableExist('guild_members')) {
if($this->db->hasTable('guild_members')) {
$query = $this->db->query('SELECT `rank_id` FROM `guild_members` WHERE `player_id`= ' . $this->data['id'] . ' LIMIT 1;');
if($query->rowCount() == 1) {
$query = $query->fetch();
$rank_id = $query['rank_id'];
}
}
else if(tableExist('guild_membership')) {
else if($this->db->hasTable('guild_membership')) {
$query = $this->db->query('SELECT `rank_id` FROM `guild_membership` WHERE `player_id`= ' . $this->data['id'] . ' LIMIT 1;');
if($query->rowCount() == 1) {
$query = $query->fetch();
$rank_id = $query['rank_id'];
}
}
else if(fieldExist('rank_id', 'players')) {
else if($this->db->hasColumn('players', 'rank_id')) {
$query = $this->db->query('SELECT `rank_id` FROM `players` WHERE `id`= ' . $this->data['id'] . ';')->fetch();
$rank_id = $query['rank_id'];
}
@@ -1944,10 +1969,10 @@ class OTS_Player extends OTS_Row_DAO
{
if( isset($rank_id) && isset($guild)) {
if($rank_id == 0) {
if(tableExist('guild_membership')) {
if($this->db->hasTable('guild_membership')) {
$this->db->query('DELETE FROM `guild_membership` WHERE `player_id` = ' . $this->getId());
}
else if(tableExist('guild_members')) {
else if($this->db->hasTable('guild_members')) {
$this->db->query('DELETE FROM `guild_members` WHERE `player_id` = ' . $this->getId());
}
else {
@@ -1956,7 +1981,7 @@ class OTS_Player extends OTS_Row_DAO
}
}
else {
if(tableExist('guild_membership')) {
if($this->db->hasTable('guild_membership')) {
$query = $this->db->query('SELECT `player_id` FROM `guild_membership` WHERE `player_id` = ' . $this->getId() . ' LIMIT 1;');
if($query->rowCount() == 1)
$this->db->query('UPDATE `guild_membership` SET `guild_id` = ' . (int)$guild . ', `rank_id` = ' . (int)$rank_id . ' WHERE `player_id` = ' . $this->getId());
@@ -1964,7 +1989,7 @@ class OTS_Player extends OTS_Row_DAO
$this->db->query('INSERT INTO `guild_membership` (`player_id`, `guild_id`, `rank_id`, `nick`) VALUES (' . $this->db->quote($this->getId()) . ', ' . $this->db->quote($guild) . ', ' . $this->db->quote($rank_id) . ', ' . $this->db->quote('') . ')');
}
}
else if(tableExist('guild_members')) {
else if($this->db->hasTable('guild_members')) {
$query = $this->db->query('SELECT `player_id` FROM `guild_members` WHERE `player_id` = ' . $this->getId() . ' LIMIT 1;');
if($query->rowCount() == 1)
$this->db->query('UPDATE `guild_members` SET `rank_id` = ' . (int)$rank_id . ' WHERE `player_id` = ' . $this->getId());

View File

@@ -296,7 +296,7 @@ class Validator
}
//check if was namelocked previously
if(tableExist('player_namelocks') && fieldExist('name', 'player_namelocks')) {
if($db->hasTable('player_namelocks') && $db->hasColumn('player_namelocks', 'name')) {
$namelock = $db->query('SELECT `player_id` FROM `player_namelocks` WHERE `name` = ' . $db->quote($name));
if($namelock->rowCount() > 0) {
self::$lastError = 'Character with this name has been namelocked.';