-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdicer.cpp
executable file
·2301 lines (2013 loc) · 89.9 KB
/
dicer.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
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
#include <vector>
#include <ctype.h>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <eosiolib/eosio.hpp>
#include <eosiolib/asset.hpp>
#include <eosiolib/types.hpp>
#include <eosiolib/action.hpp>
#include <eosiolib/symbol.hpp>
#include <eosiolib/crypto.h>
#include <eosiolib/transaction.hpp>
#include <boost/functional/hash.hpp>
#define CORE_TOKEN S(4, EOS) //EAST EOS
#define CORE_ACCOUNT N(eosio.token) //shadowbanker eosio.token
#define TEAM_TOKEN S(4, ESA) //TEST ESA
#define TEAM_ACCOUNT N(eosiodrizzle)
#define TEAM_SHADOWS N(eosioshadows)
#define TEAM_SERVICE N(shadowserver)
#define TEAM_BANKERS N(shadowbanker)
#define TEAM_BETLOGS N(dicerlogs.e)
#define TEAM_BLACKHOLE N(blackhole.e)
#define WIN96_MINING N(winninetysix)
#define UTC_BASE_YEAR 1970
#define MONTH_PER_YEAR 12
#define DAY_PER_YEAR 365
#define SEC_PER_DAY 86400
#define SEC_PER_HOUR 3600
#define SEC_PER_MIN 60
using namespace std;
using namespace eosio;
class shadowsdicer : public contract {
private:
const unsigned char g_day_per_mon[MONTH_PER_YEAR] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const string SYSTEM_KEY = "EOS7eEvCovjsN6QzJfhhyu5K6Q5WCeVFfoxEJLFnvNi1ZbXWuFqZp";
const string SYSTEM_REMARK = "--- 影骰是一款集合(骰子+接龙+竞拍+分红)的公平游戏,采用玩家可验证的算法生成随机数,庄家绝对无法操控骰子点数,官网:http://idice.app。";
const int8_t BASE58_MAP[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7,
8, -1, -1, -1, -1, -1, -1, -1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18,
19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1,
-1, -1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, 47, 48,
49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1,
};
typedef struct
{
unsigned short nYear;
unsigned char nMonth;
unsigned char nDay;
unsigned char nHour;
unsigned char nMin;
unsigned char nSec;
unsigned char DayIndex; /* 0 = Sunday */
} mytime_struct;
struct weight{
uint64_t rank;
account_name owner;
EOSLIB_SERIALIZE(weight, (rank)(owner))
};
// @abi table settings i64
struct setting{
uint64_t id; // Id
uint64_t min_bet; // 最低下注
uint64_t max_bet; // 最高下注
uint64_t dice_max; // 骰子点数
uint64_t dice_edge; // 庄家优势
uint64_t dice_water; // 庄家抽水
uint64_t is_staking; // 允许质押
uint64_t is_mining; // 允许挖矿
uint64_t is_betting; // 允许下注
uint64_t is_bidding; // 允许竞拍
uint64_t team_ratio; // 平台比例
uint64_t stake_ratio; // 质押比例
uint64_t system_ratio; // 系统比例
uint64_t dragon_ratio; // 接龙比例
uint64_t lucky_ratio; // 幸运比例
uint64_t shadows_ratio; // 简影比例
uint64_t referrer_ratio; // 推荐比例
uint64_t bidding_ratio; // 竞拍比例
uint64_t bidding_losing; // 竞拍损失
uint64_t bidding_offset; // 竞拍时间间隔
uint64_t bidding_minimum; // 竞拍最小金额
uint64_t bidding_increase; // 竞拍增长比例
uint64_t bidding_min_bonus; // 竞拍最大奖励
uint64_t bidding_max_bonus; // 竞拍最大奖励
uint64_t luck_time_offset; // 接龙权重奖池时间间隔
uint64_t luck_time_increase; // 接龙权重时间增加数量
uint64_t stake_time_offset; // 质押时间间隔
uint64_t stake_time_redeem; // 质押赎回小时
uint64_t stake_dividend_ratio;// 质押分红比例
uint64_t hand_card_limit; // 出牌限额
uint64_t max_payout_amount; // 最大赔付金额
uint64_t mining_supply_amount;// 挖矿供应量
uint64_t primary_key() const { return id; }
EOSLIB_SERIALIZE(setting, (id)(min_bet)(max_bet)(dice_max)(dice_edge)(dice_water)(is_staking)(is_mining)(is_betting)(is_bidding)(team_ratio)(stake_ratio)(system_ratio)(dragon_ratio)(lucky_ratio)(shadows_ratio)(referrer_ratio)(bidding_ratio)(bidding_losing)(bidding_offset)(bidding_minimum)(bidding_increase)(bidding_min_bonus)(bidding_max_bonus)(luck_time_offset)(luck_time_increase)(stake_time_offset)(stake_time_redeem)(stake_dividend_ratio)(hand_card_limit)(max_payout_amount)(mining_supply_amount))
};
typedef multi_index<N(settings), setting> _setting;
_setting settings;
// @abi table games i64
struct game{
uint64_t id; // Id
uint64_t bid_jackpot; // 竞拍奖池
uint64_t locked_balance; // 锁定金额
uint64_t stake_time; // 质押倒计时
uint64_t stake_owner; // 质押分红Id
uint64_t stake_token; // 质押ESA
uint64_t stake_token_mirror; // 质押ESA
uint64_t stake_jackpot; // 质押奖池
uint64_t stake_jackpot_mirror;// 质押奖池镜像
uint64_t dragon_jackpot; // 接龙奖池
vector<weight> dragon_ranks; // 接龙排行
uint64_t dragon_lucktime; // 接龙幸运倒计时
uint64_t dragon_luckround; // 接龙幸运场次
account_name dragon_luckowner;// 接龙幸运获得者
uint64_t dragon_luckpot; // 接龙幸运奖池
uint64_t dragon_luckpot_mirror;// 质押奖池镜像
uint64_t total_team_fee; // 平台分红
uint64_t total_stake_fee; // 质押分红
uint64_t total_system_fee; // 系统分红
uint64_t total_dragon_fee; // 接龙分红
uint64_t total_luck_fee; // 幸运分红
uint64_t total_shadows_fee; // 简影分红
uint64_t total_bidding_fee; // 竞拍分红
uint64_t total_order_count; // 总注单数
uint64_t total_player_count; // 总玩家数
uint64_t total_dragon_count; // 接龙数量
uint64_t total_bidding_count; // 竞拍数量
uint64_t total_drawing_count; // 总结算数量
uint64_t total_transfer_count;// 总交易数量
uint64_t total_order_amount; // 总下注额
uint64_t total_mining_amount; // 总挖矿额
uint64_t total_destroy_amount;// 总销毁矿产
uint64_t total_supply_amount; // 赔付供应量
uint64_t total_dividend_amount; // 系统总分红
uint64_t primary_key() const { return id; }
EOSLIB_SERIALIZE(game, (id)(bid_jackpot)(locked_balance)(stake_time)(stake_owner)(stake_token)(stake_token_mirror)(stake_jackpot)(stake_jackpot_mirror)(dragon_jackpot)(dragon_ranks)(dragon_lucktime)(dragon_luckround)(dragon_luckowner)(dragon_luckpot)(dragon_luckpot_mirror)(total_team_fee)(total_stake_fee)(total_system_fee)(total_dragon_fee)(total_luck_fee)(total_shadows_fee)(total_bidding_fee)(total_order_count)(total_player_count)(total_dragon_count)(total_bidding_count)(total_drawing_count)(total_transfer_count)(total_order_amount)(total_mining_amount)(total_destroy_amount)(total_supply_amount)(total_dividend_amount))
};
typedef multi_index<N(games), game> _game;
_game games;
// @abi table biddings i64
struct bidding{
uint64_t id; // Id
uint64_t status; // 竞拍完成
asset bid_price; // 竞拍价格
uint64_t bid_jackpot; // EOS奖池
uint64_t open_time; // 竞拍开始时间
uint64_t stop_time; // 竞拍结束时间
uint64_t first_token; // 第一名ESA
account_name first_owner; // 第一名获胜账号60
uint64_t second_token; // 第二名ESA
account_name second_owner; // 第二名获胜账号30
uint64_t third_token; // 第三名ESA
account_name third_owner; // 第三名获胜账号10
uint64_t primary_key() const { return id; }
uint64_t time_key() const { return stop_time; }
EOSLIB_SERIALIZE(bidding, (id)(status)(bid_price)(bid_jackpot)(open_time)(stop_time)(first_token)(first_owner)(second_token)(second_owner)(third_token)(third_owner))
};
typedef multi_index<N(biddings), bidding,
indexed_by<N(time), const_mem_fun<bidding, uint64_t, &bidding::time_key>>
> _bidding;
_bidding biddings;
// @abi table dragons i64
struct dragon{
uint64_t id; // Id
asset price; // 骰子价值
uint64_t number; // 骰子点数
account_name owner; // 下注账号
uint64_t primary_key() const { return id; }
uint64_t number_key() const { return number; }
account_name owner_key() const { return owner; }
EOSLIB_SERIALIZE(dragon, (id)(price)(number)(owner))
};
typedef multi_index<N(dragons), dragon,
indexed_by<N(number), const_mem_fun<dragon, uint64_t, &dragon::number_key>>,
indexed_by<N(owner), const_mem_fun<dragon, account_name, &dragon::owner_key>>
> _dragon;
_dragon dragons;
// @abi table queues i64
struct queue{
uint64_t id; // Id
uint64_t start; // 接龙起始Id
uint64_t stop; // 接龙截止Id
uint64_t count; // 消牌数量
uint64_t profit; // 消牌利润
uint64_t status; // 接龙状态
uint64_t number; // 接龙点数
account_name owner; // 接龙中奖账号
uint64_t primary_key() const { return id; }
uint64_t number_key() const { return number; }
EOSLIB_SERIALIZE(queue, (id)(start)(stop)(count)(profit)(status)(number)(owner))
};
typedef multi_index<N(queues), queue,
indexed_by<N(number), const_mem_fun<queue, uint64_t, &queue::number_key>>
> _queue;
_queue queues;
// @abi table players i64
struct player {
uint64_t id; // Id
account_name owner; // 账号
account_name referrer; // 推荐人
asset balance; // 账户余额
uint64_t bet_step; // 投注步长
uint64_t bet_time; // 最后下注时间
uint64_t bet_profit; // 投注获利
uint64_t bet_amount; // 下注金额
uint64_t stake_time; // 质押时间
uint64_t stake_amount; // 质押数量
uint64_t stake_profit; // 质押分红
uint64_t unstake_id; // 质押交易id
uint64_t unstake_time; // 质押锁定时间
uint64_t unstake_amount; // 质押锁定数量
uint64_t dragon_round; // 接龙场次
uint64_t dragon_count; // 出牌数量
uint64_t dragon_profit; // 接龙获利
uint64_t mining_amount; // 挖矿数量
uint64_t referrer_amount; // 推荐利润
uint64_t primary_key() const { return id; }
account_name owner_key() const { return owner; }
account_name referrer_key() const { return referrer; }
key256 weight_key() const { return get_weight_key(dragon_round,dragon_count); }
static key256 get_weight_key(uint64_t dragon_round,uint64_t dragon_count)
{
return key256::make_from_word_sequence<uint64_t>(dragon_round,dragon_count);
}
EOSLIB_SERIALIZE(player, (id)(owner)(referrer)(balance)(bet_step)(bet_time)(bet_profit)(bet_amount)(stake_time)(stake_amount)(stake_profit)(unstake_id)(unstake_time)(unstake_amount)(dragon_round)(dragon_count)(dragon_profit)(mining_amount)(referrer_amount))
};
typedef multi_index<N(players), player,
indexed_by<N(owner), const_mem_fun<player, account_name, &player::owner_key>>,
indexed_by<N(referrer), const_mem_fun<player, account_name, &player::referrer_key>>,
indexed_by<N(weight), const_mem_fun<player, key256, &player::weight_key>>
> _player;
_player players;
// @abi table orders i64
struct order {
uint64_t id; // Id
account_name player; // 下注账号
account_name referrer; // 推荐人
asset bet_amount; // 下注金额
asset bet_award; // 中奖金额
asset bet_mining; // 挖矿金额
time_point_sec bet_time; // 下注时间
uint64_t status; // 订单状态
uint64_t under_number; // 小于点数
uint64_t result_number; // 结果点数
checksum256 player_seed; // 玩家种子
checksum256 banker_seed; // 庄家种子
checksum256 banker_seed_hash; // 庄家种子hash
uint64_t primary_key() const { return id; }
EOSLIB_SERIALIZE(order, (id)(player)(referrer)(bet_amount)(bet_award)(bet_mining)(bet_time)(status)(under_number)(result_number)(player_seed)(banker_seed)(banker_seed_hash))
};
typedef multi_index<N(orders), order> _order;
_order orders;
// @abi table seeds i64
struct seed {
checksum256 hash; // 庄家种子hash
uint64_t expired; // 过期时间
uint64_t primary_key() const { return hash_to_primary(hash); }
uint64_t expired_key() const { return expired; }
EOSLIB_SERIALIZE(seed, (hash)(expired))
};
typedef multi_index<N(seeds), seed,
indexed_by<N(expired), const_mem_fun<seed, uint64_t, &seed::expired_key>>
> _seed;
_seed seeds;
// @abi table accounts i64
struct account {
asset balance;
uint64_t primary_key()const { return balance.symbol.name(); }
};
typedef eosio::multi_index<N(accounts), account> accounts;
unsigned char applib_dt_is_leap_year(unsigned short year)
{
if ((year % 400) == 0) {
return 1;
} else if ((year % 100) == 0) {
return 0;
} else if ((year % 4) == 0) {
return 1;
} else {
return 0;
}
}
unsigned char applib_dt_last_day_of_mon(unsigned char month, unsigned short year)
{
if ((month == 0) || (month > 12)) {
return g_day_per_mon[1] + applib_dt_is_leap_year(year);
}
if (month != 2) {
return g_day_per_mon[month - 1];
} else {
return g_day_per_mon[1] + applib_dt_is_leap_year(year);
}
}
unsigned char applib_dt_dayindex(unsigned short year, unsigned char month, unsigned char day)
{
char century_code, year_code, month_code, day_code;
int week = 0;
century_code = year_code = month_code = day_code = 0;
if (month == 1 || month == 2) {
century_code = (year - 1) / 100;
year_code = (year - 1) % 100;
month_code = month + 12;
day_code = day;
} else {
century_code = year / 100;
year_code = year % 100;
month_code = month;
day_code = day;
}
week = year_code + year_code / 4 + century_code / 4 - 2 * century_code + 26 * ( month_code + 1 ) / 10 + day_code - 1;
week = week > 0 ? (week % 7) : ((week % 7) + 7);
return week;
}
void utc_sec_2_mytime(unsigned int utc_sec, mytime_struct *result, bool daylightSaving)
{
int sec, day;
unsigned short y;
unsigned char m;
unsigned short d;
unsigned char dst;
if (daylightSaving) {
utc_sec += SEC_PER_HOUR;
}
/* hour, min, sec */
/* hour */
sec = utc_sec % SEC_PER_DAY;
result->nHour = sec / SEC_PER_HOUR;
/* min */
sec %= SEC_PER_HOUR;
result->nMin = sec / SEC_PER_MIN;
/* sec */
result->nSec = sec % SEC_PER_MIN;
/* year, month, day */
/* year */
/* year */
day = utc_sec / SEC_PER_DAY;
for (y = UTC_BASE_YEAR; day > 0; y++) {
d = (DAY_PER_YEAR + applib_dt_is_leap_year(y));
if (day >= d)
{
day -= d;
}
else
{
break;
}
}
result->nYear = y;
for (m = 1; m < MONTH_PER_YEAR; m++) {
d = applib_dt_last_day_of_mon(m, y);
if (day >= d) {
day -= d;
} else {
break;
}
}
result->nMonth = m;
result->nDay = (unsigned char) (day + 1);
result->DayIndex = applib_dt_dayindex(result->nYear, result->nMonth, result->nDay);
}
asset get_payout(const uint64_t& under_number, const asset& quantity,const uint64_t dice_max,const double dice_water) {
return min(get_max_payout(quantity,under_number,dice_max,dice_water), get_max_bonus());
}
asset get_max_payout(const asset& quantity,const uint64_t& under_number,const uint64_t dice_max,const double dice_water) {
auto odds =(dice_max - dice_max * get_real_amount(dice_water)) / ((double)under_number - 1.0);
return asset(odds * quantity.amount, c);
}
asset get_max_bonus() {
//auto amount = get_avaliable_balance(settingitr->locked_balance) / 500;
//return amount.amount<=500000?asset(500000,CORE_TOKEN):amount;
auto settingitr = settings.begin();
return asset(settingitr->max_payout_amount,CORE_TOKEN);
}
// asset get_avaliable_balance(const uint64_t locked_balance) {
// auto locked = asset(locked_balance, CORE_TOKEN);
// auto balance = get_token_balance(CORE_TOKEN);
// eosio_assert(balance >= locked, "系统有效赔付余额不足");
// return balance - locked;
// }
asset get_eos_balance(symbol_type symbol){
accounts accountstable(CORE_ACCOUNT,_self);
const auto& ac = accountstable.get(symbol.name());
return ac.balance;
}
asset get_esa_balance(symbol_type symbol){
accounts accountstable(TEAM_BANKERS,_self);
const auto& ac = accountstable.get(symbol.name());
return ac.balance;
}
void assert_seed(const checksum256& data_seed, const checksum256& hash) {
string data = sha256_to_hex(data_seed);
char* chr = const_cast<char*>(data.c_str());
assert_sha256(chr, strlen(chr), &hash);
}
uint64_t get_random(const checksum256& seed1, const checksum256& seed2,const uint64_t dice_max) {
size_t hashcode = 0;
hash_merge(hashcode, sha256_to_hex(seed1));
hash_merge(hashcode, sha256_to_hex(seed2));
return (hashcode % dice_max) + 1;
}
template <class T>inline void hash_merge(std::size_t& data_seed, const T& v) {
//std::hash<T> hasher;
boost::hash<std::string> hasher;
data_seed ^= hasher(v) + 0x9e3779b9 + (data_seed << 6) + (data_seed >> 2);
}
string sha256_to_hex(const checksum256& sha256) {
return hash_to_hex((char*)sha256.hash, sizeof(sha256.hash));
}
checksum256 hex_to_sha256(const string& hex_string) {
eosio_assert(hex_string.length() == 64, "无效的sha256字符串");
checksum256 checksum;
from_hex(hex_string, (char*)checksum.hash, sizeof(checksum.hash));
return checksum;
}
checksum256 string_to_sha256(const string data)
{
checksum256 hash;
sha256(const_cast<char*>(data.c_str()), data.size() * sizeof(char), &hash);
return hash;
}
size_t string_split(const string& input,string* output,const char& separator,const size_t& first_pos = 0,const bool& required = false) {
eosio_assert(first_pos != string::npos, "解析MEMO信息失败");
auto pos = input.find(separator, first_pos);
if (pos == string::npos) {
eosio_assert(!required, "解析MEMO信息错误");
return string::npos;
}
*output = input.substr(first_pos, pos - first_pos);
return pos;
}
double get_real_amount(const uint64_t amount){
return double(amount)/double(10000);
}
bool is_digits(const std::string &str){
return std::all_of(str.begin(), str.end(), ::isdigit);
}
string hash_to_hex(const char* d, uint32_t s) {
std::string r;
const char* to_hex = "0123456789abcdef";
uint8_t* c = (uint8_t*)d;
for (uint32_t i = 0; i < s; ++i)
(r += to_hex[(c[i] >> 4)]) += to_hex[(c[i] & 0x0f)];
return r;
}
uint8_t from_hex(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
eosio_assert(false, "无效的hex字符串");
return 0;
}
size_t from_hex(const string& hex_str, char* out_data, size_t out_data_len) {
auto i = hex_str.begin();
uint8_t* out_pos = (uint8_t*)out_data;
uint8_t* out_end = out_pos + out_data_len;
while (i != hex_str.end() && out_end != out_pos) {
*out_pos = from_hex((char)(*i)) << 4;
++i;
if (i != hex_str.end()) {
*out_pos |= from_hex((char)(*i));
++i;
}
++out_pos;
}
return out_pos - (uint8_t*)out_data;
}
string uint64_to_string(uint64_t input) {
string result;
uint8_t base = 10;
do {
char c = input % base;
input /= base;
if (c < 10)
c += '0';
else
c += 'A' - 10;
result = c + result;
} while (input);
return result;
}
uint64_t hash_to_uint64(const string& hash) {
return std::hash<string>{}(hash);
}
uint64_t hash_to_uint64(const checksum256& hash) {
return hash_to_uint64(sha256_to_hex(hash));
}
static uint64_t hash_to_primary(const checksum256 sha256)
{
const char* d = (char*)sha256.hash;
uint32_t s = sizeof(sha256.hash);
std::string r;
const char* to_hex = "0123456789abcdef";
uint8_t* c = (uint8_t*)d;
for (uint32_t i = 0; i < s; ++i)
(r += to_hex[(c[i] >> 4)]) += to_hex[(c[i] & 0x0f)];
return std::hash<string>{}(r);
}
bool decode_base58(const string& str, std::vector<unsigned char>& vch) {
auto psz = str.c_str();
while (*psz && isspace(*psz)) psz++;
int zeroes = 0;
int length = 0;
while (*psz == '1') {
zeroes++;
psz++;
}
int size = strlen(psz) * 733 / 1000 + 1;
std::vector<unsigned char> b256(size);
static_assert(
sizeof(BASE58_MAP) / sizeof(BASE58_MAP[0]) == 256,"BASE58_MAP 必须为256位");
while (*psz && !isspace(*psz)) {
int carry = BASE58_MAP[(uint8_t)*psz];
if (carry == -1)
return false;
int i = 0;
for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin();
(carry != 0 || i < length) && (it != b256.rend());
++it, ++i) {
carry += 58 * (*it);
*it = carry % 256;
carry /= 256;
}
assert(carry == 0);
length = i;
psz++;
}
while (isspace(*psz)) psz++;
if (*psz != 0) return false;
std::vector<unsigned char>::iterator it = b256.begin() + (size - length);
while (it != b256.end() && *it == 0) it++;
vch.reserve(zeroes + (b256.end() - it));
vch.assign(zeroes, 0x00);
while (it != b256.end()) vch.push_back(*(it++));
return true;
}
signature string_to_signature(const string& sig, const bool& checksumming = true) {
const auto pivot = sig.find('_');
eosio_assert(pivot != string::npos, "签名缺少分隔符");
const auto prefix_str = sig.substr(0, pivot);
eosio_assert(prefix_str == "SIG", "签名前缀不正确");
const auto next_pivot = sig.find('_', pivot + 1);
eosio_assert(next_pivot != string::npos, "签名缺少曲线");
const auto curve = sig.substr(pivot + 1, next_pivot - pivot - 1);
eosio_assert(curve == "K1" || curve == "R1", "签名曲线不正确");
const bool k1 = curve == "K1";
auto data_str = sig.substr(next_pivot + 1);
eosio_assert(!data_str.empty(), "签名数据不正确");
vector<unsigned char> vch;
eosio_assert(decode_base58(data_str, vch), "签名解码失败");
eosio_assert(vch.size() == 69, "签名数据无效");
if (checksumming) {
array<unsigned char, 67> check_data;
copy_n(vch.begin(), 65, check_data.begin());
check_data[65] = k1 ? 'K' : 'R';
check_data[66] = '1';
checksum160 check_sig;
ripemd160(reinterpret_cast<char*>(check_data.data()), 67, &check_sig);
eosio_assert(memcmp(&check_sig.hash, &vch.end()[-4], 4) == 0, "签名校验失败");
}
signature _sig;
unsigned int type = k1 ? 0 : 1;
_sig.data[0] = (uint8_t)type;
for (int i = 1; i < sizeof(_sig.data); i++) {
_sig.data[i] = vch[i - 1];
}
return _sig;
}
public_key string_to_public_key(const string& pubkey, const bool& checksumming = true) {
string pubkey_prefix("EOS");
auto base58substr = pubkey.substr(pubkey_prefix.length());
vector<unsigned char> vch;
eosio_assert(decode_base58(base58substr, vch), "公钥解码失败");
eosio_assert(vch.size() == 37, "公钥不正确");
if (checksumming) {
array<unsigned char, 33> pubkey_data;
copy_n(vch.begin(), 33, pubkey_data.begin());
checksum160 check_pubkey;
ripemd160(reinterpret_cast<char*>(pubkey_data.data()), 33, &check_pubkey);
eosio_assert(memcmp(&check_pubkey.hash, &vch.end()[-4], 4) == 0, "公钥校验失败");
}
public_key _pub_key;
unsigned int type = 0;
_pub_key.data[0] = (char)type;
for (int i = 1; i < sizeof(_pub_key.data); i++) {
_pub_key.data[i] = vch[i - 1];
}
return _pub_key;
}
uint64_t get_next_transfer_id()
{
auto gameitr = games.begin();
games.modify(gameitr, 0, [&](auto &s) {
s.total_transfer_count++;
});
return gameitr->total_transfer_count;
}
uint64_t get_next_dragon_id()
{
auto gameitr = games.begin();
games.modify(gameitr, 0, [&](auto &s) {
s.total_dragon_count++;
});
return gameitr->total_dragon_count;
}
uint64_t get_next_bidding_id()
{
auto gameitr = games.begin();
games.modify(gameitr, 0, [&](auto &s) {
s.total_bidding_count++;
});
return gameitr->total_bidding_count;
}
void delay_transfer(account_name contract,account_name to,asset quantity,string memo)
{
memo.append(SYSTEM_REMARK);
transaction tran;
tran.actions.emplace_back(permission_level{_self, N(active)}, contract, N(transfer), std::make_tuple(_self,to, quantity, memo));
tran.delay_sec = 0;
tran.send(get_next_transfer_id(), _self, false);
}
uint64_t get_next_drawing_id()
{
auto gameitr = games.begin();
games.modify(gameitr, 0, [&](auto &s) {
s.total_drawing_count++;
});
return gameitr->total_drawing_count;
}
void check_signature(uint64_t under_number,uint64_t expiration,account_name referrer,checksum256 banker_seed_hash,signature banker_seed_signature)
{
string data = to_string(under_number);
data += "+";
data += to_string(expiration);
data += "+";
data += name{referrer}.to_string();
data += "+";
data += sha256_to_hex(banker_seed_hash);
checksum256 hashcode;
sha256(const_cast<char*>(data.c_str()), data.size() * sizeof(char), &hashcode);
public_key key = string_to_public_key(SYSTEM_KEY);
assert_recover_key(&hashcode,(char*)&banker_seed_signature.data,sizeof(banker_seed_signature.data),(char*)&key.data,sizeof(key.data));
}
void parse_data(asset quantity,string memo,uint64_t* under_number,uint64_t* expiration,account_name* referrer,checksum256* player_seed_hash,checksum256* banker_seed_hash,signature* banker_seed_signature,uint64_t* partner)
{
eosio_assert(quantity.amount<=100000, "超出系统最大限额10EOS" );
size_t postion;
string data;
auto pluscount = count(memo.begin(),memo.end(),'+');
eosio_assert(pluscount==6, "缺少加号分隔符");
memo.erase(std::remove_if(memo.begin(),memo.end(),[](unsigned char x) { return std::isspace(x); }),memo.end());
// 获取骰子点数
postion = string_split(memo, &data, '+', 0, true);
eosio_assert(data.size()>0, "骰子点数不能为空");
eosio_assert(is_digits(data), "骰子点数必须是数字");
*under_number = stoull(data);
// 获取超时时间
postion = string_split(memo, &data, '+', ++postion, true);
eosio_assert(data.size()>0, "超时时间不能为空");
eosio_assert(is_digits(data), "超时时间必须是数字");
*expiration = stoull(data);
// 获取推荐人
postion = string_split(memo, &data, '+', ++postion, true);
eosio_assert(data.size()>0, "推荐人不能为空");
*referrer = string_to_name(data.c_str());
// 获取玩家种子
postion = string_split(memo, &data, '+', ++postion, true);
eosio_assert(data.size()>0, "玩家种子不能为空");
*player_seed_hash = hex_to_sha256(data);
// 获取庄家种子
postion = string_split(memo, &data, '+', ++postion, true);
eosio_assert(data.size()>0, "庄家种子不能为空");
*banker_seed_hash = hex_to_sha256(data);
// 获取庄家种子签名
postion = string_split(memo, &data, '+', ++postion, true);
eosio_assert(data.size()>0, "庄家种子签名不能为空");
*banker_seed_signature = string_to_signature("SIG_K1_"+data);
// 合作商参数
data = memo.substr(++postion);
eosio_assert(data.size()>0, "合作商参数不能为空");
*partner = stoull(data);
}
void verify_data(account_name from,asset quantity, uint64_t under_number,uint64_t expiration,account_name referrer,checksum256 banker_seed_hash,signature banker_seed_signature)
{
// 验证下注金额
auto gameitr = games.begin();
auto settingitr = settings.begin();
eosio_assert(settingitr!=settings.end(), "系统尚未初始化" );
eosio_assert(settingitr->is_betting==1, "下注功能未启用" );
eosio_assert(gameitr!=games.end(), "游戏尚未初始化" );
eosio_assert(quantity.amount>=settingitr->min_bet, "金额至少为0.1EOS" );
eosio_assert(quantity.amount<=settingitr->max_bet, "金额超出最大限额" );
eosio_assert(now()<expiration, "随机种子已超时");
// 验证最大赔付
auto max_bonus = get_max_bonus();
auto max_payout = get_max_payout(quantity,under_number,settingitr->dice_max,settingitr->dice_water);
eosio_assert(max_payout<=max_bonus,"下注金额超出系统最大限额");
eosio_assert(gameitr->total_supply_amount>=gameitr->locked_balance && ((gameitr->total_supply_amount-gameitr->locked_balance) >= max_payout.amount),"下注金额超出系统最大赔付额");
// 验证庄家种子
auto seed_key = hash_to_primary(banker_seed_hash);
auto seeditr = seeds.find(seed_key);
eosio_assert(seeditr == seeds.end(), "庄家随机种子已被使用");
// 验证推荐人、骰子点数、种子超时、种子签名
eosio_assert(referrer!=from,"推荐人不能是自己");
eosio_assert(is_account(referrer),"推荐人账号不存在");
auto max_dice_number = settingitr->dice_max * get_real_amount(settingitr->dice_edge) + 1;
eosio_assert(under_number>=2 && under_number<=max_dice_number, "骰子点数超出范围");
check_signature(under_number,expiration,referrer,banker_seed_hash,banker_seed_signature);
}
void write_bet(account_name from,asset quantity, uint64_t under_number,uint64_t expiration,account_name referrer,checksum256 player_seed_hash,checksum256 banker_seed_hash,uint64_t partner)
{
// 计算挖矿数量
auto gameitr = games.begin();
auto settingitr = settings.begin();
auto esa_balance = get_esa_balance(TEAM_TOKEN);
auto bet_amount = double(quantity.amount);
auto bet_mining = settingitr->is_mining==1?((double(esa_balance.amount) / double(settingitr->mining_supply_amount)) * bet_amount * 20):0; //auto mining_count = esa_balance.amount * bet_amount / (10000000000UL + gameitr->total_order_amount + bet_amount);
// 计算分红比例
auto system_fee = bet_amount * get_real_amount(settingitr->system_ratio);
auto team_fee = system_fee * get_real_amount(settingitr->team_ratio);
auto stake_fee = system_fee * get_real_amount(settingitr->stake_ratio);
auto dragon_fee = system_fee * get_real_amount(settingitr->dragon_ratio);
auto luck_fee = system_fee * get_real_amount(settingitr->lucky_ratio);
auto shadows_fee = system_fee * get_real_amount(settingitr->shadows_ratio);
auto bidding_fee = system_fee * get_real_amount(settingitr->bidding_ratio);
auto referrer_fee = system_fee * get_real_amount(settingitr->referrer_ratio);
auto left_fee = (quantity.amount>=system_fee? quantity.amount - system_fee:0);
// 写入账号数据
auto is_new_player = false;
auto playeridx = players.template get_index<N(owner)>();
auto playeritr = playeridx.find(from);
if(playeritr == playeridx.end())
{
players.emplace(_self,[&](auto & s){
s.id = players.available_primary_key();
s.owner = from;
s.balance = asset(0,CORE_TOKEN);
s.mining_amount = bet_mining;
s.bet_amount = bet_amount;
s.bet_time = now();
if(s.dragon_round != gameitr->dragon_luckround)
{
s.dragon_count = 0;
}
s.dragon_round = gameitr->dragon_luckround;
});
is_new_player = true;
}else{
playeridx.modify(playeritr, 0, [&](auto &s) {
s.mining_amount += bet_mining;
s.bet_amount += bet_amount;
s.bet_time = now();
if(s.dragon_round != gameitr->dragon_luckround)
{
s.dragon_count = 0;
}
s.dragon_round = gameitr->dragon_luckround;
});
}
//写入种子
seeds.emplace(_self,[&](auto & s){
s.hash = banker_seed_hash;
s.expired = expiration;
});
// 推荐人分红
auto referrer_esa_fee = bet_mining * get_real_amount(settingitr->referrer_ratio);
if (referrer>0 && referrer != _self && referrer_esa_fee>0) {
auto referreritr = playeridx.find(referrer);
if(referreritr != playeridx.end())
{
playeridx.modify(referreritr, 0, [&](auto &s) {
s.referrer_amount += referrer_esa_fee;
s.mining_amount += referrer_esa_fee;
});
}else{
players.emplace(_self,[&](auto & s){
s.id = players.available_primary_key();
s.owner = referrer;
s.balance = asset(0,CORE_TOKEN);
s.mining_amount = referrer_esa_fee;
s.referrer_amount += referrer_esa_fee;
});
}
}
// 写入统计数据
games.modify(gameitr, 0, [&](auto &s) {
s.bid_jackpot += bidding_fee;
s.stake_jackpot += stake_fee;
s.dragon_jackpot += dragon_fee;
s.dragon_luckpot += luck_fee;
s.locked_balance += bet_amount;
s.total_team_fee += team_fee;
s.total_stake_fee += stake_fee;
s.total_system_fee += system_fee;
s.total_dragon_fee += dragon_fee;
s.total_luck_fee += luck_fee;
s.total_shadows_fee += shadows_fee;
s.total_bidding_fee += bidding_fee;
s.total_order_count += 1;
s.total_player_count += (is_new_player?1:0);
s.total_order_amount += bet_amount;
s.total_mining_amount += bet_mining + referrer_esa_fee;
s.total_supply_amount += left_fee + referrer_fee;
});
// 写入订单数据
orders.emplace(_self,[&](auto & s){
s.id = gameitr->total_order_count;
s.player = from;
s.referrer = referrer;//(is_new_player == true?referrer:playeritr->referrer);
s.bet_amount = quantity;
s.bet_award = asset(0,CORE_TOKEN);
s.bet_mining = asset(bet_mining,TEAM_TOKEN);
s.status = 0;
s.bet_time = time_point_sec(now());
s.under_number = under_number;
s.result_number = 0;
s.player_seed = player_seed_hash;
s.banker_seed_hash = banker_seed_hash;
});
// 获得下注挖矿
if(bet_mining>0)
{
transaction transfer;
string remark = "获得投注矿产";
remark.append(SYSTEM_REMARK);
asset miningfee(bet_mining,TEAM_TOKEN);
if(partner==1)
{
transfer.actions.emplace_back(permission_level {_self, N(active) }, TEAM_BANKERS, N(transfer), std::make_tuple(_self, WIN96_MINING, referrer_esa_fee, remark));
}
transfer.actions.emplace_back(permission_level {_self, N(active) }, TEAM_BANKERS, N(transfer), std::make_tuple(_self, from, miningfee, remark));
transfer.send(get_next_transfer_id(), _self, false);
}
}
void bet(account_name from, account_name to, asset quantity, string memo)
{
uint64_t under_number;
uint64_t expiration;
account_name referrer;
checksum256 player_seed_hash;
checksum256 banker_seed_hash;
signature banker_seed_signature;
uint64_t partner;
// 提取注单数据
parse_data(quantity,memo,&under_number,&expiration,&referrer,&player_seed_hash,&banker_seed_hash,&banker_seed_signature,&partner);
// 检查金额、随机种子、种子签名
verify_data(from,quantity,under_number,expiration,referrer,banker_seed_hash,banker_seed_signature);
// 保存注单信息、锁定金额、发送矿产
write_bet(from,quantity,under_number,expiration,referrer,player_seed_hash,banker_seed_hash,partner);
}
void stake(account_name from, asset quantity) {
require_auth(from);
auto gameitr = games.begin();
auto settingitr = settings.begin();
eosio_assert(settingitr!=settings.end(), "系统尚未初始化" );
eosio_assert(settingitr->is_staking==1, "质押功能未启用" );
eosio_assert(quantity.amount>=settingitr->min_bet, "金额至少为0.1" );
eosio_assert(quantity.amount<=settingitr->max_bet, "金额超出最大限额" );
auto playeridx = players.template get_index<N(owner)>();