-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetcli_test.cpp
More file actions
96 lines (82 loc) · 2.86 KB
/
Copy pathnetcli_test.cpp
File metadata and controls
96 lines (82 loc) · 2.86 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
// 客户端联网端到端自测:NetSession(真实客户端) + 裸 socket 对手,镜像 AI 驱动打满一局。
#include "gameserver.h"
#include "netcli.h"
#include <QCoreApplication>
#include <QElapsedTimer>
#include <QEventLoop>
#include <QHostAddress>
#include <QJsonDocument>
#include <QJsonObject>
#include <QTimer>
#include <QTcpSocket>
#include <cstdio>
static void pump(int ms)
{
QEventLoop loop;
QTimer::singleShot(ms, &loop, &QEventLoop::quit);
loop.exec();
}
static QList<QJsonObject> takeLines(QByteArray& buf)
{
QList<QJsonObject> out;
int idx;
while ((idx = buf.indexOf('\n')) >= 0) {
const QByteArray line = buf.left(idx).trimmed();
buf.remove(0, idx + 1);
if (!line.isEmpty())
out.append(QJsonDocument::fromJson(line).object());
}
return out;
}
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
GameServer server;
if (!server.listen(0)) { printf("FAIL: listen\n"); return 1; }
NetSession nsA;
nsA.connectTo(QStringLiteral("127.0.0.1"), server.port());
QTcpSocket b;
b.connectToHost(QHostAddress::LocalHost, server.port());
QByteArray bufB;
QObject::connect(&b, &QTcpSocket::readyRead, [&]() { bufB += b.readAll(); });
pump(200);
nsA.queue();
b.write("{\"type\":\"queue\"}\n");
QElapsedTimer t;
t.start();
while (!nsA.matched() && t.elapsed() < 5000)
pump(20);
if (!nsA.matched()) { printf("FAIL: A not matched\n"); return 1; }
quint32 seed = 0;
for (const QJsonObject& m : takeLines(bufB))
if (m[QStringLiteral("type")] == QLatin1String("matched"))
seed = quint32(m[QStringLiteral("seed")].toDouble());
GameModel mirror;
mirror.setSeed(seed);
mirror.newGame();
int steps = 0;
while (mirror.winner() == -1 && steps++ < 500) {
const int cur = mirror.currentPlayer();
mirror.aiPlay();
const QString action = mirror.log().last();
if (cur == nsA.you()) {
nsA.sendAction(action);
} else {
QJsonObject payload;
payload[QStringLiteral("action")] = action;
QJsonObject msg;
msg[QStringLiteral("type")] = QStringLiteral("action");
msg[QStringLiteral("payload")] = payload;
b.write(QJsonDocument(msg).toJson(QJsonDocument::Compact) + '\n');
}
pump(60);
}
if (mirror.winner() == -1) { printf("FAIL: mirror no winner\n"); return 1; }
if (!nsA.gameOver()) { printf("FAIL: A no game_over\n"); return 1; }
bool overB = false;
for (const QJsonObject& m : takeLines(bufB))
if (m[QStringLiteral("type")] == QLatin1String("game_over")) overB = true;
if (!overB) { printf("FAIL: B no game_over\n"); return 1; }
printf("NETCLI TEST OK (A you=%d, winner=%d, steps=%d)\n", nsA.you(), mirror.winner(), steps);
return 0;
}