- ManiaControl Restart Command

- Fixed StatisticsManager Namespace
This commit is contained in:
Steffen Schröder 2014-01-01 17:59:29 +01:00
parent 3297070e1a
commit b84ffa081d
2 changed files with 120 additions and 193 deletions

View File

@ -14,7 +14,7 @@ use ManiaControl\Players\Player;
use ManiaControl\Players\PlayerManager; use ManiaControl\Players\PlayerManager;
use ManiaControl\Plugins\PluginManager; use ManiaControl\Plugins\PluginManager;
use ManiaControl\Server\Server; use ManiaControl\Server\Server;
use StatisticManager; use ManiaControl\Statistics\StatisticManager;
require_once __DIR__ . '/Callbacks/CallbackListener.php'; require_once __DIR__ . '/Callbacks/CallbackListener.php';
require_once __DIR__ . '/Commands/CommandListener.php'; require_once __DIR__ . '/Commands/CommandListener.php';
@ -33,7 +33,7 @@ require_once __DIR__ . '/GbxDataFetcher/gbxdatafetcher.inc.php';
require_once __DIR__ . '/ManiaExchange/mxinfofetcher.inc.php'; require_once __DIR__ . '/ManiaExchange/mxinfofetcher.inc.php';
require_once __DIR__ . '/ManiaExchange/mxinfosearcher.inc.php'; require_once __DIR__ . '/ManiaExchange/mxinfosearcher.inc.php';
require_once __DIR__ . '/Manialinks/ManialinkManager.php'; require_once __DIR__ . '/Manialinks/ManialinkManager.php';
require_once __DIR__ . '/Statistics/StatisticManager.php'; require_once __DIR__ . '/Statistics/StatisticManager.php';
require_once __DIR__ . '/Maps/Map.php'; require_once __DIR__ . '/Maps/Map.php';
require_once __DIR__ . '/Maps/MapManager.php'; require_once __DIR__ . '/Maps/MapManager.php';
require_once __DIR__ . '/Maps/MapList.php'; require_once __DIR__ . '/Maps/MapList.php';
@ -64,6 +64,8 @@ class ManiaControl implements CommandListener {
*/ */
const VERSION = '0.01'; const VERSION = '0.01';
const API_VERSION = '2013-04-16'; const API_VERSION = '2013-04-16';
const OS_UNIX = 'Unix';
const OS_WIN = 'Windows';
/** /**
* Public properties * Public properties
@ -119,6 +121,7 @@ class ManiaControl implements CommandListener {
// Register for commands // Register for commands
$this->commandManager->registerCommandListener('version', $this, 'command_Version'); $this->commandManager->registerCommandListener('version', $this, 'command_Version');
$this->commandManager->registerCommandListener('restart', $this, 'command_Restart', true);
} }
/** /**
@ -130,6 +133,28 @@ class ManiaControl implements CommandListener {
logMessage($message); logMessage($message);
} }
/**
* Get the Operating System on which ManiaControl is running
*
* @param string $compareOS
* @return string
*/
public function getOS($compareOS = null) {
$windows = defined('PHP_WINDOWS_VERSION_MAJOR');
if ($compareOS) {
// Return bool whether OS equals $compareOS
if ($compareOS == self::OS_WIN) {
return $windows;
}
return !$windows;
}
// Return OS
if ($windows) {
return self::OS_WIN;
}
return self::OS_UNIX;
}
/** /**
* Return message composed of client error message and error code * Return message composed of client error message and error code
* *
@ -144,15 +169,29 @@ class ManiaControl implements CommandListener {
} }
/** /**
* Send ManiaControl version * Handle Version Command
* *
* @param array $chat * @param array $chatCallback
* @param Player $player * @param Player $player
* @return bool
*/ */
public function command_Version(array $chat, Player $player) { public function command_Version(array $chatCallback, Player $player) {
$message = 'This server is using ManiaControl v' . ManiaControl::VERSION . '!'; $message = 'This server is using ManiaControl v' . ManiaControl::VERSION . '!';
return $this->chat->sendInformation($message, $player->login); $this->chat->sendInformation($message, $player->login);
}
/**
* Handle Restart AdminCommand
*
* @param array $chatCallback
* @param Player $player
*/
public function command_Restart(array $chatCallback, Player $player) {
if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) {
$this->authenticationManager->sendNotAllowed($player);
return;
}
$this->log($player->login . ' requested ManiaControl Restart.');
$this->restart();
} }
/** /**
@ -161,6 +200,11 @@ class ManiaControl implements CommandListener {
* @param string $message * @param string $message
*/ */
public function quit($message = '') { public function quit($message = '') {
// Log quit reason
if ($message) {
$this->log($message);
}
// OnShutdown callback // OnShutdown callback
$this->callbackManager->triggerCallback(CallbackManager::CB_MC_ONSHUTDOWN, array(CallbackManager::CB_MC_ONSHUTDOWN)); $this->callbackManager->triggerCallback(CallbackManager::CB_MC_ONSHUTDOWN, array(CallbackManager::CB_MC_ONSHUTDOWN));
@ -172,11 +216,6 @@ class ManiaControl implements CommandListener {
$this->client->query('SendHideManialinkPage'); $this->client->query('SendHideManialinkPage');
} }
// Log quit reason
if ($message) {
$this->log($message);
}
// Shutdown // Shutdown
if ($this->client) { if ($this->client) {
$this->client->Terminate(); $this->client->Terminate();
@ -186,6 +225,39 @@ class ManiaControl implements CommandListener {
exit(); exit();
} }
/**
* Restart ManiaControl
*
* @param string $message
*/
public function restart($message = null) {
// Shutdown callback
$this->callbackManager->triggerCallback(CallbackManager::CB_MC_ONSHUTDOWN, array(CallbackManager::CB_MC_ONSHUTDOWN));
// Announce restart
$this->chat->sendInformation('Restarting ManiaControl...');
$this->log('Restarting ManiaControl...');
if ($message) $this->log($message);
// Hide widgets
$this->client->query('SendHideManialinkPage');
// Close connection
$this->client->Terminate();
// Execute start script in background
if ($this->getOS(self::OS_UNIX)) {
$command = 'sh ' . escapeshellarg(ManiaControlDir . '/ManiaControl.sh') . ' > /dev/null &';
exec($command);
}
else {
$command = 'start /B ' . escapeshellarg(ManiaControlDir . '/ManiaControl.bat');
pclose(popen($command, 'r'));
}
exit();
}
/** /**
* Run ManiaControl * Run ManiaControl
*/ */

View File

@ -1,175 +1,34 @@
<?php <?php
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
use ManiaControl\Statistics\StatisticCollector;
require_once __DIR__ . '/StatisticCollector.php'; namespace ManiaControl\Statistics;
use ManiaControl\ManiaControl;
/** /**
* Statistic Manager Class * Statistic Manager Class
* *
* @author steeffeen & kremsy * @author steeffeen & kremsy
*/ */
//TODO db reference between player index and statsitics playerId
//TODO db reference between metadata statId and statistics statId
class StatisticManager { class StatisticManager {
/** /**
* Constants * Constants
*/ */
const TABLE_STATMETADATA = 'mc_statmetadata'; const TABLE_STATMETADATA = 'mc_statmetadata';
const TABLE_STATISTICS = 'mc_statistics'; const TABLE_STATISTICS = 'mc_statistics';
const STAT_TYPE_INT = '0';
/**
* Public Properties
*/
public $statisticCollector = null;
/** /**
* Private Properties * Private Properties
*/ */
private $maniaControl = null; private $maniaControl = null;
private $mysqli = null;
private $stats = array();
/** /**
* Construct player manager * Construct player manager
* *
* @param \ManiaControl\ManiaControl $maniaControl * @param ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->mysqli = $this->maniaControl->database->mysqli;
$this->initTables(); $this->initTables();
$this->statisticCollector = new StatisticCollector($maniaControl);
//Store Stats MetaData
$this->storeStatMetaData();
}
/**
* Get the value of an statistic
*
* @param $statName
* @param $playerId
* @param bool $serverLogin
* @return int
*/
public function getStatisticData($statName, $playerId, $serverLogin = false) {
$statId = $this->getStatId($statName);
if($statId == null) {
return -1;
}
if(!$serverLogin) {
$query = "SELECT SUM(value) as value FROM `" . self::TABLE_STATISTICS . "` WHERE `statId` = " . $statId . " AND `playerId` = " . $playerId . ";";
} else {
$query = "SELECT value FROM `" . self::TABLE_STATISTICS . "` WHERE `statId` = " . $statId . " AND `playerId` = " . $playerId . " AND `serverLogin` = '" . $serverLogin . "';";
}
$result = $this->mysqli->query($query);
if(!$result) {
trigger_error($this->mysqli->error);
}
$row = $result->fetch_object();
$result->close();
return $row->value;
}
/**
* Store Stats Meta Data from the Database
*/
private function storeStatMetaData() {
$query = "SELECT * FROM `" . self::TABLE_STATMETADATA . "`;";
$result = $this->mysqli->query($query);
if(!$result) {
trigger_error($this->mysqli->error);
}
while($row = $result->fetch_object()) {
$this->stats[$row->name] = $row;
}
$result->close();
}
/**
* Returns the Stats Id
*
* @param $statName
* @return int
*/
private function getStatId($statName) {
if(isset($this->stats[$statName])) {
$stat = $this->stats[$statName];
return $stat->index;
} else {
return null;
}
}
/**
* Inserts a Stat into the database
*
* @param $statName
* @param Player $player
* @param string $serverLogin
* @param $value , value to Add
* @param string $statType
*/
public function insertStat($statName, Player $player, $serverLogin, $value, $statType = self::STAT_TYPE_INT) {
$statId = $this->getStatId($statName);
if($statId == null) {
return false;
}
if($player->isFakePlayer()) {
return true;
}
$query = "INSERT INTO `" . self::TABLE_STATISTICS . "` (
`serverLogin`,
`playerId`,
`statId`,
`value`
) VALUES (
?, ?, ?, ?
) ON DUPLICATE KEY UPDATE
`value` = `value` + VALUES(`value`);";
$statement = $this->mysqli->prepare($query);
if($this->mysqli->error) {
trigger_error($this->mysqli->error);
return false;
}
$statement->bind_param('siii', $serverLogin, $player->index, $statId, $value);
$statement->execute();
if($statement->error) {
trigger_error($statement->error);
$statement->close();
return false;
}
$statement->close();
return true;
}
/**
* Increments a Statistic by one
*
* @param $statName
* @param Player $playerId
* @param $serverLogin
* @return bool
*/
public function incrementStat($statName, Player $player, $serverLogin) {
return $this->insertStat($statName, $player, $serverLogin, 1);
} }
/** /**
@ -177,36 +36,39 @@ class StatisticManager {
* *
* @param string $statName * @param string $statName
* @param string $statDescription * @param string $statDescription
* @return bool
*/ */
public function defineStatMetaData($statName, $statDescription = '') { public function defineStatMetaData($statName, $statDescription = '') {
$query = "INSERT IGNORE INTO `" . self::TABLE_STATMETADATA . "` ( $mysqli = $this->maniaControl->database->mysqli;
`name`, $query = "INSERT IGNORE INTO `" . self::TABLE_STATMETADATA . "` (
`description` `name`,
`description`
) VALUES ( ) VALUES (
?, ? ?, ?
);"; );";
$statement = $this->mysqli->prepare($query); $statement = $mysqli->prepare($query);
if($this->mysqli->error) { if ($mysqli->error) {
trigger_error($this->mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$statement->bind_param('ss', $statName, $statDescription); $statement->bind_param('ss', $statName, $statDescription);
$statement->execute(); $statement->execute();
if($statement->error) { if ($statement->error) {
trigger_error($statement->error); trigger_error($statement->error);
$statement->close(); $statement->close();
return false; return false;
} }
$statement->close(); $statement->close();
return true;
} }
/** /**
* Initialize necessary database tables * Initialize necessary Database Tables
* *
* @return bool * @return bool
*/ */
private function initTables() { private function initTables() {
$mysqli = $this->maniaControl->database->mysqli;
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATMETADATA . "` ( $query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATMETADATA . "` (
`index` int(11) NOT NULL AUTO_INCREMENT, `index` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
@ -214,43 +76,36 @@ class StatisticManager {
PRIMARY KEY (`index`), PRIMARY KEY (`index`),
UNIQUE KEY `name` (`name`) UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics Meta Data' AUTO_INCREMENT=1;"; ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics Meta Data' AUTO_INCREMENT=1;";
$statement = $mysqli->prepare($query);
$statement = $this->mysqli->prepare($query); if ($mysqli->error) {
if($this->mysqli->error) { trigger_error($mysqli->error, E_USER_ERROR);
trigger_error($this->mysqli->error, E_USER_ERROR);
return false; return false;
} }
$statement->execute(); $statement->execute();
if($statement->error) { if ($statement->error) {
trigger_error($statement->error, E_USER_ERROR); trigger_error($statement->error, E_USER_ERROR);
return false; return false;
} }
$statement->close(); $statement->close();
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATISTICS . "` ( $query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_STATISTICS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT, `statId` int(11) NOT NULL AUTO_INCREMENT,
`serverLogin` varchar(50) NOT NULL,
`playerId` int(11) NOT NULL, `playerId` int(11) NOT NULL,
`statId` int(11) NOT NULL, `serverId` int(11) NOT NULL,
`value` int(20) COLLATE utf8_unicode_ci NOT NULL, `value` int(20) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`index`), UNIQUE KEY `unique` (`statId`,`playerId`,`serverId`)
UNIQUE KEY `unique` (`statId`,`playerId`,`serverLogin`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics Data' AUTO_INCREMENT=1;";
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Statistics' AUTO_INCREMENT=1;"; $statement = $mysqli->prepare($query);
if ($mysqli->error) {
$statement = $this->mysqli->prepare($query); trigger_error($mysqli->error, E_USER_ERROR);
if($this->mysqli->error) {
trigger_error($this->mysqli->error, E_USER_ERROR);
return false; return false;
} }
$statement->execute(); $statement->execute();
if($statement->error) { if ($statement->error) {
trigger_error($statement->error, E_USER_ERROR); trigger_error($statement->error, E_USER_ERROR);
return false; return false;
} }
$statement->close(); $statement->close();
return true;
} }
} }