This repository has been archived on 2023-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
defend-together/server/console.hpp

84 lines
1.7 KiB
C++
Raw Permalink Normal View History

2020-05-01 01:16:46 +02:00
#ifndef CONSOLE_HPP
#define CONSOLE_HPP
#include <pthread.h>
#include <iostream>
2020-05-01 06:12:57 +02:00
#include <string>
2020-05-01 01:16:46 +02:00
#include <cstring>
2020-05-04 10:10:19 +02:00
#include "gameserver.hpp"
2020-05-01 01:16:46 +02:00
class ServerConsole
{
public:
ServerConsole();
void stop(void);
bool is_running(void);
private:
pthread_t thread;
};
void * console_logic(void *)
{
2020-05-01 06:12:57 +02:00
std::cout << "Started server console..." << std::endl;
std::string input_string;
2020-05-01 01:16:46 +02:00
2020-05-04 10:10:19 +02:00
while(game_is_running)
2020-05-01 01:16:46 +02:00
{
if(std::getline(std::cin, input_string))
2020-05-01 06:12:57 +02:00
{
if(input_string == "stop")
{
2020-05-04 10:10:19 +02:00
game_is_running = false;
}
2020-05-05 05:09:39 +02:00
else if (input_string.length() > 2 && input_string.substr(0,3) == "say")
{
if(input_string.length() > 4)
{
gameserver::BroadcastMessage("Server: " + input_string.substr(4));
}
else
{
std::cout << "Must pass a valid message!" << std::endl;
}
}
else
{
2020-05-05 05:09:39 +02:00
std::cout << std::endl
<< "Invalid console command!" << std::endl
<< "Valid commands are:" << std::endl
<< "stop" << std::endl
<< "say [message]" << std::endl;
}
2020-05-01 01:16:46 +02:00
}
}
return 0;
}
ServerConsole::ServerConsole(void)
{
//Intialized values
2020-05-04 10:10:19 +02:00
game_is_running = true;
2020-05-01 01:16:46 +02:00
thread = pthread_t();
pthread_create(&(this->thread), NULL, console_logic, NULL);
}
void ServerConsole::stop(void)
{
if(this->thread != 0)
{
pthread_join(this->thread, NULL);
}
}
bool ServerConsole::is_running(void)
{
2020-05-04 10:10:19 +02:00
return game_is_running;
2020-05-01 01:16:46 +02:00
}
#endif