summaryrefslogtreecommitdiff
path: root/src/inputManager.cpp
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-03-20 22:38:46 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-03-22 15:02:46 +0800
commitf25eec7a0ba4a8a868eb87c76ec8b88f76f3a124 (patch)
treea3f7c6e07b895327beee62d5f83602b6b083df33 /src/inputManager.cpp
parent45ca296a55332a4d79319f8a706f332aa8feb336 (diff)
feat: inputManager and commandHandler (#7)
server now have ability to send chat like any clients. can also run commands(registered in commands.cpp)
Diffstat (limited to 'src/inputManager.cpp')
-rw-r--r--src/inputManager.cpp54
1 files changed, 54 insertions, 0 deletions
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 <iostream>
+
+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"});
+ }
+}