improvements

This commit is contained in:
kremsy 2014-01-03 19:31:30 +01:00
parent 176b9eef6d
commit 7b6ff4dcb8
3 changed files with 105 additions and 95 deletions

View File

@ -308,11 +308,7 @@ class PlayerManager implements CallbackListener {
$playerStatement->close();
// Increment the Player Join Count
$success = $this->maniaControl->statisticManager->incrementStat(self::STAT_JOIN_COUNT, $player, $this->maniaControl->server->getLogin());
if(!$success) {
trigger_error("Error while setting the JoinCount");
}
$this->maniaControl->statisticManager->incrementStat(self::STAT_JOIN_COUNT, $player, $this->maniaControl->server->getLogin());
return true;
}

View File

@ -183,6 +183,7 @@ class StatisticManager {
trigger_error($mysqli->error);
return false;
}
$statement->bind_param('iiii', $serverId, $player->index, $statId, $value);
$statement->execute();
if($statement->error) {

View File

@ -1,16 +1,16 @@
<?php
use ManiaControl\ColorUtil;
use ManiaControl\ManiaControl;
use FML\Controls\Frame;
use FML\Controls\Gauge;
use FML\Controls\Label;
use FML\Controls\Quad;
use FML\ManiaLink;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ColorUtil;
use ManiaControl\ManiaControl;
use ManiaControl\Maps\Map;
use ManiaControl\Players\Player;
use ManiaControl\Plugins\Plugin;
use FML\ManiaLink;
use FML\Controls\Frame;
use FML\Controls\Label;
use FML\Controls\Quad;
use FML\Controls\Gauge;
/**
* ManiaControl Karma Plugin
@ -32,6 +32,8 @@ class KarmaPlugin implements CallbackListener, Plugin {
const SETTING_WIDGET_WIDTH = 'Widget-Size: Width';
const SETTING_WIDGET_HEIGHT = 'Widget-Size: Height';
const STAT_PLAYER_MAPVOTES = 'Voted Maps';
/**
* Private properties
*/
@ -65,10 +67,12 @@ class KarmaPlugin implements CallbackListener, Plugin {
$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');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCONNECT, $this,
'handlePlayerConnect');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCONNECT, $this, 'handlePlayerConnect');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERCHAT, $this, 'handlePlayerChat');
// Define player stats
$this->maniaControl->statisticManager->defineStatMetaData(self::STAT_PLAYER_MAPVOTES);
return true;
}
@ -127,11 +131,13 @@ class KarmaPlugin implements CallbackListener, Plugin {
* @param array $callback
*/
public function handle1Second(array $callback) {
if (!$this->updateManialink) return;
if(!$this->updateManialink) {
return;
}
// Get players
$players = $this->updateManialink;
if ($players === true) {
if($players === true) {
$players = $this->maniaControl->playerManager->getPlayers();
}
$this->updateManialink = false;
@ -147,14 +153,13 @@ class KarmaPlugin implements CallbackListener, Plugin {
// Update karma gauge & label
$karmaGauge = $this->manialink->karmaGauge;
$karmaLabel = $this->manialink->karmaLabel;
if (is_numeric($karma)) {
if(is_numeric($karma)) {
$karma = floatval($karma);
$karmaGauge->setRatio($karma + 0.15 - $karma * 0.15);
$karmaColor = ColorUtil::floatToStatusColor($karma);
$karmaGauge->setColor($karmaColor . '9');
$karmaLabel->setText(' ' . round($karma * 100.) . '% (' . $votes['count'] . ')');
}
else {
} else {
$karma = 0.;
$karmaGauge->setRatio(0.);
$karmaGauge->setColor('00fb');
@ -162,7 +167,7 @@ class KarmaPlugin implements CallbackListener, Plugin {
}
// Loop players
foreach ($players as $login => $player) {
foreach($players as $login => $player) {
// Get player vote
$vote = $this->getPlayerVote($player, $map);
@ -202,7 +207,7 @@ class KarmaPlugin implements CallbackListener, Plugin {
public function handlePlayerConnect(array $callback) {
$login = $callback[1][0];
$player = $this->maniaControl->playerManager->getPlayer($login);
if (!$player) {
if(!$player) {
return;
}
$this->queryManialinkUpdateFor($player);
@ -216,20 +221,20 @@ class KarmaPlugin implements CallbackListener, Plugin {
public function handlePlayerChat(array $chatCallback) {
$login = $chatCallback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login);
if (!$player) {
if(!$player) {
return;
}
$message = $chatCallback[1][2];
if ($chatCallback[1][3]) {
if($chatCallback[1][3]) {
$message = substr($message, 1);
}
if (preg_match('/[^+-]/', $message)) {
if(preg_match('/[^+-]/', $message)) {
return;
}
$vote = substr_count($message, '+');
$vote -= substr_count($message, '-');
$success = $this->handleVote($player, $vote);
if (!$success) {
if(!$success) {
$this->maniaControl->chat->sendError('Error occurred.', $player->login);
return;
}
@ -249,10 +254,10 @@ class KarmaPlugin implements CallbackListener, Plugin {
$votes = explode(',', $votesSetting);
$voteLow = intval($votes[0]);
$voteHigh = $voteLow + 2;
if (isset($votes[1])) {
if(isset($votes[1])) {
$voteHigh = intval($votes[1]);
}
if ($vote < $voteLow || $vote > $voteHigh) {
if($vote < $voteLow || $vote > $voteHigh) {
return false;
}
@ -263,8 +268,14 @@ class KarmaPlugin implements CallbackListener, Plugin {
// Save vote
$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());
}
$success = $this->savePlayerVote($player, $map, $vote);
if (!$success) {
if(!$success) {
return false;
}
$this->updateManialink = true;
@ -277,10 +288,10 @@ class KarmaPlugin implements CallbackListener, Plugin {
* @param Player $player
*/
private function queryManialinkUpdateFor(Player $player) {
if ($this->updateManialink === true) {
if($this->updateManialink === true) {
return;
}
if (!is_array($this->updateManialink)) {
if(!is_array($this->updateManialink)) {
$this->updateManialink = array();
}
$this->updateManialink[$player->login] = $player;
@ -301,7 +312,7 @@ class KarmaPlugin implements CallbackListener, Plugin {
UNIQUE KEY `player_map_vote` (`mapIndex`, `playerIndex`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Save players map votes' AUTO_INCREMENT=1;";
$mysqli->query($query);
if ($mysqli->error) {
if($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR);
}
}
@ -327,7 +338,7 @@ class KarmaPlugin implements CallbackListener, Plugin {
) ON DUPLICATE KEY UPDATE
`vote` = VALUES(`vote`);";
$result = $mysqli->query($query);
if ($mysqli->error) {
if($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
@ -348,11 +359,11 @@ class KarmaPlugin implements CallbackListener, Plugin {
AND `mapIndex` = {$map->index}
AND `vote` >= 0;";
$result = $mysqli->query($query);
if ($mysqli->error) {
if($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
if ($result->num_rows <= 0) {
if($result->num_rows <= 0) {
$result->free();
return false;
}
@ -374,18 +385,18 @@ class KarmaPlugin implements CallbackListener, Plugin {
WHERE `mapIndex` = {$map->index}
AND `vote` >= 0;";
$result = $mysqli->query($query);
if ($mysqli->error) {
if($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
if ($result->num_rows <= 0) {
if($result->num_rows <= 0) {
$result->free();
return false;
}
$item = $result->fetch_object();
$result->free();
$karma = $item->karma;
if ($karma === null) {
if($karma === null) {
return false;
}
return floatval($karma);
@ -404,13 +415,13 @@ class KarmaPlugin implements CallbackListener, Plugin {
AND `vote` >= 0
GROUP BY `vote`;";
$result = $mysqli->query($query);
if ($mysqli->error) {
if($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$votes = array();
$count = 0;
while ($vote = $result->fetch_object()) {
while($vote = $result->fetch_object()) {
$votes[$vote->vote] = $vote;
$count += $vote->count;
}
@ -425,7 +436,9 @@ class KarmaPlugin implements CallbackListener, Plugin {
* @param bool $forceBuild
*/
private function buildManialink($forceBuild = false) {
if (is_object($this->manialink) && !$forceBuild) return;
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);