From f25eec7a0ba4a8a868eb87c76ec8b88f76f3a124 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Thu, 20 Mar 2025 22:38:46 +0800 Subject: feat: inputManager and commandHandler (#7) server now have ability to send chat like any clients. can also run commands(registered in commands.cpp) --- src/inputManager.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/inputManager.cpp (limited to 'src/inputManager.cpp') diff --git a/src/inputManager.cpp b/src/inputManager.cpp new file mode 100644 index 0000000..57b7b06 --- /dev/null +++ b/src/inputManager.cpp @@ -0,0 +1,54 @@ +#include "inputManager.hpp" +#include "chatManager.hpp" +#include "commandHandler.hpp" +#include "log.hpp" +#include + +CInputManager::CInputManager() { + log(LOG, "InputManager: initialized"); +} + +CInputManager::~CInputManager() { + log(SYS, "InputManager: bye"); +} + +void CInputManager::run() { + inputLoop(); +} + +static void handleCommand(std::string &input) { + input = input.substr(1); + + std::string command; + std::string args; + + size_t spacePos = input.find(' '); + if (spacePos == std::string::npos) { + command = input; + args = ""; + } else { + command = input.substr(0, spacePos); + // skip extra spaces + size_t startPos = input.find_first_not_of(' ', spacePos); + if (startPos != std::string::npos) + args = input.substr(startPos); + } + + args.empty() ? log(TRACE, "Spawning Command: {}", command) : log(TRACE, "Spawning Command: {}, Args: {}", command, args); + const auto exe = g_pCommandHandler->newCommand(command, args); + exe.good ? exe.result.empty() ? log(LOG, "Command {} succeeded", command) : log(LOG, "Command {} succeeded: {}", command, exe.result) : exe.result.empty() ? log(ERR, "Command {} failed", command) + : log(ERR, "Command {} failed: {}", command, exe.result); +}; + +void CInputManager::inputLoop() { + while (true) { + std::string input; + std::print("> "); + std::getline(std::cin, input); + + if (input.empty()) + continue; + + g_pCommandHandler->isCommand(input) ? handleCommand(input) : g_pChatManager->broadcastMessage({.msg = input, .username = "Server"}); + } +} -- cgit v1.2.3