-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.js
1150 lines (1076 loc) · 28.2 KB
/
db.js
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const mongo = require('./mongo.js');
const { exec } = require('child_process');
const songbird = require("./songbird.js");
let db_wait = mongo.getDb();
let claims, month_end, milestones, users, linked_websites, domains, coinflip_pvp, coinflip_pvh, airdrop_one, tip_stats, user_settings, month_claim_count;
let ready = false;
const MIN_ACHIEVEMENT_TIP = 50;
db_wait.then(([db, tipbot_db]) => {
ready = true;
console.log("Connected to db")
claims = db.collection("claims");
month_end = db.collection("month_end");
milestones = db.collection("milestones");
users = db.collection("users");
linked_websites = db.collection("linked_websites");
domains = db.collection("domains");
coinflip_pvp = db.collection("coinflip_pvp");
coinflip_pvh = db.collection("coinflip_pvh");
airdrop_one = db.collection("airdrop_one");
tip_stats = tipbot_db.collection("tip_stats");
user_settings = tipbot_db.collection("user_settings");
month_claim_count = db.collection("month_claim_count");
});
const INITIAL_ACHIEVEMENT_DATA = {
faucet: {
total: 0,
current_streak: 0,
longest_streak: 0
},
messages: 0,
tips: {
xac_amount: 0 //amount of xac tips made, not total amount
},
coinflip: {
wins: 0
},
gatcha_won_xac_amount: 0,
};
setTimeout(async function() {
//one off migrations
//users.updateMany({}, { $set: { achievements: [], achievement_data: INITIAL_ACHIEVEMENT_DATA } });
/*const all = JSON.parse(require("fs").readFileSync("./utility_scripts/faucet_users_count.json"));
for (const u of await get_all_users()) {
console.log(all[u.address.toLowerCase()] ?? 0);
users.updateOne({ user: u.user }, { $set: { "achievement_data.faucet.total": all[u.address.toLowerCase()] ?? 0 } });
}*/
/*for (const u of await get_all_users()) {
if (u.achievement_data.faucet.total === null) {
console.log("fix");
users.updateOne({ user: u.user }, { $set: { "achievement_data.gatcha_won_xac_amount": 0,"achievement_data.faucet.total": 0 } });
} else {
users.updateOne({ user: u.user }, { $set: { "achievement_data.gatcha_won_xac_amount": 0 } });
}
}*/
if (!ready) {
exec("kill 1");
}
}, 5000);
const CLAIM_FREQ = 23.5*60*60*1000;
const MAX_CLAIMS_PER_MONTH = 11111;
//march 2023
const START_YEAR = 2023;
const START_MONTH = 2;
//6000 initially
const START_PAYOUT = 6000;
//return number of months since start of distribution
//starts at month 0
function get_month() {
let date = new Date();
let years = date.getUTCFullYear()-START_YEAR;
let months = date.getUTCMonth()-START_MONTH;
return years*12+months;
}
function get_amount_for_month(month_num) {
let halvings = Math.floor(month_num/6);
let payout = START_PAYOUT;
//payout halves every six months
for (let i=0; i < halvings; i++) {
payout = payout/2;
}
return payout;
}
//get amount to payout, for current month
function get_amount() {
return get_amount_for_month(get_month());
}
async function get_claims_this_month() {
let claims_array = await claims.find({"month": get_month()});
claims_array = await claims_array.toArray();
let claims_num = 0;
for (let i=0; i < claims_array.length; i++) {
claims_num += claims_array[i].claims_this_month;
}
return claims_num;
}
async function milestone_check(send_announcement) {
let current_month = get_month();
//check if monthly claims reset
let month_reset = await milestones.findOne({
type: "month_reset"
});
if (!month_reset) {
await milestones.insertOne({
type: "month_reset",
month: -1
});
month_reset = {
type: "month_reset",
month: -1
};
}
if (month_reset.month !== current_month) {
await send_announcement("It's a new month! Claims have been reset!");
if (current_month % 6 === 0) {
await send_announcement("Payouts have been halved! The faucet now gives out "+String(get_amount())+" XAC.");
}
//calculate burn
let claims = await get_month_claim_count(current_month - 1);
if (claims < MAX_CLAIMS_PER_MONTH && !isNaN(claims)) {
const burn_amount = (MAX_CLAIMS_PER_MONTH - claims) * get_amount_for_month(current_month - 1);
const burn_address = "0x1111111111111111111111111111111111111111";
const tx = await songbird.faucet_send_astral(burn_address, burn_amount);
await send_announcement(`Burned ${burn_amount} excess XAC: https://songbird-explorer.flare.network/tx/${tx}`);
}
month_reset.month = current_month;
await milestones.replaceOne({
type: "month_reset"
}, month_reset);
}
//last 500 uses of faucet
let last_uses = await milestones.findOne({
type: "last_uses"
});
if (!last_uses) {
await milestones.insertOne({
type: "last_uses",
month: -1
});
last_uses = {
type: "last_uses",
month: -1
};
}
if (last_uses.month !== current_month) {
let remaining_claims = MAX_CLAIMS_PER_MONTH - await get_claims_this_month();
if (remaining_claims <= 500) {
await send_announcement("Less than 500 claims remaining this month!");
last_uses.month = current_month;
await milestones.replaceOne({
type: "last_uses"
}, last_uses);
}
}
}
async function get_claims_all_time() {
let claims_array = await claims.find({});
claims_array = await claims_array.toArray();
let claims_num = 0;
for (let i=0; i < claims_array.length; i++) {
claims_num += claims_array[i].claims;
}
return claims_num;
}
async function get_claims_last_day() {
let claims_array = await claims.find({
last_claim: {
"$gt": Date.now()-24*60*60*1000
}
});
claims_array = await claims_array.toArray();
return claims_array.length;
}
async function get_unique_claimers() {
let claims_array = await claims.find({});
claims_array = await claims_array.toArray();
return claims_array.length;
}
async function find_claim(address) {
address = address.trim().toLowerCase();
return await claims.findOne({"address": address});
}
async function get_faucet_stats() {
/*
- total claims this month
- month #
- current payout
- total unique addresses that ever claimed faucet
- total claims for user (if they enter in address)
- total claims
*/
return {
month: get_month(),
amount: get_amount(),
claims_this_month: await get_claims_this_month(),
unique_claimers: await get_unique_claimers(),
total_claims: await get_claims_all_time(),
claims_last_day: await get_claims_last_day(),
};
}
function get_month_start_timestamp(month_num) {
let next_year = START_YEAR+Math.floor((month_num+START_MONTH)/12);
let next_calendar_month = (month_num+START_MONTH+1)%12 || 12; //if 0, that means it is 12th month, not 0th
return (new Date(`${next_year}-${next_calendar_month}-01`)).getTime();
}
function get_next_month_timestamp() {
return get_month_start_timestamp(get_month() + 1);
}
function get_next_halving_timestamp() {
let current_date = new Date();
let months_till_halving = 6-(get_month()%6);
return (new Date(Date.UTC(current_date.getUTCFullYear(), current_date.getUTCMonth()+months_till_halving))).getTime();
}
//won't work for current month, or before month 15 (before month 15 should all be 11111 anyways, iirc)
async function get_month_claim_count(month) {
let result = (await month_claim_count.findOne({
month,
}));
return result.count;
}
async function set_month_claim_count(count) {
let month = get_month();
await month_claim_count.replaceOne({
month
}, {
month,
count,
}, {
upsert: true
});
}
async function get_month_end() {
let result = (await month_end.findOne({
month: get_month() - 1,
}));
return result ? result.end : get_month_start_timestamp(get_month());
}
async function set_month_end() {
await month_end.insertOne({
month: get_month(),
end: Date.now(),
});
}
async function get_next_claim_time(address) {
let user_info = await find_claim(address);
let claims_this_month = await get_claims_this_month();
let next_claim_time = 0;
let enough_time = true;
let under_claim_limit = true;
if (claims_this_month >= MAX_CLAIMS_PER_MONTH) {
under_claim_limit = false;
next_claim_time = get_next_month_timestamp();
}
if (user_info) {
if (user_info.last_claim+CLAIM_FREQ > Date.now()) {
if (user_info.last_claim+CLAIM_FREQ > next_claim_time) {
next_claim_time = user_info.last_claim+CLAIM_FREQ;
}
enough_time = false;
//when new month starts claim cooldown resets no matter what
if (Number(user_info.last_claim) < get_month_start_timestamp(get_month())) {
enough_time = true;
}
}
}
next_claim_time = Math.ceil(next_claim_time/1000);
return {
enough_time,
under_claim_limit,
next_claim_time
};
}
async function calculate_burned() {
let burned = 0;
//first burn was month 15 so start from there
let c_month = get_month(); //current month
for (let m = 15; m < c_month; m++) {
let claims = await get_month_claim_count(m);
if (claims !== MAX_CLAIMS_PER_MONTH) {
burned += (MAX_CLAIMS_PER_MONTH - claims) * get_amount_for_month(m);
}
}
return burned;
}
async function get_all_users() {
return await (await users.find()).toArray();
}
async function count_users() {
return await users.countDocuments({});
}
async function get_user_by_address(address) {
//return address
return await users.findOne({
address,
});
}
async function get_user(user_id) {
//return address
return await users.findOne({
user: user_id,
});
}
//also handle changing addresses
async function register_user(user_id, address, change=false) {
address = address.trim().toLowerCase();
let address_used = await get_user_by_address(address);
if (address_used) {
return false;
}
let user_info = await get_user(user_id);
if (user_info) {
//replace
if (change) {
//insert
user_info.address = address;
await users.replaceOne({
user: user_id,
}, user_info);
} else {
return false;
}
} else {
await users.insertOne({
user: user_id,
address: address,
achievements: [],
//data needed for achievements
achievement_data: INITIAL_ACHIEVEMENT_DATA,
});
}
return true;
}
const ACHIEVEMENTS = {
//activity achievements
"activity-1": {
id: "activity-1",
name: "Comet",
description: "Chat activity level 1",
prize: 100,
role: false, //or role id
},
"activity-2": {
id: "activity-2",
name: "Dwarf Planet",
description: "Chat activity level 2",
prize: 500,
role: false,
},
"activity-3": {
id: "activity-3",
name: "Planet",
description: "Chat activity level 3",
prize: 1000,
role: false,
},
"activity-4": {
id: "activity-4",
name: "Star",
description: "Chat activity level 4",
prize: 3000,
role: false,
},
"activity-5": {
id: "activity-5",
name: "Nebula",
description: "Chat activity level 5",
prize: 5000,
role: false,
},
"activity-6": {
id: "activity-6",
name: "Supernova",
description: "Chat activity level 6",
prize: 10000,
role: false,
},
//faucet achievements
"faucet-2": {
id: "faucet-2",
name: "The Journey Begins",
description: "Have a 2 day faucet streak!",
prize: 500,
role: false,
},
"faucet-10": {
id: "faucet-10",
name: "Jump Into Hyperspace",
description: "Have a 10 day faucet streak!",
prize: 2000,
role: false,
},
"faucet-30": {
id: "faucet-30",
name: "Beam Me Up, Scotty",
description: "Have a 30 day faucet streak!",
prize: 6000,
role: false,
},
"faucet-50": {
id: "faucet-50",
name: "The Restaurant at the End of the Universe",
description: "Have a 50 day faucet streak!",
prize: 10000,
role: "1211426757828157440", //Faucet 50
},
"faucet-100": {
id: "faucet-100",
name: "Alpha Centauri",
description: "Have a 100 day faucet streak!",
prize: 15000,
role: "1211425830991958037", //Faucet 100
},
"faucet-365": {
id: "faucet-100",
name: "Kwisatz Haderach",
description: "Have a 365 day faucet streak! Wow!",
prize: 25000,
role: "1211426853055758396", //Faucet 365
},
"claims-250": {
id: "claims-250",
name: "Planet Express",
description: "Claim from the faucet 250 times! Don't get frozen for 1000 years, cause the faucet only lasts for 15.5.",
prize: 5000,
role: false,
},
"claims-500": {
id: "claims-500",
name: "Speaker for the Dead",
description: "Claim from the faucet 500 times! Thank you.",
prize: 10000,
role: false,
},
//tipping achievements
"tipper-1": {
id: "tipper-1",
name: "First tip!",
description: `First tip over ${MIN_ACHIEVEMENT_TIP} XAC. So you learned how to use the tipbot?`,
prize: 100,
role: false,
},
"tipper-2": {
id: "tipper-2",
name: "Tip Novice",
description: "Make 10 XAC tips!",
prize: 500,
role: false,
},
"tipper-3": {
id: "tipper-3",
name: "Tip Pro",
description: "Make 25 XAC tips!",
prize: 1000,
role: false,
},
"tipper-4": {
id: "tipper-4",
name: "Tip Master",
description: "Make 100 XAC tips. So, why are you building a clock?",
prize: 3000,
role: "1211403167158501448", //Tip Master
},
"tipper-5": {
id: "tipper-5",
name: "Tip Grandmaster",
description: "Make 200 XAC tips. Hey! The replicator is for printing food, not money!",
prize: 5000,
role: "1211404005633294396", //Tip Grandmaster
},
"tipper-6": {
id: "tipper-6",
name: "Galactic Philanthropist",
description: "Make 300 XAC tips. I hear Magrathea is coming out of hibernation just for you...",
prize: 10000,
role: "1211404404356423730", //Galactic Philanthropist
},
//coinflip achievements
"coinflip-1": {
id: "coinflip-1",
name: "First coinflip win!",
description: "First win in coinflip!",
prize: 100,
role: false,
},
"coinflip-2": {
id: "coinflip-2",
name: "Space Vegas",
description: "10 coinflip wins! Where is Space Frank Sinatra?",
prize: 1000,
role: false,
},
"coinflip-3": {
id: "coinflip-3",
name: "Coinflip Duelist",
description: "Have 50 coinflip wins.",
prize: 10000,
role: false,
},
//nft achievements
"nft-1": {
id: "nft-1",
name: "NFT Citizen",
description: "Own any Astral Credits NFT. Welcome!",
prize: 1000,
role: false,
},
"nft-2": {
id: "nft-2",
name: "NFT Magnate",
description: "Own 10k SGB worth of Astral Credits NFTs. You really like them, huh?",
prize: 5000,
role: "1211411402187743272", //NFT Magnate
},
"nft-all": {
id: "nft-all",
name: "Diamond Supporter",
description: "Have one of each NFT badge! Gotta collect them all!",
prize: 10000,
role: "1211411950760632430", //Diamond Supporter
},
//captcha gatcha related
"gatcha-jackpot": {
id: "gatcha-jackpot",
name: "Lucky Pull",
description: "Win 7500 XAC or more from one captcha gatcha payout",
prize: 2500,
role: false,
},
"gatcha-1": {
id: "gatcha-1",
name: "W0rd5 4 C45h",
description: "Win 1000 XAC total through captcha gatcha",
prize: 500,
role: false,
},
"gatcha-2": {
id: "gatcha-2",
name: "Nishi's Gatcha",
description: "Win 10000 XAC total through captcha gatcha",
prize: 1000,
role: false,
},
"gatcha-3": {
id: "gatcha-3",
name: "Gotta Keep Pulling",
description: "Win 50000 XAC total through captcha gatcha",
prize: 5000,
role: false,
},
//tipbot related, non-tipbot
"coin-collector": {
id: "coin-collector",
name: "Numismatist",
description: "Hold 10 or more supported currencies in your tipbot wallet",
prize: 500,
role: false,
},
//
//one offs (pixel planet user, discord booster, triforce delegator, xac millionaire)
"pixel-planet": {
id: "pixel-planet",
name: "Pixel Planet Painter",
description: "Paint a pixel in [Pixel Planet](https://www.astralcredits.xyz/pixels)! Updates every three hours.",
prize: 500,
role: false,
},
"booster": {
id: "booster",
name: "Team Rocket",
description: "Support by boosting the Discord server. Thank you!",
prize: 10000,
role: false,
},
"millionaire": {
id: "millionaire",
name: "XAC Millionaire",
description: "Hold 1 million XAC!",
prize: 0,
role: "1222007670199029800",
},
"triforce-delegator": {
id: "triforce-delegator",
name: "Triforce Delegator",
description: "Delegate at least 50% of your WSGB to the Triforce FTSO.", //currently doesn't require a minimum wsgb amount
prize: 1500,
role: "1222008917739962522",
},
//
}
//returns false is user already has achievement
async function add_achievement_db(user_id, achievement_id, cached_user) {
//save on db calls
if (cached_user.achievements?.includes(achievement_id)) {
return false;
}
await users.updateOne({
user: user_id,
achievements: {
$not: {
$in: [
achievement_id,
],
},
},
}, {
$push: {
achievements: achievement_id,
}
});
return true;
}
//faucet achievement info
async function add_claim_achievement_info(user_id, cached_user, last_claim) {
let override = false; //true if first of the month and person claimed last day of last month
let override_days; //downtime days to add to streak
//if first day of month
if ((new Date()).getUTCDate() === 1) {
let month_end_timestamp = await get_month_end();
//if last claim was made on last claiming day of the last month
if (last_claim > month_end_timestamp - CLAIM_FREQ - 30 * 60 * 1000) {
override = true;
override_days = Math.floor((Date.now() - last_claim) / (24 * 60 * 60 * 1000));
if (override_days === 0) {
override_days = 1;
}
}
}
//if their last claim was less than 2 days ago, streak continues
//claim freq is 23.5 hours, give them an extra 30 minutes to claim; so up to 24 hours after they are eligible to claim, they can claim again without losing streak
let update = {
$inc: {
"achievement_data.faucet.total": 1,
},
};
if ((last_claim + CLAIM_FREQ * 2 + 30 * 60 * 1000 >= Date.now()) || override) {
update["$inc"]["achievement_data.faucet.current_streak"] = override_days ?? 1;
if (cached_user.achievement_data.faucet.longest_streak < cached_user.achievement_data.faucet.current_streak + (override_days ?? 1)) {
//new longest streak
update["$inc"]["achievement_data.faucet.longest_streak"] = override_days ?? 1;
}
await users.updateOne({
user: user_id,
}, update);
} else {
update["$set"] = {
"achievement_data.faucet.current_streak": 1,
};
await users.updateOne({
user: user_id,
}, update);
}
}
async function increment_message_achievement_info(user_id) {
await users.updateOne({
user: user_id,
}, {
$inc: {
"achievement_data.messages": 1,
}
});
}
async function increment_xac_tips_achievement_info(user_id) {
await users.updateOne({
user: user_id,
}, {
$inc: {
"achievement_data.tips.xac_amount": 1,
}
});
}
async function increment_coinflip_wins_achievement_info(user_id) {
await users.updateOne({
user: user_id,
}, {
$inc: {
"achievement_data.coinflip.wins": 1,
}
});
}
async function increase_gatcha_achievement_info(user_id, amount) {
await users.updateOne({
user: user_id,
}, {
$inc: {
"achievement_data.gatcha_won_xac_amount": amount,
}
});
}
//insert or replace
async function add_claim(address, amount) {
address = address.trim().toLowerCase();
let claim_exists = await find_claim(address);
if (claim_exists) {
let current_month = get_month();
if (claim_exists.month !== current_month) {
claim_exists.claims_this_month = 0;
}
claim_exists.claims_this_month += 1;
claim_exists.claims += 1;
claim_exists.amount = amount;
claim_exists.month = current_month;
claim_exists.last_claim = Date.now();
await claims.replaceOne({ address: address }, claim_exists);
} else {
await claims.insertOne({
address: address,
amount: amount,
last_claim: Date.now(),
month: get_month(),
claims_this_month: 1,
claims: 1
});
}
}
//linked websites stuff
async function get_linked_website(address) {
return await linked_websites.findOne({
address
});
}
async function add_linked_website(address, url) {
let current_linked = await get_linked_website(address);
if (!current_linked) {
await linked_websites.insertOne({
address,
url
});
} else {
await linked_websites.replaceOne({
address
}, {
address,
url
});
}
}
async function remove_linked_website(address) {
await linked_websites.deleteOne({
address
});
}
async function check_domain_by_domain(domain) {
return domains.findOne({
domain,
});
}
async function check_domain_by_user(user) {
return domains.findOne({
user,
});
}
async function add_domain(user, domain, address, replace=false) {
if (replace) {
await domains.replaceOne({
user,
}, {
user,
domain,
address,
});
} else {
await domains.insertOne({
user,
domain,
address,
});
}
}
async function get_all_domains() {
return (await domains.find({}, { projection: { _id: 0 } })).toArray();
}
async function add_coinflip_pvp(interaction_id, player1_id, wager, server_nonce, pick) {
await coinflip_pvp.insertOne({
bet_id: interaction_id,
pick, //player 1's pick of 'heads' or 'tails'
player1: {
player_id: player1_id,
},
wager,
server_nonce,
});
}
async function get_coinflip_pvp(bet_id) {
return await coinflip_pvp.findOne({
bet_id,
});
}
async function add_coinflip_pvp_random(bet_id, player_id, player_random) {
let coinflip_info = await get_coinflip_pvp(bet_id);
if (coinflip_info.player1.player_id === player_id) {
return await coinflip_pvp.updateOne(
{
bet_id,
"player1.random": {
$exists: false,
},
},
{
$set: {
"player1.random": player_random,
},
}
);
} else {
if (coinflip_info.player2) return false;
//Create player2
return await coinflip_pvp.updateOne(
{
bet_id,
player2: {
$exists: false,
},
},
{
$set: {
player2: {
player_id: player_id,
random: player_random,
},
},
}
);
}
}
async function mark_coinflip_pvp_finished(bet_id) {
return await coinflip_pvp.updateOne(
{
bet_id,
finished: {
$exists: false,
},
},
{
$set: {
finished: true,
},
}
);
}
async function add_coinflip_pvh(interaction_id, player_id, wager, server_nonce, pick) {
await coinflip_pvh.insertOne({
bet_id: interaction_id,
pick, //player's pick of 'heads' or 'tails'
player_id,
wager,
server_nonce,
});
}
async function get_coinflip_pvh(bet_id) {
return await coinflip_pvh.findOne({
bet_id,
});
}
//could be one db call but whatever
async function add_coinflip_pvh_random(bet_id, player_random) {
return await coinflip_pvh.updateOne(
{
bet_id,
player_random: {
$exists: false,
},
},
{
$set: {
player_random,
},
}
);
}
async function airdrop_find(address) {
return airdrop_one.findOne({
address,
});
}
async function airdrop_insert(address) {
return airdrop_one.insertOne({
address,
});
}
async function get_all_linked_websites() {
try {
return await (await linked_websites.find({})).toArray();
} catch(e) {
console.log(e)
//database not connected yet
return {};
}
}
async function get_top_achievementeers() {
//limit?
return await users.aggregate([
{
$project: {
_id: 0,
user: 1,
length: {
$size: "$achievements",
}
}
},
{
$sort: {
length: -1,
}
}
]);
}
async function get_top_claimers() {
//limit?
return await users.aggregate([
{
$project: {
_id: 0,
user: 1,
"achievement_data.faucet.total": 1,
}
},
{
$sort: {
"achievement_data.faucet.total": -1,
}
}
]);
}
/*tip stats schema
{
user: string (discord user id),
usd_value: number, //usd value tipped (in 1/10s of a cent)
//add up all the type_count to get total tips
type_count: {
tip: number,
active_tip: number,
role_tip: number,
active_rain: number,
role_rain: number,
},