From ad4a06bbd01e80440f3fd2eacdbc69fff1261791 Mon Sep 17 00:00:00 2001 From: Beu Date: Mon, 20 Sep 2021 20:05:33 +0200 Subject: [PATCH] fix renew token with multiple servers --- Beu/SimpleChatColorer.php | 177 +++++++++++++++++++++++ MatchManagerSuite/MatchManagerGSheet.php | 3 +- 2 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 Beu/SimpleChatColorer.php diff --git a/Beu/SimpleChatColorer.php b/Beu/SimpleChatColorer.php new file mode 100644 index 0000000..227dd7e --- /dev/null +++ b/Beu/SimpleChatColorer.php @@ -0,0 +1,177 @@ +maniaControl = $maniaControl; + $this->maniaControl->getCallbackManager()->registerCallbackListener('ManiaPlanet.PlayerChat', $this, 'handlePlayerChat'); + $this->maniaControl->getCallbackManager()->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'updateSettings'); + + $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_CHATDAMDMINCOLORER_USEADMINCOLOR, true, "Use Admin Color of Maniacontrol settings"); + $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_CHATDAMDMINCOLORER_NUMBEROFGROUPS, 1, "Nomber of groups to setup"); + + $this->InitGroupsSettings(); + + try { + $this->maniaControl->getClient()->chatEnableManualRouting(); + $this->enabled = true; + } catch (\Exception $ex) { + $this->enabled = false; + echo "error! \n"; + } + } + + /** + * Update Widgets on Setting Changes + * + * @param Setting $setting + */ + public function updateSettings(Setting $setting) { + if ($setting->belongsToClass($this)) { + $this->InitGroupsSettings(); + } + } + + private function InitGroupsSettings() { + $i = 1; + $nbofgroups = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_CHATDAMDMINCOLORER_NUMBEROFGROUPS); + $this->groups = []; + + for ($i; $i <= $nbofgroups; $i++) { + $this->maniaControl->getSettingManager()->initSetting($this, "Group " . $i . " prefix", "", "Chat prefix of the group one"); + $this->maniaControl->getSettingManager()->initSetting($this, "Group " . $i . " players login", "", "Comma separated players login"); + + $this->groups[$this->maniaControl->getSettingManager()->getSettingValue($this, "Group " . $i . " prefix")] = explode(',', str_replace(' ', '', $this->maniaControl->getSettingManager()->getSettingValue($this, "Group " . $i . " players login"))); + } + + $allsettings = $this->maniaControl->getSettingManager()->getSettingsByClass($this); + foreach ($allsettings as $key => $value) { + $name = $value->setting; + preg_match('/^Group (\d*) prefix$/', $name, $match); + if (count($match) > 0 && $match[1] > $nbofgroups) { + $this->maniaControl->getSettingManager()->deleteSetting($this, "Group " . $match[1] . " prefix"); + $this->maniaControl->getSettingManager()->deleteSetting($this, "Group " . $match[1] . " players login"); + } + } + } + + + public function handlePlayerChat($callback) { + $playerUid = $callback[1][0]; + $login = $callback[1][1]; + $text = $callback[1][2]; + + if ($playerUid != 0 && substr($text, 0, 1) != "/" && $this->enabled) { + $source_player = $this->maniaControl->getPlayerManager()->getPlayer($login); + if ($source_player == null) { + return; + } + $nick = $source_player->nickname; + $authLevel = $source_player->authLevel; + + $prefix = ""; + + if ($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_CHATDAMDMINCOLORER_USEADMINCOLOR) && $authLevel > 0) { + $prefix = $this->maniaControl->getColorManager()->getColorByLevel($authLevel); + } else { + foreach ($this->groups as $groupprefix => $players) { + if (in_array($login, $players)) { + $prefix = $groupprefix; + break; + } + } + } + + try { + $this->maniaControl->getClient()->chatSendServerMessage('[$<' . $prefix . $nick . '$>] ' . $text); + } catch (\Exception $e) { + echo "error while sending chat message to $login: " . $e->getMessage() . "\n"; + } + } + } + + /** + * Unload the plugin and its Resources + */ + public function unload() { + $this->maniaControl->getClient()->chatEnableManualRouting(false); + $this->maniaControl->getCallbackManager()->unregisterCallbackListening('ManiaPlanet.OnPlayerChat', $this); + } +} + diff --git a/MatchManagerSuite/MatchManagerGSheet.php b/MatchManagerSuite/MatchManagerGSheet.php index 6c8332d..cfbef3c 100644 --- a/MatchManagerSuite/MatchManagerGSheet.php +++ b/MatchManagerSuite/MatchManagerGSheet.php @@ -35,7 +35,7 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin { * Constants */ const PLUGIN_ID = 156; - const PLUGIN_VERSION = 0.4; + const PLUGIN_VERSION = 0.5; const PLUGIN_NAME = 'MatchManager GSheet'; const PLUGIN_AUTHOR = 'Beu'; @@ -287,6 +287,7 @@ class MatchManagerGSheet implements CallbackListener, CommandListener, Plugin { private function refreshTokenIfNeeded() { Logger::Log('refreshTokenIfNeeded'); + $this->access_token = $this->getSecretSetting("access_token"); $expire = $this->getSecretSetting("expire"); $refreshtoken = $this->getSecretSetting("refresh_token"); $clientid = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_MATCHMANAGERGSHEET_CLIENT_ID);