diff --git a/.gitignore b/.gitignore index 7fd62ba..0434a9e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ !MatchManagerSuite/* !Beu !Beu/AFKNotifier.php +!Beu/AutomaticMapSwitcher.php !Beu/BeuCustomConfig.php !Beu/BeuDonationButton.php !Beu/BlacklistManager.php diff --git a/Beu/AutomaticMapSwitcher.php b/Beu/AutomaticMapSwitcher.php new file mode 100644 index 0000000..c9dfa57 --- /dev/null +++ b/Beu/AutomaticMapSwitcher.php @@ -0,0 +1,144 @@ +maniaControl = $maniaControl; + + $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SCHEDULE, '', 'format: "timestamp:mapuid,timestamp:mapuid"'); + $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_DEBOUCEMAPCHANGEDELAY, 30, 'delay between 2 map change requests', 110); + + $this->maniaControl->getTimerManager()->registerTimerListening($this, 'handle1Second', 1000); + } + + /** + * @see \ManiaControl\Plugins\Plugin::unload() + */ + public function unload() { + } + + /** + * handle1Second + * @return void + */ + public function handle1Second() { + $now = time(); + if ($this->debounceMapChangeTime > $now) return; + + $schedule = trim($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SCHEDULE)); + if ($schedule === '') return; + + + $matchingTimestamp = -1; + $matchingMapUid = ''; + foreach (explode(',', $schedule) as $pair) { + list($timestampText, $mapuid) = explode(':', $pair); + + if (!is_numeric($timestampText)) { + $this->maniaControl->getChat()->sendErrorToAdmins('invalid timestamp in pair: '. $pair); + Logger::logWarning('invalid timestamp in pair: '. $pair); + continue; + } + $timestamp = intval($timestampText); + + if ($this->maniaControl->getMapManager()->getMapByUid($mapuid) === null) { + $this->maniaControl->getChat()->sendErrorToAdmins('map not loaded in pair: '. $pair); + Logger::logWarning('map not loaded in pair: '. $pair); + continue; + } + + if ($matchingTimestamp < $timestamp && $now > $timestamp) { + $matchingTimestamp = $timestamp; + $matchingMapUid = $mapuid; + } + + } + + if ($matchingMapUid !== '' && $matchingMapUid !== $this->maniaControl->getMapManager()->getCurrentMap()->uid) { + $this->debounceMapChangeTime = time() + $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_DEBOUCEMAPCHANGEDELAY); + + $nextmap = $this->maniaControl->getMapManager()->getMapByUid($matchingMapUid); + + $this->maniaControl->getChat()->sendSuccess('Automatic switching to map: $z'. $nextmap->name); + Logger::logWarning('Automatic switching to map: '. $matchingMapUid); + $this->maniaControl->getMapManager()->getMapActions()->skipToMapByUid($matchingMapUid); + } + } +} \ No newline at end of file