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;
}
$action = $actionArray[0] . "." . $actionArray[1];
$action = $actionArray[0] . '.' . $actionArray[1];
$adminLogin = $callback[1][1];
$targetLogin = $actionArray[2];

View File

@ -2,6 +2,7 @@
namespace ManiaControl\Callbacks;
use ManiaControl\Callbacks\Models\BaseCallback;
use ManiaControl\ManiaControl;
/**
@ -243,15 +244,22 @@ class CallbackManager {
/**
* 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)) {
return;
}
$params = func_get_args();
if (!($callback instanceof BaseCallback)) {
$params = array_slice($params, 1, null, true);
}
foreach ($this->callbackListenings[$callbackName] as $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;
use ManiaControl\Callbacks\Models\RecordCallback;
use ManiaControl\ManiaControl;
/**
@ -15,10 +16,10 @@ class ShootManiaCallbacks implements CallbackListener {
/*
* Constants
*/
const SCB_TIMEATTACK_ONSTART = 'TimeAttack_OnStart';
const SCB_TIMEATTACK_ONRESTART = 'TimeAttack_OnRestart';
const SCB_TIMEATTACK_ONCHECKPOINT = 'TimeAttack_OnCheckpoint';
const SCB_TIMEATTACK_ONFINISH = 'TimeAttack_OnFinish';
const CB_TIMEATTACK_ONSTART = 'TimeAttack_OnStart';
const CB_TIMEATTACK_ONRESTART = 'TimeAttack_OnRestart';
const CB_TIMEATTACK_ONCHECKPOINT = 'TimeAttack_OnCheckpoint';
const CB_TIMEATTACK_ONFINISH = 'TimeAttack_OnFinish';
/*
* Private Properties
@ -36,8 +37,6 @@ class ShootManiaCallbacks implements CallbackListener {
// Register for script callbacks
$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]);
break;
case 'LibXmlRpc_Scores':
$this->maniaControl->callbackManager->triggerCallback(Callbacks::AFKSTATUS, $data[0]);
$this->maniaControl->callbackManager->triggerCallback(Callbacks::SCORES, $data[0]);
break;
case 'LibAFK_IsAFK':
$this->triggerAfkStatus($data[0]);
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) {
$login = $callback[1][0];
$time = (int)$callback[1][1];
public function handleTimeAttackOnCheckpoint($name, array $data) {
$login = $data[0];
$player = $this->maniaControl->playerManager->getPlayer($login);
if (!$player || $time <= 0) {
if (!$player) {
return;
}
// Trigger trackmania player checkpoint callback
$checkpointCallback = array($player->pid, $player->login, $time, 0, 0);
$this->maniaControl->callbackManager->triggerCallback(CallbackManager::CB_TM_PLAYERCHECKPOINT, array(CallbackManager::CB_TM_PLAYERCHECKPOINT, $checkpointCallback));
// Trigger checkpoint callback
$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) {
$login = $callback[1][0];
$time = (int)$callback[1][1];
public function handleTimeAttackOnFinish($name, array $data) {
$login = $data[0];
$player = $this->maniaControl->playerManager->getPlayer($login);
if (!$player || $time <= 0) {
if (!$player) {
return;
}
// Trigger trackmania player finish callback
$finishCallback = array($player->pid, $player->login, $time);
$this->maniaControl->callbackManager->triggerCallback(CallbackManager::CB_TM_PLAYERFINISH, array(CallbackManager::CB_TM_PLAYERFINISH, $finishCallback));
// Trigger finish callback
$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\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\Models\RecordCallback;
use ManiaControl\Commands\CommandListener;
use ManiaControl\ManiaControl;
use ManiaControl\Players\Player;
@ -27,7 +27,7 @@ class ObstaclePlugin implements CallbackListener, CommandListener, Plugin {
const CB_JUMPTO = 'Obstacle.JumpTo';
const SCB_ONFINISH = 'OnFinish';
const SCB_ONCHECKPOINT = 'OnCheckpoint';
const SETTING_JUMPTOAUTHLEVEL = 'Authentication level for JumpTo commands';
const SETTING_JUMPTO_AUTHLEVEL = 'Authentication level for JumpTo commands';
/**
* Private Properties
@ -83,10 +83,10 @@ class ObstaclePlugin implements CallbackListener, CommandListener, Plugin {
$this->maniaControl = $maniaControl;
// 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
$this->maniaControl->commandManager->registerCommandListener('jumpto', $this, 'command_JumpTo');
$this->maniaControl->commandManager->registerCommandListener('jumpto', $this, 'command_JumpTo', true);
// Register for callbacks
$this->maniaControl->callbackManager->registerScriptCallbackListener(self::SCB_ONFINISH, $this, 'callback_OnFinish');
@ -109,7 +109,7 @@ class ObstaclePlugin implements CallbackListener, CommandListener, Plugin {
* @return bool
*/
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)) {
$this->maniaControl->authenticationManager->sendNotAllowed($player);
return;
@ -140,10 +140,15 @@ class ObstaclePlugin implements CallbackListener, CommandListener, Plugin {
if (!$player) {
return;
}
$time = $data->Run->Time;
// Trigger trackmania player finish callback
$finishCallback = array($player->pid, $player->login, $time);
$this->maniaControl->callbackManager->triggerCallback(CallbackManager::CB_TM_PLAYERFINISH, array(CallbackManager::CB_TM_PLAYERFINISH, $finishCallback));
// Trigger finish callback
$finishCallback = new RecordCallback();
$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) {
$data = json_decode($callback[1]);
$player = $this->maniaControl->playerManager->getPlayer($data->Player->Login);
$time = $data->Run->Time;
if (!$player || $time <= 0) {
if (!$player) {
return;
}
// Trigger Trackmania player checkpoint callback
$checkpointCallback = array($player->pid, $player->login, $time, 0, 0);
$this->maniaControl->callbackManager->triggerCallback(CallbackManager::CB_TM_PLAYERCHECKPOINT, array(CallbackManager::CB_TM_PLAYERCHECKPOINT, $checkpointCallback));
// Trigger checkpoint callback
$checkpointCallback = new RecordCallback();
$checkpointCallback->rawCallback = $callback;
$checkpointCallback->name = $checkpointCallback::CHECKPOINT;
$checkpointCallback->setPlayer($player);
$checkpointCallback->time = $data->Run->Time;
$this->maniaControl->callbackManager->triggerCallback($checkpointCallback);
}
}