-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.h
More file actions
501 lines (452 loc) · 13.3 KB
/
Copy pathmain.h
File metadata and controls
501 lines (452 loc) · 13.3 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
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
501
#ifndef MAIN_H
#define MAIN_H
#include <cstdint>
#include <cstdlib>
#include <iterator>
#include <numeric>
#include <typeinfo>
#include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <cstring>
#include <sstream>
#include <fstream>
#include <vector>
#include <tuple>
#include <algorithm>
#include <random>
#include <thread>
#include <mutex>
#include <sys/socket.h>
#include <sys/select.h>
#include <arpa/inet.h>
#define CONN_REQ_LEN 25
#define BUF 4098
#define CARDS 170
#define TRESH 95
#define PORT 8494
#define DEL '\n'
// LOGGING SYSTEM //
class Log {
private:
std::ofstream logs;
public:
Log ( std::string path )
: logs(path) {}
/**
* Logs a message
*
* @param msg A message to log
*/
void log( std::string msg ) {
std::cout << msg << std::endl;
logs << msg << std::endl;
}
void operator()( std::string msg ) { log(msg); }
};
// CARDS CLASSES //
/**
* Card class
*/
class Card {
public:
uint8_t Number;
bool Trap;
bool Face;
int X, Y, Rotation;
/**
*
* @param num A unique card Number
* @param face Is card Faced or Flopped
* @param x Coordinate X
* @param y Coordinate Y
* @param rot Rotation
*/
explicit Card ( uint8_t num, bool face = true, int x = 0, int y = 0, int rot = 0 )
: Face(face), Trap(num<TRESH), Number(num), X(x), Y(y), Rotation(rot) {}
/**
* Flip the card
*/
void flip () {
Face = !Face;
}
/**
* Set card position and rotation
*
* @param x New coordinate X
* @param y New coordinate Y
*/
void transform( int x, int y ) {
X = x;
Y = y;
}
int rotate ( int rot ) {
if ( rot < 0 || rot > 360 ) return -1;
Rotation = rot;
return 0;
}
};
/**
* Base CardContainer class
*/
class CardContainer {
public:
std::vector<Card*> Cards;
CardContainer () {}
/**
*
* @param cards Initial Cards array
*/
CardContainer ( std::vector<Card*> cards ) : Cards(cards) {}
~CardContainer () {
for ( int i = Cards.size()-1 ; i >= 0 ; i-- ) delete Cards[i];
}
/**
* Pushe the card to container
*
* @param card A Pointer to card
*/
void push ( Card* card ) {
Cards.push_back(card);
}
/**
* Move card to another CardContainer
*
* @param i Card index
* @param container Destination
* @return Pointer to moved card if Success, nullptr if Failed (Out of Bounds)
*/
Card* move ( int i, CardContainer* container ) {
if ( i < 0 || i >= Cards.size() ) { return nullptr; }
Card* card = Cards[i];
Cards.erase(Cards.begin()+i);
container->push(card);
return card;
}
/**
* Get a card by index
*
* @param i Card index
* @return Pointer to card if Success, nullptr if Failed (Out of Bounds)
*/
Card* card ( int i ) {
if ( i < 0 || i >= Cards.size() ) { return nullptr; }
return Cards[i];
}
/**
* Change card position
*
* @param i Card index
* @param x New coordinate X
* @param y New coordinate Y
* @return 0 if Success, -1 if Error (Out of Bounds)
*/
int transform ( int i, int x, int y ) {
if ( i < 0 || i >= Cards.size() ) { return -1; }
Cards[i]->transform(x, y);
return 0;
}
/**
* Change card rotation
* @param i Card index
* @param rot New rotation (-1 for default)
* @return 0 if Success, -1 if Error (Out of Bounds)
*/
int rotate ( int i, int rot ) {
if ( i < 0 || i >= Cards.size() ) { return -1; }
return Cards[i]->rotate(rot);
}
};
/**
* Deck class
*/
class Deck : public CardContainer {
private:
std::mt19937* rng;
public:
/**
*
* @param random_engine A random engine used for shuffling
*/
Deck ( std::mt19937* random_engine )
: rng(random_engine) {}
/**
*
* @param random_engine A random engine used for shuffling
* @param cards Inital Cards array
*/
Deck ( std::mt19937* random_engine, std::vector<Card*> cards )
: rng(random_engine), CardContainer(cards) {}
/**
* Shuffle the deck
*/
void shuffle () {
std::shuffle(Cards.begin(), Cards.end(), *rng);
}
/**
* Pop the card and Move it to other container
*
* @param container Destination
* @return Pointer to a card if Success, nullptr if Failed (Empty)
*/
Card* pop_and_move ( CardContainer* container ) {
if ( Cards.empty() ) { return nullptr; }
Card* card = Cards.back();
Cards.pop_back();
container->push(card);
return card;
}
};
/**
* Table class
*/
class Table : public CardContainer {
private:
std::mt19937 *Rng;
public:
Deck TrapDoors;
Deck Treasures;
/**
* @param rng Random Number Generator for Shuffling
*/
Table ( std::mt19937* rng ) : Rng(rng), TrapDoors(Rng), Treasures(Rng) {
for ( int i = 0 ; i < TRESH; i++ ) {
Card* card = new Card(i);
TrapDoors.push(card);
}
for ( int i = TRESH ; i < CARDS; i++ ) {
Card* card = new Card(i);
Treasures.push(card);
}
TrapDoors.shuffle();
Treasures.shuffle();
}
};
// PLAYER CLASSES //
/**
* Player class
*/
class Player {
private:
uint64_t Pass;
public:
int ConnectionSocket;
std::string Name;
int Level;
int Power;
int Gold;
CardContainer Inventory;
CardContainer Equiped;
/**
*
* @param pass Secert key to authorize user in case connection lost
* @param name Username
* @param socket Player connection File Descriptor
*/
Player ( uint64_t pass, std::string name, int socket )
: Pass(pass), Name(name), ConnectionSocket(socket) {}
/**
* Get player Secret key
* */
uint64_t pass () {
return Pass;
}
/**
* Send message to Player
*
* @param message Message to send
* @param n Message length (-1 for automatic)
*/
void sendMsg( std::string message, int n = -1 ) {
message += DEL;
if ( n == -1 ) n = message.length();
send(ConnectionSocket, message.data(), n, 0);
}
};
// WorkerQueue //
/**
* Worker Queue class
*/
class WorkerQueue {
private:
std::mutex QueueMutex;
std::vector<std::tuple<Player*, std::string>> Queue;
public:
/**
* Push a Player request to a Queue
*
* @param player Request sender
* @param request Request string
*/
void push ( Player* player, std::string request ) {
std::lock_guard<std::mutex> lock(QueueMutex);
std::tuple<Player*, std::string> task(player, request);
Queue.push_back(task);
}
/**
* Pop a request from Queue
*
* @return Tuple (Player sender, String request)
*/
std::tuple<Player*, std::string> pop () {
while ( Queue.empty() ) {}
std::lock_guard<std::mutex> lock(QueueMutex);
std::tuple<Player*, std::string> task = Queue.back();
Queue.pop_back();
return task;
}
};
/**
* Player Manager class
*/
class PlayerManager {
private:
std::vector<Player*> Players;
WorkerQueue* Queue;
Log* Logger;
std::mutex join_mutex;
std::random_device& rd;
std::uniform_int_distribution<uint64_t> dist;
static void receiver ( Player* player, WorkerQueue* queue, Log* logger ) {
char buffer[BUF] = {0};
while ( true ) {
if ( read(player->ConnectionSocket, buffer, BUF) == 0 ) { logger->log(player->Name+" disconnected"); break; }
queue->push(player, buffer);
std::fill(buffer, buffer+BUF, 0);
}
};
uint64_t gen_pass () {
uint64_t pass = dist(rd);
// uint64_t pass = 0xEAEAEA00EAEA00EA;
while ( playerByPass(pass) != nullptr ) pass = dist(rd); // Ensure, that player gets a unique pass
return pass;
}
public:
/**
*
* @param queue Pointer to a requests Queue
* @param logger Pointer to a logging object
* @param rd Reference to a Random device to generate Secret Keys
*/
PlayerManager ( WorkerQueue* queue, Log* logger, std::random_device& rd )
: Queue(queue), Logger(logger), rd(rd) {}
~PlayerManager () {
for ( int i = Players.size()-1 ; i >= 0 ; i-- ) delete Players[i];
}
size_t size () const {
return Players.size();
}
/**
* Get Player by Id
*
* @param id Player Id
* @return Pointer to a Player if Success, nullptr if Failed (Out of Bounds)
*/
Player* playerById( size_t id ) {
if ( id <= 0 or id > Players.size() ) {
return nullptr;
} else {
return Players[id-1];
}
}
/**
* Get Player by Username
*
* @param name Player Username
* @return Pointer to a Player if Success, nullptr if Failed (Not Found)
*/
Player* playerByName( std::string name ) {
for ( Player* player : Players ) {
if ( player->Name == name ) {
return player;
}
}
return nullptr;
}
/**
* Get Player by Pass
*
* @param pass Player Secret key
* @return Pointer to a Player if Success, nullptr if Failed (Not Found)
*/
Player* playerByPass( uint64_t pass ) {
for ( Player* player : Players ) {
if ( pass == player->pass() ) {
return player;
}
}
return nullptr;
}
/**
* Get Id of a Player
*
* @param player Pointer to a player
* @return Player Id if Success, -1 if Failed (Not Found)
*/
size_t playerId( const Player* player ) {
for ( int i = 0 ; i < Players.size() ; i++ ) {
if ( Players[i] == player ) { return i+1; }
}
return -1;
}
/**
* Add Player to a PlayerManager
*
* @param pass New Player Secret key
* @param name New Player Username
* @param socket New Player connection File Descriptor
* @return Pointer to a New Player
*/
Player* join( std::string name, const int socket ) {
std::lock_guard<std::mutex> lock(join_mutex);
Player* new_player = new Player(gen_pass(), name, socket);
Players.push_back(new_player);
std::thread receiver_thread(receiver, new_player, Queue, Logger);
receiver_thread.detach();
return new_player;
}
/**
* Assign new connection for Player
*
* @param pass Player Secret key
* @param socket New Player connection File Descriptor
* @return Pointer to a Player if Success, nullptr if Failed (Not Found)
*/
Player* rejoin ( uint64_t pass, const int socket ) {
Player* player = playerByPass(pass);
if ( player != nullptr ) {
player->ConnectionSocket = socket;
std::thread receiver_thread(receiver, player, Queue, Logger);
receiver_thread.detach();
}
return player;
}
/**
* Send a message to Player by Username
*
* @param name Player Username
* @param message Message to send
* @param n Message length (-1 for automatic)
* @return 0 if Success, -1 if Failed (Not Found)
*/
int sendByName ( std::string name, std::string message, int n = -1 ) {
Player* player = playerByName(name);
if ( player == nullptr ) { return -1; }
player->sendMsg(message, n);
return 0;
}
/**
* Send message to all Players
*
* @param message Message to send
* @param n Message length (-1 for automatic)
*/
void sendAll ( std::string message, int n = -1 ) {
for ( Player* player : Players ) {
player->sendMsg(message, n);
}
}
};
#endif