From 9a9db1a6e136bdb02fce1c57218997f37410cd27 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Sun, 8 Mar 2020 23:08:18 +0100 Subject: [PATCH] [ChatCommandHandler] Added. --- server/source/network/ChatCommandHandler.cpp | 84 +++++++++++++++++++ server/source/network/ChatCommandHandler.hpp | 48 +++++++++++ .../source/network/ServerCommandHandler.cpp | 38 ++------- .../source/network/ServerCommandHandler.hpp | 6 ++ 4 files changed, 143 insertions(+), 33 deletions(-) create mode 100644 server/source/network/ChatCommandHandler.cpp create mode 100644 server/source/network/ChatCommandHandler.hpp diff --git a/server/source/network/ChatCommandHandler.cpp b/server/source/network/ChatCommandHandler.cpp new file mode 100644 index 00000000..3d582b2a --- /dev/null +++ b/server/source/network/ChatCommandHandler.cpp @@ -0,0 +1,84 @@ +/* + * ===================================================================================== + * + * OpenMiner + * + * Copyright (C) 2018-2020 Unarelith, Quentin Bazin + * Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md) + * + * This file is part of OpenMiner. + * + * OpenMiner is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * OpenMiner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with OpenMiner; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * ===================================================================================== + */ +#include +#include + +#include "ChatCommandHandler.hpp" +#include "ClientInfo.hpp" +#include "ServerCommandHandler.hpp" + +void ChatCommandHandler::parseCommand(const std::string &str, ClientInfo &client) const { + std::stringstream sstream; + sstream << str; + + std::vector command; + std::string line; + while (std::getline(sstream, line, ' ')) { + command.emplace_back(line); + } + + std::unordered_map commands = { + {"tp", &ChatCommandHandler::teleportationCommand} + }; + + if (!command.empty()) { + auto it = commands.find(command.at(0)); + if (it != commands.end()) { + (this->*it->second)(command, client); + } + else { + m_server.sendChatMessage(0, "Unrecognized command: " + command.at(0)); + + std::stringstream commandList; + for (auto &it : commands) { + if (commandList.tellp() != std::streampos(0)) + commandList << ", "; + commandList << it.first; + } + + m_server.sendChatMessage(0, "Available commands are: " + commandList.str()); + } + } +} + +void ChatCommandHandler::teleportationCommand(const std::vector &command, ClientInfo &client) const { + if (command.size() != 4) { + m_server.sendChatMessage(0, "Usage: /tp x y z", &client); + } + else { + s32 x = std::stoi(command.at(1)); + s32 y = std::stoi(command.at(2)); + s32 z = std::stoi(command.at(3)); + + m_server.setPlayerPosition(client.id, x, y, z); + + m_server.sendPlayerPosUpdate(client.id, true); + + m_server.sendChatMessage(0, "Teleported to " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(z), &client); + } +} + diff --git a/server/source/network/ChatCommandHandler.hpp b/server/source/network/ChatCommandHandler.hpp new file mode 100644 index 00000000..46374f53 --- /dev/null +++ b/server/source/network/ChatCommandHandler.hpp @@ -0,0 +1,48 @@ +/* + * ===================================================================================== + * + * OpenMiner + * + * Copyright (C) 2018-2020 Unarelith, Quentin Bazin + * Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md) + * + * This file is part of OpenMiner. + * + * OpenMiner is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * OpenMiner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with OpenMiner; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * ===================================================================================== + */ +#ifndef CHATCOMMANDHANDLER_HPP_ +#define CHATCOMMANDHANDLER_HPP_ + +#include +#include + +class ClientInfo; +class ServerCommandHandler; + +class ChatCommandHandler { + public: + ChatCommandHandler(ServerCommandHandler &server) : m_server(server) {} + + void parseCommand(const std::string &str, ClientInfo &client) const; + + private: + void teleportationCommand(const std::vector &command, ClientInfo &client) const; + + ServerCommandHandler &m_server; +}; + +#endif // CHATCOMMANDHANDLER_HPP_ diff --git a/server/source/network/ServerCommandHandler.cpp b/server/source/network/ServerCommandHandler.cpp index d67c8653..bd0ee46f 100644 --- a/server/source/network/ServerCommandHandler.cpp +++ b/server/source/network/ServerCommandHandler.cpp @@ -253,44 +253,16 @@ void ServerCommandHandler::setupCallbacks() { else sendChatMessage(clientID, message); } - // FIXME: Do a proper implementation later else { - std::stringstream sstream; - sstream << message.substr(1); - - std::vector command; - std::string line; - while (std::getline(sstream, line, ' ')) { - command.emplace_back(line); - } - - if (!command.empty()) { - if (command.at(0) == "tp") { - if (command.size() != 4) { - // FIXME: ID 0 should be server messages - sendChatMessage(0, "Usage: /tp x y z", &client); - } - else { - s32 x = std::stoi(command.at(1)); - s32 y = std::stoi(command.at(2)); - s32 z = std::stoi(command.at(3)); - - m_players.at(clientID).setPosition(x, y, z); - - sendPlayerPosUpdate(clientID, true); - - sendChatMessage(0, "Teleported to " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(z), &client); - } - } - else { - sendChatMessage(0, "Unrecognized command: " + command.at(0)); - sendChatMessage(0, "Available commands are: tp"); - } - } + m_chatCommandHandler.parseCommand(message.substr(1), client); } }); } +void ServerCommandHandler::setPlayerPosition(u16 clientID, s32 x, s32 y, s32 z) { + m_players.at(clientID).setPosition(x, y, z); +} + inline ServerWorld &ServerCommandHandler::getWorldForClient(u16 clientID) { auto it = m_players.find(clientID); if (it == m_players.end()) diff --git a/server/source/network/ServerCommandHandler.hpp b/server/source/network/ServerCommandHandler.hpp index 6fbcddd5..ff3d44d0 100644 --- a/server/source/network/ServerCommandHandler.hpp +++ b/server/source/network/ServerCommandHandler.hpp @@ -31,6 +31,8 @@ #include +#include "ChatCommandHandler.hpp" + struct BlockData; class ClientInfo; @@ -55,6 +57,8 @@ class ServerCommandHandler { void setupCallbacks(); + void setPlayerPosition(u16 clientID, s32 x, s32 y, s32 z); + const Server &server() const { return m_server; } private: @@ -69,6 +73,8 @@ class ServerCommandHandler { Registry &m_registry; gk::Vector3d m_spawnPosition{14.5, 14.5, 18.}; + + ChatCommandHandler m_chatCommandHandler{*this}; }; #endif // SERVERCOMMANDHANDLER_HPP_