-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminManager.cpp
More file actions
128 lines (120 loc) · 3.56 KB
/
Copy pathAdminManager.cpp
File metadata and controls
128 lines (120 loc) · 3.56 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "AdminManager.hpp"
#include <unistd.h>
#include "Server.hpp"
AdminManager::AdminManager(BaseConnectionManager & connections, Server* serv) :
SubConnectionManager(_inbox, _outbox, connections), _server(serv),_admin(NULL), _admin_peer_id()
{
makeDefaultAdmin();
}
/** Creates an admin account */
void AdminManager::makeDefaultAdmin(){
User admin(ADMIN_USERNAME,ADMIN_PASSWORD);
MemoryAccess::saveAdmin(admin);
}
/** Login handlers */
void AdminManager::loginAdmin(const JSON::Dict& data, int peer_id){
JSON::Dict response;
if (ISSTR(data.get(net::MSG::USERNAME)) && ISSTR(data.get(net::MSG::PASSWORD))){
std::string const & username = STR(data.get(MSG::USERNAME));
std::string const & password = STR(data.get(MSG::PASSWORD));
response.set("type", MSG::STATUS);
if (! adminIsLogged()){
_admin = load(username);
if(_admin != NULL){
if(_admin->getPassword() == password){
response.set("data",net::MSG::USER_LOGIN);
_admin_peer_id = peer_id;
std::cout << "[" << this << "] \033[1m" << peer_id
<< "\033[36m admin connected\033[0m" << std::endl;
}
else{
response.set("data",net::MSG::PASSWORD_ERROR);
delete _admin;
_admin = NULL;
}
}
else
response.set("data",net::MSG::USER_NOT_FOUND);
}
else
response.set("data",net::MSG::ALREADY_LOGGED_IN);
}
_doWrite(peer_id, &response);
}
void AdminManager::logoutAdmin(){
delete _admin;
_admin = NULL;
std::cout << "[" << this << "] \033[1m" << _admin_peer_id
<< "\033[36m admin disconnected\033[0m" << std::endl;
releaseClient(_admin_peer_id);
stop();
}
bool AdminManager::adminIsLogged(){
return _admin != NULL;
}
User* AdminManager::load(std::string username){
User* admin = new User(username);
try{
MemoryAccess::loadAdmin(*admin);
return admin;
}
catch(const JSON::IOError& e){
return NULL;
}
}
void AdminManager::_mainloop_out(){
std::cout << "[" << this << "] \033[31m\033[1mAdmin server start\033[0m" << endl;
while (isRunning() || _inbox.available()){
Message const & msg = _inbox.pop();
if (ISDICT(msg.data))
treatAdminMessage(msg);
delete msg.data;
}
std::cout << "[" << this << "] \033[31m\033[1mAdmin server stop\033[0m" << endl;
}
/* Admin messages handler */
void AdminManager::treatAdminMessage(const Message &message){
if (ISDICT(message.data)){
JSON::Dict const &received = DICT(message.data);
if (ISSTR(received.get("type"))) {
string messageType = STR(received.get("type")).value();
if(messageType == net::MSG::DISCONNECT){
logoutAdmin();
}
if(ISDICT(received.get("data"))){
if(messageType == net::MSG::ADMIN_LOGIN){
loginAdmin(DICT(received.get("data")),message.peer_id);
}
else if(messageType == net::MSG::CHAMPIONSHIP_CREATION){
createChampionship(DICT(received.get("data")),message.peer_id);
}
}
}
}
}
/* Requests */
void AdminManager::createChampionship(const JSON::Dict& data, int peer_id){
JSON::Dict response;
if(peer_id != _admin_peer_id){
response.set("type","WUT HACKER");
response.set("data","YOU AINT ADMIN WTF");
}
else{
response.set("type",net::MSG::CHAMPIONSHIP_CREATION);
Championship champ(
INT(data.get(net::MSG::TURN_NUMBER)),
STR(data.get(net::MSG::CHAMPIONSHIP_NAME)),
INT(data.get(net::MSG::CHAMPIONSHIP_CASHPRIZE)),
INT(data.get(net::MSG::CHAMPIONSHIP_FAME))
);
try{
MemoryAccess::load(champ);
response.set("data",net::MSG::CHAMPIONSHIP_ALREADY_EXISTS);
}
catch(const JSON::IOError& e){
response.set("data",net::MSG::CHAMPIONSHIP_CREATED);
_server->addChampionship(champ);
}
}
_doWrite(peer_id,&response);
}