-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpandafun.cpp
1750 lines (1671 loc) · 65.4 KB
/
pandafun.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
/**
* @file
* @copyright defined in eos/LICENSE.txt
*/
#include <pandafun.hpp>
#include <string.h>
namespace pandafun
{
using namespace eosio;
void pausecheck();
void blackcheck(account_name user);
void apply_signup( const pandafun::signup& signup_msg );
int hashcheck(checksum256 submit_hash, uint64_t randomnumber)
{
checksum256 hash;
char str[LENINT64];
memset(str, 0, LENINT64);
snprintf(str, LENINT64, "%llu", randomnumber);
sha256(str, strlen(str), &hash);
if(strncmp((const char *)submit_hash.hash, (const char *)hash.hash, 32) != 0)
{
/* Consumes too much time
print(" client random: ", randomnumber, " \n");
print(" client random in string: ");
prints(str);
print(" submit hash: ");
printhex( &submit_hash, 32 );
print(" hash: ");
printhex( &hash, 32 );
print(" end of hash \n ");
*/
return 1;
}
return 0;
}
void apply_init()
{
require_auth( auth_account );
config_type configs(code_account, code_account);
auto itr = configs.find(0);
if (itr == configs.end())
{
configs.emplace(code_account, [&](auto &rec)
{
rec.id = 0;
rec.normal = 0;
rec.copper = 0;
rec.silver = 0;
rec.gold = 0;
rec.diamond = 0;
rec.players = 28000;
rec.normalkey = 10000;
rec.diamondkey = 1;
rec.upgrade7days = 0;
rec.transform7days = 0;
rec.candles7days = 0;
rec.paused = RUNNING;
});
uint32_t index = now() / SECONDSADAY - 1;
asset_count_type candles(code_account, N(pray));
auto candlesitr = candles.find(index);
if (candlesitr == candles.end())
{
candles.emplace(code_account, [&](auto &rec)
{
rec.timestamp = index;
rec.number = 15000;
});
}
asset_count_type books(code_account, N(upgrade));
auto booksitr = books.find(index);
if (booksitr == books.end())
{
books.emplace(code_account, [&](auto &rec)
{
rec.timestamp = index;
rec.number = 15000;
});
}
asset_count_type tea(code_account, N(transform));
auto teaitr = tea.find(index);
if (teaitr == tea.end())
{
tea.emplace(code_account, [&](auto &rec)
{
rec.timestamp = index;
rec.number = 15000;
});
}
pandafun::signup signup_msg = {code_account, 0};
pandafun::apply_signup(signup_msg);
signup_msg.user = auth_account;
pandafun::apply_signup(signup_msg);
}
}
// Only for pandafun to sell pandas more than one to the user
void apply_transfers( const pandafun::transfers& transfers_msg )
{
eosio_assert(transfers_msg.memo.size() <= 256, "memo has more than 256 bytes" );
require_auth( auth_account );
pausecheck();
blackcheck(transfers_msg.to);
panda_table_type pandas_to(code_account, transfers_msg.to);
apply_init();
eosio_assert(transfers_msg.quantity > 0, "Transfer from account pandafun, quantity must larger than 0!");
eosio_assert(transfers_msg.quantity < 1001, "Transfer from account pandafun, quantity must less than 1001!");
config_type configs(code_account, code_account);
auto itr = configs.find(0);
eosio_assert(itr != configs.end(), "NO key 0 row found in table configs.\n");
auto normalkey = itr->normalkey;
auto normal = itr->normal;
for (short i = 0; i < transfers_msg.quantity; i++)
{
pandas_to.emplace(code_account, [&](auto &rec)
{
rec.id = normalkey;
rec.type = normal_type;
memset(rec.skills, 0, SKILLLEN);
rec.skills[INNER_DAN] = 10;
});
normalkey += 1;
normal += 1;
}
configs.modify(itr, code_account, [&](auto &rec)
{
rec.normalkey = normalkey;
rec.normal = normal;
});
}
void apply_sell( const pandafun::sell& sell_msg )
{
eosio_assert(sell_msg.memo.size() <= 256, "memo has more than 256 bytes" );
require_auth( sell_msg.seller );
pausecheck();
blackcheck(sell_msg.seller);
panda_table_type pandas(code_account, sell_msg.seller);
selling_table_type pandas_selling(code_account, sell_msg.seller);
eosio_assert( sell_msg.price.symbol == S(4,EOS) , "Only EOS token allowed.\n" );
eosio_assert( sell_msg.price.is_valid(), "Invalid sell_msg.price\n" );
eosio_assert( sell_msg.price.amount > 0, "The sell_msg.price must be positive.\n" );
auto sellitr = pandas_selling.find(sell_msg.id);
eosio_assert(sellitr == pandas_selling.end(), "sell(): The panda has been in sale.\n");
auto pandaitr = pandas.find(sell_msg.id);
eosio_assert(pandaitr != pandas.end(), "sell(): The panda does not exist.\n");
pandas_selling.emplace(code_account, [&](auto &rec)
{
rec.id = pandaitr->id;
rec.type = pandaitr->type;
memcpy(rec.skills, pandaitr->skills, SKILLLEN);
rec.price = sell_msg.price;
});
pandas.erase(pandaitr);
}
void apply_cancelsell( const pandafun::cancelsell& cancelsell_msg )
{
eosio_assert(cancelsell_msg.memo.size() <= 256, "memo has more than 256 bytes" );
require_auth( cancelsell_msg.seller );
pausecheck();
blackcheck(cancelsell_msg.seller);
panda_table_type pandas(code_account, cancelsell_msg.seller);
selling_table_type pandas_selling(code_account, cancelsell_msg.seller);
auto sellitr = pandas_selling.find(cancelsell_msg.id);
eosio_assert(sellitr != pandas_selling.end(), "cancelsell(): The panda isn't in sale.\n");
pandas.emplace(code_account, [&](auto &rec)
{
rec.id = sellitr->id;
rec.type = sellitr->type;
memcpy(rec.skills, sellitr->skills, SKILLLEN);
});
pandas_selling.erase(sellitr);
}
void apply_buy( const pandafun::buy& buy_msg )
{
eosio_assert(buy_msg.memo.size() <= 256, "memo has more than 256 bytes" );
require_auth( auth_account );
pausecheck();
blackcheck(buy_msg.buyer);
panda_table_type pandas_buyer(code_account, buy_msg.buyer);
selling_table_type pandas_selling(code_account, buy_msg.seller);
auto sellitr = pandas_selling.find(buy_msg.id);
eosio_assert(sellitr != pandas_selling.end(), "buy(): The panda isn't in sale.\n");
eosio_assert(buy_msg.price >= sellitr->price, "buy(): The buyer's price is lower than the seller's.\n");
pandas_buyer.emplace(code_account, [&](auto &rec)
{
rec.id = sellitr->id;
rec.type = sellitr->type;
memcpy(rec.skills, sellitr->skills, SKILLLEN);
});
pandas_selling.erase(sellitr);
}
void apply_recvhashs( const pandafun::recvhashs& recvhashs_msg )
{
require_auth( recvhashs_msg.from );
if (recvhashs_msg.from != auth_account)
{
pausecheck();
blackcheck(recvhashs_msg.from);
}
hashmore_type hashs(code_account, recvhashs_msg.scope);
auto itr = hashs.begin();
if (itr != hashs.end())
{
// check the exist hashs are of the same txn with the coming one
// clear it if not
if (recvhashs_msg.from == auth_account)
{
if (itr->txnid == recvhashs_msg.txnid)
{
for (uint8_t i = 0; i < HASHNUM; i++)
{
hashs.modify(itr, code_account, [&](auto &rec)
{
rec.srvhash = recvhashs_msg.hashs[i];
});
itr++;
}
}
else
{
for (uint8_t i = 0; i < HASHNUM; i++)
{
hashs.modify(itr, code_account, [&](auto &rec)
{
rec.srvhash = recvhashs_msg.hashs[i];
memset(&rec.hash, 0, sizeof(checksum256));
rec.txnid = recvhashs_msg.txnid;
});
itr++;
}
}
}
else
{
if (itr->txnid == recvhashs_msg.txnid)
{
for (uint8_t i = 0; i < HASHNUM; i++)
{
hashs.modify(itr, code_account, [&](auto &rec)
{
rec.hash = recvhashs_msg.hashs[i];
});
itr++;
}
}
else
{
for (uint8_t i = 0; i < HASHNUM; i++)
{
hashs.modify(itr, code_account, [&](auto &rec)
{
rec.hash = recvhashs_msg.hashs[i];
memset(&rec.srvhash, 0, sizeof(checksum256));
rec.txnid = recvhashs_msg.txnid;
});
itr++;
}
}
}
}
else
{
if (recvhashs_msg.from == auth_account)
{
for (uint8_t i = 0; i < HASHNUM; i++)
{
hashs.emplace(code_account, [&](auto &rec)
{
rec.id = hashs.available_primary_key();
rec.srvhash = recvhashs_msg.hashs[i];
rec.txnid = recvhashs_msg.txnid;
});
}
}
else
{
for (uint8_t i = 0; i < HASHNUM; i++)
{
hashs.emplace(code_account, [&](auto &rec)
{
rec.id = hashs.available_primary_key();
rec.hash = recvhashs_msg.hashs[i];
rec.txnid = recvhashs_msg.txnid;
});
}
}
}
}
void apply_recvhash( const pandafun::recvhash& recvhash_msg )
{
require_auth( recvhash_msg.from );
if (recvhash_msg.from != auth_account)
{
pausecheck();
blackcheck(recvhash_msg.from);
}
hash_type hashs(code_account, recvhash_msg.scope);
auto itr = hashs.find(0);
if (itr != hashs.end())
{
// sometimes we cannot clean the hash table, it will contain
// one hash, so we cannot reauire the txnid match with each other,
// so we just update the txnid based on the last one, let the action
// ensure the hash's txnid matches the action's txnid
//eosio_assert(itr->txnid == recvhash_msg.txnid, "The received txnid doesn't match the exist one.\n");
if (recvhash_msg.from == auth_account)
{
hashs.modify(itr, code_account, [&](auto &rec)
{
rec.srvhash = recvhash_msg.hash;
if (rec.txnid != recvhash_msg.txnid)
{
rec.txnid = recvhash_msg.txnid;
memset(&rec.hash, 0, sizeof(checksum256));
}
});
}
else
{
hashs.modify(itr, code_account, [&](auto &rec)
{
rec.hash = recvhash_msg.hash;
if (rec.txnid != recvhash_msg.txnid)
{
rec.txnid = recvhash_msg.txnid;
memset(&rec.srvhash, 0, sizeof(checksum256));
}
});
}
}
else
{
if (recvhash_msg.from == auth_account)
{
hashs.emplace(code_account, [&](auto &rec)
{
rec.id = 0;
rec.srvhash = recvhash_msg.hash;
rec.txnid = recvhash_msg.txnid;
});
}
else
{
hashs.emplace(code_account, [&](auto &rec)
{
rec.id = 0;
rec.hash = recvhash_msg.hash;
rec.txnid = recvhash_msg.txnid;
});
}
}
}
void apply_dice( const pandafun::dice& dice_msg )
{
require_auth( auth_account );
pausecheck();
blackcheck(dice_msg.player);
hash_type hashs(code_account, dice_msg.player);
auto itr = hashs.find(0);
eosio_assert (itr != hashs.end(), "No hash found under player.\n");
eosio_assert (itr->txnid == dice_msg.txnid, "dice(): txnid doesn't match.\n");
auto submit_hash = itr->hash;
auto server_hash = itr->srvhash;
uint64_t random = 0;
// owner
if (hashcheck(submit_hash, dice_msg.randomvalue) == 0)
random ^= dice_msg.randomvalue;
eosio_assert(hashcheck(server_hash, dice_msg.srvrandom) == 0, "dice(): server's random number's sha256 sum doesn't match the submitted one.\n");
random ^= dice_msg.srvrandom;
auto finalvalue = random % 11 + 2;
dice_type dice(code_account, dice_msg.player);
auto diceitr = dice.find(0);
if (diceitr == dice.end())
{
dice.emplace(code_account, [&](auto &rec)
{
rec.id = 0;
rec.txnid = dice_msg.txnid;
rec.value = finalvalue;
});
}
else
{
dice.modify(diceitr, code_account, [&](auto &rec)
{
rec.txnid = dice_msg.txnid;
rec.value = finalvalue;
});
}
}
void generate_rand(uint32_t input, char out[3], uint32_t mod, int needed)
{
int32_t tmp = 0;
int32_t equal = 0;
int32_t produced = 0;
for (int i = 0; produced < needed; i++)
{
for (int j = i + 1; produced < needed; j++)
{
equal = 0;
auto middle = input + j;
tmp = (char)(middle * middle * middle % mod);
for (int k = 0; k < 3; k++)
{
if (tmp == out[k])
{
equal = 1;
break;
}
}
if (!equal)
{
out[i] = tmp;
produced++;
if (produced == needed)
return;
break;
}
}
}
}
void apply_pray( const pandafun::pray& pray_msg )
{
require_auth( auth_account );
pausecheck();
blackcheck(pray_msg.player);
panda_table_type pandas(code_account, pray_msg.player);
auto pandaitr = pandas.find(pray_msg.panda_id);
eosio_assert(pandaitr != pandas.end(), "pray(): The panda does not exist.\n");
eosio_assert(pandaitr->type != diamond_type, "pray(): The diamond panda cannot pray.\n");
asset_type assets(code_account, pray_msg.player);
auto asset_itr = assets.find(0);
eosio_assert (asset_itr != assets.end(), "pray(): No asset found in table assets.\n");
eosio_assert (asset_itr->candles > 0, "pray(): No candles left..\n");
assets.modify(asset_itr, code_account, [&](auto &rec)
{
rec.candles = asset_itr->candles - 1;
print("One used, left candles: ", rec.candles, " ");
});
hash_type hashs(code_account, pray_msg.player);
auto hashitr = hashs.find(0);
eosio_assert (hashitr != hashs.end(), "No hash found under player.\n");
eosio_assert (hashitr->txnid == pray_msg.txnid, "pray(): txnid doesn't match.\n");
auto submit_hash = hashitr->hash;
auto server_hash = hashitr->srvhash;
eosio_assert(hashcheck(submit_hash, pray_msg.randomvalue) == 0, "pray(): player's random number's sha256 sum doesn't match the submitted one.\n");
eosio_assert(hashcheck(server_hash, pray_msg.srvrandom) == 0, "pray(): server's random number's sha256 sum doesn't match the submitted one.\n");
auto result = pray_msg.randomvalue ^ pray_msg.srvrandom;
const int32_t range = 45000;
auto probability = result % range;
config_type configs(code_account, code_account);
auto cfgitr = configs.find(0);
eosio_assert(cfgitr != configs.end(), "pray(): NO key 0 row found in table configs.\n");
auto candles7days = cfgitr->candles7days;
auto allplayers = cfgitr->players;
hashs.erase(hashitr);
auto lefttimes = pandaitr->skills[INNER_DAN] - 1;
if (lefttimes == 0)
{
if (pandaitr->type == normal_type)
{
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.normal = cfgitr->normal - 1;
});
}
else if (pandaitr->type == copper_type)
{
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.copper = cfgitr->copper - 1;
});
}
else if (pandaitr->type == silver_type)
{
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.silver = cfgitr->silver - 1;
});
}
else if (pandaitr->type == gold_type)
{
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.gold = cfgitr->gold - 1;
});
}
pandas.erase(pandaitr);
}
else
{
pandas.modify(pandaitr, code_account, [&](auto &rec)
{
rec.skills[INNER_DAN] = lefttimes;
});
}
// update the statistics table for candles
uint32_t index = now() / SECONDSADAY;
asset_count_type candles(code_account, N(pray));
auto candlesitr = candles.find(index);
if (candlesitr == candles.end()) // a new day
{
candles.emplace(code_account, [&](auto &rec)
{
rec.timestamp = index;
rec.number = 1;
});
// add yesterday's value to the statistics, subtract the 8th day's value before if exists
auto reverseitr = candles.rbegin();
if (reverseitr != candles.rend())
reverseitr++;
if (reverseitr != candles.rend())
{
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.candles7days = cfgitr->candles7days + reverseitr->number;
});
}
reverseitr = candles.rbegin();
for (int i = 0; i < 7; i++)
{
if (reverseitr != candles.rend())
reverseitr++;
else
break;
}
if (reverseitr != candles.rend())
{
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.candles7days = cfgitr->candles7days - reverseitr->number;
});
}
}
else
{
candles.modify(candlesitr, code_account, [&](auto &rec)
{
rec.number = candlesitr->number + 1;
});
}
// probability, according to different panda type
int32_t pup = -1;
int32_t psame = -1;
panda_type newtype = 0;
int32_t success = 1;
// three sections: [0~45000)
// 1. upgraded probability, such as [0-1000)
// 2. the same probability, such as [1000-10000)
// 3. fail probability, the left, we just judge the first two's probability
if (pandaitr->type == normal_type)
{
// (NP - N7) / (50 * (NP - N7)) + 0.06
// up: range * [(NP - N7) / (300 * (NP - N7)) + 0.014]
// same: range * [(NP - N7) / (50 * (NP - N7)) + 0.04]
// 45000 / 300 = 150
// 45000 / 50 = 900
pup = (int)(150. * (allplayers - candles7days) / (allplayers + candles7days) + 0.5) + 630;
psame = (int)(900. * (allplayers - candles7days) / (allplayers + candles7days) + 0.5) + 1800 + pup;
if (probability >= psame)
{
success = 0;
}
else if (probability < pup)
{
newtype = copper_type;
}
else
{
newtype = normal_type;
}
}
else if (pandaitr->type == copper_type)
{
// up: range * [(NP - N7) / (300 * (NP - N7)) + 0.011]
// same: range * [(NP - N7) / (50 * (NP - N7)) + 0.037]
pup = (int)(150. * (allplayers - candles7days) / (allplayers + candles7days) + 0.5) + 495;
psame = (int)(900. * (allplayers - candles7days) / (allplayers + candles7days) + 0.5) + 1665 + pup;
if (probability >= psame)
{
success = 0;
}
else if (probability < pup)
{
if (cfgitr->silver < SILVERMAX)
{
newtype = silver_type;
}
else
{
success = 0;
print("sorry, silver panda has reached the limit.\n");
}
}
else
{
newtype = copper_type;
}
}
else if (pandaitr->type == silver_type)
{
// up: range * [(NP - N7) / (450 * (NP - N7)) + 0.0067]
// same: range * [(NP - N7) / (75 * (NP - N7)) + 0.023]
pup = (int)(100. * (allplayers - candles7days) / (allplayers + candles7days) + 0.5) + 301;
psame = (int)(600. * (allplayers - candles7days) / (allplayers + candles7days) + 0.5) + 1035 + pup;
if (probability >= psame)
{
success = 0;
}
else if (probability < pup)
{
if (cfgitr->gold < GOLDENMAX)
{
newtype = gold_type;
}
else
{
success = 0;
print("sorry, gold panda has reached the limit.\n");
}
}
else
{
if (cfgitr->silver < SILVERMAX)
{
newtype = silver_type;
}
else
{
success = 0;
print("sorry, silver panda has reached the limit.\n");
}
}
}
else if (pandaitr->type == gold_type)
{
// up: range * [(NP - N7) / (750 * (NP - N7)) + 0.0027]
// same: range * [(NP - N7) / (125 * (NP - N7)) + 0.013]
pup = (int)(60. * (allplayers - candles7days) / (allplayers + candles7days) + 0.5) + 121;
psame = (int)(360. * (allplayers - candles7days) / (allplayers + candles7days) + 0.5) + 585 + pup;
if (probability >= psame)
{
success = 0;
}
else if (probability < pup)
{
if (cfgitr->diamond < DIAMONDMAX)
{
newtype = diamond_type;
}
else
{
success = 0;
print("sorry, diamond panda has reached the limit.\n");
}
}
else
{
if (cfgitr->gold < GOLDENMAX)
{
newtype = gold_type;
}
else
{
success = 0;
print("sorry, gold panda has reached the limit.\n");
}
}
}
else
{
print("pray(): Don't support this type to pray.\n");
}
pray_type prays(code_account, pray_msg.player);
auto prayitr = prays.begin();
// skip if it's a 10 continuous action
if ((prayitr != prays.end()) && prayitr->txnid != pray_msg.txnid)
{
while (prayitr != prays.end())
{
prayitr = prays.erase(prayitr);
}
}
if (success == 0)
{
prays.emplace(code_account, [&](auto &rec)
{
memset(&rec, 0, sizeof(rec));
rec.txnid = pray_msg.txnid;
rec.panda_id = cfgitr->normalkey;
});
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.normalkey = cfgitr->normalkey + 1;
});
return;
}
// success
char skill[3] = {0};
char newskills[SKILLLEN] = {0};
uint64_t newpandaid = 0;
if (newtype == diamond_type)
{
newskills[INNER_DAN] = DIAMONDDAN;
newpandaid = cfgitr->diamondkey;
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.diamondkey = cfgitr->diamondkey + 1;
rec.diamond = cfgitr->diamond + 1;
});
// three skills
generate_rand(probability, skill, SKILLS, 3);
for (int i = 0; i < 3; i++)
{
BSETLEVEL(newskills[skill[i]], 1);
if (i == 0)
BSETTIMES(newskills[skill[i]], 2);
else
BSETTIMES(newskills[skill[i]], 1);
}
}
else
{
newpandaid = cfgitr->normalkey;
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.normalkey = cfgitr->normalkey + 1;
});
if (newtype == normal_type)
{
newskills[INNER_DAN] = NORMALDAN;
// no skills
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.normal = cfgitr->normal + 1;
});
}
else if (newtype == copper_type)
{
newskills[INNER_DAN] = BRONZEDAN;
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.copper = cfgitr->copper + 1;
});
// one skill
generate_rand(probability, skill, SKILLS, 1);
BSETLEVEL(newskills[skill[0]], 0x1);
BSETTIMES(newskills[skill[0]], 1);
}
else if (newtype == silver_type)
{
newskills[INNER_DAN] = SILVERDAN;
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.silver = cfgitr->silver + 1;
});
// two skills
generate_rand(probability, skill, SKILLS, 2);
for (int i = 0; i < 2; i++)
{
BSETLEVEL(newskills[skill[i]], 0x1);
BSETTIMES(newskills[skill[i]], 1);
}
}
else if (newtype == gold_type)
{
newskills[INNER_DAN] = GOLDENDAN;
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.gold = cfgitr->gold + 1;
});
// two skills
generate_rand(probability, skill, SKILLS, 2);
for (int i = 0; i < 2; i++)
{
BSETLEVEL(newskills[skill[i]], 1);
if (i == 0)
BSETTIMES(newskills[skill[i]], 2);
else
BSETTIMES(newskills[skill[i]], 1);
}
}
}
pandas.emplace(code_account, [&](auto &rec)
{
rec.id = newpandaid;
rec.type = newtype;
memcpy(rec.skills, newskills, SKILLLEN);
});
prays.emplace(code_account, [&](auto &rec)
{
rec.panda_id = newpandaid;
rec.type = newtype;
memcpy(rec.skills, newskills, SKILLLEN);
rec.txnid = pray_msg.txnid;
rec.success = 1;
});
}
// save the hashs from hashsmore to the 'hashs' table one by one
// and call the apply_pray()
void apply_prays( const pandafun::prays& prays_msg )
{
require_auth( auth_account );
pausecheck();
blackcheck(prays_msg.player);
panda_table_type pandas(code_account, prays_msg.player);
auto pandaitr = pandas.find(prays_msg.panda_id);
eosio_assert(pandaitr != pandas.end(), "prays(): The panda does not exist.\n");
eosio_assert(pandaitr->type != diamond_type, "prays(): The diamond panda cannot pray.\n");
asset_type assets(code_account, prays_msg.player);
auto asset_itr = assets.find(0);
eosio_assert (asset_itr != assets.end(), "prays(): No asset found in table assets.\n");
eosio_assert (asset_itr->candles >= 10, "prays(): the candles left are less than 10 ..\n");
// clear the pray result table if it's not empty
pray_type prays(code_account, prays_msg.player);
auto prayitr = prays.begin();
while (prayitr != prays.end())
{
prayitr = prays.erase(prayitr);
}
hashmore_type hashsmore(code_account, prays_msg.player);
auto moreitr = hashsmore.begin();
eosio_assert(moreitr != hashsmore.end(), "The hashsmore table is empty.\n ");
eosio_assert (moreitr->txnid == prays_msg.txnid, "prays(): txnid doesn't match.\n");
for (uint8_t i = 0; i < HASHNUM; i++)
{
// ensure the panda is alive
panda_table_type pandas(code_account, prays_msg.player);
auto pandaitr = pandas.find(prays_msg.panda_id);
if (pandaitr == pandas.end())
{
print("The panda no longer exists.\n");
break;
}
eosio_assert(moreitr != hashsmore.end(), "moreitr reaches the end of the table hashsmore.\n");
// We MUST define it here, it's like the inode on the disk, the editor
// holds the inode, but it's not the same one checked out by git, different inode!!
hash_type hashs(code_account, prays_msg.player);
auto hashitr = hashs.find(0);
if (hashitr != hashs.end())
hashs.erase(hashitr);
hashs.emplace(code_account, [&](auto &rec)
{
rec.id = 0;
rec.hash = moreitr->hash;
rec.srvhash = moreitr->srvhash;
rec.txnid = moreitr->txnid;
});
moreitr++;
pandafun::pray pray_msg = {prays_msg.player, prays_msg.panda_id, prays_msg.randomvalue[i], prays_msg.srvrandom[i], prays_msg.txnid };
pandafun::apply_pray(pray_msg);
}
hashmore_type hashsmorerm(code_account, prays_msg.player);
moreitr = hashsmorerm.begin();
while (moreitr != hashsmorerm.end())
{
moreitr = hashsmorerm.erase(moreitr);
}
}
void apply_upgrade( const pandafun::upgrade& upgrade_msg )
{
require_auth( auth_account );
pausecheck();
blackcheck(upgrade_msg.player);
asset_type assets(code_account, upgrade_msg.player);
auto asset_itr = assets.find(0);
eosio_assert (asset_itr != assets.end(), "upgrade(): No asset found in table assets.\n");
eosio_assert (asset_itr->books > 0, "upgrade(): No books left..\n");
panda_table_type pandas(code_account, upgrade_msg.player);
auto pandaitr = pandas.find(upgrade_msg.panda_id);
eosio_assert(pandaitr != pandas.end(), "upgrade(): The panda does not exist.\n");
auto current_level = BGETLEVEL(pandaitr->skills[upgrade_msg.skill]);
eosio_assert(current_level > 0, "upgrade(): This panda doesn't have this skill.\n");
eosio_assert(current_level < skills[upgrade_msg.skill].top_level, "upgrade(): This skill has been the top level.\n");
hash_type hashs(code_account, upgrade_msg.player);
auto hashitr = hashs.find(0);
eosio_assert (hashitr != hashs.end(), "No hash found under player.\n");
eosio_assert (hashitr->txnid == upgrade_msg.txnid, "upgrade(): txnid doesn't match.\n");
auto submit_hash = hashitr->hash;
auto server_hash = hashitr->srvhash;
eosio_assert(hashcheck(submit_hash, upgrade_msg.randomvalue) == 0, "upgrade(): player's random number's sha256 sum doesn't match the submitted one.\n");
eosio_assert(hashcheck(server_hash, upgrade_msg.srvrandom) == 0, "upgrade(): server's random number's sha256 sum doesn't match the submitted one.\n");
auto result = upgrade_msg.randomvalue ^ upgrade_msg.srvrandom;
auto probability = result % 35000;
if (!DEBUG)
{
hashs.erase(hashitr);
}
assets.modify(asset_itr, code_account, [&](auto &rec)
{
rec.books = asset_itr->books - 1;
print("One used, left books: ", rec.books, " ");
});
// update the statistics table for books
uint32_t index = now() / SECONDSADAY;
config_type configs(code_account, code_account);
auto cfgitr = configs.find(0);
eosio_assert(cfgitr != configs.end(), "upgrade(): NO key 0 row found in table configs.\n");
asset_count_type books(code_account, N(upgrade));
auto booksitr = books.find(index);
if (booksitr == books.end()) // a new day
{
books.emplace(code_account, [&](auto &rec)
{
rec.timestamp = index;
rec.number = 1;
});
// add yesterday's value to the statistics, subtract the 8th day's value before if exists
auto reverseitr = books.rbegin();
if (reverseitr != books.rend())
reverseitr++;
if (reverseitr != books.rend())
{
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.upgrade7days = cfgitr->upgrade7days + reverseitr->number;
});
}
reverseitr = books.rbegin();
for (int i = 0; i < 7; i++)
{
if (reverseitr != books.rend())
reverseitr++;
else
break;
}
if (reverseitr != books.rend())
{
configs.modify(cfgitr, code_account, [&](auto &rec)
{
rec.upgrade7days = cfgitr->upgrade7days - reverseitr->number;
});
}
}
else
{
books.modify(booksitr, code_account, [&](auto &rec)
{
rec.number = booksitr->number + 1;
});
}
int32_t pup = -1;
int32_t newlevel = current_level;
int32_t success = 1;
auto allplayers = cfgitr->players;
auto upgrade7days = cfgitr->upgrade7days;
if (current_level == 1)
{
// 2 sections, enlarge by 35000: [0~35000]
// (NP - N7) / (50 * (NP + N7)) + 0.04 / 0.01
// 1. upgraded probability, such as [0-1000)
// 2. fail probability, the left
pup = (int)(700. * (allplayers - upgrade7days) / (allplayers + upgrade7days) + 0.5) + 1400;
if (probability < pup)
{
newlevel = current_level + 1;
}
else
{
success = 0;
}
}
else if (current_level == 2)
{
pup = (int)(200. * (allplayers - upgrade7days) / (allplayers + upgrade7days) + 0.5) + 350;
if (probability < pup)
{
newlevel = current_level + 1;