4 | | - | let waves = 100000000 |
---|
5 | | - | |
---|
6 | | - | let empty = base58'' |
---|
7 | | - | |
---|
8 | | - | let hashSize = 32 |
---|
9 | | - | |
---|
10 | | - | let keySize = 32 |
---|
11 | | - | |
---|
12 | | - | let longSize = 8 |
---|
13 | | - | |
---|
14 | | - | let fightTimeout = 100 |
---|
15 | | - | |
---|
16 | | - | let movesCount = 3 |
---|
17 | | - | |
---|
18 | | - | let rock = base58'1' |
---|
19 | | - | |
---|
20 | | - | let paper = base58'2' |
---|
21 | | - | |
---|
22 | | - | let scissors = base58'3' |
---|
23 | | - | |
---|
24 | | - | func get (bytes,index) = take(drop(bytes, index), 1) |
---|
25 | | - | |
---|
26 | | - | |
---|
27 | | - | func getBinaryOr (key,default) = if (isDefined(getBinary(this, key))) |
---|
28 | | - | then getBinaryValue(this, key) |
---|
29 | | - | else default |
---|
30 | | - | |
---|
31 | | - | |
---|
32 | | - | func isValidMove (move) = if (if ((move == rock)) |
---|
33 | | - | then true |
---|
34 | | - | else (move == paper)) |
---|
35 | | - | then true |
---|
36 | | - | else (move == scissors) |
---|
37 | | - | |
---|
38 | | - | |
---|
39 | | - | func isValidMoves (moves) = if (if (if ((size(moves) >= movesCount)) |
---|
40 | | - | then isValidMove(get(moves, 0)) |
---|
41 | | - | else false) |
---|
42 | | - | then isValidMove(get(moves, 1)) |
---|
43 | | - | else false) |
---|
44 | | - | then isValidMove(get(moves, 2)) |
---|
45 | | - | else false |
---|
46 | | - | |
---|
47 | | - | |
---|
48 | | - | func round (p1Moves,p2Moves,round) = { |
---|
49 | | - | let m1 = get(p1Moves, round) |
---|
50 | | - | let m2 = get(p2Moves, round) |
---|
51 | | - | if ((m1 == m2)) |
---|
52 | | - | then 0 |
---|
53 | | - | else if (if (if (if ((m1 == scissors)) |
---|
54 | | - | then (m2 == paper) |
---|
55 | | - | else false) |
---|
56 | | - | then true |
---|
57 | | - | else if ((m1 == rock)) |
---|
58 | | - | then (m2 == scissors) |
---|
59 | | - | else false) |
---|
60 | | - | then true |
---|
61 | | - | else if ((m1 == paper)) |
---|
62 | | - | then (m2 == rock) |
---|
63 | | - | else false) |
---|
64 | | - | then -1 |
---|
65 | | - | else 1 |
---|
66 | | - | } |
---|
67 | | - | |
---|
68 | | - | |
---|
69 | | - | func fight (p1Moves,p2Moves) = ((round(p1Moves, p2Moves, 0) + round(p1Moves, p2Moves, 1)) + round(p1Moves, p2Moves, 2)) |
---|
70 | | - | |
---|
71 | | - | |
---|
72 | | - | func store (fightId,data) = DataEntry(fightId, (getBinaryOr(fightId, empty) + data)) |
---|
76 | | - | func create (movesHash) = { |
---|
77 | | - | let p = extract(i.payment) |
---|
78 | | - | if (isDefined(p.assetId)) |
---|
79 | | - | then throw("Payment should be in WAVES.") |
---|
80 | | - | else if ((p.amount != (1 * waves))) |
---|
81 | | - | then throw("Amount should be 1 WAVE.") |
---|
82 | | - | else if ((size(movesHash) != hashSize)) |
---|
83 | | - | then throw("Invalid moves.") |
---|
84 | | - | else WriteSet([store(((toBase58String(i.callerPublicKey) + "_") + toBase58String(i.transactionId)), movesHash)]) |
---|
85 | | - | } |
---|
86 | | - | |
---|
87 | | - | |
---|
88 | | - | |
---|
89 | | - | @Callable(i) |
---|
90 | | - | func join (fightId,moves) = { |
---|
91 | | - | let p = extract(i.payment) |
---|
92 | | - | if (isDefined(p.assetId)) |
---|
93 | | - | then throw("Payment should be in WAVES.") |
---|
94 | | - | else if ((p.amount != (1 * waves))) |
---|
95 | | - | then throw("Amount should be 1 WAVE.") |
---|
96 | | - | else if (!(isDefined(getBinary(this, fightId)))) |
---|
97 | | - | then throw("Fight is not found.") |
---|
98 | | - | else if ((size(getBinaryValue(this, fightId)) != hashSize)) |
---|
99 | | - | then throw("Fight is already started.") |
---|
100 | | - | else if (!(isValidMoves(moves))) |
---|
101 | | - | then throw("Invalid moves.") |
---|
102 | | - | else WriteSet([store(fightId, ((toBytes(height) + i.callerPublicKey) + moves))]) |
---|
103 | | - | } |
---|
104 | | - | |
---|
105 | | - | |
---|
106 | | - | |
---|
107 | | - | @Callable(i) |
---|
108 | | - | func complete (fightId,moves) = if (!(isDefined(getBinary(this, fightId)))) |
---|
109 | | - | then throw("Fight is not found.") |
---|
110 | | - | else { |
---|
111 | | - | let fightData = getBinaryValue(this, fightId) |
---|
112 | | - | if ((size(fightData) == hashSize)) |
---|
113 | | - | then throw("Fight is not started.") |
---|
114 | | - | else if ((size(fightData) > (((hashSize + longSize) + keySize) + movesCount))) |
---|
115 | | - | then throw("Fight is already finished.") |
---|
116 | | - | else { |
---|
117 | | - | let hashedMoves = take(getBinaryValue(this, fightId), hashSize) |
---|
118 | | - | if (if ((hashedMoves != blake2b256(moves))) |
---|
119 | | - | then true |
---|
120 | | - | else !(isValidMoves(moves))) |
---|
121 | | - | then throw("Invalid moves.") |
---|
122 | | - | else { |
---|
123 | | - | let p2PublicKey = takeRightBytes(dropRightBytes(fightData, movesCount), keySize) |
---|
124 | | - | let r = fight(take(moves, movesCount), takeRightBytes(fightData, movesCount)) |
---|
125 | | - | ScriptResult(WriteSet([store(fightId, moves)]), TransferSet(if ((r == 0)) |
---|
126 | | - | then [ScriptTransfer(i.caller, (1 * waves), unit), ScriptTransfer(addressFromPublicKey(p2PublicKey), (1 * waves), unit)] |
---|
127 | | - | else if ((0 > r)) |
---|
128 | | - | then [ScriptTransfer(i.caller, (2 * waves), unit)] |
---|
129 | | - | else [ScriptTransfer(addressFromPublicKey(p2PublicKey), (2 * waves), unit)])) |
---|
130 | | - | } |
---|
131 | | - | } |
---|
132 | | - | } |
---|
133 | | - | |
---|
134 | | - | |
---|
135 | | - | |
---|
136 | | - | @Callable(i) |
---|
137 | | - | func timeout (fightId) = if (!(isDefined(getBinary(this, fightId)))) |
---|
138 | | - | then throw("Fight is not found.") |
---|
139 | | - | else { |
---|
140 | | - | let fightData = getBinaryValue(this, fightId) |
---|
141 | | - | if ((size(fightData) == hashSize)) |
---|
142 | | - | then throw("Fight is not started.") |
---|
143 | | - | else if ((size(fightData) > (((hashSize + longSize) + keySize) + movesCount))) |
---|
144 | | - | then throw("Fight is already finished.") |
---|
145 | | - | else { |
---|
146 | | - | let h = toInt(take(drop(fightData, hashSize), longSize)) |
---|
147 | | - | if ((fightTimeout > (height - h))) |
---|
148 | | - | then throw("Player1 still has a time to complete the fight.") |
---|
149 | | - | else { |
---|
150 | | - | let p2PublicKey = takeRightBytes(dropRightBytes(fightData, movesCount), keySize) |
---|
151 | | - | ScriptResult(WriteSet([store(fightId, p2PublicKey)]), TransferSet([ScriptTransfer(addressFromPublicKey(p2PublicKey), (2 * waves), unit)])) |
---|
152 | | - | } |
---|
153 | | - | } |
---|
154 | | - | } |
---|
| 7 | + | func foo (value) = WriteSet([DataEntry("data", size(value))]) |
---|