-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGame.sol
145 lines (123 loc) · 3.49 KB
/
Game.sol
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
pragma ton-solidity ^0.42.0;
pragma AbiHeader expire;
pragma AbiHeader time;
pragma AbiHeader pubkey;
import 'interfaces/LotteryInterface.sol';
import 'utils/MessageUtil.sol';
import 'utils/TextUtil.sol';
struct GameInfo {
uint128 depositAmount;
uint128 rewardAmount;
uint64 depositCount;
uint32 minCount;
uint32 gameChance;
}
/**
* Error codes
* • 100 — Method only for root
*/
contract Game is MessageUtil,
TextUtil {
/*************
* CONSTANTS *
*************/
/**************
* STRUCTURES *
**************/
/*************
* VARIABLES *
*************/
address private _rootAddress;
mapping(address => uint128) _deposits;
uint128 private _depositAmount;
uint64 private _depositCount;
uint32 private _minCount;
uint32 private _gameChance;
/*************
* MODIFIERS *
*************/
modifier accept {
tvm.accept();
_;
}
modifier onlyRoot {
require(msg.sender == _rootAddress, 100, "Method only for root");
_;
}
/***************
* CONSTRUCTOR *
***************/
constructor(
address rootAddress,
uint32 minCount,
uint32 gameChance
) public {
_rootAddress = rootAddress;
_minCount = minCount;
_gameChance = gameChance;
_saveText();
LotteryInterface(_rootAddress).returnChange{value: 0, flag: 64}();
}
/***********
* GETTERS *
***********/
function getRootAddress() public view returns (address rootAddress) { return _rootAddress; }
function getInfo() public view returns (GameInfo info) {
return GameInfo(
_depositAmount,
address(this).balance,
_depositCount,
_minCount,
_gameChance
);
}
function getDeposits() public view returns (
address[] owners,
uint128[] deposits,
uint64 totalLength
) {
optional(address, uint128) client = _deposits.min();
while (client.hasValue()) {
(address addr, uint128 reward) = client.get();
owners.push(addr);
deposits.push(reward);
client = _deposits.next(addr);
}
return (owners, deposits, _depositCount);
}
/************
* EXTERNAL *
************/
function deposit(address sender, uint128 reward) external onlyRoot {
if (_deposits.exists(sender)) {
_deposits[sender] += reward;
} else {
_deposits[sender] = reward;
_depositCount++;
}
_depositAmount += reward;
if (_depositCount >= _minCount) {
uint32 chance = rnd.next(uint32(100));
if (chance < _gameChance) {
int rndReward = rnd.next(_depositAmount);
optional(address, uint128) client = _deposits.min();
while (client.hasValue()) {
(address addr, uint128 value) = client.get();
rndReward -= value;
if (rndReward <= 0) {
LotteryInterface(_rootAddress).onGameOver{value: 0.1e9, flag: 1}(addr, address(this).balance, _depositAmount, _depositCount);
addr.transfer({value: 0, flag: 128, body: _getTransferBody(TEXT_REWARD)});
break;
}
client = _deposits.next(addr);
}
}
}
}
/***********
* RECEIVE *
***********/
/***********
* PRIVATE *
***********/
}