vote plugin, possability to react on trigger events, now built in only for map ralated votes

This commit is contained in:
kremsy 2014-07-04 11:34:17 +02:00
parent 82f639be46
commit 2709df4d7e
3 changed files with 78 additions and 10 deletions

View File

@ -158,6 +158,26 @@ class CallbackManager {
return $this->removeCallbackListener($this->callbackListenings, $listener); return $this->removeCallbackListener($this->callbackListenings, $listener);
} }
//TODO better name (used only in customvotesPlugin)
/**
* Unregister a single Callback Listening from an Callback Listener
*
* @param String $callbackName
* @param CallbackListener $listener
* @return bool
*/
public function unregisterCallbackListening($callbackName, CallbackListener $listener){
foreach($this->callbackListenings as &$listenings){
foreach ($listenings as $key => &$listening) {
if($key == $callbackName && $listening->listener === $listener){
unset($listenings[$key]);
return true;
}
}
}
return false;
}
/** /**
* Remove the Callback Listener from the given Listeners Array * Remove the Callback Listener from the given Listeners Array
* *

View File

@ -622,10 +622,9 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$message = $player->getEscapedNickname() . '$s started a vote to switch to ' . $map->getEscapedName() . '!'; $message = $player->getEscapedNickname() . '$s started a vote to switch to ' . $map->getEscapedName() . '!';
$votesPlugin->defineVote('switchmap', "Goto " . $map->name, true, $message); $votesPlugin->defineVote('switchmap', "Goto " . $map->name, true, $message)->setStopCallback(Callbacks::ENDMAP);
$votesPlugin->startVote($player, 'switchmap', function ($result) use (&$votesPlugin, &$map) { $votesPlugin->startVote($player, 'switchmap', function ($result) use (&$votesPlugin, &$map) {
// TODO: $result isn't used -> bug? does it skip even if vote is not successful?
$votesPlugin->undefineVote('switchmap'); $votesPlugin->undefineVote('switchmap');
//Don't queue on Map-Change //Don't queue on Map-Change

View File

@ -17,6 +17,7 @@ use FML\ManiaLink;
use FML\Script\Features\KeyAction; use FML\Script\Features\KeyAction;
use ManiaControl\Callbacks\CallbackListener; use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager; use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Callbacks\TimerListener; use ManiaControl\Callbacks\TimerListener;
use ManiaControl\Commands\CommandListener; use ManiaControl\Commands\CommandListener;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
@ -161,11 +162,16 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
//Define Votes //Define Votes
$this->defineVote("teambalance", "Vote for Team Balance"); $this->defineVote("teambalance", "Vote for Team Balance");
$this->defineVote("skipmap", "Vote for Skip Map"); $this->defineVote("skipmap", "Vote for Skip Map")
$this->defineVote("nextmap", "Vote for Skip Map"); ->setStopCallback(Callbacks::ENDMAP);
$this->defineVote("skip", "Vote for Skip Map"); $this->defineVote("nextmap", "Vote for Skip Map")
$this->defineVote("restartmap", "Vote for Restart Map"); ->setStopCallback(Callbacks::ENDMAP);
$this->defineVote("restart", "Vote for Restart Map"); $this->defineVote("skip", "Vote for Skip Map")
->setStopCallback(Callbacks::ENDMAP);
$this->defineVote("restartmap", "Vote for Restart Map")
->setStopCallback(Callbacks::ENDMAP);
$this->defineVote("restart", "Vote for Restart Map")
->setStopCallback(Callbacks::ENDMAP);
$this->defineVote("pausegame", "Vote for Pause Game"); $this->defineVote("pausegame", "Vote for Pause Game");
$this->defineVote("replay", "Vote to replay current map"); $this->defineVote("replay", "Vote to replay current map");
@ -194,6 +200,7 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
* @param bool $idBased * @param bool $idBased
* @param string $startText * @param string $startText
* @param float $neededRatio * @param float $neededRatio
* @return \MCTeam\VoteCommand
*/ */
public function defineVote($voteIndex, $voteName, $idBased = false, $startText = '', $neededRatio = -1) { public function defineVote($voteIndex, $voteName, $idBased = false, $startText = '', $neededRatio = -1) {
if ($neededRatio < 0) { if ($neededRatio < 0) {
@ -202,6 +209,8 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
$voteCommand = new VoteCommand($voteIndex, $voteName, $idBased, $neededRatio); $voteCommand = new VoteCommand($voteIndex, $voteName, $idBased, $neededRatio);
$voteCommand->startText = $startText; $voteCommand->startText = $startText;
$this->voteCommands[$voteIndex] = $voteCommand; $this->voteCommands[$voteIndex] = $voteCommand;
return $voteCommand;
} }
/** /**
@ -396,6 +405,11 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
$emptyManialink = new ManiaLink(self::MLID_WIDGET); $emptyManialink = new ManiaLink(self::MLID_WIDGET);
$this->maniaControl->manialinkManager->sendManialink($emptyManialink); $this->maniaControl->manialinkManager->sendManialink($emptyManialink);
//Remove the Listener for the Stop Callback if a stop callback is defined
if ($this->currentVote && $this->currentVote->stopCallback) {
$this->maniaControl->callbackManager->unregisterCallbackListening($this->currentVote->stopCallback, $this);
}
unset($this->currentVote); unset($this->currentVote);
} }
@ -428,7 +442,7 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
* *
* @param Player $player * @param Player $player
* @param int $voteIndex * @param int $voteIndex
* @param callable $function * @param callable $function (Get's only called if the vote is successfull and returns as Parameter the Voting-Results
*/ */
public function startVote(Player $player, $voteIndex, $function = null) { public function startVote(Player $player, $voteIndex, $function = null) {
//Player is muted //Player is muted
@ -457,13 +471,19 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
$maxTime = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_VOTE_TIME); $maxTime = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_VOTE_TIME);
$this->currentVote = $this->voteCommands[$voteIndex]; /** @var VoteCommand $voteCommand */
$voteCommand = $this->voteCommands[$voteIndex];
$this->currentVote = new CurrentVote($this->voteCommands[$voteIndex], $player, time() + $maxTime); $this->currentVote = new CurrentVote($voteCommand, $player, time() + $maxTime);
$this->currentVote->neededRatio = floatval($this->maniaControl->settingManager->getSettingValue($this, self::SETTING_DEFAULT_RATIO)); $this->currentVote->neededRatio = floatval($this->maniaControl->settingManager->getSettingValue($this, self::SETTING_DEFAULT_RATIO));
$this->currentVote->neededPlayerRatio = floatval($this->maniaControl->settingManager->getSettingValue($this, self::SETTING_DEFAULT_PLAYER_RATIO)); $this->currentVote->neededPlayerRatio = floatval($this->maniaControl->settingManager->getSettingValue($this, self::SETTING_DEFAULT_PLAYER_RATIO));
$this->currentVote->function = $function; $this->currentVote->function = $function;
if ($voteCommand->getStopCallback()) {
$this->maniaControl->callbackManager->registerCallbackListener($voteCommand->getStopCallback(), $this, 'handleStopCallback');
$this->currentVote->stopCallback = $voteCommand->getStopCallback();
}
if ($this->currentVote->voteCommand->startText) { if ($this->currentVote->voteCommand->startText) {
$message = $this->currentVote->voteCommand->startText; $message = $this->currentVote->voteCommand->startText;
} else { } else {
@ -473,6 +493,13 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkP
$this->maniaControl->chat->sendSuccess($message); $this->maniaControl->chat->sendSuccess($message);
} }
/**
* Destroys the Vote on the Stop Callback
*/
public function handleStopCallback() {
$this->destroyVote();
}
/** /**
* Destroy the Vote on Canceled Callback * Destroy the Vote on Canceled Callback
*/ */
@ -777,6 +804,8 @@ class VoteCommand {
public $idBased = false; public $idBased = false;
public $startText = ''; public $startText = '';
private $stopCallback = '';
/** /**
* Construct a new Vote Command * Construct a new Vote Command
* *
@ -791,6 +820,25 @@ class VoteCommand {
$this->idBased = $idBased; $this->idBased = $idBased;
$this->neededRatio = $neededRatio; $this->neededRatio = $neededRatio;
} }
/**
* Defines a Stop Callback
*
* @param $stopCallback
*/
public function setStopCallback($stopCallback) {
$this->stopCallback = $stopCallback;
}
/**
* Gets the Stop Callback
*
* @return string
*/
public function getStopCallback() {
return $this->stopCallback;
}
} }
/** /**
@ -809,6 +857,7 @@ class CurrentVote {
public $map = null; public $map = null;
public $player = null; public $player = null;
public $function = null; public $function = null;
public $stopCallback = "";
private $playersVoted = array(); private $playersVoted = array();