-
Notifications
You must be signed in to change notification settings - Fork 1
/
pokercontract.cpp
4821 lines (4017 loc) · 150 KB
/
pokercontract.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 <set>
#include <algorithm>
#include <functional>
#include <iterator>
#include <string>
#include <eosiolib/time.hpp>
#include "pokercontract.hpp"
#include <math.h>
#include <eosiolib/print.hpp> // for DEBUG!
#define ACE_CARD 14
#define MAX_INDEX_IN_COMBO 4
#define COMBO_SIZE 5
globalstate pokercontract::get_default_parameters()
{
globalstate gs;
gs.version = 393;
gs.small_blind_values.push_back(eosio::asset(100, EOS_SYMBOL));
gs.small_blind_values.push_back(eosio::asset(200, EOS_SYMBOL));
gs.small_blind_values.push_back(eosio::asset(500, EOS_SYMBOL));
gs.small_blind_values.push_back(eosio::asset(1000, EOS_SYMBOL));
gs.small_blind_values.push_back(eosio::asset(10000, EOS_SYMBOL));
gs.max_players_count_values.push_back(6);
gs.penalty_percent = 50;
gs.max_penalty_value = eosio::asset(500000, EOS_SYMBOL);
gs.player_pay_percent = 70;
gs.master_pay_percent = 30;
gs.rake_percent = 3;
gs.max_rake_value = eosio::asset(50000, EOS_SYMBOL);
gs.warning_timeout_sec = 25;
gs.last_timeout_sec = 15;
gs.delete_table_timeout_sec = 10;
gs.delete_tables_count = 2;
gs.freezing = 0;
gs.min_sb_buyin = 40;
gs.max_sb_buyin = 200;
gs.r = eosio::asset(0, EOS_SYMBOL);
return gs;
}
ACTION pokercontract::setparams(eosio::name owner, globalstate& gs)
{
require_auth(owner);
eosio_assert(owner == _self, "Only owner can run setparams");
globalstate gstate_for_version = get_default_parameters();
globalstate gstate = global.get();
gstate.version = gstate_for_version.version;
eosio_assert(gs.small_blind_values.size()>=1, "small_blind_values must contain at least one value");
std::set<eosio::asset> check;
for(eosio::asset a: gs.small_blind_values)
{
eosio_assert(a.amount > 0, "small_blind_values must be positive");
eosio_assert(a.symbol == EOS_SYMBOL, "wrong small blind symbol");
check.insert(a);
}
eosio_assert(check.size()==gs.small_blind_values.size(),"small_blind_values contains same elements");
gstate.small_blind_values = gs.small_blind_values;
eosio_assert(gs.max_players_count_values.size()>=1, "max_players_count_values must contain at least one value");
std::set<uint8_t> check_count_players;
for(uint8_t count: gs.max_players_count_values)
{
eosio_assert(count > 0, "max_players_count_values must be positive");
check_count_players.insert(count);
}
eosio_assert(check_count_players.size()==gs.max_players_count_values.size(),"max_players_count_values contains same elements");
eosio_assert( (*(check_count_players.begin())) >= 2,"max_players_count_values minimum value must be greater or equal 2");
eosio_assert( (*(check_count_players.rbegin())) <= 9,"max_players_count_values maximum value must be less or equal 9");
gstate.max_players_count_values = gs.max_players_count_values;
eosio_assert(gs.penalty_percent > 0, "penalty_percent must be greater than 0 and less or equal 50");
eosio_assert(gs.penalty_percent <= 50, "penalty_percent must be greater than 0 and less or equal 50");
gstate.penalty_percent = gs.penalty_percent;
eosio_assert(gs.max_penalty_value.symbol == EOS_SYMBOL, "wrong max_penalty_value symbol");
eosio_assert(gs.max_penalty_value.amount > 0, "max_penalty_value must be greater than 0");
eosio_assert(gs.max_penalty_value.amount <= 500000, "max_penalty_value must be less or equal 50 EOS");
gstate.max_penalty_value = gs.max_penalty_value;
eosio_assert(gs.player_pay_percent >= 50, "player_pay_percent must be greater or equal 50 and less or equal 80");
eosio_assert(gs.player_pay_percent <= 80, "player_pay_percent must be greater or equal 50 and less or equal 80");
gstate.player_pay_percent = gs.player_pay_percent;
eosio_assert(gs.master_pay_percent >= 20, "master_pay_percent must be greater or equal 20 and less or equal 50");
eosio_assert(gs.master_pay_percent <= 50, "master_pay_percent must be greater or equal 20 and less or equal 50");
gstate.master_pay_percent = gs.master_pay_percent;
eosio_assert(gs.master_pay_percent + gs.player_pay_percent == 100, "sum of master_pay_percent and player_pay_percent must be 100");
eosio_assert(gs.rake_percent >= 0.1, "rake_percent must be greater or equal 0.1 and less or equal 5.0");
eosio_assert(gs.rake_percent <= 5.0, "rake_percent must be greater or equal 0.1 and less or equal 5.0");
gstate.rake_percent = gs.rake_percent;
double min_rake_percent = 0.1;
int n = (gs.rake_percent*10)/(min_rake_percent*10);
float check_rake_percent = n * min_rake_percent;
eosio_assert(check_rake_percent == gs.rake_percent, "rake_percent must be a multiple of 0.1");
eosio_assert(gs.max_rake_value.symbol == EOS_SYMBOL, "wrong max_rake_value symbol");
eosio_assert(gs.max_rake_value.amount > 0, "max_rake_value must be greater than 0");
eosio_assert(gs.max_rake_value.amount <= 500000, "max_rake_value must be less or equal 50 EOS");
gstate.max_rake_value = gs.max_rake_value;
eosio_assert(gs.warning_timeout_sec >= 10, "warning_timeout_sec must be greater or equal 10 and less or equal 30");
eosio_assert(gs.warning_timeout_sec <= 30, "warning_timeout_sec must be greater or equal 10 and less or equal 30");
gstate.warning_timeout_sec = gs.warning_timeout_sec;
eosio_assert(gs.last_timeout_sec >= 10, "last_timeout_sec must be greater or equal 10 and less or equal 60");
eosio_assert(gs.last_timeout_sec <= 60, "last_timeout_sec must be greater or equal 10 and less or equal 60");
gstate.last_timeout_sec = gs.last_timeout_sec;
eosio_assert(gs.delete_table_timeout_sec >= 1, "delete_table_timeout_sec must be greater or equal 1 and less or equal 600");
eosio_assert(gs.delete_table_timeout_sec <= 600, "delete_table_timeout_sec must be greater or equal 1 and less or equal 600");
gstate.delete_table_timeout_sec = gs.delete_table_timeout_sec;
eosio_assert(gs.delete_tables_count >= 1, "delete_tables_count must be greater or equal 1 and less or equal 10");
eosio_assert(gs.delete_tables_count <= 10, "delete_tables_count must be greater or equal 1 and less or equal 10");
gstate.delete_tables_count = gs.delete_tables_count;
eosio_assert( (gs.freezing == 0) || (gs.freezing == 1), "freezing must be 0 or 1");
gstate.freezing = gs.freezing;
eosio_assert( gs.client_version.length() > 0, "client_version length must be > 0");
gstate.client_version = gs.client_version;
global.set(gstate, owner);
}
ACTION pokercontract::setref(eosio::name owner, uint32_t percent)
{
require_auth(owner);
eosio_assert(owner == _self, "Only owner can run setref");
eosio_assert(percent <= 100, "percent must be less or equal 100");
globalref gref = global_ref.get();
gref.percent = percent;
global_ref.set(gref, owner);
}
void setSuitsByGraterValue( std::set<Card>& spades,
std::set<Card>& hearts,
std::set<Card>& diamonds,
std::set<Card>& clubs,
const std::multiset<Card>& cards)
{
for( auto it = cards.begin(); it != cards.end(); it++)
{
if( (*it).suit == S_SPADES)
spades.insert( *it );
else if( (*it).suit == S_HEARTS)
hearts.insert( *it );
else if( (*it).suit == S_DIAMONDS)
diamonds.insert( *it );
else if( (*it).suit == S_CLUBS)
clubs.insert( *it );
}
}
bool getBestFlushInSuit(const std::set<Card>& cards, Combination& combo)
{
if(cards.size() < COMBO_SIZE)
return false;
uint8_t index_in_combo = 0;
for(auto it_cur = cards.begin(); it_cur!= cards.end(); it_cur++)
{
combo.cards[index_in_combo] = *it_cur;
auto it_next = std::next(it_cur,1);
if(it_next == cards.end())
break;
if( it_cur->value - 1 == it_next->value )
{
if( ++index_in_combo == MAX_INDEX_IN_COMBO)
{
combo.cards[index_in_combo] = *it_next;
combo.type = (combo.cards[0].value == ACE_CARD) ? C_ROYAL_FLUSH : C_STRAIGHT_FLUSH;
return true;
}
}
else
index_in_combo = 0;
}
// check a wheel = 5 4 3 2 + A
if( (combo.cards[0].value == 5) && (index_in_combo == 3) )
{
if((*cards.begin()).value == ACE_CARD)
{
combo.cards[MAX_INDEX_IN_COMBO] = *cards.begin();
combo.type = C_STRAIGHT_FLUSH;
return true;
}
}
// just simple flush
std::copy_n( cards.begin(), COMBO_SIZE, combo.cards.begin() );
combo.type = C_FLUSH;
return true;
}
Combination getBestFlushCombo(const Combination& combo1, const Combination& combo2)
{
if(combo1.type > combo2.type)
return combo1;
if(combo2.type > combo1.type)
return combo2;
if(std::lexicographical_compare(combo1.cards.begin(), combo1.cards.end(),
combo2.cards.begin(), combo2.cards.end()))
return combo1;
else return combo2;
}
int getBestFlush(const std::multiset<Card>& cards, Combination& combo)
{
if(cards.size()<5)
return 0;
combo.type = C_NO_COMBINATION;
std::vector< std::set<Card> > suits(4); // spades hearts diamonds clubs
std::vector< Combination > tmp_combo(4);
setSuitsByGraterValue(suits[0], suits[1], suits[2], suits[3], cards);
for(int i = 0; i< 4; i++)
{
if(getBestFlushInSuit(suits[i], tmp_combo[i]) == false)
continue;
combo = tmp_combo[i];
if(tmp_combo[i].type == C_ROYAL_FLUSH)
return combo.type;
if(i > 0) // compare with previous
combo = getBestFlushCombo(tmp_combo[i-1], tmp_combo[i]);
}
return combo.type;
}
int getBestFourOfAKind(const std::multiset<Card>& cards, Combination& combo)
{
if(cards.size() < 5)
return 0;
uint8_t index_in_combo = 0; // if this index == 3 then we have four of a kind!
for(auto it_cur = cards.begin(); it_cur != cards.end(); it_cur++)
{
combo.cards[index_in_combo] = *it_cur;
auto it_next = std::next(it_cur,1);
if(it_next == cards.end())
break;
if( (*it_cur).value == (*it_next).value )
{
if(++index_in_combo == 3)
{
combo.cards[index_in_combo] = *it_next;
// we always have previous or next card in this case
if( (*cards.begin()).value != (*it_cur).value )
combo.cards[MAX_INDEX_IN_COMBO] = *cards.begin();
else
combo.cards[MAX_INDEX_IN_COMBO] = *(++it_next);
combo.type = C_FOUR_OF_A_KIND;
return combo.type;
}
}
else
index_in_combo = 0;
}
return 0;
}
Combination getBestFourOfAKindCombo(const Combination& combo1, const Combination& combo2)
{
if(combo1.cards[0].value > combo2.cards[0].value)
return combo1;
else
return combo2;
}
int getThreeOfAKindOnly(const std::multiset<Card>& cards, Combination& combo)
{
if(cards.size()<3)
return 0;
uint8_t cards_in_line=1;
for(auto it_cur = cards.begin(); it_cur != cards.end(); it_cur++)
{
auto it_next = std::next(it_cur,1);
if(it_next == cards.end())
break;
if( (*it_cur).value == (*it_next).value )
cards_in_line++;
else
cards_in_line = 1;
if(cards_in_line == 3) // have three of a kind
{
it_cur--;
for(int i=0; i<3; i++)
{
combo.cards[i] = *it_cur;
it_cur++;
}
combo.type = C_THREE_OF_A_KIND;
return combo.type;
}
}
return 0;
}
int getBestFullHouse(const std::multiset<Card>& cards, Combination& combo)
{
if(cards.size()<5)
return 0;
std::multiset<Card> remaining_cards(cards);
// looking for three of a kind
if(!getThreeOfAKindOnly(cards, combo))
return 0;
for(int i=0; i<3; i++)
{
auto it_for_remove = remaining_cards.find(combo.cards[i]);
eosio_assert(it_for_remove != remaining_cards.end(), "find assertion");
remaining_cards.erase(it_for_remove);
}
// now looking for a pair in remaining cards
for(auto it_cur = remaining_cards.begin(); it_cur != remaining_cards.end(); it_cur++)
{
auto it_next = std::next(it_cur,1);
if(it_next == remaining_cards.end())
break;
if( (*it_cur).value == (*it_next).value ) // got it!
{
combo.cards[3] = *it_cur;
combo.cards[MAX_INDEX_IN_COMBO] = *it_next;
combo.type = C_FULL_HOUSE;
return combo.type;
}
}
return 0;
}
int getBestStraight(const std::multiset<Card>& cards, Combination& combo)
{
if(cards.size()<5)
return 0;
uint8_t cards_in_line = 1;
for(auto it_cur = cards.begin(); it_cur != cards.end(); it_cur++)
{
combo.cards[cards_in_line-1] = *it_cur;
auto it_next = std::next(it_cur, 1);
if(it_next == cards.end())
break;
if( (*it_cur).value == (*it_next).value )
continue;
if( (*it_cur).value - 1 == (*it_next).value )
cards_in_line++;
else
cards_in_line = 1;
if(cards_in_line == 5) // got it!
{
combo.cards[MAX_INDEX_IN_COMBO] = *it_next;
combo.type = C_STRAIGHT;
return combo.type;
}
}
// check a wheel = 5 4 3 2 + A
if( (combo.cards[0].value == 5) && (cards_in_line == 4) )
{
if((*cards.begin()).value == ACE_CARD)
{
combo.cards[MAX_INDEX_IN_COMBO] = *cards.begin();
combo.type = C_STRAIGHT;
return combo.type;
}
}
return 0;
}
int getBestThreeOfAKind(const std::multiset<Card>& cards, Combination& combo)
{
if(cards.size()<5)
return 0;
std::multiset<Card> remaining_cards(cards);
if(!getThreeOfAKindOnly(cards, combo))
return 0;
for(int i=0; i<3; i++)
{
auto it_for_remove = remaining_cards.find(combo.cards[i]);
eosio_assert(it_for_remove != remaining_cards.end(), "find assertion");
remaining_cards.erase(it_for_remove);
}
auto it = remaining_cards.begin();
combo.cards[3] = *it;
combo.cards[MAX_INDEX_IN_COMBO] = *(++it);
combo.type = C_THREE_OF_A_KIND;
return combo.type;
}
int getBestPairOnly(const std::multiset<Card>& cards, Combination& combo)
{
for(auto it_cur = cards.begin(); it_cur != cards.end(); it_cur++)
{
auto it_next = std::next(it_cur, 1);
if(it_next == cards.end())
break;
if( (*it_cur).value == (*it_next).value) // got first pair!
{
combo.cards[0] = *it_cur;
combo.cards[1] = *it_next;
return C_PAIR;
}
}
return 0;
}
int getBestTwoPairs(const std::multiset<Card>& cards, Combination& combo)
{
if(cards.size()<5)
return 0;
std::multiset<Card> remaining_cards(cards);
Combination tmp_combo;
uint8_t combo_index = 0;
uint8_t pair_conts = 2;
while(pair_conts--)
{
if(getBestPairOnly(remaining_cards, tmp_combo) == 0)
{
return 0;
}
for(int i=0; i<2; i++)
{
combo.cards[combo_index] = tmp_combo.cards[i];
auto it_for_remove = remaining_cards.find(combo.cards[combo_index++]);
eosio_assert(it_for_remove != remaining_cards.end(), "find assertion");
remaining_cards.erase(it_for_remove);
}
}
combo.cards[MAX_INDEX_IN_COMBO] = *remaining_cards.begin();
combo.type = C_TWO_PAIRS;
return C_TWO_PAIRS;
}
int getBestPair(const std::multiset<Card>& cards, Combination& combo)
{
if(cards.size()<5)
return 0;
std::multiset<Card> remaining_cards(cards);
uint8_t combo_index = 0;
if(getBestPairOnly(remaining_cards, combo) == 0)
return 0;
for( int i=0; i<2; i++)
{
auto it_for_remove = remaining_cards.find(combo.cards[i]);
eosio_assert(it_for_remove != remaining_cards.end(), "find assertion");
remaining_cards.erase(it_for_remove);
}
auto it = remaining_cards.begin();
for( int index=2; index<=MAX_INDEX_IN_COMBO; index++)
{
combo.cards[index] = *(it++);
}
combo.type = C_PAIR;
return C_PAIR;
}
int getTheHighestCard(const std::multiset<Card>& cards, Combination& combo)
{
if(cards.size() < 5)
return 0;
auto it = cards.begin();
for(int i=0; i<=MAX_INDEX_IN_COMBO; i++)
combo.cards[i] = *(it++);
combo.type = C_HIGH_CARD;
return combo.type;
}
// return type of combo
int getCombination(const std::multiset<Card>& cards, Combination& combo)
{
if(cards.size()<5)
return 0;
bool have_flush = false;
Combination flush_combo;
int res = getBestFlush(cards, combo);
if(res == C_ROYAL_FLUSH || res == C_STRAIGHT_FLUSH)
{
return combo.type;
}
if(res == C_FLUSH)
{
flush_combo = combo;
have_flush = true;
}
res = getBestFourOfAKind(cards, combo);
if(res == C_FOUR_OF_A_KIND)
{
return combo.type;
}
res = getBestFullHouse(cards, combo);
if(res == C_FULL_HOUSE)
{
return combo.type;
}
if(have_flush)
{
combo = flush_combo;
return combo.type;
}
res = getBestStraight(cards, combo);
if(res == C_STRAIGHT)
{
return combo.type;
}
res = getBestThreeOfAKind(cards, combo);
if(res == C_THREE_OF_A_KIND)
{
return combo.type;
}
res = getBestTwoPairs(cards, combo);
if(res == C_TWO_PAIRS)
{
return combo.type;
}
res = getBestPair(cards, combo);
if(res == C_PAIR)
{
return combo.type;
}
res = getTheHighestCard(cards, combo);
if(res == C_HIGH_CARD)
{
return combo.type;
}
return 0;
}
const bool operator > (const Combination& combo1, const Combination& combo2)
{
if(combo1.type > combo2.type)
return true;
if(combo1.type < combo2.type)
return false;
switch(combo1.type)
{
case C_HIGH_CARD:
case C_PAIR:
case C_TWO_PAIRS:
case C_FOUR_OF_A_KIND:
case C_THREE_OF_A_KIND:
case C_FULL_HOUSE:
case C_FLUSH:
{
for(int i=0; i<combo1.cards.size();i++)
{
if(combo1.cards[i] == combo2.cards[i])
continue;
return combo1.cards[i].value > combo2.cards[i].value;
}
return false;
}
case C_STRAIGHT:
case C_STRAIGHT_FLUSH:
{
return combo1.cards[0].value > combo2.cards[0].value;
}
case C_ROYAL_FLUSH:
return false;
default:
return true; // C_NO_COMBO
}
eosio_assert(false,"wrong combination type");
return false;
}
//-----------------------------------------------------------------------------
#define UINT_BIT ( sizeof( unsigned int ) * CHAR_BIT )
#define C1 0x1010101
#define C2 0x1010104
typedef unsigned long long int ULONG64;
typedef unsigned int UINT32;
typedef unsigned char UINT8;
typedef unsigned char BOOL;
struct CGost89Crypt
{
UINT32 m_uiKey[8];
UINT8 m_iTable[8][16];
};
static const UINT8 m_iTable[8][16] =
{
0xF, 0xC, 0x2, 0xA, 0x6, 0x4, 0x5, 0x0, 0x7, 0x9, 0xE, 0xD, 0x1, 0xB, 0x8, 0x3,
0xB, 0x6, 0x3, 0x4, 0xC, 0xF, 0xE, 0x2, 0x7, 0xD, 0x8, 0x0, 0x5, 0xA, 0x9, 0x1,
0x1, 0xC, 0xB, 0x0, 0xF, 0xE, 0x6, 0x5, 0xA, 0xD, 0x4, 0x8, 0x9, 0x3, 0x7, 0x2,
0x1, 0x5, 0xE, 0xC, 0xA, 0x7, 0x0, 0xD, 0x6, 0x2, 0xB, 0x4, 0x9, 0x3, 0xF, 0x8,
0x0, 0xC, 0x8, 0x9, 0xD, 0x2, 0xA, 0xB, 0x7, 0x3, 0x6, 0x5, 0x4, 0xE, 0xF, 0x1,
0x8, 0x0, 0xF, 0x3, 0x2, 0x5, 0xE, 0xB, 0x1, 0xA, 0x4, 0x7, 0xC, 0x9, 0xD, 0x6,
0x3, 0x0, 0x6, 0xF, 0x1, 0xE, 0x9, 0x2, 0xD, 0x8, 0xC, 0x4, 0xB, 0xA, 0x5, 0x7,
0x1, 0xA, 0x6, 0x8, 0xF, 0xB, 0x0, 0x4, 0xC, 0x3, 0x5, 0x9, 0x7, 0xD, 0x2, 0xE
};
struct GostData
{
union
{
ULONG64 m_lData;
UINT8 m_chData[8];
UINT32 m_iData[2];
};
};
static const int g_iKeyOffset[32] =
{
0, 1, 2, 3, 4, 5, 6, 7,
0, 1, 2, 3, 4, 5, 6, 7,
0, 1, 2, 3, 4, 5, 6, 7,
7, 6, 5, 4, 3, 2, 1, 0
};
inline unsigned int GetBit(void * src, unsigned int pos)
{
unsigned char* ptr = (unsigned char*)src + pos / CHAR_BIT;
return (*ptr >> (pos % CHAR_BIT)) & 1;
}
inline void SetBit(void* dst, unsigned int pos, unsigned int value)
{
unsigned char* ptr = (unsigned char*)dst + pos / CHAR_BIT;
unsigned bit_num = pos % CHAR_BIT;
*ptr = (*ptr &~(1 << bit_num)) | ((value & 1) << bit_num);
}
ULONG64 GetRGPCH(ULONG64 inData)
{
UINT32 N1;
UINT32 N2;
N1 = (UINT32)inData;
N2 = inData >> 32;
N1 = (N1 + C1) % 0x10000000;
N2 = (N2 + C2) % 0xFFFFFFFF;
inData = N2;
inData = (inData << 32) | N1;
return inData;
}
void DoMainStep(struct GostData* inData, BOOL bMac, BOOL bCrypt, const struct CGost89Crypt* inCryptEntity)
{
unsigned int iLeft = inData->m_iData[0]; // Разбиваем на 2 блока.
unsigned int iRight = inData->m_iData[1];
unsigned int iSum = 0;
unsigned int iMaxLoop;
if (bMac) // Выработка имитовставки.
{
iMaxLoop = 16;
bCrypt = true;
}
else
iMaxLoop = 32;
unsigned int i;
unsigned int z;
for (z = 0; z < iMaxLoop; z++)
{
if (bCrypt) // S = N1 + X (mod 2 в 32).
iSum = iLeft + inCryptEntity->m_uiKey[g_iKeyOffset[z]] % INT_MAX;
else
iSum = iLeft + inCryptEntity->m_uiKey[g_iKeyOffset[31 - z]] % INT_MAX;
int iBitArray[8];
memset(iBitArray, 0, sizeof(int) * 8);
// Число S разбивается на 8 частей: S0,S1,S2,S3, S4,S5,S6,S7 по 4 бита каждая, где S0 - младшая, а S7 - старшая части числа S.
int iOffset = 31;
for (i = 0; i < 8; i++)
{
SetBit(&iBitArray[i], 0, GetBit(&iSum, iOffset));
iOffset--;
SetBit(&iBitArray[i], 1, GetBit(&iSum, iOffset));
iOffset--;
SetBit(&iBitArray[i], 2, GetBit(&iSum, iOffset));
iOffset--;
SetBit(&iBitArray[i], 3, GetBit(&iSum, iOffset));
iOffset--;
};
// Для всех i от 0 до 7: Si = T(i, Si), где T(a, b) означает ячейку таблицы замен с номером строки a и номером столбца b (счет с нуля).
for (i = 0; i < 8; i++)
iBitArray[i] = inCryptEntity->m_iTable[i][iBitArray[i]];
iOffset = 0; // Укладываем обратно.
for (i = 0; i < 8; i++)
{
SetBit(&iSum, iOffset, GetBit(&iBitArray[i], 0));
iOffset++;
SetBit(&iSum, iOffset, GetBit(&iBitArray[i], 1));
iOffset++;
SetBit(&iSum, iOffset, GetBit(&iBitArray[i], 2));
iOffset++;
SetBit(&iSum, iOffset, GetBit(&iBitArray[i], 3));
iOffset++;
};
// Новое число S, полученное на предыдущем шаге циклически сдвигается в сторону старших разрядов на 11 бит.
iSum = (iSum << 11) | (iSum >> 21);
// S = S xor N2, где xor - операция исключающего или.
iSum = iRight ^ iSum;
iRight = iLeft; //# N2 = N1.
iLeft = iSum; //# N1 = S.
}
inData->m_iData[0] = iRight;
inData->m_iData[1] = iLeft;
}
void GostGammaBlockEncode(struct GostData* inData, unsigned int iBlockCount, ULONG64 inKey, const struct CGost89Crypt* inCryptEntity)
{
unsigned int i = 0;
for (i = 0; i < iBlockCount; i++)
{
inKey = GetRGPCH(inKey); // Инициализируем РГПСЧ зашифрованной синхропосылкой.
DoMainStep((struct GostData*)&inKey, false, true, inCryptEntity); // Шифруем синхропосылку главным шагом.
inData->m_lData ^= inKey; // Накладываем гамму.
//inData++;
}
}
unsigned char* decrypt_data1(unsigned char* data, unsigned int* key, unsigned int* synchro)
{
CGost89Crypt g_ctx;
memset(&g_ctx, 0, sizeof(CGost89Crypt));
memcpy(&g_ctx.m_uiKey, &key[0], 32);
memcpy(&g_ctx.m_iTable, &m_iTable, 128);
ULONG64 sync_tmp;
memcpy(&sync_tmp, &synchro[0], 8);
unsigned char* tmp = new unsigned char[8];
memcpy(tmp, data, 8);
GostGammaBlockEncode((GostData*)tmp, 1, sync_tmp, &g_ctx);
return tmp;
}
int freeMem(unsigned char* arrayPtr) {
delete[] arrayPtr;
return 0;
}
//-----------------------------------------------------------------------------
void Table::initTheDeckOfCards()
{
the_deck_of_cards = the_const_deck;
}
uint8_t Table::getTableStatus() const
{
return table_status;
}
void Table::setTableStatus(const uint8_t new_status)
{
table_status = new_status;
}
void Table::updateOutPlayerCurRoundBets(Player& out_plr, uint8_t plr_index)
{
eosio::asset out_player_bet = out_plr.cur_round_bets;
for(Player& plr: players)
{
if(plr.name == out_plr.name)
continue;
if(plr.status == P_NO_PLAYER || plr.status == P_WAIT_NEW_GAME)
continue;
if(plr.all_in_flag == P_ALL_IN && plr.all_in_round == current_game_round)
{
if(out_player_bet < plr.acts.back().bet_)
plr.all_in_bank += out_player_bet;
else
plr.all_in_bank += plr.acts.back().bet_;
}
}
bank += out_player_bet;
table_cur_round_bets -= out_player_bet;
current_round_players_bet_acts[plr_index] = 0;
}
void Table::addNewAct(Player& player, uint8_t player_index, Act& act)
{
switch(act.act_)
{
case ACT_BET:
{
eosio_assert(act.bet_.is_valid(), "Invalid bet value");
eosio_assert(act.bet_.amount > 0, "Act: Quantity must be positive");
// all_in
if(act.bet_ == player.stack + player.cur_round_bets)
{
act.description = ACT_ALLIN;
player.all_in_flag = P_ALL_IN;
player.all_in_bank = bank;
player.all_in_round = current_game_round;
allin_players_count++;
break;
}
// сумма ставок в этом раунде + stack <= bet
eosio_assert(act.bet_ <= player.stack + player.cur_round_bets, "Insufficient stack");
// NO BET`s YET
if(current_bet.amount == 0)
{
int n = act.bet_/small_blind;
eosio::asset check = n*small_blind;
eosio_assert(check == act.bet_,"Invalid bet value");
eosio_assert(act.bet_ >= 2*small_blind, "Invalid bet value");
break;
}
// CALL
if(act.bet_ == current_bet)
{
act.description = ACT_CALL;
break;
}
// ONLY RAISE
eosio_assert(act.bet_ >= current_bet*2, "Invalid bet value");
act.description = ACT_RISE;
break;
}
case ACT_CHECK:
{
if(current_game_round == 0)
{
eosio_assert(players[bb_index].name == player.name ||
player.extra_bb_status == 1, "Wrong act");
eosio_assert( current_bet == small_blind*2, "Wrong act");
}
else
{
eosio_assert(current_bet.amount == 0, "Wrong act");
}
break;
}
case ACT_FOLD:
{
if(player.cur_round_bets.amount != 0)
updateOutPlayerCurRoundBets(player, player_index);
player.status = P_FOLD;
current_folds_count++;
break;
}
default:
eosio_assert(false,"Wrong Act type.");
}
if(act.act_ == ACT_BET)
{
if(act.bet_ > current_bet)
{
current_bet = act.bet_;
}
table_cur_round_bets += act.bet_ - player.cur_round_bets;
current_bank += act.bet_ - player.cur_round_bets;
current_round_players_bet_acts[player_index]++;
}
players_acts.push_back(PlayerAct(player_index, player.name, act));
}
void Player::clearGameInfo()
{
cards_indexes.clear();
acts.clear();
cur_round_bets = eosio::asset(0, EOS_SYMBOL);
all_in_bank = eosio::asset(0, EOS_SYMBOL);
all_in_round = 0;
all_in_flag = 0;
count_of_acts = 0;
have_event = 0;
sum_of_bets = eosio::asset(0, EOS_SYMBOL);
rake = eosio::asset(0, EOS_SYMBOL);
late_trxs.clear();
applied_late_trxs.clear();
}
void Player::addNewAct(const Act& act)
{
acts.push_back(act);
if( (act.act_ == ACT_BET) || (act.act_ == ACT_SMALL_BLIND) || (act.act_ == ACT_BIG_BLIND))
{
eosio::asset odds = eosio::asset(0,EOS_SYMBOL);
if(act.bet_ > cur_round_bets)
odds = act.bet_ - cur_round_bets;
stack -= odds;
cur_round_bets += odds;
if(stack.amount == 0)
all_in_flag = P_ALL_IN;
sum_of_bets += odds;
}
count_of_acts++;
}