This repository was archived by the owner on Dec 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopponent.cpp
374 lines (338 loc) · 10.4 KB
/
opponent.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
#include "opponent.h"
// help functions for reading and writing to socket
static inline QByteArray IntToArray(qint32 source)
{
QByteArray temp;
QDataStream data(&temp, QIODevice::ReadWrite);
data << source;
return temp;
}
static inline qint32 ArrayToInt(QByteArray source)
{
qint32 temp;
QDataStream data(&source, QIODevice::ReadWrite);
data >> temp;
return temp;
}
Opponent::Opponent(QObject *parent)
: QObject(parent)
, m_socket(new QTcpSocket(this))
, sz(0)
, m_role(DDZ::NIL)
{
}
Opponent::~Opponent()
{
}
/**
* @brief Opponent::writeData
* send data by first sending the size of the raw QByteArray
* @param data
* @return true on write success, false otherwise
*/
bool Opponent::writeData(const QByteArray &data)
{
if (m_socket->state() == QAbstractSocket::ConnectedState) {
m_socket->write(IntToArray(data.size()));
m_socket->write(data);
return m_socket->waitForBytesWritten();
}
else
return false;
}
void Opponent::receiveData()
{
qint32 dataSize = sz;
while (m_socket->bytesAvailable() > 0) {
// store all incoming data into buffer
buffer.append(m_socket->readAll());
while ((dataSize == 0 && buffer.size() >= 4) || (dataSize > 0 && buffer.size() >= dataSize)) {
// read in size of current data array, and subsequently size of the
// next data array if it happens to come together
if (dataSize == 0 && buffer.size() >= 4) {
dataSize = ArrayToInt(buffer.left(4));
sz = dataSize;
buffer.remove(0, 4);
}
// if the data for the current array is fully received, pass it on
if (dataSize > 0 && buffer.size() >= dataSize) {
QByteArray data = buffer.left(dataSize);
buffer.remove(0, dataSize);
dataSize = 0;
sz = dataSize;
processData(data);
}
}
}
}
bool Opponent::connectTo(const QString &server_addr, const int &server_port)
{
m_socket->connectToHost(server_addr, server_port);
if (m_socket->waitForConnected(1000)) {
QObject::connect(m_socket, SIGNAL(readyRead()), this, SLOT(receiveData()));
return true;
}
return false;
}
QAbstractSocket::SocketState Opponent::getListenState()
{
return m_socket->state();
}
void Opponent::setSocket(QTcpSocket *newSock)
{
if (m_socket->state() == QAbstractSocket::UnconnectedState) {
delete m_socket;
m_socket = newSock;
QObject::connect(m_socket, SIGNAL(readyRead()), this, SLOT(receiveData()));
}
}
/**
* @brief Opponent::initServer
* request receiver to start a tcp server
*/
void Opponent::initServer()
{
QJsonObject json;
json[QLatin1String("intent")] = QLatin1String("Welcome");
json[QLatin1String("serverStart")] = true;
writeData(json);
}
/**
* @brief Opponent::notifyServerUp
* inform receiver the connection details of the sender's server
* @param addr
* @param port
*/
void Opponent::notifyServerUp(const QHostAddress &addr, const quint16 port)
{
QJsonObject json;
json[QLatin1String("intent")] = QLatin1String("updateAddress");
json[QLatin1String("addr")] = addr.toString();
json[QLatin1String("port")] = port;
writeData(json);
}
void Opponent::setRemoteServer(const QJsonObject &json)
{
if (json.contains("addr") && json["addr"].isString())
{
serverAddr = json["addr"].toString();
}
if (json.contains("port") && json["port"].isDouble())
{
serverPort = json["port"].toInt();
}
}
QJsonObject Opponent::getConnInfo()
{
QJsonObject json;
json[QLatin1String("addr")] = serverAddr;
json[QLatin1String("port")] = serverPort;
return json;
}
void Opponent::initialHandshake(const QJsonObject &json)
{
if (json.contains(QLatin1String("serverStart")) && json[QLatin1String("serverStart")].isBool()) {
if (json[QLatin1String("serverStart")].toBool())
emit serverStart(0);
else {
QString serverAddr;
if (json.contains(QLatin1String("addr"))
&& json[QLatin1String("addr")].isString())
serverAddr.append(json["addr"].toString());
if (json.contains(QLatin1String("port"))
&& json[QLatin1String("port")].isDouble()) {
serverAddr.append(QLatin1Char(':'));
serverAddr.append(json[QLatin1String("port")].toVariant().toString());
}
emit addressReceived(serverAddr);
}
}
}
/**
* @brief Opponent::secondConnect
* ask receiver to connect to second server
* @param connInfo
*/
void Opponent::secondConnect(QJsonObject connInfo)
{
connInfo[QLatin1String("intent")] = QLatin1String("Welcome");
connInfo[QLatin1String("serverStart")] = false;
writeData(connInfo);
}
/**
* @brief Opponent::announceReadyBW
* starting message to indicate that sender is ready, along with BW info
* @param black
*/
void Opponent::announceReadyBW(bool black)
{
QJsonObject json;
json[QLatin1String("intent")] = QLatin1String("start");
json[QLatin1String("black")] = black;
writeData(json);
}
/**
* @brief Opponent::giveDeck
* sender passes the shuffled cards to the receiver
* @param hand
* @param hide
*/
void Opponent::giveDeck(const QVector<Card> &hand, const QVector<Card> &hide)
{
QJsonObject json;
json[QLatin1String("intent")] = QLatin1String("distribute");
QJsonArray handArray;
for (const auto &c : hand) {
QJsonObject cardObj;
cardObj[QLatin1String("value")] = static_cast<int>(c);
handArray.append(cardObj);
}
json[QLatin1String("hand")] = handArray;
QJsonArray hideArray;
for (const auto &c : hide) {
QJsonObject cardObj;
cardObj[QLatin1String("value")] = static_cast<int>(c);
hideArray.append(cardObj);
}
json[QLatin1String("hide")] = hideArray;
writeData(json);
}
void Opponent::receiveDeck(const QJsonObject &json)
{
QVector<Card> shownDeck, hiddenDeck;
if (json.contains(QLatin1String("hand")) && json[QLatin1String("hand")].isArray()) {
QJsonArray handArray = json[QLatin1String("hand")].toArray();
shownDeck.reserve(handArray.size());
for (const auto &c : handArray) {
QJsonObject cardObj = c.toObject();
if (cardObj.contains(QLatin1String("value")) && cardObj[QLatin1String("value")].isDouble())
shownDeck.append(Card(cardObj[QLatin1String("value")].toDouble()));
}
}
if (json.contains(QLatin1String("hide")) && json[QLatin1String("hide")].isArray()) {
QJsonArray hideArray = json[QLatin1String("hide")].toArray();
hiddenDeck.reserve(hideArray.size());
for (const auto &c : hideArray) {
QJsonObject cardObj = c.toObject();
if (cardObj.contains(QLatin1String("value")) && cardObj[QLatin1String("value")].isDouble())
hiddenDeck.append(Card(cardObj[QLatin1String("value")].toDouble()));
}
}
emit incomingDecks(shownDeck, hiddenDeck);
}
/**
* @brief Opponent::announceBid
* any calls for landlord are sent to the receiver
* @param bid
*/
void Opponent::announceBid(bool bid)
{
QJsonObject json;
json[QLatin1String("intent")] = QLatin1String("landlord");
json[QLatin1String("bid")] = bid;
writeData(json);
}
void Opponent::receiveBid(const QJsonObject &json)
{
if (json.contains(QLatin1String("bid")) && json[QLatin1String("bid")].isBool())
emit incomingBid(json[QLatin1String("bid")].toBool());
}
/**
* @brief Opponent::confirmRoles
* ask bidding for landlord is over and time to start game
*/
void Opponent::confirmRoles()
{
QJsonObject json;
json[QLatin1String("intent")] = QLatin1String("roles");
writeData(json);
}
/**
* @brief Opponent::informCardsDealt
* inform receivers the set of cards dealt by the sender
* @param hand
*/
void Opponent::informCardsDealt(const HandCategory &hand)
{
QJsonObject json;
json[QLatin1String("intent")] = QLatin1String("move");
QJsonObject dealtHand;
hand.write(dealtHand);
json[QLatin1String("hand")] = dealtHand;
writeData(json);
}
void Opponent::updateLastDealt(const QJsonObject &json)
{
if (json.contains(QLatin1String("hand")) && json[QLatin1String("hand")].isObject()) {
lastDealtHand.read(json[QLatin1String("hand")].toObject());
emit dealt();
}
}
/**
* @brief Opponent::informGameOver
* sender (winner) will let others know when game has ended
*/
void Opponent::informGameOver()
{
QJsonObject json;
json[QLatin1String("intent")] = QLatin1String("gameover");
writeData(json);
}
/**
* @brief Opponent::nextRoundReady
* should the sender want to continue playing, it will be made known this way
* @return
*/
bool Opponent::nextRoundReady()
{
lastDealtHand.setPass();
QJsonObject json;
json["intent"] = QLatin1String("nextgame");
return writeData(json);
}
/**
* @brief Opponent::processData
* process all fully received data array
* @param rawData
*/
void Opponent::processData(const QByteArray &rawData)
{
QJsonDocument doc = QJsonDocument::fromJson(rawData);
QJsonObject &&json = doc.object();
if (json.contains(QLatin1String("intent")) && json[QLatin1String("intent")].isString())
{
auto intent = json[QLatin1String("intent")].toString();
if (intent == QLatin1String("Welcome"))
initialHandshake(json);
else if (intent == QLatin1String("updateAddress"))
setRemoteServer(json);
else if (intent == QLatin1String("start")) {
if (json.contains("black") && json["black"].isBool())
emit blackAndWhite(json["black"].toBool());
}
else if (intent == QLatin1String("distribute"))
receiveDeck(json);
else if (intent == QLatin1String("landlord"))
receiveBid(json);
else if (intent == QLatin1String("roles"))
emit rolesConfirmed();
else if (intent == QLatin1String("move"))
updateLastDealt(json);
else if (intent == QLatin1String("gameover"))
emit wonGame();
else if (intent == QLatin1String("nextgame"))
emit canStart();
}
}
HandCategory Opponent::getLastDealt() const
{
return lastDealtHand;
}
DDZ::Role Opponent::getRole() const
{
return m_role;
}
void Opponent::setRole(const DDZ::Role &role)
{
m_role = role;
}