files should have the same name as their classes

who renamed this class?..
This commit is contained in:
Steffen Schröder 2014-04-28 17:54:25 +02:00
parent cf78a2a141
commit 5cac6b4cd1

View File

@ -26,36 +26,37 @@ use ManiaControl\Plugins\Plugin;
/** /**
* ManiaControl Local Records Plugin * ManiaControl Local Records Plugin
* *
* @author steeffeen * @author steeffeen
* @copyright ManiaControl Copyright © 2014 ManiaControl Team * @copyright ManiaControl Copyright © 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/ */
class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerListener, Plugin { class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerListener, Plugin {
/** /*
* Constants * Constants
*/ */
const ID = 7; const ID = 7;
const VERSION = 0.1; const VERSION = 0.1;
const MLID_RECORDS = 'ml_local_records'; const MLID_RECORDS = 'ml_local_records';
const TABLE_RECORDS = 'mc_localrecords'; const TABLE_RECORDS = 'mc_localrecords';
const SETTING_WIDGET_TITLE = 'Widget Title'; const SETTING_WIDGET_TITLE = 'Widget Title';
const SETTING_WIDGET_POSX = 'Widget Position: X'; const SETTING_WIDGET_POSX = 'Widget Position: X';
const SETTING_WIDGET_POSY = 'Widget Position: Y'; const SETTING_WIDGET_POSY = 'Widget Position: Y';
const SETTING_WIDGET_WIDTH = 'Widget Width'; const SETTING_WIDGET_WIDTH = 'Widget Width';
const SETTING_WIDGET_LINESCOUNT = 'Widget Displayed Lines Count'; const SETTING_WIDGET_LINESCOUNT = 'Widget Displayed Lines Count';
const SETTING_WIDGET_LINEHEIGHT = 'Widget Line Height'; const SETTING_WIDGET_LINEHEIGHT = 'Widget Line Height';
const SETTING_WIDGET_ENABLE = 'Enable Local Records Widget'; const SETTING_WIDGET_ENABLE = 'Enable Local Records Widget';
const SETTING_NOTIFY_ONLY_DRIVER = 'Notify only the Driver on New Records'; const SETTING_NOTIFY_ONLY_DRIVER = 'Notify only the Driver on New Records';
const SETTING_NOTIFY_BEST_RECORDS = 'Notify Publicly only for the X Best Records'; const SETTING_NOTIFY_BEST_RECORDS = 'Notify Publicly only for the X Best Records';
const SETTING_ADJUST_OUTER_BORDER = 'Adjust outer Border to Number of actual Records'; const SETTING_ADJUST_OUTER_BORDER = 'Adjust outer Border to Number of actual Records';
const CB_LOCALRECORDS_CHANGED = 'LocalRecords.Changed'; const CB_LOCALRECORDS_CHANGED = 'LocalRecords.Changed';
const ACTION_SHOW_RECORDSLIST = 'LocalRecords.ShowRecordsList'; const ACTION_SHOW_RECORDSLIST = 'LocalRecords.ShowRecordsList';
/* /*
* Private Properties * Private Properties
*/ */
/** /**
*
* @var maniaControl $maniaControl * @var maniaControl $maniaControl
*/ */
private $maniaControl = null; private $maniaControl = null;
@ -64,21 +65,22 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
/** /**
* Prepares the Plugin * Prepares the Plugin
* *
* @param ManiaControl $maniaControl * @param ManiaControl $maniaControl
* @return mixed * @return mixed
*/ */
public static function prepare(ManiaControl $maniaControl) { public static function prepare(ManiaControl $maniaControl) {
//do nothing // do nothing
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::load() * @see \ManiaControl\Plugins\Plugin::load()
*/ */
public function load(ManiaControl $maniaControl) { public function load(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->initTables(); $this->initTables();
// Init settings // Init settings
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_TITLE, 'Local Records'); $this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_TITLE, 'Local Records');
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_POSX, -139.); $this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_POSX, -139.);
@ -86,11 +88,11 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_WIDTH, 40.); $this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_WIDTH, 40.);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_LINESCOUNT, 15); $this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_LINESCOUNT, 15);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_LINEHEIGHT, 4.); $this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_LINEHEIGHT, 4.);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_ENABLE, true); $this->maniaControl->settingManager->initSetting($this, self::SETTING_WIDGET_ENABLE, true);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_NOTIFY_ONLY_DRIVER, false); $this->maniaControl->settingManager->initSetting($this, self::SETTING_NOTIFY_ONLY_DRIVER, false);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_NOTIFY_BEST_RECORDS, -1); $this->maniaControl->settingManager->initSetting($this, self::SETTING_NOTIFY_BEST_RECORDS, -1);
$this->maniaControl->settingManager->initSetting($this, self::SETTING_ADJUST_OUTER_BORDER, false); $this->maniaControl->settingManager->initSetting($this, self::SETTING_ADJUST_OUTER_BORDER, false);
// Register for callbacks // Register for callbacks
$this->maniaControl->timerManager->registerTimerListening($this, 'handle1Second', 1000); $this->maniaControl->timerManager->registerTimerListening($this, 'handle1Second', 1000);
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_AFTERINIT, $this, 'handleAfterInit'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_AFTERINIT, $this, 'handleAfterInit');
@ -98,21 +100,21 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERFINISH, $this, 'handlePlayerFinish'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERFINISH, $this, 'handlePlayerFinish');
$this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect'); $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERCHECKPOINT, $this, 'handlePlayerCheckpoint'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERCHECKPOINT, $this, 'handlePlayerCheckpoint');
$this->maniaControl->callbackManager->registerCallbackListener(SettingManager::CB_SETTINGS_CHANGED, $this, 'handleSettingsChanged'); $this->maniaControl->callbackManager->registerCallbackListener(SettingManager::CB_SETTINGS_CHANGED, $this, 'handleSettingsChanged');
$this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
$this->maniaControl->commandManager->registerCommandListener('records', $this, 'showRecordsList'); $this->maniaControl->commandManager->registerCommandListener('records', $this, 'showRecordsList');
$this->maniaControl->commandManager->registerCommandListener('delrec', $this, 'deleteRecord', true); $this->maniaControl->commandManager->registerCommandListener('delrec', $this, 'deleteRecord', true);
return true; return true;
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::unload() * @see \ManiaControl\Plugins\Plugin::unload()
*/ */
public function unload() { public function unload() {
$this->maniaControl->callbackManager->unregisterCallbackListener($this); $this->maniaControl->callbackManager->unregisterCallbackListener($this);
$this->maniaControl->timerManager->unregisterTimerListenings($this); $this->maniaControl->timerManager->unregisterTimerListenings($this);
unset($this->maniaControl);
} }
/** /**
@ -120,7 +122,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
*/ */
private function initTables() { private function initTables() {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_RECORDS . "` ( $query = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_RECORDS . "` (
`index` int(11) NOT NULL AUTO_INCREMENT, `index` int(11) NOT NULL AUTO_INCREMENT,
`mapIndex` int(11) NOT NULL, `mapIndex` int(11) NOT NULL,
`playerIndex` int(11) NOT NULL, `playerIndex` int(11) NOT NULL,
@ -133,16 +135,17 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
if ($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR); trigger_error($mysqli->error, E_USER_ERROR);
} }
$mysqli->query("ALTER TABLE `" . self::TABLE_RECORDS . "` ADD `checkpoints` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL"); $mysqli->query("ALTER TABLE `" . self::TABLE_RECORDS . "` ADD `checkpoints` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL");
if ($mysqli->error) { if ($mysqli->error) {
if(!strstr($mysqli->error, 'Duplicate')) { if (!strstr($mysqli->error, 'Duplicate')) {
trigger_error($mysqli->error, E_USER_ERROR); trigger_error($mysqli->error, E_USER_ERROR);
} }
} }
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getId() * @see \ManiaControl\Plugins\Plugin::getId()
*/ */
public static function getId() { public static function getId() {
@ -150,6 +153,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getName() * @see \ManiaControl\Plugins\Plugin::getName()
*/ */
public static function getName() { public static function getName() {
@ -157,6 +161,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getVersion() * @see \ManiaControl\Plugins\Plugin::getVersion()
*/ */
public static function getVersion() { public static function getVersion() {
@ -164,6 +169,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getAuthor() * @see \ManiaControl\Plugins\Plugin::getAuthor()
*/ */
public static function getAuthor() { public static function getAuthor() {
@ -171,6 +177,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getDescription() * @see \ManiaControl\Plugins\Plugin::getDescription()
*/ */
public static function getDescription() { public static function getDescription() {
@ -186,45 +193,45 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
/** /**
* Handle 1Second callback * Handle 1Second callback
* *
* @param $time * @param $time
*/ */
public function handle1Second($time) { public function handle1Second($time) {
if (!$this->updateManialink) { if (!$this->updateManialink) {
return; return;
} }
$this->updateManialink = false; $this->updateManialink = false;
if($this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_ENABLE)) { if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_ENABLE)) {
$manialink = $this->buildManialink(); $manialink = $this->buildManialink();
$this->maniaControl->manialinkManager->sendManialink($manialink); $this->maniaControl->manialinkManager->sendManialink($manialink);
} }
} }
public function handleSettingsChanged($class, $settingName, $value) {
public function handleSettingsChanged($class, $settingName, $value) { if (!$class = get_class()) {
if (!$class = get_class()) { return;
return; }
} if ($settingName == 'Enable Local Records Widget' && $value == true) {
if ($settingName == 'Enable Local Records Widget' && $value == true) { $this->updateManialink = true;
$this->updateManialink = true; }
} elseif ($settingName == 'Enable Local Records Widget' && $value == false) { elseif ($settingName == 'Enable Local Records Widget' && $value == false) {
$ml = new ManiaLink(self::MLID_RECORDS); $ml = new ManiaLink(self::MLID_RECORDS);
$mltext = $ml->render()->saveXML(); $mltext = $ml->render()->saveXML();
$this->maniaControl->manialinkManager->sendManialink($mltext); $this->maniaControl->manialinkManager->sendManialink($mltext);
} }
} }
/** /**
* Handle PlayerCheckpoint callback * Handle PlayerCheckpoint callback
* *
* @param $callback * @param $callback
*/ */
public function handlePlayerCheckpoint($callback) { public function handlePlayerCheckpoint($callback) {
$data = $callback[1]; $data = $callback[1];
$login = $data[1]; $login = $data[1];
$time = $data[2]; $time = $data[2];
//$lap = $data[3]; // $lap = $data[3];
$cpIndex = $data[4]; $cpIndex = $data[4];
if (!isset($this->checkpoints[$login]) || $cpIndex <= 0) { if (!isset($this->checkpoints[$login]) || $cpIndex <= 0) {
$this->checkpoints[$login] = array(); $this->checkpoints[$login] = array();
@ -234,7 +241,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
/** /**
* Handle PlayerConnect callback * Handle PlayerConnect callback
* *
* @param Player $player * @param Player $player
*/ */
public function handlePlayerConnect(Player $player) { public function handlePlayerConnect(Player $player) {
@ -243,7 +250,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
/** /**
* Handle BeginMap callback * Handle BeginMap callback
* *
* @param Map $map * @param Map $map
*/ */
public function handleMapBegin(Map $map) { public function handleMapBegin(Map $map) {
@ -252,7 +259,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
/** /**
* Handle PlayerFinish callback * Handle PlayerFinish callback
* *
* @param array $callback * @param array $callback
*/ */
public function handlePlayerFinish(array $callback) { public function handlePlayerFinish(array $callback) {
@ -261,37 +268,37 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
// Invalid player or time // Invalid player or time
return; return;
} }
$login = $data[1]; $login = $data[1];
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
if (!$player) { if (!$player) {
// Invalid player // Invalid player
return; return;
} }
$time = $data[2]; $time = $data[2];
$map = $this->maniaControl->mapManager->getCurrentMap(); $map = $this->maniaControl->mapManager->getCurrentMap();
// Check old record of the player // Check old record of the player
$oldRecord = $this->getLocalRecord($map, $player); $oldRecord = $this->getLocalRecord($map, $player);
$oldRank = -1; $oldRank = -1;
if ($oldRecord) { if ($oldRecord) {
$oldRank = $oldRecord->rank; $oldRank = $oldRecord->rank;
if ($oldRecord->time < $time) { if ($oldRecord->time < $time) {
// Not improved // Not improved
return; return;
} }
if ($oldRecord->time == $time) { if ($oldRecord->time == $time) {
// Same time // Same time
$message = '$<' . $player->nickname . '$> equalized his/her $<$ff0' . $oldRecord->rank . '.$> Local Record: $<$fff' . Formatter::formatTime($oldRecord->time).'$>!'; $message = '$<' . $player->nickname . '$> equalized his/her $<$ff0' . $oldRecord->rank . '.$> Local Record: $<$fff' . Formatter::formatTime($oldRecord->time) . '$>!';
$this->maniaControl->chat->sendInformation('$3c0'.$message); $this->maniaControl->chat->sendInformation('$3c0' . $message);
return; return;
} }
} }
// Save time // Save time
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$query = "INSERT INTO `" . self::TABLE_RECORDS . "` ( $query = "INSERT INTO `" . self::TABLE_RECORDS . "` (
`mapIndex`, `mapIndex`,
`playerIndex`, `playerIndex`,
`time`, `time`,
@ -310,122 +317,130 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
return; return;
} }
$this->updateManialink = true; $this->updateManialink = true;
// Announce record // Announce record
$newRecord = $this->getLocalRecord($map, $player); $newRecord = $this->getLocalRecord($map, $player);
$this->maniaControl->callbackManager->triggerCallback(self::CB_LOCALRECORDS_CHANGED, $newRecord); $this->maniaControl->callbackManager->triggerCallback(self::CB_LOCALRECORDS_CHANGED, $newRecord);
$notifyOnlyDriver = $this->maniaControl->settingManager->getSetting($this, self::SETTING_NOTIFY_ONLY_DRIVER); $notifyOnlyDriver = $this->maniaControl->settingManager->getSetting($this, self::SETTING_NOTIFY_ONLY_DRIVER);
$notifyOnlyBestRecords = $this->maniaControl->settingManager->getSetting($this, self::SETTING_NOTIFY_BEST_RECORDS); $notifyOnlyBestRecords = $this->maniaControl->settingManager->getSetting($this, self::SETTING_NOTIFY_BEST_RECORDS);
if ($notifyOnlyDriver || $notifyOnlyBestRecords > 0 && $newRecord->rank > $notifyOnlyBestRecords) { if ($notifyOnlyDriver || $notifyOnlyBestRecords > 0 && $newRecord->rank > $notifyOnlyBestRecords) {
$improvement = ((!$oldRecord || $newRecord->rank < $oldRecord->rank) ? 'gained the' : 'improved your'); $improvement = ((!$oldRecord || $newRecord->rank < $oldRecord->rank) ? 'gained the' : 'improved your');
$message = 'You ' . $improvement . ' $<$ff0' . $newRecord->rank . '.$> Local Record: $<$fff' . Formatter::formatTime($newRecord->time); $message = 'You ' . $improvement . ' $<$ff0' . $newRecord->rank . '.$> Local Record: $<$fff' . Formatter::formatTime($newRecord->time);
if($oldRecord) $oldRank = ($improvement == 'improved your') ? '' : $oldRecord->rank.'. '; if ($oldRecord) $oldRank = ($improvement == 'improved your') ? '' : $oldRecord->rank . '. ';
if($oldRecord) $message .= '$> ($<$ff0'.$oldRank.'$>$<$fff-'.Formatter::formatTime(($oldRecord->time-$newRecord->time)).'$>)!'; if ($oldRecord) $message .= '$> ($<$ff0' . $oldRank . '$>$<$fff-' . Formatter::formatTime(($oldRecord->time - $newRecord->time)) . '$>)!';
$this->maniaControl->chat->sendInformation('$3c0'.$message, $player->login); $this->maniaControl->chat->sendInformation('$3c0' . $message, $player->login);
} else { }
else {
$improvement = ((!$oldRecord || $newRecord->rank < $oldRecord->rank) ? 'gained the' : 'improved the'); $improvement = ((!$oldRecord || $newRecord->rank < $oldRecord->rank) ? 'gained the' : 'improved the');
$message = '$<' . $player->nickname . '$> ' . $improvement . ' $<$ff0' . $newRecord->rank . '.$> Local Record: $<$fff' . Formatter::formatTime($newRecord->time); $message = '$<' . $player->nickname . '$> ' . $improvement . ' $<$ff0' . $newRecord->rank . '.$> Local Record: $<$fff' . Formatter::formatTime($newRecord->time);
if($oldRecord) $oldRank = ($improvement == 'improved the') ? '' : $oldRecord->rank.'. '; if ($oldRecord) $oldRank = ($improvement == 'improved the') ? '' : $oldRecord->rank . '. ';
if($oldRecord) $message .= '$> ($<$ff0'.$oldRank.'$>$<$fff-'.Formatter::formatTime(($oldRecord->time-$newRecord->time)).'$>)!'; if ($oldRecord) $message .= '$> ($<$ff0' . $oldRank . '$>$<$fff-' . Formatter::formatTime(($oldRecord->time - $newRecord->time)) . '$>)!';
$this->maniaControl->chat->sendInformation('$3c0'.$message); $this->maniaControl->chat->sendInformation('$3c0' . $message);
} }
} }
/** /**
* Handle PlayerManialinkPageAnswer callback * Handle PlayerManialinkPageAnswer callback
* *
* @param array $callback * @param array $callback
*/ */
public function handleManialinkPageAnswer(array $callback) { public function handleManialinkPageAnswer(array $callback) {
$actionId = $callback[1][2]; $actionId = $callback[1][2];
$login = $callback[1][1]; $login = $callback[1][1];
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
if($actionId == self::ACTION_SHOW_RECORDSLIST) { if ($actionId == self::ACTION_SHOW_RECORDSLIST) {
$this->showRecordsList(array(), $player); $this->showRecordsList(array(), $player);
} }
} }
/**
* Delete a Player's record
*
* @param array $chat
* @param Player $player
*/
public function deleteRecord(array $chat, Player $player) { public function deleteRecord(array $chat, Player $player) {
if(!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_MASTERADMIN)) { if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_MASTERADMIN)) {
$this->maniaControl->authenticationManager->sendNotAllowed($player); $this->maniaControl->authenticationManager->sendNotAllowed($player);
return; return;
} }
$chatCommand = explode(' ', $chat[1][2]); $chatCommand = explode(' ', $chat[1][2]);
$recordId = (int)$chatCommand[1]; $recordId = (int) $chatCommand[1];
if(is_integer($recordId)) { if (is_integer($recordId)) {
$currentMap = $this->maniaControl->mapManager->getCurrentMap(); $currentMap = $this->maniaControl->mapManager->getCurrentMap();
$records = $this->getLocalRecords($currentMap); $records = $this->getLocalRecords($currentMap);
if(count($records) < $recordId) { if (count($records) < $recordId) {
$this->maniaControl->chat->sendError('Cannot remove record $<$fff'.$recordId.'$>!', $player); $this->maniaControl->chat->sendError('Cannot remove record $<$fff' . $recordId . '$>!', $player);
return; return;
} }
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$query = "DELETE FROM `" . self::TABLE_RECORDS . "` WHERE `mapIndex` = ".$currentMap->index." AND `playerIndex` = ".$player->index.""; $query = "DELETE FROM `" . self::TABLE_RECORDS . "` WHERE `mapIndex` = " . $currentMap->index . " AND `playerIndex` = " . $player->index . "";
$mysqli->query($query); $mysqli->query($query);
if ($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return null; return;
} }
$this->maniaControl->callbackManager->triggerCallback(self::CB_LOCALRECORDS_CHANGED, null); $this->maniaControl->callbackManager->triggerCallback(self::CB_LOCALRECORDS_CHANGED, null);
$this->maniaControl->chat->sendInformation('Record no. $<$fff'.$recordId.'$> has been removed!'); $this->maniaControl->chat->sendInformation('Record no. $<$fff' . $recordId . '$> has been removed!');
} else { }
$this->maniaControl->chat->sendError('Cannot remove record $<$fff'.$recordId.'$>, because it\'s not an integer!', $player); else {
$this->maniaControl->chat->sendError('Cannot remove record $<$fff' . $recordId . '$>, because it\'s not an integer!', $player);
} }
} }
/** /**
* Shows a ManiaLink list with the local records. * Shows a ManiaLink list with the local records.
* *
* @param array $chat * @param array $chat
* @param Player $player * @param Player $player
*/ */
public function showRecordsList(array $chat, Player $player) { public function showRecordsList(array $chat, Player $player) {
$width = $this->maniaControl->manialinkManager->styleManager->getListWidgetsWidth(); $width = $this->maniaControl->manialinkManager->styleManager->getListWidgetsWidth();
$height = $this->maniaControl->manialinkManager->styleManager->getListWidgetsHeight(); $height = $this->maniaControl->manialinkManager->styleManager->getListWidgetsHeight();
// get PlayerList // get PlayerList
$records = $this->getLocalRecords($this->maniaControl->mapManager->getCurrentMap()); $records = $this->getLocalRecords($this->maniaControl->mapManager->getCurrentMap());
$pagesId = ''; $pagesId = '';
if (count($records) > 15) { if (count($records) > 15) {
$pagesId = 'RecordsListPages'; $pagesId = 'RecordsListPages';
} }
//create manialink // create manialink
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID); $maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
$script = $maniaLink->getScript(); $script = $maniaLink->getScript();
$paging = new Paging(); $paging = new Paging();
$script->addFeature($paging); $script->addFeature($paging);
// Main frame // Main frame
$frame = $this->maniaControl->manialinkManager->styleManager->getDefaultListFrame($script, $paging); $frame = $this->maniaControl->manialinkManager->styleManager->getDefaultListFrame($script, $paging);
$maniaLink->add($frame); $maniaLink->add($frame);
// Start offsets // Start offsets
$x = -$width / 2; $x = -$width / 2;
$y = $height / 2; $y = $height / 2;
// Predefine Description Label // Predefine Description Label
$descriptionLabel = $this->maniaControl->manialinkManager->styleManager->getDefaultDescriptionLabel(); $descriptionLabel = $this->maniaControl->manialinkManager->styleManager->getDefaultDescriptionLabel();
$frame->add($descriptionLabel); $frame->add($descriptionLabel);
// Headline // Headline
$headFrame = new Frame(); $headFrame = new Frame();
$frame->add($headFrame); $frame->add($headFrame);
$headFrame->setY($y - 5); $headFrame->setY($y - 5);
$array = array("Rank" => $x + 5, "Nickname" => $x + 18, "Login" => $x + 70, "Time" => $x + 101); $array = array("Rank" => $x + 5, "Nickname" => $x + 18, "Login" => $x + 70, "Time" => $x + 101);
$this->maniaControl->manialinkManager->labelLine($headFrame, $array); $this->maniaControl->manialinkManager->labelLine($headFrame, $array);
$i = 0; $i = 0;
$y = $height / 2 - 10; $y = $height / 2 - 10;
$pageFrames = array(); $pageFrames = array();
foreach($records as $listRecord) { foreach ($records as $listRecord) {
if (!isset($pageFrame)) { if (!isset($pageFrame)) {
$pageFrame = new Frame(); $pageFrame = new Frame();
$frame->add($pageFrame); $frame->add($pageFrame);
@ -434,13 +449,13 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
} }
array_push($pageFrames, $pageFrame); array_push($pageFrames, $pageFrame);
$y = $height / 2 - 10; $y = $height / 2 - 10;
$paging->addPage($pageFrame); $paging->addPage($pageFrame);
} }
$recordFrame = new Frame(); $recordFrame = new Frame();
$pageFrame->add($recordFrame); $pageFrame->add($recordFrame);
if ($i % 2 != 0) { if ($i % 2 != 0) {
$lineQuad = new Quad_BgsPlayerCard(); $lineQuad = new Quad_BgsPlayerCard();
$recordFrame->add($lineQuad); $recordFrame->add($lineQuad);
@ -448,27 +463,28 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig); $lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig);
$lineQuad->setZ(0.001); $lineQuad->setZ(0.001);
} }
if(strlen($listRecord->nickname) < 2) $listRecord->nickname = $listRecord->login; if (strlen($listRecord->nickname) < 2) $listRecord->nickname = $listRecord->login;
$array = array($listRecord->rank => $x + 5, '$fff'.$listRecord->nickname => $x + 18, $listRecord->login => $x + 70, Formatter::formatTime($listRecord->time) => $x + 101); $array = array($listRecord->rank => $x + 5, '$fff' . $listRecord->nickname => $x + 18, $listRecord->login => $x + 70,
Formatter::formatTime($listRecord->time) => $x + 101);
$this->maniaControl->manialinkManager->labelLine($recordFrame, $array); $this->maniaControl->manialinkManager->labelLine($recordFrame, $array);
$recordFrame->setY($y); $recordFrame->setY($y);
$y -= 4; $y -= 4;
$i++; $i++;
if ($i % 15 == 0) { if ($i % 15 == 0) {
unset($pageFrame); unset($pageFrame);
} }
} }
// Render and display xml // Render and display xml
$this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'PlayerList'); $this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'PlayerList');
} }
/** /**
* Get current checkpoint string for dedimania record * Get current checkpoint string for dedimania record
* *
* @param string $login * @param string $login
* @return string * @return string
*/ */
@ -477,8 +493,8 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
return null; return null;
} }
$string = ''; $string = '';
$count = count($this->checkpoints[$login]); $count = count($this->checkpoints[$login]);
foreach($this->checkpoints[$login] as $index => $check) { foreach ($this->checkpoints[$login] as $index => $check) {
$string .= $check; $string .= $check;
if ($index < $count - 1) { if ($index < $count - 1) {
$string .= ','; $string .= ',';
@ -489,7 +505,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
/** /**
* Build the local records manialink * Build the local records manialink
* *
* @return string * @return string
*/ */
private function buildManialink() { private function buildManialink() {
@ -497,37 +513,37 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
if (!$map) { if (!$map) {
return null; return null;
} }
$title = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_TITLE); $title = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_TITLE);
$pos_x = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_POSX); $pos_x = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_POSX);
$pos_y = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_POSY); $pos_y = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_POSY);
$width = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_WIDTH); $width = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_WIDTH);
$lines = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_LINESCOUNT); $lines = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_LINESCOUNT);
$lineHeight = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_LINEHEIGHT); $lineHeight = $this->maniaControl->settingManager->getSetting($this, self::SETTING_WIDGET_LINEHEIGHT);
$labelStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultLabelStyle(); $labelStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultLabelStyle();
$quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadStyle(); $quadStyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadStyle();
$quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadSubstyle(); $quadSubstyle = $this->maniaControl->manialinkManager->styleManager->getDefaultQuadSubstyle();
$records = $this->getLocalRecords($map); $records = $this->getLocalRecords($map);
if (!is_array($records)) { if (!is_array($records)) {
trigger_error("Couldn't fetch player records."); trigger_error("Couldn't fetch player records.");
return null; return null;
} }
$manialink = new ManiaLink(self::MLID_RECORDS); $manialink = new ManiaLink(self::MLID_RECORDS);
$frame = new Frame(); $frame = new Frame();
$manialink->add($frame); $manialink->add($frame);
$frame->setPosition($pos_x, $pos_y); $frame->setPosition($pos_x, $pos_y);
$backgroundQuad = new Quad(); $backgroundQuad = new Quad();
$frame->add($backgroundQuad); $frame->add($backgroundQuad);
$backgroundQuad->setVAlign(Control::TOP); $backgroundQuad->setVAlign(Control::TOP);
$adjustOuterBorder = $this->maniaControl->settingManager->getSetting($this, self::SETTING_ADJUST_OUTER_BORDER); $adjustOuterBorder = $this->maniaControl->settingManager->getSetting($this, self::SETTING_ADJUST_OUTER_BORDER);
$height = 7. + ($adjustOuterBorder ? count($records) : $lines) * $lineHeight; $height = 7. + ($adjustOuterBorder ? count($records) : $lines) * $lineHeight;
$backgroundQuad->setSize($width * 1.05, $height); $backgroundQuad->setSize($width * 1.05, $height);
$backgroundQuad->setStyles($quadStyle, $quadSubstyle); $backgroundQuad->setStyles($quadStyle, $quadSubstyle);
$backgroundQuad->setAction(self::ACTION_SHOW_RECORDSLIST); $backgroundQuad->setAction(self::ACTION_SHOW_RECORDSLIST);
$titleLabel = new Label(); $titleLabel = new Label();
$frame->add($titleLabel); $frame->add($titleLabel);
$titleLabel->setPosition(0, $lineHeight * -0.9); $titleLabel->setPosition(0, $lineHeight * -0.9);
@ -536,24 +552,23 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$titleLabel->setTextSize(2); $titleLabel->setTextSize(2);
$titleLabel->setText($title); $titleLabel->setText($title);
$titleLabel->setTranslate(true); $titleLabel->setTranslate(true);
// Times // Times
foreach($records as $index => $record) { foreach ($records as $index => $record) {
if ($index >= $lines) { if ($index >= $lines) {
break; break;
} }
$y = -8. - $index * $lineHeight; $y = -8. - $index * $lineHeight;
$recordFrame = new Frame(); $recordFrame = new Frame();
$frame->add($recordFrame); $frame->add($recordFrame);
$recordFrame->setPosition(0, $y); $recordFrame->setPosition(0, $y);
/*$backgroundQuad = new Quad(); /*
$recordFrame->add($backgroundQuad); * $backgroundQuad = new Quad(); $recordFrame->add($backgroundQuad); $backgroundQuad->setSize($width * 1.04, $lineHeight * 1.4); $backgroundQuad->setStyles($quadStyle, $quadSubstyle);
$backgroundQuad->setSize($width * 1.04, $lineHeight * 1.4); */
$backgroundQuad->setStyles($quadStyle, $quadSubstyle);*/
$rankLabel = new Label(); $rankLabel = new Label();
$recordFrame->add($rankLabel); $recordFrame->add($rankLabel);
$rankLabel->setHAlign(Control::LEFT); $rankLabel->setHAlign(Control::LEFT);
@ -563,7 +578,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$rankLabel->setTextPrefix('$o'); $rankLabel->setTextPrefix('$o');
$rankLabel->setText($record->rank); $rankLabel->setText($record->rank);
$rankLabel->setTextEmboss(true); $rankLabel->setTextEmboss(true);
$nameLabel = new Label(); $nameLabel = new Label();
$recordFrame->add($nameLabel); $recordFrame->add($nameLabel);
$nameLabel->setHAlign(Control::LEFT); $nameLabel->setHAlign(Control::LEFT);
@ -572,7 +587,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$nameLabel->setTextSize(1); $nameLabel->setTextSize(1);
$nameLabel->setText($record->nickname); $nameLabel->setText($record->nickname);
$nameLabel->setTextEmboss(true); $nameLabel->setTextEmboss(true);
$timeLabel = new Label(); $timeLabel = new Label();
$recordFrame->add($timeLabel); $recordFrame->add($timeLabel);
$timeLabel->setHAlign(Control::RIGHT); $timeLabel->setHAlign(Control::RIGHT);
@ -582,21 +597,21 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
$timeLabel->setText(Formatter::formatTime($record->time)); $timeLabel->setText(Formatter::formatTime($record->time));
$timeLabel->setTextEmboss(true); $timeLabel->setTextEmboss(true);
} }
return $manialink; return $manialink;
} }
/** /**
* Fetch local records for the given map * Fetch local records for the given map
* *
* @param Map $map * @param Map $map
* @param int $limit * @param int $limit
* @return array * @return array
*/ */
public function getLocalRecords(Map $map, $limit = -1) { public function getLocalRecords(Map $map, $limit = -1) {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$limit = ($limit > 0 ? "LIMIT " . $limit : ""); $limit = ($limit > 0 ? "LIMIT " . $limit : "");
$query = "SELECT * FROM ( $query = "SELECT * FROM (
SELECT recs.*, @rank := @rank + 1 as `rank` FROM `" . self::TABLE_RECORDS . "` recs, (SELECT @rank := 0) ra SELECT recs.*, @rank := @rank + 1 as `rank` FROM `" . self::TABLE_RECORDS . "` recs, (SELECT @rank := 0) ra
WHERE recs.`mapIndex` = {$map->index} WHERE recs.`mapIndex` = {$map->index}
ORDER BY recs.`time` ASC ORDER BY recs.`time` ASC
@ -609,7 +624,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
return null; return null;
} }
$records = array(); $records = array();
while($record = $result->fetch_object()) { while ($record = $result->fetch_object()) {
array_push($records, $record); array_push($records, $record);
} }
$result->free(); $result->free();
@ -618,14 +633,14 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
/** /**
* Retrieve the local record for the given map and login * Retrieve the local record for the given map and login
* *
* @param Map $map * @param Map $map
* @param Player $player * @param Player $player
* @return mixed * @return mixed
*/ */
private function getLocalRecord(Map $map, Player $player) { private function getLocalRecord(Map $map, Player $player) {
$mysqli = $this->maniaControl->database->mysqli; $mysqli = $this->maniaControl->database->mysqli;
$query = "SELECT records.* FROM ( $query = "SELECT records.* FROM (
SELECT recs.*, @rank := @rank + 1 as `rank` FROM `" . self::TABLE_RECORDS . "` recs, (SELECT @rank := 0) ra SELECT recs.*, @rank := @rank + 1 as `rank` FROM `" . self::TABLE_RECORDS . "` recs, (SELECT @rank := 0) ra
WHERE recs.`mapIndex` = {$map->index} WHERE recs.`mapIndex` = {$map->index}
ORDER BY recs.`time` ASC) records ORDER BY recs.`time` ASC) records
@ -640,4 +655,3 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList
return $record; return $record;
} }
} }