2013-11-28 01:12:52 +01:00
|
|
|
<?php
|
2014-01-03 19:31:30 +01:00
|
|
|
use FML\Controls\Frame;
|
|
|
|
use FML\Controls\Gauge;
|
|
|
|
use FML\Controls\Label;
|
|
|
|
use FML\Controls\Quad;
|
|
|
|
use FML\ManiaLink;
|
2013-11-28 01:12:52 +01:00
|
|
|
use ManiaControl\Callbacks\CallbackListener;
|
|
|
|
use ManiaControl\Callbacks\CallbackManager;
|
2014-01-03 19:31:30 +01:00
|
|
|
use ManiaControl\ColorUtil;
|
|
|
|
use ManiaControl\ManiaControl;
|
2013-11-28 01:12:52 +01:00
|
|
|
use ManiaControl\Maps\Map;
|
|
|
|
use ManiaControl\Players\Player;
|
|
|
|
use ManiaControl\Plugins\Plugin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ManiaControl Karma Plugin
|
|
|
|
*
|
|
|
|
* @author steeffeen
|
|
|
|
*/
|
2013-12-03 22:21:17 +01:00
|
|
|
class KarmaPlugin implements CallbackListener, Plugin {
|
2013-11-28 01:12:52 +01:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
2014-01-03 19:31:30 +01:00
|
|
|
const ID = 5;
|
|
|
|
const VERSION = 0.1;
|
|
|
|
const MLID_KARMA = 'KarmaPlugin.MLID';
|
|
|
|
const TABLE_KARMA = 'mc_karma';
|
2013-11-28 01:12:52 +01:00
|
|
|
const SETTING_AVAILABLE_VOTES = 'Available Votes (X-Y: Comma separated)';
|
2014-01-03 19:31:30 +01:00
|
|
|
const SETTING_WIDGET_TITLE = 'Widget-Title';
|
|
|
|
const SETTING_WIDGET_POSX = 'Widget-Position: X';
|
|
|
|
const SETTING_WIDGET_POSY = 'Widget-Position: Y';
|
|
|
|
const SETTING_WIDGET_WIDTH = 'Widget-Size: Width';
|
|
|
|
const SETTING_WIDGET_HEIGHT = 'Widget-Size: Height';
|
|
|
|
|
|
|
|
const STAT_PLAYER_MAPVOTES = 'Voted Maps';
|
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
/**
|
|
|
|
* Private properties
|
|
|
|
*/
|
2013-12-31 11:53:10 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var maniaControl $maniaControl
|
|
|
|
*/
|
2013-12-14 23:29:17 +01:00
|
|
|
private $maniaControl = null;
|
2013-11-28 01:12:52 +01:00
|
|
|
private $updateManialink = false;
|
|
|
|
private $manialink = null;
|
|
|
|
|
|
|
|
/**
|
2013-12-31 11:53:10 +01:00
|
|
|
*
|
2013-12-14 23:29:17 +01:00
|
|
|
* @see \ManiaControl\Plugins\Plugin::load()
|
2013-11-28 01:12:52 +01:00
|
|
|
*/
|
2013-12-14 23:29:17 +01:00
|
|
|
public function load(ManiaControl $maniaControl) {
|
2013-11-28 01:12:52 +01:00
|
|
|
$this->maniaControl = $maniaControl;
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
// Init database
|
|
|
|
$this->initTables();
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
// Init settings
|
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_AVAILABLE_VOTES, '-2,2');
|
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_TITLE, 'Map-Karma');
|
2013-12-29 23:39:30 +01:00
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_POSX, 160 - 27.5);
|
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_POSY, 90 - 10 - 6);
|
2013-11-28 01:12:52 +01:00
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_WIDTH, 25.);
|
2013-12-29 23:39:30 +01:00
|
|
|
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_HEIGHT, 12.);
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
// Register for callbacks
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_ONINIT, $this, 'handleOnInit');
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_BEGINMAP, $this, 'handleBeginMap');
|
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MC_1_SECOND, $this, 'handle1Second');
|
2014-01-03 19:31:30 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCONNECT, $this, 'handlePlayerConnect');
|
2013-11-28 01:12:52 +01:00
|
|
|
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handlePlayerChat');
|
2014-01-03 19:31:30 +01:00
|
|
|
|
|
|
|
// Define player stats
|
|
|
|
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_PLAYER_MAPVOTES);
|
|
|
|
|
2013-12-14 23:29:17 +01:00
|
|
|
return true;
|
2013-11-28 01:12:52 +01:00
|
|
|
}
|
|
|
|
|
2013-12-14 23:29:17 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::unload()
|
|
|
|
*/
|
|
|
|
public function unload() {
|
|
|
|
$this->maniaControl->callbackManager->unregisterCallbackListener($this);
|
|
|
|
unset($this->maniaControl);
|
|
|
|
}
|
2013-12-09 10:04:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getId()
|
|
|
|
*/
|
|
|
|
public static function getId() {
|
|
|
|
return self::ID;
|
|
|
|
}
|
2013-12-14 23:29:17 +01:00
|
|
|
|
2013-12-03 22:21:17 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getName()
|
|
|
|
*/
|
|
|
|
public static function getName() {
|
|
|
|
return 'Karma Plugin';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getVersion()
|
|
|
|
*/
|
|
|
|
public static function getVersion() {
|
|
|
|
return self::VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getAuthor()
|
|
|
|
*/
|
|
|
|
public static function getAuthor() {
|
|
|
|
return 'steeffeen';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @see \ManiaControl\Plugins\Plugin::getDescription()
|
|
|
|
*/
|
|
|
|
public static function getDescription() {
|
|
|
|
return 'Plugin offering Karma Voting for Maps.';
|
|
|
|
}
|
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
/**
|
|
|
|
* Handle ManiaControl 1 Second callback
|
|
|
|
*
|
2013-12-31 11:53:10 +01:00
|
|
|
* @param array $callback
|
2013-11-28 01:12:52 +01:00
|
|
|
*/
|
|
|
|
public function handle1Second(array $callback) {
|
2014-01-03 19:31:30 +01:00
|
|
|
if(!$this->updateManialink) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
// Get players
|
|
|
|
$players = $this->updateManialink;
|
2014-01-03 19:31:30 +01:00
|
|
|
if($players === true) {
|
2013-11-28 01:12:52 +01:00
|
|
|
$players = $this->maniaControl->playerManager->getPlayers();
|
|
|
|
}
|
|
|
|
$this->updateManialink = false;
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
// Get map karma
|
2014-01-03 19:31:30 +01:00
|
|
|
$map = $this->maniaControl->mapManager->getCurrentMap();
|
2013-11-28 01:12:52 +01:00
|
|
|
$karma = $this->getMapKarma($map);
|
|
|
|
$votes = $this->getMapVotes($map);
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
// Build karma manialink
|
|
|
|
$this->buildManialink();
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-12-05 00:53:09 +01:00
|
|
|
// Update karma gauge & label
|
2013-11-28 01:12:52 +01:00
|
|
|
$karmaGauge = $this->manialink->karmaGauge;
|
2013-12-05 00:53:09 +01:00
|
|
|
$karmaLabel = $this->manialink->karmaLabel;
|
2014-01-03 19:31:30 +01:00
|
|
|
if(is_numeric($karma)) {
|
2013-11-28 01:12:52 +01:00
|
|
|
$karma = floatval($karma);
|
|
|
|
$karmaGauge->setRatio($karma + 0.15 - $karma * 0.15);
|
|
|
|
$karmaColor = ColorUtil::floatToStatusColor($karma);
|
|
|
|
$karmaGauge->setColor($karmaColor . '9');
|
2013-12-05 00:53:09 +01:00
|
|
|
$karmaLabel->setText(' ' . round($karma * 100.) . '% (' . $votes['count'] . ')');
|
2014-01-03 19:31:30 +01:00
|
|
|
} else {
|
2013-12-05 00:53:09 +01:00
|
|
|
$karma = 0.;
|
2013-11-28 01:12:52 +01:00
|
|
|
$karmaGauge->setRatio(0.);
|
|
|
|
$karmaGauge->setColor('00fb');
|
2013-12-05 00:53:09 +01:00
|
|
|
$karmaLabel->setText('-');
|
2013-11-28 01:12:52 +01:00
|
|
|
}
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
// Loop players
|
2014-01-03 19:31:30 +01:00
|
|
|
foreach($players as $login => $player) {
|
2013-11-28 01:12:52 +01:00
|
|
|
// Get player vote
|
|
|
|
$vote = $this->getPlayerVote($player, $map);
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-12-05 00:53:09 +01:00
|
|
|
// Adjust manialink for player's vote
|
2013-11-28 01:12:52 +01:00
|
|
|
$votesFrame = $this->manialink->votesFrame;
|
|
|
|
$votesFrame->removeChildren();
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
// Send manialink
|
|
|
|
$manialinkText = $this->manialink->render()->saveXML();
|
2013-11-28 02:04:06 +01:00
|
|
|
$this->maniaControl->manialinkManager->sendManialink($manialinkText, $login);
|
2013-11-28 01:12:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle ManiaControl OnInit callback
|
|
|
|
*
|
2013-12-31 11:53:10 +01:00
|
|
|
* @param array $callback
|
2013-11-28 01:12:52 +01:00
|
|
|
*/
|
|
|
|
public function handleOnInit(array $callback) {
|
|
|
|
$this->updateManialink = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle BeginMap ManiaControl callback
|
|
|
|
*
|
2013-12-31 11:53:10 +01:00
|
|
|
* @param array $callback
|
2013-11-28 01:12:52 +01:00
|
|
|
*/
|
|
|
|
public function handleBeginMap(array $callback) {
|
|
|
|
$this->updateManialink = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle PlayerConnect callback
|
|
|
|
*
|
2013-12-31 11:53:10 +01:00
|
|
|
* @param array $callback
|
2013-11-28 01:12:52 +01:00
|
|
|
*/
|
|
|
|
public function handlePlayerConnect(array $callback) {
|
2014-01-03 19:31:30 +01:00
|
|
|
$login = $callback[1][0];
|
2013-11-28 01:12:52 +01:00
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($login);
|
2014-01-03 19:31:30 +01:00
|
|
|
if(!$player) {
|
2013-11-28 01:12:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->queryManialinkUpdateFor($player);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle PlayerChat callback
|
|
|
|
*
|
2013-12-31 11:53:10 +01:00
|
|
|
* @param array $chatCallback
|
2013-11-28 01:12:52 +01:00
|
|
|
*/
|
|
|
|
public function handlePlayerChat(array $chatCallback) {
|
2014-01-03 19:31:30 +01:00
|
|
|
$login = $chatCallback[1][1];
|
2013-11-28 01:12:52 +01:00
|
|
|
$player = $this->maniaControl->playerManager->getPlayer($login);
|
2014-01-03 19:31:30 +01:00
|
|
|
if(!$player) {
|
2013-11-28 01:12:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$message = $chatCallback[1][2];
|
2014-01-03 19:31:30 +01:00
|
|
|
if($chatCallback[1][3]) {
|
2013-11-28 01:12:52 +01:00
|
|
|
$message = substr($message, 1);
|
|
|
|
}
|
2014-01-03 19:31:30 +01:00
|
|
|
if(preg_match('/[^+-]/', $message)) {
|
2013-11-28 01:12:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$vote = substr_count($message, '+');
|
|
|
|
$vote -= substr_count($message, '-');
|
|
|
|
$success = $this->handleVote($player, $vote);
|
2014-01-03 19:31:30 +01:00
|
|
|
if(!$success) {
|
2013-12-09 13:45:58 +01:00
|
|
|
$this->maniaControl->chat->sendError('Error occurred.', $player->login);
|
2013-11-28 01:12:52 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->maniaControl->chat->sendSuccess('Vote updated!', $player->login);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a vote done by a player
|
|
|
|
*
|
2013-12-31 11:53:10 +01:00
|
|
|
* @param Player $player
|
2014-01-03 19:31:30 +01:00
|
|
|
* @param int $vote
|
2013-11-28 01:12:52 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function handleVote(Player $player, $vote) {
|
|
|
|
// Check vote
|
|
|
|
$votesSetting = $this->maniaControl->settingManager->getSetting($this, self::SETTING_AVAILABLE_VOTES);
|
2014-01-03 19:31:30 +01:00
|
|
|
$votes = explode(',', $votesSetting);
|
|
|
|
$voteLow = intval($votes[0]);
|
|
|
|
$voteHigh = $voteLow + 2;
|
|
|
|
if(isset($votes[1])) {
|
2013-11-28 01:12:52 +01:00
|
|
|
$voteHigh = intval($votes[1]);
|
|
|
|
}
|
2014-01-03 19:31:30 +01:00
|
|
|
if($vote < $voteLow || $vote > $voteHigh) {
|
2013-11-28 01:12:52 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
// Calculate actual voting
|
|
|
|
$vote -= $voteLow;
|
|
|
|
$voteHigh -= $voteLow;
|
|
|
|
$vote /= $voteHigh;
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
// Save vote
|
2014-01-03 19:31:30 +01:00
|
|
|
$map = $this->maniaControl->mapManager->getCurrentMap();
|
|
|
|
|
|
|
|
$voted = $this->getPlayerVote($player, $map);
|
|
|
|
if(!$voted){
|
|
|
|
$this->maniaControl->statisticManager->incrementStat(self::STAT_PLAYER_MAPVOTES, $player, $this->maniaControl->server->getLogin());
|
|
|
|
}
|
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
$success = $this->savePlayerVote($player, $map, $vote);
|
2014-01-03 19:31:30 +01:00
|
|
|
if(!$success) {
|
2013-11-28 01:12:52 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->updateManialink = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query the player to update the manialink
|
|
|
|
*
|
2013-12-31 11:53:10 +01:00
|
|
|
* @param Player $player
|
2013-11-28 01:12:52 +01:00
|
|
|
*/
|
|
|
|
private function queryManialinkUpdateFor(Player $player) {
|
2014-01-03 19:31:30 +01:00
|
|
|
if($this->updateManialink === true) {
|
2013-11-28 01:12:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-01-03 19:31:30 +01:00
|
|
|
if(!is_array($this->updateManialink)) {
|
2013-11-28 01:12:52 +01:00
|
|
|
$this->updateManialink = array();
|
|
|
|
}
|
|
|
|
$this->updateManialink[$player->login] = $player;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create necessary database tables
|
|
|
|
*/
|
|
|
|
private function initTables() {
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2014-01-03 19:31:30 +01:00
|
|
|
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_KARMA . "` (
|
2013-11-28 01:12:52 +01:00
|
|
|
`index` int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
`mapIndex` int(11) NOT NULL,
|
|
|
|
`playerIndex` int(11) NOT NULL,
|
|
|
|
`vote` float NOT NULL DEFAULT '-1',
|
|
|
|
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
|
|
PRIMARY KEY (`index`),
|
|
|
|
UNIQUE KEY `player_map_vote` (`mapIndex`, `playerIndex`)
|
|
|
|
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Save players map votes' AUTO_INCREMENT=1;";
|
2013-12-09 13:45:58 +01:00
|
|
|
$mysqli->query($query);
|
2014-01-03 19:31:30 +01:00
|
|
|
if($mysqli->error) {
|
2013-11-28 01:12:52 +01:00
|
|
|
trigger_error($mysqli->error, E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the vote of the player for the map
|
|
|
|
*
|
2013-12-31 11:53:10 +01:00
|
|
|
* @param Player $player
|
2014-01-03 19:31:30 +01:00
|
|
|
* @param Map $map
|
|
|
|
* @param float $vote
|
2013-11-28 01:12:52 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function savePlayerVote(Player $player, Map $map, $vote) {
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2014-01-03 19:31:30 +01:00
|
|
|
$query = "INSERT INTO `" . self::TABLE_KARMA . "` (
|
2013-11-28 01:12:52 +01:00
|
|
|
`mapIndex`,
|
|
|
|
`playerIndex`,
|
|
|
|
`vote`
|
|
|
|
) VALUES (
|
|
|
|
{$map->index},
|
|
|
|
{$player->index},
|
|
|
|
{$vote}
|
|
|
|
) ON DUPLICATE KEY UPDATE
|
|
|
|
`vote` = VALUES(`vote`);";
|
|
|
|
$result = $mysqli->query($query);
|
2014-01-03 19:31:30 +01:00
|
|
|
if($mysqli->error) {
|
2013-11-28 01:12:52 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current vote of the player for the map
|
|
|
|
*
|
2013-12-31 11:53:10 +01:00
|
|
|
* @param Player $player
|
2014-01-03 19:31:30 +01:00
|
|
|
* @param Map $map
|
2013-11-28 01:12:52 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function getPlayerVote(Player $player, Map $map) {
|
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2014-01-03 19:31:30 +01:00
|
|
|
$query = "SELECT * FROM `" . self::TABLE_KARMA . "`
|
2013-11-28 01:12:52 +01:00
|
|
|
WHERE `playerIndex` = {$player->index}
|
|
|
|
AND `mapIndex` = {$map->index}
|
|
|
|
AND `vote` >= 0;";
|
|
|
|
$result = $mysqli->query($query);
|
2014-01-03 19:31:30 +01:00
|
|
|
if($mysqli->error) {
|
2013-11-28 01:12:52 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-03 19:31:30 +01:00
|
|
|
if($result->num_rows <= 0) {
|
2013-11-28 01:12:52 +01:00
|
|
|
$result->free();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$item = $result->fetch_object();
|
|
|
|
$result->free();
|
|
|
|
$vote = $item->vote;
|
|
|
|
return floatval($vote);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current karma of the map
|
|
|
|
*
|
2013-12-31 11:53:10 +01:00
|
|
|
* @param Map $map
|
2013-12-14 23:29:17 +01:00
|
|
|
* @return float | bool
|
2013-11-28 01:12:52 +01:00
|
|
|
*/
|
2013-12-29 19:21:29 +01:00
|
|
|
public function getMapKarma(Map $map) {
|
2013-11-28 01:12:52 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2014-01-03 19:31:30 +01:00
|
|
|
$query = "SELECT AVG(`vote`) AS `karma` FROM `" . self::TABLE_KARMA . "`
|
2013-11-28 01:12:52 +01:00
|
|
|
WHERE `mapIndex` = {$map->index}
|
|
|
|
AND `vote` >= 0;";
|
|
|
|
$result = $mysqli->query($query);
|
2014-01-03 19:31:30 +01:00
|
|
|
if($mysqli->error) {
|
2013-11-28 01:12:52 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-03 19:31:30 +01:00
|
|
|
if($result->num_rows <= 0) {
|
2013-11-28 01:12:52 +01:00
|
|
|
$result->free();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$item = $result->fetch_object();
|
|
|
|
$result->free();
|
|
|
|
$karma = $item->karma;
|
2014-01-03 19:31:30 +01:00
|
|
|
if($karma === null) {
|
2013-11-28 01:12:52 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return floatval($karma);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-12-09 13:45:58 +01:00
|
|
|
* Get the current Votes for the Map
|
2013-11-28 01:12:52 +01:00
|
|
|
*
|
2013-12-31 11:53:10 +01:00
|
|
|
* @param Map $map
|
2013-12-14 23:29:17 +01:00
|
|
|
* @return array
|
2013-11-28 01:12:52 +01:00
|
|
|
*/
|
2013-12-29 19:21:29 +01:00
|
|
|
public function getMapVotes(Map $map) {
|
2013-11-28 01:12:52 +01:00
|
|
|
$mysqli = $this->maniaControl->database->mysqli;
|
2014-01-03 19:31:30 +01:00
|
|
|
$query = "SELECT `vote`, COUNT(`vote`) AS `count` FROM `" . self::TABLE_KARMA . "`
|
2013-11-28 01:12:52 +01:00
|
|
|
WHERE `mapIndex` = {$map->index}
|
|
|
|
AND `vote` >= 0
|
|
|
|
GROUP BY `vote`;";
|
|
|
|
$result = $mysqli->query($query);
|
2014-01-03 19:31:30 +01:00
|
|
|
if($mysqli->error) {
|
2013-11-28 01:12:52 +01:00
|
|
|
trigger_error($mysqli->error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$votes = array();
|
2013-12-05 00:53:09 +01:00
|
|
|
$count = 0;
|
2014-01-03 19:31:30 +01:00
|
|
|
while($vote = $result->fetch_object()) {
|
2013-11-28 01:12:52 +01:00
|
|
|
$votes[$vote->vote] = $vote;
|
2013-12-05 00:53:09 +01:00
|
|
|
$count += $vote->count;
|
2013-11-28 01:12:52 +01:00
|
|
|
}
|
2013-12-05 00:53:09 +01:00
|
|
|
$votes['count'] = $count;
|
2013-11-28 01:12:52 +01:00
|
|
|
$result->free();
|
|
|
|
return $votes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build karma voting manialink if necessary
|
|
|
|
*
|
2013-12-31 11:53:10 +01:00
|
|
|
* @param bool $forceBuild
|
2013-11-28 01:12:52 +01:00
|
|
|
*/
|
|
|
|
private function buildManialink($forceBuild = false) {
|
2014-01-03 19:31:30 +01:00
|
|
|
if(is_object($this->manialink) && !$forceBuild) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$title = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_TITLE);
|
|
|
|
$pos_x = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_POSX);
|
|
|
|
$pos_y = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_POSY);
|
|
|
|
$width = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_WIDTH);
|
|
|
|
$height = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_HEIGHT);
|
|
|
|
$labelStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultLabelStyle();
|
|
|
|
$quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadStyle();
|
2013-11-28 03:47:08 +01:00
|
|
|
$quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadSubstyle();
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
$manialink = new ManiaLink(self::MLID_KARMA);
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
$frame = new Frame();
|
|
|
|
$manialink->add($frame);
|
|
|
|
$frame->setPosition($pos_x, $pos_y);
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
$backgroundQuad = new Quad();
|
|
|
|
$frame->add($backgroundQuad);
|
|
|
|
$backgroundQuad->setY($height * 0.15);
|
|
|
|
$backgroundQuad->setSize($width, $height);
|
2013-11-28 03:47:08 +01:00
|
|
|
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
$titleLabel = new Label();
|
|
|
|
$frame->add($titleLabel);
|
|
|
|
$titleLabel->setY($height * 0.36);
|
|
|
|
$titleLabel->setWidth($width * 0.85);
|
2013-11-28 03:47:08 +01:00
|
|
|
$titleLabel->setStyle($labelStyle);
|
2013-11-28 01:12:52 +01:00
|
|
|
$titleLabel->setTranslate(true);
|
|
|
|
$titleLabel->setTextSize(1);
|
2013-12-29 23:39:30 +01:00
|
|
|
$titleLabel->setScale(0.90);
|
2013-11-28 01:12:52 +01:00
|
|
|
$titleLabel->setText($title);
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
$karmaGauge = new Gauge();
|
|
|
|
$frame->add($karmaGauge);
|
|
|
|
$karmaGauge->setSize($width * 0.95, $height * 0.92);
|
|
|
|
$karmaGauge->setDrawBg(false);
|
|
|
|
$manialink->karmaGauge = $karmaGauge;
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-12-05 00:53:09 +01:00
|
|
|
$karmaLabel = new Label();
|
|
|
|
$frame->add($karmaLabel);
|
|
|
|
$karmaLabel->setPosition(0, -0.4, 1);
|
|
|
|
$karmaLabel->setSize($width * 0.9, $height * 0.9);
|
|
|
|
$karmaLabel->setStyle($labelStyle);
|
|
|
|
$karmaLabel->setTextSize(1);
|
|
|
|
$manialink->karmaLabel = $karmaLabel;
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
$votesFrame = new Frame();
|
|
|
|
$frame->add($votesFrame);
|
|
|
|
$manialink->votesFrame = $votesFrame;
|
2014-01-03 19:31:30 +01:00
|
|
|
|
2013-11-28 01:12:52 +01:00
|
|
|
$this->manialink = $manialink;
|
|
|
|
}
|
|
|
|
}
|