From a125deaf8de2c3420a7de40345597ed3d9bbd884 Mon Sep 17 00:00:00 2001 From: Max Klaversma Date: Sun, 27 Apr 2014 11:02:46 +0200 Subject: [PATCH] Now saving checkpoints for localrecords --- application/plugins/LocalRecords.php | 56 ++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/application/plugins/LocalRecords.php b/application/plugins/LocalRecords.php index 9307f541..1e0a9e16 100644 --- a/application/plugins/LocalRecords.php +++ b/application/plugins/LocalRecords.php @@ -56,6 +56,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList */ private $maniaControl = null; private $updateManialink = false; + private $checkpoints = array(); /** * Prepares the Plugin @@ -92,6 +93,7 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList $this->maniaControl->callbackManager->registerCallbackListener(MapManager::CB_BEGINMAP, $this, 'handleMapBegin'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERFINISH, $this, 'handlePlayerFinish'); $this->maniaControl->callbackManager->registerCallbackListener(PlayerManager::CB_PLAYERCONNECT, $this, 'handlePlayerConnect'); + $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_TM_PLAYERCHECKPOINT, $this, 'handlePlayerCheckpoint'); $this->maniaControl->callbackManager->registerCallbackListener(SettingManager::CB_SETTINGS_CHANGED, $this, 'handleSettingsChanged'); $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer'); $this->maniaControl->commandManager->registerCommandListener('records', $this, 'showRecordsList'); @@ -127,6 +129,13 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList if ($mysqli->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"); + if ($mysqli->error) { + if(!strstr($mysqli->error, 'Duplicate')) { + trigger_error($mysqli->error, E_USER_ERROR); + } + } } /** @@ -202,6 +211,23 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList } } + /** + * Handle PlayerCheckpoint callback + * + * @param $callback + */ + public function handlePlayerCheckpoint($callback) { + $data = $callback[1]; + $login = $data[1]; + $time = $data[2]; + //$lap = $data[3]; + $cpIndex = $data[4]; + if (!isset($this->checkpoints[$login]) || $cpIndex <= 0) { + $this->checkpoints[$login] = array(); + } + $this->checkpoints[$login][$cpIndex] = $time; + } + /** * Handle PlayerConnect callback * @@ -262,13 +288,16 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList $query = "INSERT INTO `" . self::TABLE_RECORDS . "` ( `mapIndex`, `playerIndex`, - `time` + `time`, + `checkpoints` ) VALUES ( {$map->index}, {$player->index}, - {$time} + {$time}, + '{$this->getCheckpoints($player->login)}' ) ON DUPLICATE KEY UPDATE - `time` = VALUES(`time`);"; + `time` = VALUES(`time`), + `checkpoints` = VALUES(`checkpoints`);"; $mysqli->query($query); if ($mysqli->error) { trigger_error($mysqli->error); @@ -429,6 +458,27 @@ class LocalRecordsPlugin implements CallbackListener, CommandListener, TimerList $this->maniaControl->manialinkManager->displayWidget($maniaLink, $player, 'PlayerList'); } + /** + * Get current checkpoint string for dedimania record + * + * @param string $login + * @return string + */ + private function getCheckpoints($login) { + if (!$login || !isset($this->checkpoints[$login])) { + return null; + } + $string = ''; + $count = count($this->checkpoints[$login]); + foreach($this->checkpoints[$login] as $index => $check) { + $string .= $check; + if ($index < $count - 1) { + $string .= ','; + } + } + return $string; + } + /** * Build the local records manialink *