[ChatCommandHandler] Added.

This commit is contained in:
Quentin Bazin 2020-03-08 23:08:18 +01:00
parent 9f457b6718
commit 9a9db1a6e1
4 changed files with 143 additions and 33 deletions

View File

@ -0,0 +1,84 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
* 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 <functional>
#include <sstream>
#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<std::string> command;
std::string line;
while (std::getline(sstream, line, ' ')) {
command.emplace_back(line);
}
std::unordered_map<std::string, decltype(&ChatCommandHandler::teleportationCommand)> 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<std::string> &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);
}
}

View File

@ -0,0 +1,48 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
* 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 <string>
#include <vector>
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<std::string> &command, ClientInfo &client) const;
ServerCommandHandler &m_server;
};
#endif // CHATCOMMANDHANDLER_HPP_

View File

@ -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<std::string> 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())

View File

@ -31,6 +31,8 @@
#include <gk/core/Vector3.hpp>
#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_