summaryrefslogtreecommitdiff
path: root/src/inputManager.cpp
blob: 57b7b06d2ba2d21d328ec476c667c22bfca87df6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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"});
	}
}