-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetcli.cpp
More file actions
155 lines (134 loc) · 5.16 KB
/
Copy pathnetcli.cpp
File metadata and controls
155 lines (134 loc) · 5.16 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
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
#include "netcli.h"
#include <QJsonArray>
#include <QJsonDocument>
// ---------------------------------------------------------------------------
// NetClient
// ---------------------------------------------------------------------------
NetClient::NetClient(QObject* parent)
: QObject(parent)
{
connect(&m_socket, &QTcpSocket::connected, this, &NetClient::connected);
connect(&m_socket, &QTcpSocket::readyRead, this, &NetClient::onReadyRead);
}
void NetClient::connectTo(const QString& host, quint16 port)
{
m_socket.connectToHost(host, port);
}
void NetClient::send(const QJsonObject& msg)
{
if (isConnected())
m_socket.write(QJsonDocument(msg).toJson(QJsonDocument::Compact) + '\n');
}
void NetClient::onReadyRead()
{
m_buf += m_socket.readAll();
int idx;
while ((idx = m_buf.indexOf('\n')) >= 0) {
const QByteArray line = m_buf.left(idx).trimmed();
m_buf.remove(0, idx + 1);
if (!line.isEmpty())
emit message(QJsonDocument::fromJson(line).object());
}
}
// ---------------------------------------------------------------------------
// NetSession
// ---------------------------------------------------------------------------
NetSession::NetSession(QObject* parent)
: QObject(parent)
{
connect(&m_client, &NetClient::message, this, &NetSession::onMessage);
}
void NetSession::connectTo(const QString& host, quint16 port)
{
m_client.connectTo(host, port);
}
void NetSession::queue()
{
QJsonObject o;
o[QStringLiteral("type")] = QStringLiteral("queue");
m_client.send(o);
}
void NetSession::sendAction(const QString& action)
{
QJsonObject payload;
payload[QStringLiteral("action")] = action;
QJsonObject o;
o[QStringLiteral("type")] = QStringLiteral("action");
o[QStringLiteral("payload")] = payload;
m_client.send(o);
}
void NetSession::surrender()
{
QJsonObject o;
o[QStringLiteral("type")] = QStringLiteral("surrender");
m_client.send(o);
}
void NetSession::onMessage(const QJsonObject& msg)
{
const QString type = msg[QStringLiteral("type")].toString();
if (type == QLatin1String("matched")) {
m_matched = true;
m_you = msg[QStringLiteral("you")].toInt(-1);
m_state = msg[QStringLiteral("state")].toObject();
} else if (type == QLatin1String("state")) {
m_state = msg[QStringLiteral("state")].toObject();
} else if (type == QLatin1String("action_result")) {
// 状态随后由 state 消息同步,这里不重复处理
} else if (type == QLatin1String("game_over")) {
m_winner = msg[QStringLiteral("winner")].toInt(-1);
}
// error 忽略(非法动作会在 renderBoard 中不体现,后续可扩展)
emit updated();
}
QString NetSession::renderBoard() const
{
if (!m_matched)
return QStringLiteral("尚未匹配。");
const int rows = m_state[QStringLiteral("rows")].toInt(9);
const int cols = m_state[QStringLiteral("cols")].toInt(9);
const QJsonArray view = m_state[QStringLiteral("view")].toArray();
const QJsonArray rowH = m_state[QStringLiteral("rowHints")].toArray();
const QJsonArray colH = m_state[QStringLiteral("colHints")].toArray();
QString s;
s += QStringLiteral("玩家%1 视角(. 迷雾 ~ 水 # 己方 @ 敌方)\n").arg(m_you + 1);
s += QStringLiteral(" ");
for (int c = 0; c < cols; c++) s += QStringLiteral(" %1").arg(c + 1);
s += QLatin1Char('\n');
s += QStringLiteral(" +");
for (int c = 0; c < cols; c++) s += QStringLiteral("--");
s += QStringLiteral("+\n");
for (int r = 0; r < rows; r++) {
s += QStringLiteral(" %1 |").arg(r + 1);
const QJsonArray row = view[r].toArray();
for (int c = 0; c < cols; c++) {
const QString v = row[c].toString();
QChar ch = QLatin1Char('.');
if (v == QLatin1String("water")) ch = QLatin1Char('~');
else if (v == QLatin1String("own")) ch = QLatin1Char('#');
else if (v == QLatin1String("enemy")) ch = QLatin1Char('@');
s += QLatin1Char(' ');
s += ch;
}
s += QStringLiteral(" | %1\n").arg(rowH[r].toInt());
}
s += QStringLiteral(" +");
for (int c = 0; c < cols; c++) s += QStringLiteral("--");
s += QStringLiteral("+\n ");
for (int c = 0; c < cols; c++) s += QStringLiteral(" %1").arg(colH[c].toInt());
s += QLatin1Char('\n');
s += QStringLiteral("轮到 玩家%1 · 胜者 %2\n")
.arg(m_state[QStringLiteral("currentPlayer")].toInt(0) + 1)
.arg(m_state[QStringLiteral("winner")].toInt(-1));
for (const QJsonValue& v : m_state[QStringLiteral("myShips")].toArray()) {
const QJsonObject o = v.toObject();
QString cells;
for (const QJsonValue& cc : o[QStringLiteral("cells")].toArray())
cells += QStringLiteral("(%1,%2)").arg(cc.toArray()[0].toInt() + 1).arg(cc.toArray()[1].toInt() + 1);
s += QStringLiteral(" 己方 体积%1 血量%2/%3 %4\n")
.arg(o[QStringLiteral("size")].toInt())
.arg(o[QStringLiteral("hp")].toInt())
.arg(o[QStringLiteral("maxHp")].toInt())
.arg(cells);
}
return s;
}