-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserpentGamblingGame.se
97 lines (72 loc) · 2.4 KB
/
serpentGamblingGame.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
# Bets: (starts at id=10)
# 0...10: Contract data
# 10+id: Bounty for number id. id in {0, ..., maxBets}
# 10+maxBets+id: Gambler
# 10+maxBets+id+1: Number bet
# 10+maxBets+id+2: Bet amount
# TEST
# 5: random in {1, ..., maxBets}
# 110+numBets*slots+id...: ether returned
#
# Lines with "#test" are optional and can be deleted
init:
# Bounty
contract.storage[1] = 0
# Number of bets
contract.storage[2] = 0
# Master (only the master can send transactions to this contract)
contract.storage[3] = msg.sender
# Number of possible bets (maxBets)
contract.storage[4] = 10
code:
if msg.sender != contract.storage[3]:
return(1)
slots = 3
numNums = contract.storage[4]
numBets = contract.storage[2]
totalBet = contract.storage[1]
# Insert bet info [0, gambler, number bet]
if msg.data[0] == 0:
gambler = msg.data[1]
numberBet = msg.data[2]
if numberBet > numNums:
return(1)
if numberBet == 0:
return(1)
# Increase the amount bet for the number numberBet
contract.storage[10+numberBet] += msg.value
# Store data of the bet
id = 10+numNums+slots*numBets+1
contract.storage[id] = gambler
contract.storage[id + 1] = numberBet
contract.storage[id + 2] = msg.value
contract.storage[1] += msg.value
contract.storage[2] += 1
# Time expired
elif msg.data[0] == 1:
# Winner number
random = (block.timestamp % contract.storage[4]) +1
contract.storage[5] = random #test
# Amount bet on the winner number
betAmount = contract.storage[10 + random]
i = 0
# No winner. Funds are given back
if betAmount == 0:
while i < numBets:
id = 10+numNums+slots*i+1
send(contract.storage[id], contract.storage[id+2])
contract.storage[110+numBets*slots+id] = contract.storage[id] #test
contract.storage[110+numBets*slots+id+1] = contract.storage[id+2] #test
i += 1
# There is at least 1 winner. Funds are distributed among the winners.
else:
while i < numBets:
id = 10+numNums+slots*i+1
if random == contract.storage[id+1]:
userPrize = ((contract.storage[id+2] * totalBet * 1000) / betAmount) / 1000
send(contract.storage[id], userPrize)
contract.storage[110+numBets*slots+id] = contract.storage[id] #test
contract.storage[110+numBets*slots+id+1] = userPrize #test
i += 1
#suicide(contract.storage[3]) # Uncomment to delete the contract after using it.
return(0)