voteplug
This commit is contained in:
parent
630a45f41c
commit
4d699150aa
@ -5,15 +5,18 @@ namespace ManiaControl\Plugins;
|
|||||||
use FML\Controls\Control;
|
use FML\Controls\Control;
|
||||||
use FML\Controls\Frame;
|
use FML\Controls\Frame;
|
||||||
use FML\Controls\Gauge;
|
use FML\Controls\Gauge;
|
||||||
|
use FML\Controls\Label;
|
||||||
|
use FML\Controls\Labels\Label_Button;
|
||||||
use FML\Controls\Labels\Label_Text;
|
use FML\Controls\Labels\Label_Text;
|
||||||
use FML\Controls\Quad;
|
use FML\Controls\Quad;
|
||||||
|
use FML\Controls\Quads\Quad_BgsPlayerCard;
|
||||||
use FML\ManiaLink;
|
use FML\ManiaLink;
|
||||||
use FML\Script\Script;
|
|
||||||
use ManiaControl\Callbacks\CallbackListener;
|
use ManiaControl\Callbacks\CallbackListener;
|
||||||
use ManiaControl\Callbacks\CallbackManager;
|
use ManiaControl\Callbacks\CallbackManager;
|
||||||
use ManiaControl\ColorUtil;
|
use ManiaControl\ColorUtil;
|
||||||
use ManiaControl\Commands\CommandListener;
|
use ManiaControl\Commands\CommandListener;
|
||||||
use ManiaControl\ManiaControl;
|
use ManiaControl\ManiaControl;
|
||||||
|
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
|
||||||
use ManiaControl\Players\Player;
|
use ManiaControl\Players\Player;
|
||||||
|
|
||||||
|
|
||||||
@ -22,7 +25,7 @@ use ManiaControl\Players\Player;
|
|||||||
*
|
*
|
||||||
* @author kremsy and steeffeen
|
* @author kremsy and steeffeen
|
||||||
*/
|
*/
|
||||||
class CustomVotesPlugin implements CommandListener, CallbackListener, Plugin {
|
class CustomVotesPlugin implements CommandListener, CallbackListener, ManialinkPageAnswerListener, Plugin {
|
||||||
/**
|
/**
|
||||||
* Constants
|
* Constants
|
||||||
*/
|
*/
|
||||||
@ -33,9 +36,12 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, Plugin {
|
|||||||
|
|
||||||
const MLID_WIDGET = 'CustomVotesPlugin.WidgetId';
|
const MLID_WIDGET = 'CustomVotesPlugin.WidgetId';
|
||||||
|
|
||||||
const VOTE_FOR_ACTION = '1';
|
const VOTE_FOR_ACTION = '1';
|
||||||
const VOTE_AGAINST_ACTION = '-1';
|
const VOTE_AGAINST_ACTION = '-1';
|
||||||
|
|
||||||
|
const ACTION_POSITIVE_VOTE = 'CustomVotesPlugin.PositivVote';
|
||||||
|
const ACTION_NEGATIVE_VOTE = 'CustomVotesPlugin.NegativeVote';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private properties
|
* Private properties
|
||||||
*/
|
*/
|
||||||
@ -49,6 +55,7 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, Plugin {
|
|||||||
private $currentVote = '';
|
private $currentVote = '';
|
||||||
private $currentVoteExpireTime = 0;
|
private $currentVoteExpireTime = 0;
|
||||||
private $playersVoted = array();
|
private $playersVoted = array();
|
||||||
|
private $playersVotedPositiv = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load the plugin
|
* Load the plugin
|
||||||
@ -63,7 +70,8 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, Plugin {
|
|||||||
|
|
||||||
$this->maniaControl->commandManager->registerCommandListener('vote', $this, 'chat_vote');
|
$this->maniaControl->commandManager->registerCommandListener('vote', $this, 'chat_vote');
|
||||||
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_1_SECOND, $this, 'handle1Second');
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_1_SECOND, $this, 'handle1Second');
|
||||||
|
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_POSITIVE_VOTE, $this, 'handlePositiveVote');
|
||||||
|
$this->maniaControl->manialinkManager->registerManialinkPageAnswerListener(self::ACTION_NEGATIVE_VOTE, $this, 'handleNegativeVote');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,13 +91,13 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, Plugin {
|
|||||||
*/
|
*/
|
||||||
public function chat_vote(array $chat, Player $player) {
|
public function chat_vote(array $chat, Player $player) {
|
||||||
$command = explode(" ", $chat[1][2]);
|
$command = explode(" ", $chat[1][2]);
|
||||||
var_dump($command);
|
|
||||||
if(isset($command[1])) {
|
if(isset($command[1])) {
|
||||||
|
|
||||||
$this->startVote($player, strtolower($command[1]));
|
$this->startVote($player, strtolower($command[1]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines a Vote
|
* Defines a Vote
|
||||||
*
|
*
|
||||||
@ -111,9 +119,33 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, Plugin {
|
|||||||
}
|
}
|
||||||
$this->maniaControl->chat->sendChat("Vote started");
|
$this->maniaControl->chat->sendChat("Vote started");
|
||||||
$this->currentVote = $voteName;
|
$this->currentVote = $voteName;
|
||||||
$this->currentVoteExpireTime = time() + 20; //TODO as setting
|
$this->currentVoteExpireTime = time() + 60; //TODO as setting
|
||||||
|
|
||||||
$this->playersVoted[$player->login] = self::VOTE_FOR_ACTION;
|
$this->playersVoted[$player->login] = self::VOTE_FOR_ACTION;
|
||||||
|
$this->playersVotedPositiv++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handlePositiveVote(array $callback, Player $player) {
|
||||||
|
if(isset($this->playersVoted[$player->login])){
|
||||||
|
if($this->playersVoted[$player->login] == self::VOTE_AGAINST_ACTION){
|
||||||
|
$this->playersVoted[$player->login] = self::VOTE_FOR_ACTION;
|
||||||
|
$this->playersVotedPositiv++;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
$this->playersVoted[$player->login] = self::VOTE_FOR_ACTION;
|
||||||
|
$this->playersVotedPositiv++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleNegativeVote(array $callback, Player $player) {
|
||||||
|
if(isset($this->playersVoted[$player->login])){
|
||||||
|
if($this->playersVoted[$player->login] == self::VOTE_FOR_ACTION){
|
||||||
|
$this->playersVoted[$player->login] = self::VOTE_AGAINST_ACTION;
|
||||||
|
$this->playersVotedPositiv--;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
$this->playersVoted[$player->login] = self::VOTE_AGAINST_ACTION;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -126,8 +158,10 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, Plugin {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$votePercentage = $this->playersVotedPositiv / count($this->playersVoted);
|
||||||
|
|
||||||
$timeUntilExpire = $this->currentVoteExpireTime - time();
|
$timeUntilExpire = $this->currentVoteExpireTime - time();
|
||||||
$this->showVoteWidget($timeUntilExpire);
|
$this->showVoteWidget($timeUntilExpire, $votePercentage);
|
||||||
|
|
||||||
if($timeUntilExpire <= 0) {
|
if($timeUntilExpire <= 0) {
|
||||||
$this->maniaControl->chat->sendChat("Vote finished");
|
$this->maniaControl->chat->sendChat("Vote finished");
|
||||||
@ -139,8 +173,7 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, Plugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function showVoteWidget($timeUntilExpire, $votePercentage) {
|
||||||
private function showVoteWidget($timeUntilExpire) {
|
|
||||||
//$pos_x = $this->maniaControl->settingManager->getSetting($this, self::SETTING_MAP_WIDGET_POSX);
|
//$pos_x = $this->maniaControl->settingManager->getSetting($this, self::SETTING_MAP_WIDGET_POSX);
|
||||||
//$pos_y = $this->maniaControl->settingManager->getSetting($this, self::SETTING_MAP_WIDGET_POSY);
|
//$pos_y = $this->maniaControl->settingManager->getSetting($this, self::SETTING_MAP_WIDGET_POSY);
|
||||||
//$width = $this->maniaControl->settingManager->getSetting($this, self::SETTING_MAP_WIDGET_WIDTH);
|
//$width = $this->maniaControl->settingManager->getSetting($this, self::SETTING_MAP_WIDGET_WIDTH);
|
||||||
@ -155,8 +188,8 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, Plugin {
|
|||||||
$labelStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultLabelStyle();
|
$labelStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultLabelStyle();
|
||||||
|
|
||||||
$maniaLink = new ManiaLink(self::MLID_WIDGET);
|
$maniaLink = new ManiaLink(self::MLID_WIDGET);
|
||||||
$script = new Script();
|
//$script = new Script();
|
||||||
$maniaLink->setScript($script);
|
//$maniaLink->setScript($script);
|
||||||
|
|
||||||
// mainframe
|
// mainframe
|
||||||
$frame = new Frame();
|
$frame = new Frame();
|
||||||
@ -169,7 +202,6 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, Plugin {
|
|||||||
$frame->add($backgroundQuad);
|
$frame->add($backgroundQuad);
|
||||||
$backgroundQuad->setSize($width, $height);
|
$backgroundQuad->setSize($width, $height);
|
||||||
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
|
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
|
||||||
$script->addMapInfoButton($backgroundQuad);
|
|
||||||
|
|
||||||
$label = new Label_Text();
|
$label = new Label_Text();
|
||||||
$frame->add($label);
|
$frame->add($label);
|
||||||
@ -182,7 +214,7 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, Plugin {
|
|||||||
|
|
||||||
$label = new Label_Text();
|
$label = new Label_Text();
|
||||||
$frame->add($label);
|
$frame->add($label);
|
||||||
$label->setY($height / 2 - 8);
|
$label->setY($height / 2 - 7);
|
||||||
$label->setAlign(Control::CENTER, Control::CENTER);
|
$label->setAlign(Control::CENTER, Control::CENTER);
|
||||||
$label->setSize($width - 5, $height);
|
$label->setSize($width - 5, $height);
|
||||||
$label->setTextSize(1.3);
|
$label->setTextSize(1.3);
|
||||||
@ -198,42 +230,68 @@ class CustomVotesPlugin implements CommandListener, CallbackListener, Plugin {
|
|||||||
$label->setText("Time left: " . $timeUntilExpire . "s");
|
$label->setText("Time left: " . $timeUntilExpire . "s");
|
||||||
$label->setTextColor("FFF");
|
$label->setTextColor("FFF");
|
||||||
|
|
||||||
$voteGauge = new Gauge();
|
|
||||||
$frame->add($voteGauge);
|
|
||||||
$voteGauge->setSize($width * 0.95, 6);
|
|
||||||
$voteGauge->setDrawBg(false);
|
|
||||||
$votePerc = 0.5;
|
|
||||||
$voteGauge->setRatio($votePerc + 0.15 - $votePerc * 0.15);
|
|
||||||
$gaugeColor = ColorUtil::floatToStatusColor($votePerc);
|
|
||||||
$voteGauge->setColor($gaugeColor . '9');
|
|
||||||
|
|
||||||
|
//Time Gaunge
|
||||||
|
$timeGauge = new Gauge();
|
||||||
|
$frame->add($timeGauge);
|
||||||
|
$timeGauge->setSize($width * 0.95, 6);
|
||||||
|
$timeGauge->setDrawBg(false);
|
||||||
|
$maxTime = 60; //TODO set maxtime
|
||||||
|
$timeGaugeRatio = (100 / $maxTime * $timeUntilExpire) / 100;
|
||||||
|
$timeGauge->setRatio($timeGaugeRatio + 0.15 - $timeGaugeRatio * 0.15);
|
||||||
|
$gaugeColor = ColorUtil::floatToStatusColor($timeGaugeRatio);
|
||||||
|
$timeGauge->setColor($gaugeColor . '9');
|
||||||
|
|
||||||
|
//Vote Gauge
|
||||||
$voteGauge = new Gauge();
|
$voteGauge = new Gauge();
|
||||||
$frame->add($voteGauge);
|
$frame->add($voteGauge);
|
||||||
$voteGauge->setY($height / 2 - 20);
|
$voteGauge->setY($height / 2 - 20);
|
||||||
$voteGauge->setSize($width * 0.7, 12);
|
$voteGauge->setSize($width * 0.65, 12);
|
||||||
$voteGauge->setDrawBg(false);
|
$voteGauge->setDrawBg(false);
|
||||||
$votePerc = 0.5;
|
$voteGauge->setRatio($votePercentage + 0.15 - $votePercentage * 0.15);
|
||||||
$voteGauge->setRatio($votePerc + 0.15 - $votePerc * 0.15);
|
$gaugeColor = ColorUtil::floatToStatusColor($votePercentage);
|
||||||
$gaugeColor = ColorUtil::floatToStatusColor($votePerc);
|
|
||||||
$voteGauge->setColor($gaugeColor . '9');
|
$voteGauge->setColor($gaugeColor . '9');
|
||||||
|
|
||||||
|
|
||||||
/*$karmaLabel = new Label();
|
$voteLabel = new Label();
|
||||||
$frame->add($karmaLabel);
|
$frame->add($voteLabel);
|
||||||
$karmaLabel->setPosition(0, -0.4, 1);
|
$voteLabel->setY($height / 2 - 20.4);
|
||||||
$karmaLabel->setSize($width * 0.9, $height * 0.9);
|
$voteLabel->setSize($width * 0.65, 12);
|
||||||
$karmaLabel->setStyle($labelStyle);
|
$voteLabel->setStyle($labelStyle);
|
||||||
$karmaLabel->setTextSize(1);*/
|
$voteLabel->setTextSize(1);
|
||||||
|
$voteLabel->setText(' ' . round($votePercentage * 100.) . '% (' . count($this->playersVoted) . ')');
|
||||||
|
|
||||||
/*
|
// Mute Player
|
||||||
$karmaLabel->setText(' ' . round($karma * 100.) . '% (' . $votes['count'] . ')');
|
$y = $height / 2 - 20.4;
|
||||||
$karma = 0.;
|
$quad = new Quad_BgsPlayerCard();
|
||||||
$karmaGauge->setRatio(0.);
|
$frame->add($quad);
|
||||||
$karmaGauge->setColor('00fb');
|
$quad->setX(-$width / 2 + 4);
|
||||||
$karmaLabel->setText('-');
|
$quad->setY($y);
|
||||||
*/
|
$quad->setSubStyle($quad::SUBSTYLE_BgPlayerCardBig);
|
||||||
|
$quad->setSize(5, 5);
|
||||||
|
$quad->setAction(self::ACTION_NEGATIVE_VOTE);
|
||||||
|
|
||||||
|
$label = new Label_Button();
|
||||||
|
$frame->add($label);
|
||||||
|
$label->setX(-$width / 2 + 4);
|
||||||
|
$label->setAlign(Control::CENTER, Control::CENTER);
|
||||||
|
$label->setY($y);
|
||||||
|
$label->setStyle($labelStyle);
|
||||||
|
$label->setTextSize(1);
|
||||||
|
$label->setSize(3,3);
|
||||||
|
$label->setTextColor("F00");
|
||||||
|
$label->setText("F1");
|
||||||
|
|
||||||
|
$quad = clone $quad;
|
||||||
|
$frame->add($quad);
|
||||||
|
$quad->setX($width / 2 - 4);
|
||||||
|
$quad->setAction(self::ACTION_POSITIVE_VOTE);
|
||||||
|
|
||||||
|
$label = clone $label;
|
||||||
|
$frame->add($label);
|
||||||
|
$label->setX($width / 2 - 4);
|
||||||
|
$label->setTextColor("0F0");
|
||||||
|
$label->setText("F2");
|
||||||
|
|
||||||
// Send manialink
|
// Send manialink
|
||||||
$manialinkText = $maniaLink->render()->saveXML();
|
$manialinkText = $maniaLink->render()->saveXML();
|
||||||
|
Loading…
Reference in New Issue
Block a user