- subfolders for different classes

- Improved file and class naming
- "handler"-> "listener" (callbacks, commands)
This commit is contained in:
Steffen Schröder 2013-11-12 15:48:25 +01:00
parent 348db3602a
commit 9e5e444552
21 changed files with 515 additions and 394 deletions

View File

@ -19,7 +19,7 @@ if (!is_dir('logs')) {
ini_set('error_log', 'logs/ManiaControl_' . getmypid() . '.log'); ini_set('error_log', 'logs/ManiaControl_' . getmypid() . '.log');
// Load ManiaControl class // Load ManiaControl class
require_once __DIR__ . '/core/maniaControl.php'; require_once __DIR__ . '/core/ManiaControl.php';
// Start ManiaControl // Start ManiaControl
error_log('Loading ManiaControl v' . ManiaControl::VERSION . '...'); error_log('Loading ManiaControl v' . ManiaControl::VERSION . '...');

View File

@ -0,0 +1,13 @@
<?php
namespace ManiaControl\Callbacks;
/**
* Interface for CallbackListener
*
* @author steeffeen & kremsy
*/
interface CallbackListener {
}
?>

View File

@ -1,13 +1,17 @@
<?php <?php
namespace ManiaControl; namespace ManiaControl\Callbacks;
require_once __DIR__ . '/CallbackListener.php';
use ManiaControl\ManiaControl;
/** /**
* Class for handling server and controller callbacks * Class for managing server and controller callbacks
* *
* @author steeffeen * @author steeffeen & kremsy
*/ */
class Callbacks { class CallbackManager {
/** /**
* Constants * Constants
*/ */
@ -23,10 +27,8 @@ class Callbacks {
// ManiaPlanet callbacks // ManiaPlanet callbacks
const CB_MP_SERVERSTART = 'ManiaPlanet.ServerStart'; const CB_MP_SERVERSTART = 'ManiaPlanet.ServerStart';
const CB_MP_SERVERSTOP = 'ManiaPlanet.ServerStop'; const CB_MP_SERVERSTOP = 'ManiaPlanet.ServerStop';
const CB_MP_BEGINMAP = 'ManiaPlanet.BeginMap';
const CB_MP_BEGINMATCH = 'ManiaPlanet.BeginMatch'; const CB_MP_BEGINMATCH = 'ManiaPlanet.BeginMatch';
const CB_MP_ENDMATCH = 'ManiaPlanet.EndMatch'; const CB_MP_ENDMATCH = 'ManiaPlanet.EndMatch';
const CB_MP_ENDMAP = 'ManiaPlanet.EndMap';
const CB_MP_MAPLISTMODIFIED = 'ManiaPlanet.MapListModified'; const CB_MP_MAPLISTMODIFIED = 'ManiaPlanet.MapListModified';
const CB_MP_ECHO = 'ManiaPlanet.Echo'; const CB_MP_ECHO = 'ManiaPlanet.Echo';
const CB_MP_BILLUPDATED = 'ManiaPlanet.BillUpdated'; const CB_MP_BILLUPDATED = 'ManiaPlanet.BillUpdated';
@ -50,21 +52,19 @@ class Callbacks {
* Private properties * Private properties
*/ */
private $maniaControl = null; private $maniaControl = null;
private $callbackHandlers = array(); private $callbackListeners = array();
private $last1Second = -1; private $last1Second = -1;
private $last5Second = -1; private $last5Second = -1;
private $last1Minute = -1; private $last1Minute = -1;
private $last3Minute = -1; private $last3Minute = -1;
/** /**
* Construct callbacks handler * Construct callbacks manager
* *
* @param ManiaControl $maniaControl * @param \ManiaControl\ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
// Init values
$this->last1Second = time(); $this->last1Second = time();
$this->last5Second = time(); $this->last5Second = time();
$this->last1Minute = time(); $this->last1Minute = time();
@ -72,89 +72,24 @@ class Callbacks {
} }
/** /**
* Perform OnInit callback * Register a new callback listener
*
* @param string $callbackName
* @param \ManiaControl\Callbacks\CallbackListener $listener
* @param string $method
* @return bool
*/ */
public function onInit() { public function registerCallbackListener($callbackName, CallbackListener $listener, $method) {
// On init callback if (!method_exists($listener, $method)) {
$this->triggerCallback(self::CB_MC_ONINIT, array(self::CB_MC_ONINIT)); trigger_error(
"Given listener (" . get_class($listener) . ") can't handle callback '{$callbackName}' (no method '{$method}')!");
// Simulate begin map return false;
$map = $this->maniaControl->server->getCurrentMap();
if ($map) {
$this->triggerCallback(self::CB_MC_BEGINMAP, array(self::CB_MC_BEGINMAP, array($map)));
} }
} if (!array_key_exists($callbackName, $this->callbackListeners)) {
$this->callbackListeners[$callbackName] = array();
/**
* Handles the given array of callbacks
*/
public function handleCallbacks() {
// Perform ManiaControl callbacks
if ($this->last1Second <= time() - 1) {
$this->last1Second = time();
// 1 second
$this->triggerCallback(self::CB_MC_1_SECOND, array(self::CB_MC_1_SECOND));
if ($this->last5Second <= time() - 5) {
$this->last5Second = time();
// 5 second
$this->triggerCallback(self::CB_MC_5_SECOND, array(self::CB_MC_5_SECOND));
if ($this->last1Minute <= time() - 60) {
$this->last1Minute = time();
// 1 minute
$this->triggerCallback(self::CB_MC_1_MINUTE, array(self::CB_MC_1_MINUTE));
if ($this->last3Minute <= time() - 180) {
$this->last3Minute = time();
// 3 minute
$this->triggerCallback(self::CB_MC_3_MINUTE, array(self::CB_MC_3_MINUTE));
}
}
}
}
// Get server callbacks
if (!$this->maniaControl->client) {
return;
}
$this->maniaControl->client->resetError();
$this->maniaControl->client->readCB();
$callbacks = $this->maniaControl->client->getCBResponses();
if (!is_array($callbacks) || $this->maniaControl->client->isError()) {
trigger_error("Error reading server callbacks. " . $this->maniaControl->getClientErrorText());
return;
}
// Handle callbacks
foreach ($callbacks as $index => $callback) {
$callbackName = $callback[0];
switch ($callbackName) {
case self::CB_MP_BEGINMAP:
{
// Map begin
$this->triggerCallback($callbackName, $callback);
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
break;
}
case self::CB_MP_ENDMAP:
{
// Map end
$this->triggerCallback($callbackName, $callback);
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
break;
}
default:
{
$this->triggerCallback($callbackName, $callback);
break;
}
}
} }
array_push($this->callbackListeners[$callbackName], array($listener, $method));
return true;
} }
/** /**
@ -164,28 +99,119 @@ class Callbacks {
* @param array $data * @param array $data
*/ */
public function triggerCallback($callbackName, array $callback) { public function triggerCallback($callbackName, array $callback) {
if (!array_key_exists($callbackName, $this->callbackHandlers)) { if (!array_key_exists($callbackName, $this->callbackListeners)) {
return; return;
} }
foreach ($this->callbackHandlers[$callbackName] as $handler) { foreach ($this->callbackListeners[$callbackName] as $listener) {
call_user_func(array($handler[0], $handler[1]), $callback); call_user_func(array($listener[0], $listener[1]), $callback);
} }
} }
/** /**
* Add a new callback handler * Trigger internal and manage server callbacks
*/ */
public function registerCallbackHandler($callback, $handler, $method) { public function manageCallbacks() {
if (!is_object($handler) || !method_exists($handler, $method)) { $this->manageTimedCallbacks();
trigger_error("Given handler can't handle callback '{$callback}' (no method '{$method}')!");
// Get server callbacks
if (!$this->maniaControl->client) {
return; return;
} }
if (!array_key_exists($callback, $this->callbackHandlers) || !is_array($this->callbackHandlers[$callback])) { $this->maniaControl->client->readCB();
// Init callback handler array $callbacks = $this->maniaControl->client->getCBResponses();
$this->callbackHandlers[$callback] = array(); if (!is_array($callbacks)) {
trigger_error("Error reading server callbacks. " . $this->maniaControl->getClientErrorText());
return;
} }
// Register callback handler
array_push($this->callbackHandlers[$callback], array($handler, $method)); // Handle callbacks
foreach ($callbacks as $index => $callback) {
$callbackName = $callback[0];
switch ($callbackName) {
case 'ManiaPlanet.BeginMap':
{
$this->triggerCallback(self::CB_MC_BEGINMAP, $callback);
break;
}
case 'ManiaPlanet.EndMap':
{
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
break;
}
case self::CB_MP_MODESCRIPTCALLBACK:
{
$this->handleScriptCallback($callback);
break;
}
default:
{
var_dump($callback);
$this->triggerCallback($callbackName, $callback);
break;
}
}
}
}
/**
* Handle the given script callback
*
* @param array $callback
*/
private function handleScriptCallback(array $callback) {
$scriptCallbackName = $callback[1][0];
switch ($scriptCallbackName) {
case 'EndMap':
{
$this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACK, $callback);
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
break;
}
case 'LibXmlRpc_EndMap':
{
$this->triggerCallback(self::CB_MP_MODESCRIPTCALLBACK, $callback);
$this->triggerCallback(self::CB_MC_ENDMAP, $callback);
break;
}
default:
{
var_dump($callback);
break;
}
}
}
/**
* Manage recurring timed callbacks
*/
private function manageTimedCallbacks() {
// 1 second
if ($this->last1Second > time() - 1) {
return;
}
$this->last1Second = time();
$this->triggerCallback(self::CB_MC_1_SECOND, array(self::CB_MC_1_SECOND));
// 5 second
if ($this->last5Second > time() - 5) {
return;
}
$this->last5Second = time();
$this->triggerCallback(self::CB_MC_5_SECOND, array(self::CB_MC_5_SECOND));
// 1 minute
if ($this->last1Minute > time() - 60) {
return;
}
$this->last1Minute = time();
$this->triggerCallback(self::CB_MC_1_MINUTE, array(self::CB_MC_1_MINUTE));
// 3 minute
if ($this->last3Minute > time() - 180) {
return;
}
$this->last3Minute = time();
$this->triggerCallback(self::CB_MC_3_MINUTE, array(self::CB_MC_3_MINUTE));
} }
} }

View File

@ -0,0 +1,13 @@
<?php
namespace ManiaControl\Commands;
/**
* Interface for command listeners
*
* @author steeffeen & kremsy
*/
interface CommandListener {
}
?>

View File

@ -1,6 +1,14 @@
<?php <?php
namespace ManiaControl; namespace ManiaControl\Commands;
require_once __DIR__ . '/CommandListener.php';
use ManiaControl\Authentication;
use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Players\Player;
/** /**
* Class for handling chat commands * Class for handling chat commands
@ -8,56 +16,57 @@ namespace ManiaControl;
* @author steeffeen & kremsy * @author steeffeen & kremsy
*/ */
// TODO: settings for command auth levels // TODO: settings for command auth levels
class Commands { class CommandManager implements CallbackListener, CommandListener {
/** /**
* Private properties * Private properties
*/ */
private $maniaControl = null; private $maniaControl = null;
private $commandHandlers = array(); private $commandListeners = array();
private $openBills = array(); private $openBills = array();
private $serverShutdownTime = -1; private $serverShutdownTime = -1;
private $serverShutdownEmpty = false; private $serverShutdownEmpty = false;
/** /**
* Construct commands handler * Construct commands handler
*
* @param \ManiaControl\ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
// Register for callbacks $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_5_SECOND, $this, 'each5Seconds');
$this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MC_5_SECOND, $this, 'each5Seconds'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_BILLUPDATED, $this, 'handleBillUpdated');
$this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MP_BILLUPDATED, $this, 'handleBillUpdated'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handleChatCallback');
$this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MP_PLAYERCHAT, $this, 'handleChatCallback');
// Register basic commands // Register basic commands
$commands = array('help', 'version', 'shutdown', 'shutdownserver', 'systeminfo', 'setservername', 'getplanets', 'donate', $commands = array('help', 'version', 'shutdown', 'shutdownserver', 'systeminfo', 'setservername', 'getplanets', 'donate',
'pay', 'kick', 'nextmap', 'restartmap', 'addmap', 'removemap'); 'pay', 'kick', 'nextmap', 'restartmap', 'addmap', 'removemap');
foreach ($commands as $command) { foreach ($commands as $command) {
$this->registerCommandHandler($command, $this, 'command_' . $command); $this->registerCommandListener($command, $this, 'command_' . $command);
} }
} }
/** /**
* Register a command handler * Register a command listener
* *
* @param string $commandName * @param string $commandName
* @param object $handler * @param CommandListener $listener
* @param string $method * @param string $method
* @return bool * @return bool
*/ */
public function registerCommandHandler($commandName, $handler, $method) { public function registerCommandListener($commandName, CommandListener $listener, $method) {
$command = strtolower($commandName); $command = strtolower($commandName);
if (!is_object($handler) || !method_exists($handler, $method)) { if (!method_exists($listener, $method)) {
trigger_error("Given handler can't handle command '{$command}' (no method '{$method}')!"); trigger_error("Given listener can't handle command '{$command}' (no method '{$method}')!");
return false; return false;
} }
if (!array_key_exists($command, $this->commandHandlers) || !is_array($this->commandHandlers[$command])) { if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) {
// Init handlers array // Init listeners array
$this->commandHandlers[$command] = array(); $this->commandListeners[$command] = array();
} }
// Register command handler // Register command listener
array_push($this->commandHandlers[$command], array($handler, $method)); array_push($this->commandListeners[$command], array($listener, $method));
return true; return true;
} }
@ -68,26 +77,25 @@ class Commands {
* @return bool * @return bool
*/ */
public function handleChatCallback(array $callback) { public function handleChatCallback(array $callback) {
$chat = $callback[1];
// Check for command // Check for command
if (!$chat[3]) { if (!$callback[1][3]) {
return false; return false;
} }
// Check for valid player // Check for valid player
$player = $this->maniaControl->playerHandler->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($callback[1][1]);
if (!$player) { if (!$player) {
return false; return false;
} }
// Handle command // Handle command
$command = explode(" ", substr($chat[2], 1)); $command = explode(" ", substr($callback[1][2], 1));
$command = strtolower($command[0]); $command = strtolower($command[0]);
if (!array_key_exists($command, $this->commandHandlers) || !is_array($this->commandHandlers[$command])) { if (!array_key_exists($command, $this->commandListeners) || !is_array($this->commandListeners[$command])) {
// No command handler registered // No command handler registered
return true; return true;
} }
// Inform command handlers // Inform command handlers
foreach ($this->commandHandlers[$command] as $handler) { foreach ($this->commandListeners[$command] as $listener) {
call_user_func(array($handler[0], $handler[1]), $callback, $player); call_user_func(array($listener[0], $listener[1]), $callback, $player);
} }
return true; return true;
} }
@ -502,7 +510,7 @@ class Commands {
* Handle nextmap command * Handle nextmap command
* *
* @param array $chat * @param array $chat
* @param Player $player * @param \ManiaControl\Players\Player $player
* @return bool * @return bool
*/ */
private function command_nextmap(array $chat, Player $player) { private function command_nextmap(array $chat, Player $player) {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace ManiaControl; namespace ManiaControl\Manialinks;
/** /**
* Handler for manialink ids * Handler for manialink ids
@ -27,4 +27,5 @@ class ManialinkIdHandler {
return $mlIds; return $mlIds;
} }
} }
?> ?>

View File

@ -1,6 +1,6 @@
<?php <?php
namespace ManiaControl; namespace ManiaControl\Manialinks;
/** /**
* Manialink utility class * Manialink utility class

View File

@ -0,0 +1,84 @@
<?php
namespace ManiaControl\Maps;
use ManiaControl\ManiaControl;
/**
* Map class
*
* @author kremsy & steeffeen
*/
class Map {
/**
* Public properties
*/
public $mapFetcher = null;
public $id = 0;
public $name = 'undefined';
public $uid = '';
public $fileName = '';
public $environment = '';
public $goldTime; // TODO: format?
public $copperPrice = 0;
public $mapType = '';
public $mapStyle = '';
public $mx = null;
public $authorLogin = '';
public $authorNick = '';
public $authorZone = '';
public $authorEInfo = '';
public $comment = '';
public $titleUid = '';
public $startTime = 0;
/**
* Private properties
*/
private $maniaControl = null;
/**
* Create a new map object from rpc data
*
* @param \ManiaControl\ManiaControl $maniaControl
* @param array $rpc_infos
*/
public function __construct(ManiaControl $maniaControl, $rpc_infos = null) {
$this->maniaControl = $maniaControl;
$this->startTime = time();
if (!$rpc_infos) {
return;
}
$this->name = $rpc_infos['Name']; // in aseco stripped new lines on name
$this->uid = $rpc_infos['UId'];
$this->fileName = $rpc_infos['FileName'];
$this->authorLogin = $rpc_infos['Author'];
$this->environment = $rpc_infos['Environnement'];
$this->goldTime = $rpc_infos['GoldTime'];
$this->copperPrice = $rpc_infos['CopperPrice'];
$this->mapType = $rpc_infos['MapType'];
$this->mapStyle = $rpc_infos['MapStyle'];
$mapsDirectory = $this->maniaControl->server->getMapsDirectory();
if ($this->maniaControl->server->checkAccess($mapsDirectory)) {
$this->mapFetcher = new \GBXChallMapFetcher(true);
try {
$this->mapFetcher->processFile($mapsDirectory . $this->fileName);
}
catch (Exception $e) {
trigger_error($e->getMessage(), E_USER_WARNING);
}
$this->authorNick = $this->mapFetcher->authorNick;
$this->authorEInfo = $this->mapFetcher->authorEInfo;
$this->authorZone = $this->mapFetcher->authorZone;
$this->comment = $this->mapFetcher->comment;
}
// TODO: define timeout if mx is down
$serverInfo = $this->maniaControl->server->getSystemInfo();
$title = strtoupper(substr($serverInfo['TitleId'], 0, 2));
$this->mx = new \MXInfoFetcher($title, $this->uid, false);
}
}

View File

@ -0,0 +1,77 @@
<?php
namespace ManiaControl\Maps;
require_once __DIR__ . '/Map.php';
use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
/**
* Manager for maps
*
* @author kremsy & steeffeen
*/
class MapManager implements CallbackListener {
/**
* Private properties
*/
private $maniaControl = null;
private $mapList = array();
/**
* Construct map manager
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
// TODO: database init
// TODO: erasemap from server
// TODO: implement of a method which are called by xlist command and results maplists from maniaexcahnge (or extra class for it)
// TODO: admin add from maniaexchange, would handle it here
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initTables();
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_BEGINMAP, $this, 'handleBeginMap');
}
/**
* Initialize necessary database tables
*
* @return bool
*/
private function initTables() {
// TODO: Initialize database table
return true;
}
/**
* Add a map to the MapList
*
* @param \ManiaControl\Maps\Map $map
* @return bool
*/
private function addMap(Map $map) {
if (!$map) {
return false;
}
// TODO: Save map in database
$this->mapList[$map->uid] = $map;
return true;
}
/**
* Handle BeginMap callback
*
* @param array $callback
*/
public function handleBeginMap(array $callback) {
$rpcMap = $this->maniaControl->server->getCurrentMap();
$map = new Map($this->maniaControl, $rpcMap);
$this->addMap($map);
}
}

View File

@ -1,6 +1,6 @@
<?php <?php
namespace ManiaControl; namespace ManiaControl\Players;
/** /**
* Class representing players * Class representing players

View File

@ -1,17 +1,24 @@
<?php <?php
namespace ManiaControl; namespace ManiaControl\Players;
require_once __DIR__ . '/Player.php';
use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
/** /**
* Class managing players * Class managing players
* *
* @author kremsy & steeffeen * @author kremsy & steeffeen
*/ */
class PlayerHandler { class PlayerManager implements CallbackListener {
/** /**
* Constants * Constants
*/ */
const TABLE_PLAYERS = 'mc_players'; const TABLE_PLAYERS = 'mc_players';
const SETTING_LEAVE_JOIN_MESSAGES = 'EnableLeaveJoinMessages';
/** /**
* Private properties * Private properties
@ -22,16 +29,17 @@ class PlayerHandler {
/** /**
* Construct player handler * Construct player handler
* *
* @param ManiaControl $maniaControl * @param \ManiaControl\ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->initTables(); $this->initTables();
$this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MC_ONINIT, $this, 'onInit'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit');
$this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MP_PLAYERCONNECT, $this, 'playerConnect'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCONNECT, $this, 'playerConnect');
$this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MP_PLAYERDISCONNECT, $this, 'playerDisconnect'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERDISCONNECT, $this,
'playerDisconnect');
} }
/** /**
@ -73,17 +81,19 @@ class PlayerHandler {
* @param array $callback * @param array $callback
*/ */
public function onInit(array $callback) { public function onInit(array $callback) {
//register settings // register settings
$this->maniaControl->settingManager->initSetting($this, "Leave Join Messages",true); $this->maniaControl->settingManager->initSetting($this, "Leave Join Messages", true);
$this->maniaControl->client->query('GetPlayerList', 300, 0, 2); $this->maniaControl->client->query('GetPlayerList', 300, 0, 2);
$playerList = $this->maniaControl->client->getResponse(); $playerList = $this->maniaControl->client->getResponse();
foreach ($playerList as $player) { foreach ($playerList as $playerItem) {
if ($player['PlayerId'] <= 0) { if ($playerItem['PlayerId'] <= 0) {
continue; continue;
} }
$callback = array(Callbacks::CB_MP_PLAYERCONNECT, array($player['Login'])); $this->maniaControl->client->query('GetDetailedPlayerInfo', $playerItem['Login']);
$this->playerConnect($callback); $playerInfo = $this->maniaControl->client->getResponse();
$player = new Player($playerInfo);
$this->addPlayer($player);
} }
} }
@ -99,13 +109,17 @@ class PlayerHandler {
$player = new Player($playerInfo); $player = new Player($playerInfo);
$this->addPlayer($player); $this->addPlayer($player);
if($this->maniaControl->settingManager->getSetting($this,"Leave Join Messages")){ if (!$this->maniaControl->settingManager->getSetting($this, self::SETTING_LEAVE_JOIN_MESSAGES)) {
$string = array(0 => 'New Player', 1 => '$0f0Operator', 2 => '$0f0Admin', 3 => '$0f0MasterAdmin', 4 => '$0f0MasterAdmin'); return;
$this->maniaControl->chat->sendChat('$ff0'.$string[$player->authLevel].': '. $player->nickname . '$z $ff0Nation:$fff ' . $player->getCountry() . ' $ff0Ladder: $fff' . $player->ladderRank);
} }
//TODO: remove $w, $l and stuff out of nick $string = array(0 => 'New Player', 1 => '$0f0Operator', 2 => '$0f0Admin', 3 => '$0f0MasterAdmin', 4 => '$0f0MasterAdmin');
//TODO: postfix playerConnect callBack as soon as needed $this->maniaControl->chat->sendChat(
//Todo: Better style colours of the message or anything else '$ff0' . $string[$player->authLevel] . ': ' . $player->nickname . '$z $ff0Nation:$fff ' . $player->getCountry() .
' $ff0Ladder: $fff' . $player->ladderRank);
// TODO: remove $w, $l and stuff out of nick
// TODO: postfix playerConnect callBack as soon as needed
// TODO: Better style colours of the message or anything else
} }
/** /**
@ -117,7 +131,7 @@ class PlayerHandler {
$login = $callback[1][0]; $login = $callback[1][0];
$player = $this->removePlayer($login); $player = $this->removePlayer($login);
if($this->maniaControl->settingManager->getSetting($this,"Leave Join Messages")){ if ($this->maniaControl->settingManager->getSetting($this, "Leave Join Messages")) {
$played = TimeFormatter::formatTime(time() - $player->joinTime); $played = TimeFormatter::formatTime(time() - $player->joinTime);
$this->maniaControl->chat->sendChat($player->nickname . '$z $ff0has left the game. Played:$fff ' . $played); $this->maniaControl->chat->sendChat($player->nickname . '$z $ff0has left the game. Played:$fff ' . $played);
} }

View File

@ -1,27 +1,29 @@
<?php <?php
namespace ManiaControl; namespace ManiaControl\Plugins;
use ManiaControl\ManiaControl;
/** /**
* Plugin parent class * Plugin parent class
* *
* @author Lukas Kremsmayr and steeffeen * @author steeffeen & kremsy
*/ */
abstract class Plugin { abstract class Plugin {
/** /**
* Private properties * Private properties
*/ */
protected $maniaControl; protected $maniaControl = null;
protected $name; protected $name = 'undefined';
protected $version; protected $version = 'undefined';
protected $author; protected $author = 'undefined';
protected $description; protected $description = 'undefined';
/** /**
* Create plugin instance * Create a new plugin
* *
* @param ManiaControl $maniaControl * @param \ManiaControl\ManiaControl $maniaControl
*/ */
public abstract function __construct(ManiaControl $maniaControl); public abstract function __construct(ManiaControl $maniaControl);

View File

@ -1,13 +1,17 @@
<?php <?php
namespace ManiaControl; namespace ManiaControl\Plugins;
require_once __DIR__ . '/Plugin.php';
use ManiaControl\ManiaControl;
/** /**
* Class handling plugins * Class managing plugins
* *
* @author Lukas Kremsmayr and steeffeen * @author steeffeen & kremsy
*/ */
class PluginHandler { class PluginManager {
/** /**
* Constants * Constants
*/ */
@ -23,7 +27,7 @@ class PluginHandler {
/** /**
* Construct plugin handler * Construct plugin handler
* *
* @param ManiaControl $maniaControl * @param \ManiaControl\ManiaControl $maniaControl
*/ */
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;

View File

@ -0,0 +1,43 @@
<?php
namespace ManiaControl\Settings;
use ManiaControl\ManiaControl;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
/**
* Ingame setting configurator class
*
* @author kremsy & steeffeen
*/
class SettingConfigurator implements CallbackListener {
/**
* Private properties
*/
private $maniaControl = null;
/**
* Construct setting configurator
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'onInit');
$this->maniaControl->manialinkIdHandler->reserveManiaLinkIds(100);
}
/**
* Handle OnInit callback
*
* @param array $callback
*/
public function onInit(array $callback) {
// TODO: handle callback
// $this->maniaControl->manialinkUtil->
// $this->maniaControl->chat->sendChat("test");
}
}

View File

@ -2,6 +2,10 @@
namespace ManiaControl; namespace ManiaControl;
require_once __DIR__ . '/SettingConfigurator.php';
use ManiaControl\Settings\SettingConfigurator;
/** /**
* Class managing settings and configurations * Class managing settings and configurations
* *
@ -22,6 +26,7 @@ class SettingManager {
* Private properties * Private properties
*/ */
private $maniaControl = null; private $maniaControl = null;
private $configurator = null;
private $arrayDelimiter = ';;'; private $arrayDelimiter = ';;';
/** /**
@ -33,6 +38,8 @@ class SettingManager {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->initTables(); $this->initTables();
$this->configurator = new SettingConfigurator($maniaControl);
} }
/** /**
@ -187,8 +194,8 @@ class SettingManager {
@value := ?, @value := ?,
@value @value
) ON DUPLICATE KEY UPDATE ) ON DUPLICATE KEY UPDATE
`type` = VALUE(`type`), `type` = VALUES(`type`),
`default` = VALUE(`default`);"; `default` = VALUES(`default`);";
$settingStatement = $mysqli->prepare($settingQuery); $settingStatement = $mysqli->prepare($settingQuery);
if ($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);

View File

@ -2,6 +2,8 @@
namespace ManiaControl; namespace ManiaControl;
use ManiaControl\Players\Player;
/** /**
* Class handling authentication levels * Class handling authentication levels
* *
@ -42,7 +44,7 @@ class Authentication {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
// Remove all XSuperadmins // Remove all XSuperadmins
$adminQuery = "UPDATE `" . PlayerHandler::TABLE_PLAYERS . "` $adminQuery = "UPDATE `" . Players\PlayerManager::TABLE_PLAYERS . "`
SET `authLevel` = ? SET `authLevel` = ?
WHERE `authLevel` = ?;"; WHERE `authLevel` = ?;";
$adminStatement = $mysqli->prepare($adminQuery); $adminStatement = $mysqli->prepare($adminQuery);
@ -61,7 +63,7 @@ class Authentication {
// Set XSuperAdmins // Set XSuperAdmins
$xAdmins = $config->xsuperadmins->xpath('login'); $xAdmins = $config->xsuperadmins->xpath('login');
$adminQuery = "INSERT INTO `" . PlayerHandler::TABLE_PLAYERS . "` ( $adminQuery = "INSERT INTO `" . Players\PlayerManager::TABLE_PLAYERS . "` (
`login`, `login`,
`authLevel` `authLevel`
) VALUES ( ) VALUES (
@ -138,7 +140,7 @@ class Authentication {
/** /**
* Check if the player has enough rights * Check if the player has enough rights
* *
* @param Player $login * @param \ManiaControl\Players\Player $login
* @param int $neededAuthLevel * @param int $neededAuthLevel
* @return bool * @return bool
*/ */

View File

@ -2,31 +2,30 @@
namespace ManiaControl; namespace ManiaControl;
/** use ManiaControl\Players\PlayerManager;
* Needed includes use ManiaControl\Callbacks\CallbackManager;
* use ManiaControl\Commands\CommandManager;
* @author steeffeen & kremsy use ManiaControl\Manialinks\ManialinkIdHandler;
*/ use ManiaControl\Maps\MapManager;
require_once __DIR__ . '/authentication.php'; use ManiaControl\Plugins\PluginManager;
require_once __DIR__ . '/callbacks.php';
require_once __DIR__ . '/chat.php'; require_once __DIR__ . '/Authentication.php';
require_once __DIR__ . '/commands.php'; require_once __DIR__ . '/Callbacks/CallbackManager.php';
require_once __DIR__ . '/database.php'; require_once __DIR__ . '/Chat.php';
require_once __DIR__ . '/fileUtil.php'; require_once __DIR__ . '/Commands/CommandManager.php';
require_once __DIR__ . '/manialinkIdHandler.php'; require_once __DIR__ . '/Database.php';
require_once __DIR__ . '/player.php'; require_once __DIR__ . '/FileUtil.php';
require_once __DIR__ . '/playerHandler.php'; require_once __DIR__ . '/Manialinks/ManialinkIdHandler.php';
require_once __DIR__ . '/plugin.php'; require_once __DIR__ . '/Manialinks/ManialinkUtil.php';
require_once __DIR__ . '/pluginHandler.php'; require_once __DIR__ . '/Maps/Map.php';
require_once __DIR__ . '/server.php'; require_once __DIR__ . '/Maps/MapManager.php';
require_once __DIR__ . '/settingManager.php'; require_once __DIR__ . '/Players/PlayerManager.php';
require_once __DIR__ . '/settingConfigurator.php'; require_once __DIR__ . '/Plugins/PluginManager.php';
require_once __DIR__ . '/map.php'; require_once __DIR__ . '/Server.php';
require_once __DIR__ . '/mapHandler.php'; require_once __DIR__ . '/Settings/SettingManager.php';
require_once __DIR__ . '/GbxDataFetcher/gbxdatafetcher.inc.php'; 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';
list($endiantest) = array_values(unpack('L1L', pack('V', 1))); list($endiantest) = array_values(unpack('L1L', pack('V', 1)));
if ($endiantest == 1) { if ($endiantest == 1) {
require_once __DIR__ . '/PhpRemote/GbxRemote.inc.php'; require_once __DIR__ . '/PhpRemote/GbxRemote.inc.php';
@ -38,7 +37,7 @@ else {
/** /**
* ManiaControl Server Controller for ManiaPlanet Server * ManiaControl Server Controller for ManiaPlanet Server
* *
* @author steeffeen and Lukas * @author steeffeen & kremsy
*/ */
class ManiaControl { class ManiaControl {
/** /**
@ -52,17 +51,19 @@ class ManiaControl {
* Public properties * Public properties
*/ */
public $authentication = null; public $authentication = null;
public $callbacks = null; public $callbackManager = null;
public $chat = null; public $chat = null;
public $client = null; public $client = null;
public $commands = null; public $commandManager = null;
public $database = null; public $database = null;
public $manialinkIdHandler = null; public $manialinkIdHandler = null;
public $playerHandler = null; public $mapManager = null;
public $pluginHandler = null; public $playerManager = null;
public $pluginManager = null;
public $server = null; public $server = null;
public $settingConfigurator = null; public $settingConfigurator = null;
public $settingManager = null; public $settingManager = null;
/** /**
* Private properties * Private properties
*/ */
@ -73,17 +74,16 @@ class ManiaControl {
*/ */
public function __construct() { public function __construct() {
$this->database = new Database($this); $this->database = new Database($this);
$this->callbackManager = new CallbackManager($this);
$this->manialinkIdHandler = new ManialinkIdHandler();
$this->settingManager = new SettingManager($this); $this->settingManager = new SettingManager($this);
$this->chat = new Chat($this); $this->chat = new Chat($this);
$this->callbacks = new Callbacks($this);
$this->server = new Server($this); $this->server = new Server($this);
$this->authentication = new Authentication($this); $this->authentication = new Authentication($this);
$this->playerHandler = new PlayerHandler($this); $this->playerManager = new PlayerManager($this);
$this->mapHandler = new MapHandler($this); $this->mapManager = new MapManager($this);
$this->manialinkIdHandler = new ManialinkIdHandler(); $this->commandManager = new CommandManager($this);
$this->commands = new Commands($this); $this->pluginManager = new PluginManager($this);
$this->pluginHandler = new PluginHandler($this);
$this->settingConfigurator = new SettingConfigurator($this);
} }
/** /**
@ -134,7 +134,7 @@ class ManiaControl {
error_log('Starting ManiaControl v' . self::VERSION . '!'); error_log('Starting ManiaControl v' . self::VERSION . '!');
// Load plugins // Load plugins
$this->pluginHandler->loadPlugins(); $this->pluginManager->loadPlugins();
// Connect to server // Connect to server
$this->connect(); $this->connect();
@ -146,7 +146,7 @@ class ManiaControl {
$this->chat->sendInformation('ManiaControl v' . self::VERSION . ' successfully started!'); $this->chat->sendInformation('ManiaControl v' . self::VERSION . ' successfully started!');
// OnInit // OnInit
$this->callbacks->onInit(); $this->callbackManager->triggerCallback(CallbackManager::CB_MC_ONINIT, array(CallbackManager::CB_MC_ONINIT));
// Main loop // Main loop
while (!$this->shutdownRequested) { while (!$this->shutdownRequested) {
@ -155,8 +155,8 @@ class ManiaControl {
// Disable script timeout // Disable script timeout
set_time_limit(30); set_time_limit(30);
// Handle server callbacks // Manager callbacks
$this->callbacks->handleCallbacks(); $this->callbackManager->manageCallbacks();
// Yield for next tick // Yield for next tick
$loopEnd = microtime(true); $loopEnd = microtime(true);

View File

@ -1,79 +0,0 @@
<?php
namespace ManiaControl;
/**
* Map Object
*
* @author kremsy & steeffeen
*/
class Map {
/**
* Private properties
*/
private $maniaControl = 0;
/**
* Public properties
*/
public $mapFetcher = null;
public $id = 0;
public $name = '';
public $uid = 0;
public $fileName = '';
public $environment = '';
public $goldTime; //format?
public $copperPrice = 0;
public $mapType = '';
public $mapStyle = '';
public $mx = null;
public $authorLogin = '';
public $authorNick = '';
public $authorZone = '';
public $authorEInfo = '';
public $comment = '';
public $titleUid = '';
public $starttime = 0;
// instantiates the map with an RPC response
public function __construct(ManiaControl $maniaControl, $rpc_infos = null) {
$this->maniaControl = $maniaControl;
if ($rpc_infos) {
$this->name = $rpc_infos['Name']; //in aseco stripped new lines on name
$this->uid = $rpc_infos['UId'];
$this->fileName = $rpc_infos['FileName'];
$this->authorLogin = $rpc_infos['Author'];
$this->environment = $rpc_infos['Environnement'];
$this->goldTime = $rpc_infos['GoldTime'];
$this->copperPrice = $rpc_infos['CopperPrice'];
$this->mapType = $rpc_infos['MapType'];
$this->mapStyle = $rpc_infos['MapStyle'];
$this->mapFetcher = new \GBXChallMapFetcher(true);
try{
$this->mapFetcher->processFile($this->maniaControl->server->getMapsDirectory() . $this->fileName);
} catch (Exception $e){
trigger_error($e->getMessage(), E_USER_WARNING);
}
$this->authorNick = $this->mapFetcher->authorNick;
$this->authorEInfo = $this->mapFetcher->authorEInfo;
$this->authorZone = $this->mapFetcher->authorZone;
$this->comment = $this->mapFetcher->comment;
//additional properties anyway in the mapfetcher object
//TODO: change to SM to gameerkennung
//TODO: define timeout if mx is down
$this->mx = new \MXInfoFetcher('SM', $this->uid, false); //SM -> change to gameerkennung
} else {
$this->name = 'undefined';
}
$this->starttime = time();
}
}

View File

@ -1,78 +0,0 @@
<?php
/**
* Handler for maps
*
* @author kremsy & steeffeen
*/
namespace ManiaControl;
class mapHandler {
/**
* Private properties
*/
private $maniaControl = null;
private $mapList = array();
/**
* Construct map handler
* @param ManiaControl $maniaControl
*/
//TODO: database init
//TODO: erasemap from server
//TODO: implement of a method which are called by xlist command and results maplists from maniaexcahnge (or extra class for it)
//TODO: admin add from maniaexchange, would handle it here
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
$this->initTables();
$this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MC_ONINIT, $this, 'onInit');
$this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MP_BEGINMAP, $this, 'beginMap');
$this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MP_ENDMAP, $this, 'endMap');
}
/**
* Initialize all necessary tables
*
* @return bool
*/
private function initTables() {
//TODO: Initialize database table
}
/**
* Handle OnInit callback
*
* @param array $callback
*/
public function onInit(){
$this->maniaControl->client->query('GetMapList', 300, 0);
$mapList = $this->maniaControl->client->getResponse();
foreach ($mapList as $map) {
$map = new Map($this->maniaControl, $map);
$this->addMap($map);
}
}
/**
* Add a map to the MapList
*
* @param Map $map
* @return bool
*/
private function addMap(Map $map) {
//TODO: ADD Maps to database
if (!$map) {
return false;
}
$this->mapList[$map->uid] = $map;
return true;
}
}

View File

@ -1,21 +0,0 @@
<?php
/**
* @author Lukas Kremsmayr and steeffeen
*/
namespace ManiaControl;
class settingConfigurator {
private $maniaControl = null;
public function __construct(ManiaControl $maniaControl){
$this->maniaControl = $maniaControl;
$this->maniaControl->callbacks->registerCallbackHandler(Callbacks::CB_MC_ONINIT, $this, 'onInit');
$this->maniaControl->manialinkIdHandler->reserveManiaLinkIds(100);
}
public function onInit(array $callback){
// $this->maniaControl->manialinkUtil->
// $this->maniaControl->chat->sendChat("test");
}
}

View File

@ -1,7 +1,12 @@
<?php <?php
use ManiaControl\ManiaControl;
use ManiaControl\Plugins\Plugin;
namespace ManiaControl; /**
* Basic test plugin
*
* @author steeffeen
*/
class TestPlugin extends Plugin { class TestPlugin extends Plugin {
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {