From 97bcaa494f1e47ba13d12aaf1c11d9e8edcf32d8 Mon Sep 17 00:00:00 2001 From: Beu Date: Fri, 8 Jan 2021 22:48:20 +0100 Subject: [PATCH] Add GuestlistManager --- Beu/GuestlistManager.php | 218 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 Beu/GuestlistManager.php diff --git a/Beu/GuestlistManager.php b/Beu/GuestlistManager.php new file mode 100644 index 0000000..7fcbd81 --- /dev/null +++ b/Beu/GuestlistManager.php @@ -0,0 +1,218 @@ +maniaControl = $maniaControl;iaControl\Plugins\Plugin::getAuthor() + */ + public static function getAuthor() { + return self::PLUGIN_AUTHOR; + } + + /** + * @see \ManiaControl\Plugins\Plugin::getDescription() + */ + public static function getDescription() { + return 'Tool to manage the Guestlist'; + } + + /** + * @see \ManiaControl\Plugins\Plugin::load() + */ + public function load(ManiaControl $maniaControl) { + $this->maniaControl = $maniaControl; + + $this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_GUESTLIST_FILE, "guestlist.txt", 'guestlist file'); + $this->maniaControl->getCommandManager()->registerCommandListener('addalltogl', $this, 'doaddalltogl', true, 'Add all connected players to the guestlist'); + $this->maniaControl->getCommandManager()->registerCommandListener('addtogl', $this, 'doaddtogl', true, 'Add someone to the guestlist'); + $this->maniaControl->getCommandManager()->registerCommandListener('savegl', $this, 'dosavegl', true, 'Save the guestlist'); + $this->maniaControl->getCommandManager()->registerCommandListener('loadgl', $this, 'doloadgl', true, 'Load the guestlist'); + $this->maniaControl->getCommandManager()->registerCommandListener('cleangl', $this, 'docleangl', true, 'Clean the guestlist'); + + return true; + } + + /** + * @see \ManiaControl\Plugins\Plugin::unload() + */ + public function unload() { + } + + /** + * Add all connected players to the guestlist + * + * @param array $chat + * @param \ManiaControl\Players\Player $player + */ + public function doaddalltogl(Array $chat, Player $player) { + $players = $this->maniaControl->getPlayerManager()->getPlayers(); + $guestlist = $this->maniaControl->getClient()->getGuestList(); + $i = 0; + foreach ($players as &$index) { + if ($this->addLoginToGL($index->login, $guestlist)) { + $i++; + } + } + $this->maniaControl->getChat()->sendSuccess( "All connected players have been added to the Guestlist"); + $this->maniaControl->getChat()->sendSuccess( "Added: " . $i . "/" . count($players), $player); + } + + /** + * Add players to the guestlist + * + * @param array $chat + * @param \ManiaControl\Players\Player $player + */ + public function doaddtogl(Array $chat, Player $player) { + $command = explode(" ", $chat[1][2]); + $peopletoadd = $command[1]; + + if (empty($peopletoadd)) { + $this->maniaControl->getChat()->sendError("You must set the nickname as argument", $player); + } else { + $mysqli = $this->maniaControl->getDatabase()->getMysqli(); + $query = 'SELECT login FROM `' . PlayerManager::TABLE_PLAYERS . '` WHERE nickname LIKE "' . $peopletoadd . '"'; + $result = $mysqli->query($query); + $array = mysqli_fetch_array($result); + + if (isset($array[0])) { + $login = $array[0]; + } elseif (strlen($peopletoadd) == 22) { + $login = $peopletoadd ; + } + if ($mysqli->error) { + trigger_error($mysqli->error, E_USER_ERROR); + } + + if (!isset($login)) { + $this->maniaControl->getChat()->sendError( "Login not found. FYI The player must be connected" , $player); + } else { + if ($this->addLoginToGL($login)) { + $this->maniaControl->getChat()->sendSuccess( "Player " . $peopletoadd . " added to the Guestlist" , $player); + } else { + $this->maniaControl->getChat()->sendSuccess( "Player " . $peopletoadd . " already in the Guestlist" , $player); + } + } + } + } + + /** + * Add login to the guestlist + * + * @param string $login + * @param array $guestlist + */ + public function addLoginToGL(String $login, array $guestlist = []) { + if (empty($guestlist)) { + $guestlist = $this->maniaControl->getClient()->getGuestList(); + } + $logintoadd = ""; + $logintoadd = array_search($login ,array_column($guestlist, 'login')); + if (strlen($logintoadd) == 0) { + $this->maniaControl->getClient()->addGuest($login); + return true; + } else { + return false; + } + } + + /** + * load from the guestlist file + * + * @param array $chat + * @param \ManiaControl\Players\Player $player + */ + public function doloadgl(Array $chat, Player $player) { + if (!empty(self::SETTING_GUESTLIST_FILE)) { + $this->maniaControl->getClient()->loadGuestList($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_GUESTLIST_FILE)); + $this->maniaControl->getChat()->sendSuccess( "Guestlist loaded!" , $player); + } else { + $this->maniaControl->getChat()->sendError( "Guestlist option empty" , $player); + } + } + + /** + * save to the guestlist file + * + * @param array $chat + * @param \ManiaControl\Players\Player $player + */ + public function dosavegl(Array $chat, Player $player) { + if (!empty(self::SETTING_GUESTLIST_FILE)) { + $this->maniaControl->getClient()->saveGuestList($this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_GUESTLIST_FILE)); + $this->maniaControl->getChat()->sendSuccess( "Guestlist saved!" , $player); + } else { + $this->maniaControl->getChat()->sendError( "Guestlist option empty" , $player); + } + } + + /** + * clean the guestlist + * + * @param array $chat + * @param \ManiaControl\Players\Player $player + */ + public function docleangl(Array $chat, Player $player) { + $this->maniaControl->getClient()->cleanGuestList(); + $this->maniaControl->getChat()->sendSuccess( "Guestlist cleaned!" , $player); + } +}