-
Notifications
You must be signed in to change notification settings - Fork 0
/
DebugAPI.cpp
500 lines (432 loc) · 16.5 KB
/
DebugAPI.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include <optional>
#include <string>
#include "AI.h"
#include "API.h"
#include "utils.hpp"
#include "structures.h"
#undef GetMessage
#undef SendMessage
#undef PeekMessage
#define PI 3.14159265358979323846
ShipDebugAPI::ShipDebugAPI(ILogic& logic, bool file, bool print, bool warnOnly, int32_t playerID) :
logic(logic)
{
std::string fileName = "logs/api-" + std::to_string(playerID) + "-log.txt";
auto fileLogger = std::make_shared<spdlog::sinks::basic_file_sink_mt>(fileName, true);
auto printLogger = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
std::string pattern = "[api " + std::to_string(playerID) + "] [%H:%M:%S.%e] [%l] %v";
fileLogger->set_pattern(pattern);
printLogger->set_pattern(pattern);
if (file)
fileLogger->set_level(spdlog::level::trace);
else
fileLogger->set_level(spdlog::level::off);
if (print)
printLogger->set_level(spdlog::level::info);
else
printLogger->set_level(spdlog::level::off);
if (warnOnly)
printLogger->set_level(spdlog::level::warn);
logger = std::make_unique<spdlog::logger>("apiLogger", spdlog::sinks_init_list{fileLogger, printLogger});
logger->flush_on(spdlog::level::warn);
}
void ShipDebugAPI::StartTimer()
{
startPoint = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(startPoint);
//logger->info("=== AI.play() ===");
//logger->info("StartTimer: {}", std::ctime(&t));
}
void ShipDebugAPI::EndTimer()
{
//logger->info("Time elapsed: {}ms", Time::TimeSinceStart(startPoint));
}
int32_t ShipDebugAPI::GetFrameCount() const
{
return logic.GetCounter();
}
std::future<bool> ShipDebugAPI::SendTextMessage(int32_t toID, std::string message)
{
logger->info("SendTextMessage: toID = {}, message = {}, called at {}ms", toID, message, Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=, message = std::move(message)]()
{ auto result = logic.Send(toID, std::move(message), false);
if (!result)
logger->warn("SendTextMessage: failed at {}ms", Time::TimeSinceStart(startPoint));
return result; });
}
std::future<bool> ShipDebugAPI::SendBinaryMessage(int32_t toID, std::string message)
{
logger->info("SendBinaryMessage: toID = {}, message = {}, called at {}ms", toID, message, Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=, message = std::move(message)]()
{ auto result = logic.Send(toID, std::move(message), true);
if (!result)
logger->warn("SendBinaryMessage: failed at {}ms", Time::TimeSinceStart(startPoint));
return result; });
}
bool ShipDebugAPI::HaveMessage()
{
logger->info("HaveMessage: called at {}ms", Time::TimeSinceStart(startPoint));
auto result = logic.HaveMessage();
if (!result)
logger->warn("HaveMessage: failed at {}ms", Time::TimeSinceStart(startPoint));
return result;
}
std::pair<int32_t, std::string> ShipDebugAPI::GetMessage()
{
logger->info("GetMessage: called at {}ms", Time::TimeSinceStart(startPoint));
auto result = logic.GetMessage();
if (result.first == -1)
logger->warn("GetMessage: failed at {}ms", Time::TimeSinceStart(startPoint));
return result;
}
bool ShipDebugAPI::Wait()
{
logger->info("Wait: called at {}ms", Time::TimeSinceStart(startPoint));
if (logic.GetCounter() == -1)
return false;
else
return logic.WaitThread();
}
std::future<bool> ShipDebugAPI::Move(int64_t timeInMilliseconds, double angleInRadian)
{
logger->info("Move: timeInMilliseconds = {}, angleInRadian = {}, called at {}ms", timeInMilliseconds, angleInRadian, Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ auto result = logic.Move(timeInMilliseconds, angleInRadian);
if (!result)
logger->warn("Move: failed at {}ms", Time::TimeSinceStart(startPoint));
return result; });
}
std::future<bool> ShipDebugAPI::MoveDown(int64_t timeInMilliseconds)
{
return Move(timeInMilliseconds, 0);
}
std::future<bool> ShipDebugAPI::MoveRight(int64_t timeInMilliseconds)
{
return Move(timeInMilliseconds, PI * 0.5);
}
std::future<bool> ShipDebugAPI::MoveUp(int64_t timeInMilliseconds)
{
return Move(timeInMilliseconds, PI);
}
std::future<bool> ShipDebugAPI::MoveLeft(int64_t timeInMilliseconds)
{
return Move(timeInMilliseconds, PI * 1.5);
}
std::future<bool> ShipDebugAPI::Attack(double angleInRadian)
{
logger->info("Attack: angleInRadian = {}, called at {}ms", angleInRadian, Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ auto result = logic.Attack(angleInRadian);
if (!result)
logger->warn("Attack: failed at {}ms", Time::TimeSinceStart(startPoint));
return result; });
}
std::future<bool> ShipDebugAPI::Recover(int64_t recover)
{
logger->info("Recover: recover={}, called at {}ms", recover, Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ auto result= logic.Recover(recover);
if(!result)
logger->warn("Recover: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}
std::future<bool> ShipDebugAPI::Produce()
{
logger->info("Produce: called at {}ms", Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ auto result= logic.Produce();
if(!result)
logger->warn("Produce: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}
std::future<bool> ShipDebugAPI::Rebuild(THUAI7::ConstructionType constructionType)
{
logger->info("Rebuild: rebuilding {}, called at {}ms", THUAI7::constructionDict[constructionType], Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ auto result= logic.Rebuild(constructionType);
if(!result)
logger->warn("Rebuild: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}
std::future<bool> ShipDebugAPI::Construct(THUAI7::ConstructionType constructionType)
{
logger->info("Construct: constructing {}, called at {}ms", THUAI7::constructionDict[constructionType], Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ auto result= logic.Construct(constructionType);
if(!result)
logger->warn("Construct: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}
std::vector<std::shared_ptr<const THUAI7::Ship>> ShipDebugAPI::GetShips() const
{
return logic.GetShips();
}
std::vector<std::shared_ptr<const THUAI7::Ship>> ShipDebugAPI::GetEnemyShips() const
{
return logic.GetEnemyShips();
}
std::vector<std::shared_ptr<const THUAI7::Bullet>> ShipDebugAPI::GetBullets() const
{
return logic.GetBullets();
}
std::vector<std::vector<THUAI7::PlaceType>> ShipDebugAPI::GetFullMap() const
{
return logic.GetFullMap();
}
THUAI7::PlaceType ShipDebugAPI::GetPlaceType(int32_t cellX, int32_t cellY) const
{
return logic.GetPlaceType(cellX, cellY);
}
int32_t ShipDebugAPI::GetConstructionHp(int32_t cellX, int32_t cellY) const
{
return logic.GetConstructionHp(cellX, cellY);
}
int32_t ShipDebugAPI::GetWormholeHp(int32_t cellX, int32_t cellY) const
{
return logic.GetWormholeHp(cellX, cellY);
}
int32_t ShipDebugAPI::GetResourceState(int32_t cellX, int32_t cellY) const
{
return logic.GetResourceState(cellX, cellY);
}
int32_t ShipDebugAPI::GetHomeHp() const
{
return logic.GetHomeHp();
}
std::shared_ptr<const THUAI7::GameInfo> ShipDebugAPI::GetGameInfo() const
{
return logic.GetGameInfo();
}
std::vector<int64_t> ShipDebugAPI::GetPlayerGUIDs() const
{
return logic.GetPlayerGUIDs();
}
std::shared_ptr<const THUAI7::Ship> ShipDebugAPI::GetSelfInfo() const
{
return logic.ShipGetSelfInfo();
}
bool ShipDebugAPI::HaveView(int32_t targetX, int32_t targetY) const
{
auto selfInfo = GetSelfInfo();
return logic.HaveView(targetX, targetY, selfInfo->x, selfInfo->y, selfInfo->viewRange);
}
int32_t ShipDebugAPI::GetEnergy() const
{
return logic.GetEnergy();
}
int32_t ShipDebugAPI::GetScore() const
{
return logic.GetScore();
}
void ShipDebugAPI::Print(std::string str) const
{
logger->info(str);
}
void ShipDebugAPI::PrintShip() const
{
for (const auto& Ship : logic.GetShips())
{
logger->info("******Ship Info******");
logger->info("type={}, playerID={}, GUID={}, x={}, y={}", THUAI7::shipTypeDict[Ship->shipType], Ship->playerID, Ship->guid, Ship->x, Ship->y);
logger->info("state={},speed={}, view range={},facing direction={}", THUAI7::shipStateDict[Ship->shipState], Ship->speed, Ship->viewRange, Ship->facingDirection);
logger->info("************************\n");
}
}
void ShipDebugAPI::PrintSelfInfo() const
{
auto Ship = logic.ShipGetSelfInfo();
logger->info("******Self Info******");
logger->info("type={}, playerID={}, GUID={}, x={}, y={}", THUAI7::shipTypeDict[Ship->shipType], Ship->playerID, Ship->guid, Ship->x, Ship->y);
logger->info("state={},speed={}, view range={},facing direction={}", THUAI7::shipStateDict[Ship->shipState], Ship->speed, Ship->viewRange, Ship->facingDirection);
logger->info("*********************\n");
}
std::future<bool> ShipDebugAPI::EndAllAction()
{
return std::async(std::launch::async, [this]()
{ return logic.EndAllAction(); });
}
TeamDebugAPI::TeamDebugAPI(ILogic& logic, bool file, bool print, bool warnOnly, int32_t playerID) :
logic(logic)
{
std::string fileName = "logs/api-" + std::to_string(playerID) + "-log.txt";
auto fileLogger = std::make_shared<spdlog::sinks::basic_file_sink_mt>(fileName, true);
auto printLogger = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
std::string pattern = "[api" + std::to_string(playerID) + "] [%H:%M:%S.%e] [%l] %v";
fileLogger->set_pattern(pattern);
printLogger->set_pattern(pattern);
if (file)
fileLogger->set_level(spdlog::level::trace);
else
fileLogger->set_level(spdlog::level::off);
if (print)
printLogger->set_level(spdlog::level::info);
else
printLogger->set_level(spdlog::level::off);
if (warnOnly)
printLogger->set_level(spdlog::level::warn);
logger = std::make_unique<spdlog::logger>("apiLogger", spdlog::sinks_init_list{fileLogger, printLogger});
}
void TeamDebugAPI::StartTimer()
{
startPoint = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(startPoint);
//logger->info("=== AI.play() ===");
//logger->info("StartTimer: {}", std::ctime(&t));
}
void TeamDebugAPI::EndTimer()
{
//logger->info("Time elapsed: {}ms", Time::TimeSinceStart(startPoint));
}
int32_t TeamDebugAPI::GetFrameCount() const
{
return logic.GetCounter();
}
std::future<bool> TeamDebugAPI::SendTextMessage(int32_t toID, std::string message)
{
logger->info("SendTextMessage: toID = {}, message = {}, called at {}ms", toID, message, Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=, message = std::move(message)]()
{ auto result = logic.Send(toID, std::move(message), false);
if (!result)
logger->warn("SendTextMessage: failed at {}ms", Time::TimeSinceStart(startPoint));
return result; });
}
std::future<bool> TeamDebugAPI::SendBinaryMessage(int32_t toID, std::string message)
{
logger->info("SendBinaryMessage: toID = {}, message = {}, called at {}ms", toID, message, Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=, message = std::move(message)]()
{ auto result = logic.Send(toID, std::move(message), true);
if (!result)
logger->warn("SendBinaryMessage: failed at {}ms", Time::TimeSinceStart(startPoint));
return result; });
}
bool TeamDebugAPI::HaveMessage()
{
logger->info("HaveMessage: called at {}ms", Time::TimeSinceStart(startPoint));
auto result = logic.HaveMessage();
if (!result)
logger->warn("HaveMessage: failed at {}ms", Time::TimeSinceStart(startPoint));
return result;
}
std::pair<int32_t, std::string> TeamDebugAPI::GetMessage()
{
logger->info("GetMessage: called at {}ms", Time::TimeSinceStart(startPoint));
auto result = logic.GetMessage();
if (result.first == -1)
logger->warn("GetMessage: failed at {}ms", Time::TimeSinceStart(startPoint));
return result;
}
bool TeamDebugAPI::Wait()
{
logger->info("Wait: called at {}ms", Time::TimeSinceStart(startPoint));
if (logic.GetCounter() == -1)
return false;
else
return logic.WaitThread();
}
std::vector<std::shared_ptr<const THUAI7::Ship>> TeamDebugAPI::GetShips() const
{
return logic.GetShips();
}
std::vector<std::shared_ptr<const THUAI7::Ship>> TeamDebugAPI::GetEnemyShips() const
{
return logic.GetEnemyShips();
}
std::vector<std::shared_ptr<const THUAI7::Bullet>> TeamDebugAPI::GetBullets() const
{
return logic.GetBullets();
}
std::vector<std::vector<THUAI7::PlaceType>> TeamDebugAPI::GetFullMap() const
{
return logic.GetFullMap();
}
THUAI7::PlaceType TeamDebugAPI::GetPlaceType(int32_t cellX, int32_t cellY) const
{
return logic.GetPlaceType(cellX, cellY);
}
int32_t TeamDebugAPI::GetConstructionHp(int32_t cellX, int32_t cellY) const
{
return logic.GetConstructionHp(cellX, cellY);
}
int32_t TeamDebugAPI::GetWormholeHp(int32_t cellX, int32_t cellY) const
{
return logic.GetWormholeHp(cellX, cellY);
}
int32_t TeamDebugAPI::GetResourceState(int32_t cellX, int32_t cellY) const
{
return logic.GetResourceState(cellX, cellY);
}
int32_t TeamDebugAPI::GetHomeHp() const
{
return logic.GetHomeHp();
}
std::shared_ptr<const THUAI7::GameInfo> TeamDebugAPI::GetGameInfo() const
{
return logic.GetGameInfo();
}
std::vector<int64_t> TeamDebugAPI::GetPlayerGUIDs() const
{
return logic.GetPlayerGUIDs();
}
std::shared_ptr<const THUAI7::Team> TeamDebugAPI::GetSelfInfo() const
{
return logic.TeamGetSelfInfo();
}
int32_t TeamDebugAPI::GetEnergy() const
{
return logic.GetEnergy();
}
int32_t TeamDebugAPI::GetScore() const
{
return logic.GetScore();
}
std::future<bool> TeamDebugAPI::InstallModule(int32_t playerID, THUAI7::ModuleType moduleType)
{
logger->info("InstallModule: install {} for ship {}, called at {}ms", THUAI7::moduleTypeDict[moduleType], playerID, Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ auto result= logic.InstallModule(playerID, moduleType);
if(!result)
logger->info("InstallModule: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}
std::future<bool> TeamDebugAPI::Recycle(int32_t playerID)
{
logger->info("Recycle: called at {}ms", Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ auto result = logic.Recycle(playerID);
if(!result)
logger->warn("Recycle: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}
std::future<bool> TeamDebugAPI::BuildShip(THUAI7::ShipType ShipType, int32_t birthIndex)
{
logger->info("BuildShip: build {} at birthIndex {}, called at {}ms", THUAI7::shipTypeDict[ShipType], birthIndex, Time::TimeSinceStart(startPoint));
return std::async(std::launch::async, [=]()
{ auto result=logic.BuildShip(ShipType, birthIndex);
if(!result)
logger->warn("BuildShip: failed at {}ms",Time::TimeSinceStart(startPoint));
return result; });
}
void TeamDebugAPI::PrintSelfInfo() const
{
auto Team = logic.TeamGetSelfInfo();
logger->info("******Self Info******");
logger->info("playerID={}, teamID={}, score={}, energy={}", Team->playerID, Team->teamID, Team->score, Team->energy);
logger->info("*********************\n");
}
void ShipDebugAPI::Play(IAI& ai)
{
ai.play(*this);
}
void TeamDebugAPI::Play(IAI& ai)
{
ai.play(*this);
}
void TeamDebugAPI::Print(std::string str) const
{
logger->info(str);
}
std::future<bool> TeamDebugAPI::EndAllAction()
{
return std::async(std::launch::async, [this]()
{ return logic.EndAllAction(); });
}