-
Notifications
You must be signed in to change notification settings - Fork 17
/
lotto.se
233 lines (195 loc) · 7.49 KB
/
lotto.se
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
extern rng = [random_numbers]
data tickets[2^80](owner, numbers[5], powerball)
data results[2^80](payout, claimed)
data jackpot_results(count, winners[2^80])
data config(ticket_cost, admin, next_id, rng_address, lottery_length, redemption_length, lottery_deadline, redemption_deadline, winning_numbers[6])
data payouts(zero_and_pb, one, one_and_pb, two, two_and_pb, three, three_and_pb, four, four_and_pb, five, jackpot_base, jackpot_increment, current_jackpot)
# 0
def initialize():
self.config.admin = msg.sender
# 1
def set_configuration(ticket_cost, rng_address, lottery_length, redemption_length):
if msg.sender == self.config.admin:
if (redemption_deadline == 0) or block.number > redemption_deadline:
self.config.lottery_length = lottery_length
self.config.redemption_length = redemption_length
self.config.ticket_cost = ticket_cost
if rng_address != 0:
self.config.rng_address = rng_address
else:
self.config.rng_address = create('rng.se')
# 2
def buy_ticket(first, second, third, fourth, fifth, powerball):
if msg.value >= self.config.ticket_cost:
if block.number <= self.config.lottery_deadline:
id = self.config.next_id
if self._validate_ticket(first, second, third, fourth, fifth, powerball) != 1:
return(-3)
self.tickets[id].owner = msg.sender
self.tickets[id].numbers[0] = first
self.tickets[id].numbers[1] = second
self.tickets[id].numbers[2] = third
self.tickets[id].numbers[3] = fourth
self.tickets[id].numbers[4] = fifth
self.tickets[id].powerball = powerball
self.config.next_id = id + 1
return([id], 1)
else:
return(-2)
else:
return(-1)
# 3
def get_ticket_owner(id):
return(self.tickets[id].owner)
# 4
def get_ticket_numbers(id):
return([self.tickets[id].numbers[0], self.tickets[id].numbers[1], self.tickets[id].numbers[2], self.tickets[id].numbers[3], self.tickets[id].numbers[4], self.tickets[id].powerball], 6)
# 5
def transfer_ticket(id, new_owner):
if self.tickets[id].owner == msg.sender:
self.tickets[id].owner = new_owner
# 6
def check_winners():
if (block.number <= self.config.lottery_deadline):
return(-1)
elif self.config.winning_numbers[0] != 0:
return(-2)
else:
winning_numbers = self.config.rng_address.random_numbers(as=rng, outsz = 6)
i = 0
while i < 6:
self.config.winning_numbers[i] = winning_numbers[i]
i += 1
i = 0
while i < self.config.next_id:
self._calculate_result(i)
i += 1
self._calculate_jackpot_results()
return(winning_numbers, 6)
# 7
def claim_winnings(id):
owner = self.tickets[id].owner
if (owner == msg.sender) and (block.number <= self.config.redemption_deadline):
if self.results[id].claimed == 1:
return(-2)
else:
payout = self.results[id].payout
send(owner, payout)
self.results[id].claimed = 1
return(payout)
else:
return(-1)
# 8
def get_balance():
return([self.balance])
# 9
def set_payouts(zero_and_pb, one, one_and_pb, two, two_and_pb, three, three_and_pb, four, four_and_pb, five, jackpot_base, jackpot_increment):
if msg.sender == self.config.admin:
if (redemption_deadline == 0) or block.number > redemption_deadline:
self.payouts.zero_and_pb = zero_and_pb
self.payouts.one = one
self.payouts.one_and_pb = one_and_pb
self.payouts.two = two
self.payouts.two_and_pb = two_and_pb
self.payouts.three = three
self.payouts.three_and_pb = three_and_pb
self.payouts.four = four
self.payouts.four_and_pb = four_and_pb
self.payouts.five = five
self.payouts.jackpot_base = jackpot_base
self.payouts.jackpot_increment = jackpot_increment
self.payouts.current_jackpot = jackpot_base
return(1)
else:
return(-1)
# 10
def withdraw(amount):
if block.number <= self.config.redemption_deadline:
return(-1)
elif msg.sender != self.config.admin:
return(-2)
else:
send(msg.sender, amount)
return(amount)
# 11
def start_lotto():
if msg.sender == self.config.admin and block.number > self.config.redemption_deadline:
self.config.lottery_deadline = block.number + self.config.lottery_length
self.config.redemption_deadline = self.config.lottery_deadline + self.config.redemption_length
self.config.next_id = 0
self.jackpot_results.count = 0
i = 0
while i < 6:
self.config.winning_numbers[i] = 0
i += 1
return(1)
else:
return(-1)
def _calculate_result(id):
if msg.sender == self:
matches = 0
i = 0
while i < 5:
j = 0
while j < 5:
if self.tickets[id].numbers[i] == self.config.winning_numbers[j]:
matches += 1
j += 1
i += 1
if self.tickets[id].powerball == self.config.winning_numbers[5]:
powerball = 1
else:
powerball = 0
if (matches == 0) and (powerball == 1):
self.results[id].payout = self.payouts.zero_and_pb
elif (matches == 1) and (powerball == 0):
self.results[id].payout = self.payouts.one
elif (matches == 1) and (powerball == 1):
self.results[id].payout = self.payouts.one_and_pb
elif (matches == 2) and (powerball == 0):
self.results[id].payout = self.payouts.two
elif (matches == 2) and (powerball == 1):
self.results[id].payout = self.payouts.two_and_pb
elif (matches == 3) and (powerball == 0):
self.results[id].payout = self.payouts.three
elif (matches == 3) and (powerball == 1):
self.results[id].payout = self.payouts.three_and_pb
elif (matches == 4) and (powerball == 0):
self.results[id].payout = self.payouts.four
elif (matches == 4) and (powerball == 1):
self.results[id].payout = self.payouts.four_and_pb
elif (matches == 5) and (powerball == 0):
self.results[id].payout = self.payouts.five
elif (matches == 5) and (powerball == 1):
self._add_jackpot_winner(id)
def _add_jackpot_winner(id):
if msg.sender == self:
count = self.jackpot_results.count
self.jackpot_results.count = count + 1
self.jackpot_results.winners[count] = id
def _calculate_jackpot_results():
count = self.jackpot_results.count
i = 0
while i < count:
id = self.jackpot_results.winners[i]
self.results[id].payout = (self.payouts.current_jackpot / count)
i += 1
if count > 0:
self.payouts.current_jackpot = self.payouts.jackpot_base
else:
self.payouts.current_jackpot += self.payouts.jackpot_increment
def _validate_ticket(first, second, third, fourth, fifth, powerball):
if (powerball < 1) or (powerball > 35):
return(-1)
numbers = [first, second, third, fourth, fifth]
i = 0
while i < 5:
if (numbers[i] < 1) or (numbers[i] > 59):
return(-1)
j = 0
while j < 5:
if (i != j) and (numbers[i] == numbers[j]):
return(-1)
j += 1
i += 1
return(1)