forked from RealTimeChris/MBot-GameHouse-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseEntities.hpp
765 lines (713 loc) · 35.6 KB
/
DatabaseEntities.hpp
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
// DatabaseEntities.hpp - Database stuff.
// May 24, 2021
// Chris M.
// https://github.com/RealTimeChris
#pragma once
#ifndef _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
#define _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
#endif
#include <mongocxx/instance.hpp>
#include <mongocxx/database.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/pool.hpp>
#include <discordcoreapi/Index.hpp>
namespace DiscordCoreAPI {
struct DiscordUserData {
std::vector<Uint64> botCommanders{ 0, 0, 0 };
Float hoursOfRobberyCooldown{ 0.100f };
Int32 hoursOfDrugSaleCooldown{ 3 };
Int32 hoursOfDepositCooldown{ 24 };
String currencyName{ "MBux" };
String userName{ "" };
Snowflake userId{ 0 };
};
struct Card {
Uint32 value{ 0 };
String suit{ "" };
String type{ "" };
};
class Deck {
public:
std::vector<Card> cards{};
Deck() {
this->cards.resize(52);
for (auto x = 0; x < 52; x += 1) {
this->cards[x].suit = "";
this->cards[x].type = "";
this->cards[x].value = 0;
if (trunc(x / 13) == 0) {
this->cards[x].suit = ":hearts:";
} else if (trunc(x / 13) == 1) {
this->cards[x].suit = ":diamonds:";
} else if (trunc(x / 13) == 2) {
this->cards[x].suit = ":clubs:";
} else if (trunc(x / 13) == 3) {
this->cards[x].suit = ":spades:";
}
if (x % 13 == 0) {
this->cards[x].type = "Ace";
this->cards[x].value = 0;
} else if (x % 13 == 1) {
this->cards[x].type = "2";
this->cards[x].value = 2;
} else if (x % 13 == 2) {
this->cards[x].type = "3";
this->cards[x].value = 3;
} else if (x % 13 == 3) {
this->cards[x].type = "4";
this->cards[x].value = 4;
} else if (x % 13 == 4) {
this->cards[x].type = "5";
this->cards[x].value = 5;
} else if (x % 13 == 5) {
this->cards[x].type = "6";
this->cards[x].value = 6;
} else if (x % 13 == 6) {
this->cards[x].type = "7";
this->cards[x].value = 7;
} else if (x % 13 == 7) {
this->cards[x].type = "8";
this->cards[x].value = 8;
} else if (x % 13 == 8) {
this->cards[x].type = "9";
this->cards[x].value = 9;
} else if (x % 13 == 9) {
this->cards[x].type = "10";
this->cards[x].value = 10;
} else if (x % 13 == 10) {
this->cards[x].type = "Jack";
this->cards[x].value = 10;
} else if (x % 13 == 11) {
this->cards[x].type = "Queen";
this->cards[x].value = 10;
} else if (x % 13 == 12) {
this->cards[x].type = "King";
this->cards[x].value = 10;
}
}
}
// Draws a random card from the Deck.
Card drawRandomcard() {
if (this->cards.size() == 0) {
Card voidCard{
.value = 0,
.suit = "",
.type = "",
};
voidCard.suit = ":black_large_square:";
voidCard.type = "null";
voidCard.value = 0;
return voidCard;
}
std::mt19937_64 randomEngine{ static_cast<Uint64>(
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count()) };
Uint32 cardIndex = static_cast<Uint32>(static_cast<Float>(randomEngine()) / static_cast<Float>(randomEngine.max()) * static_cast<Float>(this->cards.size()));
Card currentCard = this->cards.at(cardIndex);
this->cards.erase(this->cards.begin() + cardIndex);
return currentCard;
}
};
struct RouletteBet {
std::vector<String> winningNumbers{};
String betOptions{ "" };
Uint32 payoutAmount{ 0 };
String betType{ "" };
Uint32 betAmount{ 0 };
Uint64 userId{ 0 };
};
struct Roulette {
std::vector<RouletteBet> rouletteBets{};
Bool currentlySpinning{ false };
};
struct LargestPayout {
String timeStamp{ "" };
String userName{ "" };
Uint64 userId{ 0 };
Int32 amount{ 0 };
};
struct CasinoStats {
LargestPayout largestBlackjackPayout{};
LargestPayout largestCoinFlipPayout{};
LargestPayout largestRoulettePayout{};
LargestPayout largestSlotsPayout{};
Int32 totalBlackjackPayout{ 0 };
Int32 totalCoinFlipPayout{ 0 };
Int32 totalRoulettePayout{ 0 };
Int32 totalSlotsPayout{ 0 };
Int32 totalPayout{ 0 };
};
struct InventoryItem {
String itemName{ "" };
String emoji{ "" };
Uint32 itemCost{ 0 };
Uint32 selfMod{ 0 };
Int32 oppMod{ 0 };
};
struct InventoryRole {
Uint32 roleCost{ 0 };
String roleName{ "" };
Uint64 roleId{ 0 };
};
struct GuildShop {
std::vector<InventoryItem> items{};
std::vector<InventoryRole> roles{};
};
struct Currency {
Uint32 timeOfLastDeposit{ 0 };
Uint32 wallet{ 10000 };
Uint32 bank{ 10000 };
};
struct DiscordGuildData {
std::vector<Uint64> gameChannelIds{};
String borderColor{ "FeFeFe" };
std::vector<Card> blackjackStack{};
String guildName{ "" };
Uint32 memberCount{ 0 };
CasinoStats casinoStats{};
Roulette rouletteGame{};
Uint64 guildId{ 0 };
GuildShop guildShop{};
};
struct DiscordGuildMemberData {
Uint32 lastTimeRobbed{ 0 };
Uint32 lastTimeWorked{ 0 };
String guildMemberMention{ "" };
std::vector<InventoryItem> items{};
std::vector<InventoryRole> roles{};
Uint64 guildMemberId{ 0 };
String displayName{ "" };
String globalId{ "" };
String userName{ "" };
Uint64 guildId{ 0 };
Currency currency{};
};
enum class DatabaseWorkloadType {
DISCORD_USER_WRITE = 0,
DISCORD_USER_READ = 1,
DISCORD_GUILD_WRITE = 2,
DISCORD_GUILD_READ = 3,
DISCORD_GUILD_MEMBER_WRITE = 4,
DISCORD_GUILD_MEMBER_READ = 5
};
struct DatabaseWorkload {
DiscordGuildMemberData guildMemberData{};
DatabaseWorkloadType workloadType{};
DiscordGuildData guildData{};
DiscordUserData userData{};
};
struct DatabaseReturnValue {
DiscordGuildMemberData discordGuildMember{};
DiscordGuildData discordGuild{};
DiscordUserData discordUser{};
};
class DatabaseManagerAgent {
public:
static void initialize(Uint64 botUserIdNew) {
DatabaseManagerAgent::botUserId = botUserIdNew;
auto newClient = DatabaseManagerAgent::getClient();
mongocxx::database newDataBase = (*newClient)[std::to_string(DatabaseManagerAgent::botUserId).c_str()];
mongocxx::collection newCollection = newDataBase[std::to_string(DatabaseManagerAgent::botUserId).c_str()];
}
static mongocxx::pool::entry getClient() {
return DatabaseManagerAgent::thePool.acquire();
}
static DatabaseReturnValue submitWorkloadAndGetResults(DatabaseWorkload workload) {
std::lock_guard<std::mutex> workloadLock{ DatabaseManagerAgent::workloadMutex01 };
while (DatabaseManagerAgent::botUserId == 0) {
std::this_thread::sleep_for(1ms);
}
DatabaseReturnValue newData{};
mongocxx::pool::entry thePtr = DatabaseManagerAgent::getClient();
try {
auto newDataBase = (*thePtr)[std::to_string(DatabaseManagerAgent::botUserId).c_str()];
auto newCollection = newDataBase[std::to_string(DatabaseManagerAgent::botUserId).c_str()];
switch (workload.workloadType) {
case (DatabaseWorkloadType::DISCORD_USER_WRITE): {
auto doc = DatabaseManagerAgent::convertUserDataToDBDoc(workload.userData);
bsoncxx::builder::basic::document document{};
document.append(bsoncxx::builder::basic::kvp("_id", bsoncxx::types::b_int64(static_cast<Int64>(workload.userData.userId))));
auto resultNew = newCollection.find_one(document.view());
auto resultNewer = newCollection.find_one_and_replace(document.view(), std::move(doc.extract()),
mongocxx::v_noabi::options::find_one_and_replace{}.return_document(mongocxx::v_noabi::options::return_document::k_after));
if (resultNewer.get_ptr() == NULL) {
auto doc02 = DatabaseManagerAgent::convertUserDataToDBDoc(workload.userData);
newCollection.insert_one(std::move(doc02.extract()));
return newData;
}
break;
}
case (DatabaseWorkloadType::DISCORD_USER_READ): {
bsoncxx::builder::basic::document document{};
document.append(bsoncxx::builder::basic::kvp("_id", bsoncxx::types::b_int64(static_cast<Int64>(workload.userData.userId))));
auto resultNew = newCollection.find_one(document.view());
if (resultNew.get_ptr() != NULL) {
DiscordUserData userData = DatabaseManagerAgent::parseUserData(*resultNew.get_ptr());
newData.discordUser = userData;
return newData;
} else {
return newData;
}
break;
}
case (DatabaseWorkloadType::DISCORD_GUILD_WRITE): {
auto doc = DatabaseManagerAgent::convertGuildDataToDBDoc(workload.guildData);
bsoncxx::builder::basic::document document{};
document.append(bsoncxx::builder::basic::kvp("_id", bsoncxx::types::b_int64(static_cast<Int64>(workload.guildData.guildId))));
auto resultNewer = newCollection.find_one_and_replace(document.view(), std::move(doc.extract()),
mongocxx::v_noabi::options::find_one_and_replace{}.return_document(mongocxx::v_noabi::options::return_document::k_after));
if (resultNewer.get_ptr() == NULL) {
auto doc02 = DatabaseManagerAgent::convertGuildDataToDBDoc(workload.guildData);
newCollection.insert_one(std::move(doc02.extract()));
return newData;
}
break;
}
case (DatabaseWorkloadType::DISCORD_GUILD_READ): {
bsoncxx::builder::basic::document document{};
document.append(bsoncxx::builder::basic::kvp("_id", bsoncxx::types::b_int64(static_cast<Int64>(workload.guildData.guildId))));
auto resultNew = newCollection.find_one(document.view());
if (resultNew.get_ptr() != NULL) {
DiscordGuildData guildData = DatabaseManagerAgent::parseGuildData(*resultNew.get_ptr());
newData.discordGuild = guildData;
return newData;
} else {
return newData;
}
break;
}
case (DatabaseWorkloadType::DISCORD_GUILD_MEMBER_WRITE): {
auto doc = DatabaseManagerAgent::convertGuildMemberDataToDBDoc(workload.guildMemberData);
bsoncxx::builder::basic::document document{};
document.append(bsoncxx::builder::basic::kvp("_id", workload.guildMemberData.globalId.c_str()));
auto resultNewer = newCollection.find_one_and_replace(document.view(), std::move(doc.extract()),
mongocxx::v_noabi::options::find_one_and_replace{}.return_document(mongocxx::v_noabi::options::return_document::k_after));
if (resultNewer.get_ptr() == NULL) {
auto doc02 = DatabaseManagerAgent::convertGuildMemberDataToDBDoc(workload.guildMemberData);
newCollection.insert_one(std::move(doc02.extract()));
return newData;
}
break;
}
case (DatabaseWorkloadType::DISCORD_GUILD_MEMBER_READ): {
bsoncxx::builder::basic::document document{};
document.append(bsoncxx::builder::basic::kvp("_id", workload.guildMemberData.globalId.c_str()));
auto resultNew = newCollection.find_one(document.view());
if (resultNew.get_ptr() != NULL) {
DiscordGuildMemberData guildMemberData = DatabaseManagerAgent::parseGuildMemberData(*resultNew.get_ptr());
newData.discordGuildMember = guildMemberData;
return newData;
} else {
return newData;
}
break;
}
}
return newData;
} catch (...) {
reportException("DatabaseManagerAgent::run() Error: ");
return newData;
}
}
protected:
static mongocxx::instance instance;
static std::mutex workloadMutex01;
static mongocxx::pool thePool;
static Uint64 botUserId;
static bsoncxx::builder::basic::document convertUserDataToDBDoc(DiscordUserData& discordUserData) {
bsoncxx::builder::basic::document buildDoc;
try {
using bsoncxx::builder::basic::kvp;
buildDoc.append(kvp("_id", bsoncxx::types::b_int64(static_cast<Int64>(discordUserData.userId))));
buildDoc.append(kvp("userId", bsoncxx::types::b_int64(static_cast<Int64>(discordUserData.userId))));
buildDoc.append(kvp("userName", discordUserData.userName.c_str()));
buildDoc.append(kvp("currencyName", discordUserData.currencyName.c_str()));
buildDoc.append(kvp("hoursOfDepositCooldown", bsoncxx::types::b_int32(static_cast<Int32>(discordUserData.hoursOfDepositCooldown))));
buildDoc.append(kvp("hoursOfDrugSaleCooldown", bsoncxx::types::b_int32(static_cast<Int32>(discordUserData.hoursOfDrugSaleCooldown))));
buildDoc.append(kvp("hoursOfRobberyCooldown", bsoncxx::types::b_double(static_cast<Double>(discordUserData.hoursOfRobberyCooldown))));
buildDoc.append(kvp("botCommanders", [discordUserData](bsoncxx::builder::basic::sub_array subArray) {
for (auto& value: discordUserData.botCommanders) {
subArray.append(bsoncxx::types::b_int64(static_cast<Int64>(value)));
}
}));
return buildDoc;
} catch (...) {
reportException("DatabaseManagerAgent::convertUserDataToDBDoc()");
return buildDoc;
}
}
static DiscordUserData parseUserData(bsoncxx::document::value& docValue) {
DiscordUserData userData{};
try {
userData.userName = docValue.view()["userName"].get_utf8().value.to_string();
userData.currencyName = docValue.view()["currencyName"].get_utf8().value.to_string();
userData.hoursOfDepositCooldown = docValue.view()["hoursOfDepositCooldown"].get_int32();
userData.hoursOfDrugSaleCooldown = docValue.view()["hoursOfDrugSaleCooldown"].get_int32();
userData.hoursOfRobberyCooldown = static_cast<Float>(docValue.view()["hoursOfRobberyCooldown"].get_double());
userData.userId = docValue.view()["userId"].get_int64().value;
auto botCommandersArray = docValue.view()["botCommanders"].get_array();
std::vector<Uint64> newVector;
for (const auto& value: botCommandersArray.value) {
newVector.push_back(value.get_int64().value);
}
userData.botCommanders = newVector;
return userData;
} catch (...) {
reportException("DatabaseManagerAgent::parseUserData()");
return userData;
}
}
static bsoncxx::builder::basic::document convertGuildDataToDBDoc(DiscordGuildData& discordGuildData) {
bsoncxx::builder::basic::document buildDoc;
try {
using bsoncxx::builder::basic::kvp;
buildDoc.append(kvp("_id", bsoncxx::types::b_int64(static_cast<Int64>(discordGuildData.guildId))));
buildDoc.append(kvp("guildId", bsoncxx::types::b_int64(static_cast<Int64>(discordGuildData.guildId))));
buildDoc.append(kvp("guildName", discordGuildData.guildName.c_str()));
buildDoc.append(kvp("memberCount", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildData.memberCount))));
buildDoc.append(kvp("blackjackStack", [discordGuildData](bsoncxx::builder::basic::sub_array subArray) {
for (auto& value: discordGuildData.blackjackStack) {
subArray.append([value](bsoncxx::builder::basic::sub_document subDocument) {
subDocument.append(kvp("suit", value.suit.c_str()), kvp("type", value.type.c_str()),
kvp("value", bsoncxx::types::b_int32(static_cast<Int32>(value.value))));
});
}
}));
buildDoc.append(kvp("borderColor", discordGuildData.borderColor.c_str()));
buildDoc.append(kvp("gameChannelIds", [discordGuildData](bsoncxx::builder::basic::sub_array subArray) {
for (auto& value: discordGuildData.gameChannelIds) {
subArray.append(bsoncxx::types::b_int64(static_cast<Int64>(value)));
}
}));
buildDoc.append(kvp("guildShop", [discordGuildData](bsoncxx::builder::basic::sub_document subDocument) {
subDocument.append(kvp("items",
[discordGuildData](bsoncxx::builder::basic::sub_array subArray) {
for (const auto& value: discordGuildData.guildShop.items) {
subArray.append([value](bsoncxx::builder::basic::sub_document subDocument2) {
subDocument2.append(kvp("itemName", value.itemName.c_str()), kvp("itemCost", bsoncxx::types::b_int32(static_cast<Int32>(value.itemCost))),
kvp("selfMod", bsoncxx::types::b_int32(static_cast<Int32>(value.selfMod))), kvp("oppMod", bsoncxx::types::b_int32(static_cast<Int32>(value.oppMod))),
kvp("emoji", value.emoji.c_str()));
});
}
})),
subDocument.append(kvp("roles", [discordGuildData](bsoncxx::builder::basic::sub_array subArray) {
for (const auto& value: discordGuildData.guildShop.roles) {
subArray.append([value](bsoncxx::builder::basic::sub_document subDocument2) {
subDocument2.append(kvp("roleName", value.roleName.c_str()));
subDocument2.append(kvp("roleId", bsoncxx::types::b_int64(static_cast<Int64>(value.roleId))));
subDocument2.append(kvp("roleCost", bsoncxx::types::b_int32(static_cast<Int32>(value.roleCost))));
});
}
}));
}));
buildDoc.append(kvp("casinoStats", [discordGuildData](bsoncxx::builder::basic::sub_document subDocument) {
subDocument.append(kvp("largestBlackjackPayout", [discordGuildData](bsoncxx::builder::basic::sub_document subDoc2) {
subDoc2.append(kvp("amount", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildData.casinoStats.largestBlackjackPayout.amount))));
subDoc2.append(kvp("userId", bsoncxx::types::b_int64(static_cast<Int64>(discordGuildData.casinoStats.largestBlackjackPayout.userId))));
subDoc2.append(kvp("timeStamp", discordGuildData.casinoStats.largestBlackjackPayout.timeStamp.c_str()));
subDoc2.append(kvp("userName", discordGuildData.casinoStats.largestBlackjackPayout.userName.c_str()));
}));
subDocument.append(kvp("totalBlackjackPayout", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildData.casinoStats.totalBlackjackPayout))));
subDocument.append(kvp("largestCoinFlipPayout", [discordGuildData](bsoncxx::builder::basic::sub_document subDoc2) {
subDoc2.append(kvp("amount", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildData.casinoStats.largestCoinFlipPayout.amount))));
subDoc2.append(kvp("userId", bsoncxx::types::b_int64(static_cast<Int64>(discordGuildData.casinoStats.largestCoinFlipPayout.userId))));
subDoc2.append(kvp("timeStamp", discordGuildData.casinoStats.largestCoinFlipPayout.timeStamp.c_str()));
subDoc2.append(kvp("userName", discordGuildData.casinoStats.largestCoinFlipPayout.userName.c_str()));
}));
subDocument.append(kvp("totalCoinFlipPayout", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildData.casinoStats.totalCoinFlipPayout))));
subDocument.append(kvp("largestRoulettePayout", [discordGuildData](bsoncxx::builder::basic::sub_document subDoc2) {
subDoc2.append(kvp("amount", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildData.casinoStats.largestRoulettePayout.amount))));
subDoc2.append(kvp("userId", bsoncxx::types::b_int64(static_cast<Int64>(discordGuildData.casinoStats.largestRoulettePayout.userId))));
subDoc2.append(kvp("timeStamp", discordGuildData.casinoStats.largestRoulettePayout.timeStamp.c_str()));
subDoc2.append(kvp("userName", discordGuildData.casinoStats.largestRoulettePayout.userName.c_str()));
}));
subDocument.append(kvp("totalRoulettePayout", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildData.casinoStats.totalRoulettePayout))));
subDocument.append(kvp("largestSlotsPayout", [discordGuildData](bsoncxx::builder::basic::sub_document subDoc2) {
subDoc2.append(kvp("amount", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildData.casinoStats.largestSlotsPayout.amount))));
subDoc2.append(kvp("userId", bsoncxx::types::b_int64(static_cast<Int64>(discordGuildData.casinoStats.largestSlotsPayout.userId))));
subDoc2.append(kvp("timeStamp", discordGuildData.casinoStats.largestSlotsPayout.timeStamp.c_str()));
subDoc2.append(kvp("userName", discordGuildData.casinoStats.largestSlotsPayout.userName.c_str()));
}));
subDocument.append(kvp("totalSlotsPayout", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildData.casinoStats.totalSlotsPayout))));
subDocument.append(kvp("totalPayout", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildData.casinoStats.totalPayout))));
}));
buildDoc.append(kvp("rouletteGame", [discordGuildData](bsoncxx::builder::basic::sub_document subDocument) {
subDocument.append(kvp("rouletteBets",
[discordGuildData](bsoncxx::builder::basic::sub_array subArray) {
for (auto& value: discordGuildData.rouletteGame.rouletteBets) {
subArray.append([value](bsoncxx::builder::basic::sub_document subDoc2) {
subDoc2.append(kvp("betAmount", bsoncxx::types::b_int32(static_cast<Int32>(value.betAmount))));
subDoc2.append(kvp("betOptions", value.betOptions.c_str()));
subDoc2.append(kvp("betType", value.betType.c_str()));
subDoc2.append(kvp("userId", bsoncxx::types::b_int64(static_cast<Int64>(value.userId))));
subDoc2.append(kvp("payoutAmount", bsoncxx::types::b_int32(static_cast<Int32>(value.payoutAmount))));
subDoc2.append(kvp("winningNumbers", [value](bsoncxx::builder::basic::sub_array subArray2) {
for (auto& value2: value.winningNumbers) {
subArray2.append(value2.c_str());
}
}));
});
}
}),
kvp("currentlySpinning", bsoncxx::types::b_bool(discordGuildData.rouletteGame.currentlySpinning)));
}));
return buildDoc;
} catch (...) {
reportException("DatabaseManagerAgent::convertGuildDataToDBDoc()");
return buildDoc;
}
};
static DiscordGuildData parseGuildData(bsoncxx::document::value& docValue) {
DiscordGuildData guildData{};
try {
guildData.guildId = docValue.view()["guildId"].get_int64().value;
guildData.guildName = docValue.view()["guildName"].get_utf8().value.to_string();
guildData.memberCount = docValue.view()["memberCount"].get_int32().value;
if (docValue.view()["blackjackStack"].type() != bsoncxx::v_noabi::type::k_null) {
for (auto& value: docValue.view()["blackjackStack"].get_array().value) {
Card blackjackCard{};
blackjackCard.suit = value.get_document().view()["suit"].get_utf8().value.to_string();
blackjackCard.type = value.get_document().view()["type"].get_utf8().value.to_string();
blackjackCard.value = value.get_document().view()["value"].get_int32().value;
guildData.blackjackStack.push_back(blackjackCard);
}
}
guildData.borderColor = docValue.view()["borderColor"].get_utf8().value.to_string();
for (auto& value: docValue.view()["gameChannelIds"].get_array().value) {
guildData.gameChannelIds.push_back(value.get_int64().value);
}
for (auto& value: docValue.view()["guildShop"].get_document().value["items"].get_array().value) {
InventoryItem item;
item.emoji = value["emoji"].get_utf8().value.to_string();
item.itemCost = value["itemCost"].get_int32().value;
item.itemName = value["itemName"].get_utf8().value.to_string();
item.oppMod = value["oppMod"].get_int32().value;
item.selfMod = value["selfMod"].get_int32().value;
guildData.guildShop.items.push_back(item);
}
for (auto& value: docValue.view()["guildShop"].get_document().value["roles"].get_array().value) {
InventoryRole role;
role.roleCost = value["roleCost"].get_int32().value;
role.roleId = value["roleId"].get_int64().value;
role.roleName = value["roleName"].get_utf8().value.to_string();
guildData.guildShop.roles.push_back(role);
}
auto largestBlackjackPayout = docValue.view()["casinoStats"].get_document().value["largestBlackjackPayout"].get_document();
guildData.casinoStats.largestBlackjackPayout.amount = largestBlackjackPayout.value["amount"].get_int32().value;
guildData.casinoStats.largestBlackjackPayout.timeStamp = largestBlackjackPayout.value["timeStamp"].get_utf8().value.to_string();
guildData.casinoStats.largestBlackjackPayout.userId = largestBlackjackPayout.value["userId"].get_int64().value;
guildData.casinoStats.largestBlackjackPayout.userName = largestBlackjackPayout.value["userName"].get_utf8().value.to_string();
guildData.casinoStats.totalBlackjackPayout = docValue.view()["casinoStats"].get_document().value["totalBlackjackPayout"].get_int32().value;
auto largestCoinflipPayout = docValue.view()["casinoStats"].get_document().value["largestCoinFlipPayout"].get_document();
guildData.casinoStats.largestCoinFlipPayout.amount = largestCoinflipPayout.value["amount"].get_int32().value;
guildData.casinoStats.largestCoinFlipPayout.timeStamp = largestCoinflipPayout.value["timeStamp"].get_utf8().value.to_string();
guildData.casinoStats.largestCoinFlipPayout.userId = largestCoinflipPayout.value["userId"].get_int64().value;
guildData.casinoStats.largestCoinFlipPayout.userName = largestCoinflipPayout.value["userName"].get_utf8().value.to_string();
guildData.casinoStats.totalCoinFlipPayout = docValue.view()["casinoStats"].get_document().value["totalCoinFlipPayout"].get_int32().value;
auto largestRoulettePayout = docValue.view()["casinoStats"].get_document().value["largestRoulettePayout"].get_document();
guildData.casinoStats.largestRoulettePayout.amount = largestRoulettePayout.value["amount"].get_int32().value;
guildData.casinoStats.largestRoulettePayout.timeStamp = largestRoulettePayout.value["timeStamp"].get_utf8().value.to_string();
guildData.casinoStats.largestRoulettePayout.userId = largestRoulettePayout.value["userId"].get_int64().value;
guildData.casinoStats.largestRoulettePayout.userName = largestRoulettePayout.value["userName"].get_utf8().value.to_string();
guildData.casinoStats.totalRoulettePayout = docValue.view()["casinoStats"].get_document().value["totalRoulettePayout"].get_int32().value;
auto largestSlotsPayout = docValue.view()["casinoStats"].get_document().value["largestSlotsPayout"].get_document();
guildData.casinoStats.largestSlotsPayout.amount = largestSlotsPayout.value["amount"].get_int32().value;
guildData.casinoStats.largestSlotsPayout.timeStamp = largestSlotsPayout.value["timeStamp"].get_utf8().value.to_string();
guildData.casinoStats.largestSlotsPayout.userId = largestSlotsPayout.value["userId"].get_int64().value;
guildData.casinoStats.largestSlotsPayout.userName = largestSlotsPayout.value["userName"].get_utf8().value.to_string();
guildData.casinoStats.totalSlotsPayout = docValue.view()["casinoStats"].get_document().value["totalSlotsPayout"].get_int32().value;
guildData.casinoStats.totalPayout = docValue.view()["casinoStats"].get_document().value["totalPayout"].get_int32().value;
guildData.rouletteGame.currentlySpinning = docValue.view()["rouletteGame"].get_document().value["currentlySpinning"].get_bool().value;
for (auto& value: docValue.view()["rouletteGame"].get_document().value["rouletteBets"].get_array().value) {
RouletteBet rouletteBet{};
rouletteBet.betAmount = value.get_document().value["betAmount"].get_int32().value;
rouletteBet.betOptions = value.get_document().value["betOptions"].get_utf8().value.to_string();
rouletteBet.betType = value.get_document().value["betType"].get_utf8().value.to_string();
rouletteBet.payoutAmount = value.get_document().value["payoutAmount"].get_int32().value;
rouletteBet.userId = value.get_document().value["userId"].get_int64().value;
for (auto& value2: value.get_document().value["winningNumbers"].get_array().value) {
rouletteBet.winningNumbers.push_back(value2.get_utf8().value.to_string());
}
guildData.rouletteGame.rouletteBets.push_back(rouletteBet);
}
return guildData;
} catch (...) {
reportException("DatabaseManagerAgent::parseGuildData(), From Guild: " + guildData.guildName);
return guildData;
}
};
static bsoncxx::builder::basic::document convertGuildMemberDataToDBDoc(DiscordGuildMemberData& discordGuildMemberData) {
bsoncxx::builder::basic::document buildDoc;
try {
using bsoncxx::builder::basic::kvp;
buildDoc.append(kvp("guildMemberMention", discordGuildMemberData.guildMemberMention.c_str()));
buildDoc.append(kvp("_id", discordGuildMemberData.globalId.c_str()));
buildDoc.append(kvp("guildId", bsoncxx::types::b_int64(static_cast<Int64>(discordGuildMemberData.guildId))));
buildDoc.append(kvp("guildMemberId", bsoncxx::types::b_int64(static_cast<Int64>(discordGuildMemberData.guildMemberId))));
buildDoc.append(kvp("globalId", discordGuildMemberData.globalId.c_str()));
buildDoc.append(kvp("userName", discordGuildMemberData.userName.c_str()));
buildDoc.append(kvp("displayName", discordGuildMemberData.displayName.c_str()));
buildDoc.append(kvp("lastTimeRobbed", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildMemberData.lastTimeRobbed))));
buildDoc.append(kvp("lastTimeWorked", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildMemberData.lastTimeWorked))));
buildDoc.append(kvp("currency", [discordGuildMemberData](bsoncxx::builder::basic::sub_document subDocument) {
subDocument.append(kvp("bank", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildMemberData.currency.bank))),
kvp("wallet", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildMemberData.currency.wallet))),
kvp("timeOfLastDeposit", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildMemberData.currency.timeOfLastDeposit))));
}));
buildDoc.append(kvp("items",
[discordGuildMemberData](bsoncxx::builder::basic::sub_array subArray) {
for (Uint32 x = 0; x < discordGuildMemberData.items.size(); x += 1) {
subArray.append([discordGuildMemberData, x](bsoncxx::builder::basic::sub_document subDocument) {
subDocument.append(kvp("itemName", discordGuildMemberData.items.at(x).itemName.c_str()),
kvp("itemCost", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildMemberData.items.at(x).itemCost))),
kvp("selfMod", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildMemberData.items.at(x).selfMod))),
kvp("oppMod", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildMemberData.items.at(x).oppMod))),
kvp("emoji", discordGuildMemberData.items.at(x).emoji.c_str()));
});
}
})),
buildDoc.append(kvp("roles", [discordGuildMemberData](bsoncxx::builder::basic::sub_array subArray) {
for (Uint32 x = 0; x < discordGuildMemberData.roles.size(); x += 1) {
subArray.append([discordGuildMemberData, x](bsoncxx::builder::basic::sub_document subDocument) {
subDocument.append(kvp("roleName", discordGuildMemberData.roles.at(x).roleName.c_str()),
kvp("roleId", bsoncxx::types::b_int64(static_cast<Int64>(discordGuildMemberData.roles.at(x).roleId))),
kvp("roleCost", bsoncxx::types::b_int32(static_cast<Int32>(discordGuildMemberData.roles.at(x).roleCost))));
});
}
}));
return buildDoc;
} catch (...) {
reportException("DatabaseManagerAgent::convertGuildMemberDataToDBDoc()");
return buildDoc;
}
};
static DiscordGuildMemberData parseGuildMemberData(bsoncxx::document::value& docValue) {
DiscordGuildMemberData guildMemberData{};
try {
guildMemberData.guildMemberMention = docValue.view()["guildMemberMention"].get_utf8().value.to_string();
guildMemberData.guildId = docValue.view()["guildId"].get_int64().value;
guildMemberData.displayName = docValue.view()["displayName"].get_utf8().value.to_string();
guildMemberData.globalId = docValue.view()["globalId"].get_utf8().value.to_string();
guildMemberData.guildMemberId = docValue.view()["guildMemberId"].get_int64().value;
guildMemberData.userName = docValue.view()["userName"].get_utf8().value.to_string();
guildMemberData.lastTimeWorked = docValue.view()["lastTimeWorked"].get_int32().value;
guildMemberData.lastTimeRobbed = docValue.view()["lastTimeRobbed"].get_int32().value;
guildMemberData.currency.bank = docValue.view()["currency"].get_document().value["bank"].get_int32().value;
guildMemberData.currency.wallet = docValue.view()["currency"].get_document().value["wallet"].get_int32().value;
guildMemberData.currency.timeOfLastDeposit = docValue.view()["currency"].get_document().value["timeOfLastDeposit"].get_int32().value;
for (auto& value: docValue.view()["items"].get_array().value) {
InventoryItem item;
item.emoji = value["emoji"].get_utf8().value.to_string();
item.itemName = value["itemName"].get_utf8().value.to_string();
item.itemCost = value["itemCost"].get_int32().value;
item.oppMod = value["oppMod"].get_int32().value;
item.selfMod = value["selfMod"].get_int32().value;
guildMemberData.items.push_back(item);
}
for (auto& value: docValue.view()["roles"].get_array().value) {
InventoryRole role;
role.roleCost = value["roleCost"].get_int32().value;
role.roleId = value["roleId"].get_int64().value;
role.roleName = value["roleName"].get_utf8().value.to_string();
guildMemberData.roles.push_back(role);
}
return guildMemberData;
} catch (...) {
reportException("DatabaseManagerAgent::parseGuildMemberData()");
return guildMemberData;
}
}
};
class DiscordUser {
public:
static int32_t guildCount;
DiscordUserData data{};
DiscordUser(String userNameNew, Uint64 userIdNew) {
this->data.userId = userIdNew;
this->data.userName = userNameNew;
this->getDataFromDB();
this->data.userId = userIdNew;
this->data.userName = userNameNew;
}
void writeDataToDB() {
DatabaseWorkload workload{};
workload.workloadType = DatabaseWorkloadType::DISCORD_USER_WRITE;
workload.userData = this->data;
DatabaseManagerAgent::submitWorkloadAndGetResults(workload);
}
void getDataFromDB() {
DatabaseWorkload workload{};
workload.workloadType = DatabaseWorkloadType::DISCORD_USER_READ;
workload.userData = this->data;
auto result = DatabaseManagerAgent::submitWorkloadAndGetResults(workload);
if (result.discordUser.userId != 0) {
this->data = result.discordUser;
}
}
};
class DiscordGuild {
public:
DiscordGuildData data{};
DiscordGuild(GuildData guildData) {
this->data.guildId = guildData.id;
this->data.guildName = guildData.name;
this->data.memberCount = guildData.memberCount;
this->getDataFromDB();
this->data.guildId = guildData.id;
this->data.guildName = guildData.name;
this->data.memberCount = guildData.memberCount;
}
void writeDataToDB() {
DatabaseWorkload workload{};
workload.workloadType = DatabaseWorkloadType::DISCORD_GUILD_WRITE;
workload.guildData = this->data;
DatabaseManagerAgent::submitWorkloadAndGetResults(workload);
}
void getDataFromDB() {
DatabaseWorkload workload{};
workload.workloadType = DatabaseWorkloadType::DISCORD_GUILD_READ;
workload.guildData = this->data;
auto result = DatabaseManagerAgent::submitWorkloadAndGetResults(workload);
if (result.discordGuild.guildId != 0) {
this->data = result.discordGuild;
}
}
};
class DiscordGuildMember {
public:
DiscordGuildMemberData data{};
DiscordGuildMember(GuildMemberData guildMemberData) {
this->data.guildMemberId = guildMemberData.id;
this->data.guildId = guildMemberData.guildId;
this->data.globalId = std::to_string(this->data.guildId) + " + " + std::to_string(this->data.guildMemberId);
this->getDataFromDB();
if (guildMemberData.nick == "") {
this->data.displayName = guildMemberData.getUserData().userName;
this->data.guildMemberMention = "<@" + std::to_string(this->data.guildMemberId) + ">";
} else {
this->data.displayName = guildMemberData.nick;
this->data.guildMemberMention = "<@!" + std::to_string(this->data.guildMemberId) + ">";
}
this->data.userName = guildMemberData.getUserData().userName;
}
void writeDataToDB() {
DatabaseWorkload workload{};
workload.workloadType = DatabaseWorkloadType::DISCORD_GUILD_MEMBER_WRITE;
workload.guildMemberData = this->data;
DatabaseManagerAgent::submitWorkloadAndGetResults(workload);
}
void getDataFromDB() {
DatabaseWorkload workload{};
workload.workloadType = DatabaseWorkloadType::DISCORD_GUILD_MEMBER_READ;
workload.guildMemberData = this->data;
auto result = DatabaseManagerAgent::submitWorkloadAndGetResults(workload);
if (result.discordGuildMember.globalId != "") {
this->data = result.discordGuildMember;
}
}
};
mongocxx::instance DatabaseManagerAgent::instance{};
mongocxx::pool DatabaseManagerAgent::thePool{ mongocxx::uri{} };
std::mutex DatabaseManagerAgent::workloadMutex01{};
Uint64 DatabaseManagerAgent::botUserId{ 0 };
Int32 DiscordUser::guildCount{ 0 };
}