introductoin of callback models

added record callback model
This commit is contained in:
Steffen Schröder 2014-05-24 17:22:48 +02:00
parent dd490b02a9
commit 5faa16d5be
6 changed files with 143 additions and 50 deletions

View File

@ -201,7 +201,7 @@ class AdminLists implements ManialinkPageAnswerListener, CallbackListener {
return; return;
} }
$action = $actionArray[0] . "." . $actionArray[1]; $action = $actionArray[0] . '.' . $actionArray[1];
$adminLogin = $callback[1][1]; $adminLogin = $callback[1][1];
$targetLogin = $actionArray[2]; $targetLogin = $actionArray[2];

View File

@ -2,6 +2,7 @@
namespace ManiaControl\Callbacks; namespace ManiaControl\Callbacks;
use ManiaControl\Callbacks\Models\BaseCallback;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
/** /**
@ -243,15 +244,22 @@ class CallbackManager {
/** /**
* Trigger a specific Callback * Trigger a specific Callback
* *
* @param string $callbackName * @param mixed $callback
*/ */
public function triggerCallback($callbackName) { public function triggerCallback($callback) {
if ($callback instanceof BaseCallback) {
$callbackName = $callback->name;
} else {
$callbackName = $callback;
}
if (!array_key_exists($callbackName, $this->callbackListenings)) { if (!array_key_exists($callbackName, $this->callbackListenings)) {
return; return;
} }
$params = func_get_args(); $params = func_get_args();
$params = array_slice($params, 1, null, true); if (!($callback instanceof BaseCallback)) {
$params = array_slice($params, 1, null, true);
}
foreach ($this->callbackListenings[$callbackName] as $listening) { foreach ($this->callbackListenings[$callbackName] as $listening) {
/** @var Listening $listening */ /** @var Listening $listening */

View File

@ -0,0 +1,35 @@
<?php
namespace ManiaControl\Callbacks\Models;
use ManiaControl\Players\Player;
/**
* Base Model Class for Callbacks
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
abstract class BaseCallback {
/*
* Public Properties
*/
public $name = null;
public $rawCallback = null;
public $pid = null;
public $login = null;
public $player = null;
/**
* Set the corresponding Player
*
* @param Player $player
*/
public function setPlayer(Player $player) {
$this->pid = $player->pid;
$this->login = $player->login;
$this->player = $player;
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace ManiaControl\Callbacks\Models;
/**
* Base Model Class for Callbacks
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class RecordCallback extends BaseCallback {
/*
* Constants
*/
const CHECKPOINT = 'RecordCallback.Checkpoint';
const FINISH = 'RecordCallback.Finish';
/*
* Public Properties
*/
public $time = null;
}

View File

@ -2,6 +2,7 @@
namespace ManiaControl\Callbacks; namespace ManiaControl\Callbacks;
use ManiaControl\Callbacks\Models\RecordCallback;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
/** /**
@ -15,10 +16,10 @@ class ShootManiaCallbacks implements CallbackListener {
/* /*
* Constants * Constants
*/ */
const SCB_TIMEATTACK_ONSTART = 'TimeAttack_OnStart'; const CB_TIMEATTACK_ONSTART = 'TimeAttack_OnStart';
const SCB_TIMEATTACK_ONRESTART = 'TimeAttack_OnRestart'; const CB_TIMEATTACK_ONRESTART = 'TimeAttack_OnRestart';
const SCB_TIMEATTACK_ONCHECKPOINT = 'TimeAttack_OnCheckpoint'; const CB_TIMEATTACK_ONCHECKPOINT = 'TimeAttack_OnCheckpoint';
const SCB_TIMEATTACK_ONFINISH = 'TimeAttack_OnFinish'; const CB_TIMEATTACK_ONFINISH = 'TimeAttack_OnFinish';
/* /*
* Private Properties * Private Properties
@ -36,8 +37,6 @@ class ShootManiaCallbacks implements CallbackListener {
// Register for script callbacks // Register for script callbacks
$callbackManager->registerCallbackListener(Callbacks::SCRIPTCALLBACK, $this, 'handleScriptCallbacks'); $callbackManager->registerCallbackListener(Callbacks::SCRIPTCALLBACK, $this, 'handleScriptCallbacks');
$callbackManager->registerScriptCallbackListener(self::SCB_TIMEATTACK_ONCHECKPOINT, $this, 'callback_TimeAttack_OnCheckpoint');
$callbackManager->registerScriptCallbackListener(self::SCB_TIMEATTACK_ONFINISH, $this, 'callback_TimeAttack_OnFinish');
} }
/** /**
@ -52,11 +51,17 @@ class ShootManiaCallbacks implements CallbackListener {
$this->maniaControl->server->rankingManager->updateRankings($data[0]); $this->maniaControl->server->rankingManager->updateRankings($data[0]);
break; break;
case 'LibXmlRpc_Scores': case 'LibXmlRpc_Scores':
$this->maniaControl->callbackManager->triggerCallback(Callbacks::AFKSTATUS, $data[0]); $this->maniaControl->callbackManager->triggerCallback(Callbacks::SCORES, $data[0]);
break; break;
case 'LibAFK_IsAFK': case 'LibAFK_IsAFK':
$this->triggerAfkStatus($data[0]); $this->triggerAfkStatus($data[0]);
break; break;
case self::CB_TIMEATTACK_ONCHECKPOINT:
$this->handleTimeAttackOnCheckpoint($name, $data);
break;
case self::CB_TIMEATTACK_ONFINISH:
$this->handleTimeAttackOnFinish($name, $data);
break;
} }
} }
@ -71,36 +76,48 @@ class ShootManiaCallbacks implements CallbackListener {
} }
/** /**
* Handle TimeAttack OnCheckpoint Script Callback * Handle TimeAttack OnCheckpoint Callback
* *
* @param array $callback * @param string $name
* @param array $data
*/ */
public function callback_TimeAttack_OnCheckpoint(array $callback) { public function handleTimeAttackOnCheckpoint($name, array $data) {
$login = $callback[1][0]; $login = $data[0];
$time = (int)$callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
if (!$player || $time <= 0) { if (!$player) {
return; return;
} }
// Trigger trackmania player checkpoint callback
$checkpointCallback = array($player->pid, $player->login, $time, 0, 0); // Trigger checkpoint callback
$this->maniaControl->callbackManager->triggerCallback(CallbackManager::CB_TM_PLAYERCHECKPOINT, array(CallbackManager::CB_TM_PLAYERCHECKPOINT, $checkpointCallback)); $checkpointCallback = new RecordCallback();
$checkpointCallback->rawCallback = array($name, $data);
$checkpointCallback->name = $checkpointCallback::CHECKPOINT;
$checkpointCallback->setPlayer($player);
$checkpointCallback->time = (int)$data[1];
$this->maniaControl->callbackManager->triggerCallback($checkpointCallback);
} }
/** /**
* Handle TimeAttack OnFinish Script Callback * Handle TimeAttack OnFinish Callback
* *
* @param array $callback * @param string $name
* @param array $data
*/ */
public function callback_TimeAttack_OnFinish(array $callback) { public function handleTimeAttackOnFinish($name, array $data) {
$login = $callback[1][0]; $login = $data[0];
$time = (int)$callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
if (!$player || $time <= 0) { if (!$player) {
return; return;
} }
// Trigger trackmania player finish callback
$finishCallback = array($player->pid, $player->login, $time); // Trigger finish callback
$this->maniaControl->callbackManager->triggerCallback(CallbackManager::CB_TM_PLAYERFINISH, array(CallbackManager::CB_TM_PLAYERFINISH, $finishCallback)); $finishCallback = new RecordCallback();
$finishCallback->rawCallback = array($name, $data);
$finishCallback->name = $finishCallback::FINISH;
$finishCallback->setPlayer($player);
$finishCallback->time = (int)$data[1];
$this->maniaControl->callbackManager->triggerCallback($finishCallback);
} }
} }

View File

@ -4,7 +4,7 @@ namespace steeffeen;
use ManiaControl\Admin\AuthenticationManager; use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\Models\RecordCallback;
use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandListener;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Players\Player; use ManiaControl\Players\Player;
@ -20,14 +20,14 @@ class ObstaclePlugin implements CallbackListener, CommandListener, Plugin {
/* /*
* Constants * Constants
*/ */
const ID = 24; const ID = 24;
const VERSION = 0.2; const VERSION = 0.2;
const NAME = 'Obstacle Plugin'; const NAME = 'Obstacle Plugin';
const AUTHOR = 'steeffeen'; const AUTHOR = 'steeffeen';
const CB_JUMPTO = 'Obstacle.JumpTo'; const CB_JUMPTO = 'Obstacle.JumpTo';
const SCB_ONFINISH = 'OnFinish'; const SCB_ONFINISH = 'OnFinish';
const SCB_ONCHECKPOINT = 'OnCheckpoint'; const SCB_ONCHECKPOINT = 'OnCheckpoint';
const SETTING_JUMPTOAUTHLEVEL = 'Authentication level for JumpTo commands'; const SETTING_JUMPTO_AUTHLEVEL = 'Authentication level for JumpTo commands';
/** /**
* Private Properties * Private Properties
@ -83,10 +83,10 @@ class ObstaclePlugin implements CallbackListener, CommandListener, Plugin {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
// Init settings // Init settings
$this->maniaControl->settingManager->initSetting($this, self::SETTING_JUMPTOAUTHLEVEL, AuthenticationManager::AUTH_LEVEL_MODERATOR); $this->maniaControl->settingManager->initSetting($this, self::SETTING_JUMPTO_AUTHLEVEL, AuthenticationManager::AUTH_LEVEL_MODERATOR);
// Register for commands // Register for commands
$this->maniaControl->commandManager->registerCommandListener('jumpto', $this, 'command_JumpTo'); $this->maniaControl->commandManager->registerCommandListener('jumpto', $this, 'command_JumpTo', true);
// Register for callbacks // Register for callbacks
$this->maniaControl->callbackManager->registerScriptCallbackListener(self::SCB_ONFINISH, $this, 'callback_OnFinish'); $this->maniaControl->callbackManager->registerScriptCallbackListener(self::SCB_ONFINISH, $this, 'callback_OnFinish');
@ -109,7 +109,7 @@ class ObstaclePlugin implements CallbackListener, CommandListener, Plugin {
* @return bool * @return bool
*/ */
public function command_JumpTo(array $chatCallback, Player $player) { public function command_JumpTo(array $chatCallback, Player $player) {
$authLevel = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_JUMPTOAUTHLEVEL); $authLevel = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_JUMPTO_AUTHLEVEL);
if (!$this->maniaControl->authenticationManager->checkRight($player, $authLevel)) { if (!$this->maniaControl->authenticationManager->checkRight($player, $authLevel)) {
$this->maniaControl->authenticationManager->sendNotAllowed($player); $this->maniaControl->authenticationManager->sendNotAllowed($player);
return; return;
@ -140,10 +140,15 @@ class ObstaclePlugin implements CallbackListener, CommandListener, Plugin {
if (!$player) { if (!$player) {
return; return;
} }
$time = $data->Run->Time;
// Trigger trackmania player finish callback // Trigger finish callback
$finishCallback = array($player->pid, $player->login, $time); $finishCallback = new RecordCallback();
$this->maniaControl->callbackManager->triggerCallback(CallbackManager::CB_TM_PLAYERFINISH, array(CallbackManager::CB_TM_PLAYERFINISH, $finishCallback)); $finishCallback->rawCallback = $callback;
$finishCallback->name = $finishCallback::FINISH;
$finishCallback->setPlayer($player);
$finishCallback->time = $data->Run->Time;
$this->maniaControl->callbackManager->triggerCallback($finishCallback);
} }
/** /**
@ -154,12 +159,17 @@ class ObstaclePlugin implements CallbackListener, CommandListener, Plugin {
public function callback_OnCheckpoint(array $callback) { public function callback_OnCheckpoint(array $callback) {
$data = json_decode($callback[1]); $data = json_decode($callback[1]);
$player = $this->maniaControl->playerManager->getPlayer($data->Player->Login); $player = $this->maniaControl->playerManager->getPlayer($data->Player->Login);
$time = $data->Run->Time; if (!$player) {
if (!$player || $time <= 0) {
return; return;
} }
// Trigger Trackmania player checkpoint callback
$checkpointCallback = array($player->pid, $player->login, $time, 0, 0); // Trigger checkpoint callback
$this->maniaControl->callbackManager->triggerCallback(CallbackManager::CB_TM_PLAYERCHECKPOINT, array(CallbackManager::CB_TM_PLAYERCHECKPOINT, $checkpointCallback)); $checkpointCallback = new RecordCallback();
$checkpointCallback->rawCallback = $callback;
$checkpointCallback->name = $checkpointCallback::CHECKPOINT;
$checkpointCallback->setPlayer($player);
$checkpointCallback->time = $data->Run->Time;
$this->maniaControl->callbackManager->triggerCallback($checkpointCallback);
} }
} }