-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.cpp
213 lines (179 loc) · 5.2 KB
/
core.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "core.h"
#include <thread>
#include <QThread>
#include "config.h"
#include "helpers/logger.h"
using namespace ks;
using namespace ks::messages;
namespace koala { namespace chillin { namespace client {
Core::Core(QObject *parent):
QObject(parent),
gameRunning(false)
{
network = new Network(this);
protocol = new Protocol(network, this);
}
Core::~Core()
{
delete protocol;
delete network;
}
void Core::registerAI(AbstractAI *ai)
{
ai->setCommandSendQueue(&commandSendQueue);
this->ai = ai;
}
bool Core::connectToServer()
{
auto netConfig = Config::instance()->getConfig().value("net").toObject();
auto max_tries = netConfig.value("max_tries").toInt();
int retry_waiting_time = netConfig.value("retry_waiting_time").toDouble() * 1000.;
while (true)
{
if (network->secureConnect())
return true;
max_tries--;
if (max_tries <= 0)
break;
Logger::instance()->log(QString("Reconnecting in %1 seconds ...").arg(
netConfig.value("retry_waiting_time").toDouble()
));
QThread::msleep(retry_waiting_time);
}
return false;
}
bool Core::joinGame()
{
auto generalConfig = Config::instance()->getConfig().value("general").toObject();
auto aiConfig = Config::instance()->getConfig().value("ai").toObject();
KSObject *joinMessage;
if (generalConfig.value("offline_mode").toBool())
{
JoinOfflineGame *msg = new JoinOfflineGame();
msg->team_nickname(aiConfig.value("team_nickname").toString().toStdString());
msg->agent_name(aiConfig.value("agent_name").toString().toStdString());
joinMessage = (KSObject*) msg;
}
else
{
JoinOnlineGame *msg = new JoinOnlineGame();
msg->token(aiConfig.value("token").toString().toStdString());
msg->agent_name(aiConfig.value("agent_name").toString().toStdString());
joinMessage = (KSObject*) msg;
}
sendMessage(joinMessage);
ClientJoined *clientJoinedMessage;
while (true)
{
ParsedMessage *msg = protocol->recvMessage();
if (msg->message->name() == ClientJoined::nameStatic())
{
clientJoinedMessage = (ClientJoined*) msg->message;
break;
}
}
if (clientJoinedMessage->joined())
{
ai->setSides(clientJoinedMessage->sides(), clientJoinedMessage->side_name());
Logger::instance()->succeed("Joined the game successfully");
Logger::instance()->succeed(QString("Side: %1").arg(
QString::fromStdString(clientJoinedMessage->side_name())
));
return true;
}
Logger::instance()->err("Failed to join the game");
return false;
}
void Core::loop()
{
bool running = true;
while (running)
{
ParsedMessage *msg = protocol->recvMessage();
if (dynamic_cast<BaseSnapshot*> (msg->message))
handleSnapshot(msg);
else if (msg->message->name() == StartGame::nameStatic())
handleStartGame(msg);
else if (msg->message->name() == EndGame::nameStatic())
{
handleEndGame(msg);
running = false;
}
delete msg;
}
}
void Core::handleSnapshot(const ParsedMessage *msg)
{
auto aiConfig = Config::instance()->getConfig().value("ai").toObject();
auto create_new_thread = aiConfig.value("create_new_thread").toBool();
BaseSnapshot *x = (BaseSnapshot*) msg->message;
ai->update(x);
if (!gameRunning)
{
gameRunning = true;
ai->initialize();
if (!create_new_thread || ai->allowedToDecide())
{
std::thread t(&AbstractAI::decide, ai);
t.detach();
}
}
else if (create_new_thread && ai->allowedToDecide())
{
std::thread t(&AbstractAI::decide, ai);
t.detach();
}
}
void Core::handleStartGame(const ParsedMessage *msg)
{
std::thread t(&Core::sendCommandThread, this);
t.detach();
}
void Core::handleEndGame(const ParsedMessage *msg)
{
EndGame *x = (EndGame*) msg->message;
QString winner = QString::fromStdString(x->winner_sidename());
if (!x->has_winner_sidename() || winner == "")
winner = "draw";
Logger::instance()->log(QString("Winner side: %1").arg(winner));
if (x->has_details() && x->details().size())
{
Logger::instance()->log("Details:");
for (auto detail : x->details())
{
Logger::instance()->log(QString(" %1:").arg(
QString::fromStdString(detail.first)
));
for (auto sideval : detail.second)
Logger::instance()->log(QString(" %1 -> %2").arg(
QString::fromStdString(sideval.first),
QString::fromStdString(sideval.second)
));
}
}
quit();
}
void Core::sendMessage(const KSObject *msg)
{
protocol->sendMessage(msg);
}
void Core::sendCommandThread()
{
while (true)
{
BaseCommand *msg;
commandSendQueue.wait_dequeue(msg);
if (!msg)
break;
if (gameRunning)
sendMessage(msg);
delete msg;
}
}
void Core::quit()
{
gameRunning = false;
commandSendQueue.enqueue(0);
network->stop();
}
}}}