1 | 1 | | {-# STDLIB_VERSION 6 #-} |
---|
2 | 2 | | {-# SCRIPT_TYPE ACCOUNT #-} |
---|
3 | 3 | | {-# CONTENT_TYPE DAPP #-} |
---|
4 | 4 | | let separator = "_" |
---|
5 | 5 | | |
---|
6 | 6 | | let kGoalP1 = "goalP1" |
---|
7 | 7 | | |
---|
8 | 8 | | let kGoalP2 = "goalP2" |
---|
9 | 9 | | |
---|
10 | 10 | | let kDeflection = "deflection" |
---|
11 | 11 | | |
---|
12 | 12 | | let kVertex1 = "vertex1" |
---|
13 | 13 | | |
---|
14 | 14 | | let kVertex2 = "vertex2" |
---|
15 | 15 | | |
---|
16 | 16 | | let kVertex3 = "vertex3" |
---|
17 | 17 | | |
---|
18 | 18 | | let kMaxBet = "maxBet" |
---|
19 | 19 | | |
---|
20 | 20 | | func randomize (bytes,range) = { |
---|
21 | 21 | | let hash = sha256(bytes) |
---|
22 | 22 | | let index = toInt(hash) |
---|
23 | 23 | | (index % range) |
---|
24 | 24 | | } |
---|
25 | 25 | | |
---|
26 | 26 | | |
---|
27 | 27 | | func getPoint (bytes) = { |
---|
28 | 28 | | let x = randomize(take(bytes, (size(bytes) / 2)), 100) |
---|
29 | 29 | | let y = randomize(takeRight(bytes, (size(bytes) / 2)), 100) |
---|
30 | 30 | | $Tuple2(x, y) |
---|
31 | 31 | | } |
---|
32 | 32 | | |
---|
33 | 33 | | |
---|
34 | 34 | | func getGoalLinePoints () = { |
---|
35 | 35 | | let p1 = split(getStringValue(this, kGoalP1), separator) |
---|
36 | 36 | | let p2 = split(getStringValue(this, kGoalP2), separator) |
---|
37 | 37 | | $Tuple2($Tuple2(parseIntValue(p1[0]), parseIntValue(p1[1])), $Tuple2(parseIntValue(p2[0]), parseIntValue(p2[1]))) |
---|
38 | 38 | | } |
---|
39 | 39 | | |
---|
40 | 40 | | |
---|
41 | 41 | | func getDeflection () = getIntegerValue(this, kDeflection) |
---|
42 | 42 | | |
---|
43 | 43 | | |
---|
44 | 44 | | func getMaxBet () = getIntegerValue(this, kMaxBet) |
---|
45 | 45 | | |
---|
46 | 46 | | |
---|
47 | 47 | | func getPolygonPoints () = { |
---|
48 | 48 | | let p1 = split(getStringValue(this, kVertex1), separator) |
---|
49 | 49 | | let p2 = split(getStringValue(this, kVertex2), separator) |
---|
50 | 50 | | let p3 = split(getStringValue(this, kVertex3), separator) |
---|
51 | 51 | | $Tuple3($Tuple2(parseIntValue(p1[0]), parseIntValue(p1[1])), $Tuple2(parseIntValue(p2[0]), parseIntValue(p2[1])), $Tuple2(parseIntValue(p3[0]), parseIntValue(p3[1]))) |
---|
52 | 52 | | } |
---|
53 | 53 | | |
---|
54 | 54 | | |
---|
55 | 55 | | func getLine (p1,p2) = { |
---|
56 | 56 | | let a = (p2._2 - p1._2) |
---|
57 | 57 | | let b = (p1._1 - p2._1) |
---|
58 | 58 | | let c = ((p1._2 * (p2._1 + p1._1)) - (p1._1 * (p1._2 + p2._2))) |
---|
59 | 59 | | $Tuple3(a, b, c) |
---|
60 | 60 | | } |
---|
61 | 61 | | |
---|
62 | 62 | | |
---|
63 | 63 | | func getOrthoLine (p1,nVec) = { |
---|
64 | 64 | | let a = nVec._2 |
---|
65 | 65 | | let b = -(nVec._1) |
---|
66 | 66 | | let c = ((nVec._1 * p1._2) - (nVec._2 * p1._1)) |
---|
67 | 67 | | $Tuple3(a, b, c) |
---|
68 | 68 | | } |
---|
69 | 69 | | |
---|
70 | 70 | | |
---|
71 | 71 | | func getVec (start,end) = { |
---|
72 | 72 | | let x = (end._1 - start._1) |
---|
73 | 73 | | let y = (end._2 - start._2) |
---|
74 | 74 | | $Tuple2(x, y) |
---|
75 | 75 | | } |
---|
76 | 76 | | |
---|
77 | 77 | | |
---|
78 | 78 | | func mulVectors (vA,vB) = ((vA._1 * vB._2) - (vA._2 * vB._1)) |
---|
79 | 79 | | |
---|
80 | 80 | | |
---|
81 | 81 | | func getLineLength (p1,p2) = { |
---|
82 | 82 | | let a = pow((p2._1 - p1._1), 0, 2, 0, 0, DOWN) |
---|
83 | 83 | | let b = pow((p2._2 - p1._2), 0, 2, 0, 0, DOWN) |
---|
84 | 84 | | sqrt((a + b), 0, 0, DOWN) |
---|
85 | 85 | | } |
---|
86 | 86 | | |
---|
87 | 87 | | |
---|
88 | 88 | | func calcCrosses (accum,nextValue) = { |
---|
89 | 89 | | let vp1p2 = getVec(nextValue[0]._1, nextValue[0]._2) |
---|
90 | 90 | | let vp1p3 = getVec(nextValue[0]._1, nextValue[1]._1) |
---|
91 | 91 | | let vp1p4 = getVec(nextValue[0]._1, nextValue[1]._2) |
---|
92 | 92 | | let vp3p4 = getVec(nextValue[1]._1, nextValue[1]._2) |
---|
93 | 93 | | let vp3p1 = getVec(nextValue[1]._1, nextValue[0]._1) |
---|
94 | 94 | | let vp3p2 = getVec(nextValue[1]._1, nextValue[0]._2) |
---|
95 | 95 | | let v1 = mulVectors(vp3p4, vp3p1) |
---|
96 | 96 | | let v2 = mulVectors(vp3p4, vp3p2) |
---|
97 | 97 | | let v3 = mulVectors(vp1p2, vp1p3) |
---|
98 | 98 | | let v4 = mulVectors(vp1p2, vp1p4) |
---|
99 | 99 | | if (if ((0 > (v1 * v2))) |
---|
100 | 100 | | then (0 > (v3 * v4)) |
---|
101 | 101 | | else false) |
---|
102 | 102 | | then (accum + 1) |
---|
103 | 103 | | else accum |
---|
104 | 104 | | } |
---|
105 | 105 | | |
---|
106 | 106 | | |
---|
107 | 107 | | func isInside (p1,segment1,segment2,segment3) = { |
---|
108 | 108 | | let segmentRay = $Tuple2(p1, $Tuple2(p1._1, 0)) |
---|
109 | 109 | | let arr = [[segmentRay, segment1], [segmentRay, segment2], [segmentRay, segment3]] |
---|
110 | 110 | | let count = { |
---|
111 | 111 | | let $l = arr |
---|
112 | 112 | | let $s = size($l) |
---|
113 | 113 | | let $acc0 = 0 |
---|
114 | 114 | | func $f0_1 ($a,$i) = if (($i >= $s)) |
---|
115 | 115 | | then $a |
---|
116 | 116 | | else calcCrosses($a, $l[$i]) |
---|
117 | 117 | | |
---|
118 | 118 | | func $f0_2 ($a,$i) = if (($i >= $s)) |
---|
119 | 119 | | then $a |
---|
120 | 120 | | else throw("List size exceeds 3") |
---|
121 | 121 | | |
---|
122 | 122 | | $f0_2($f0_1($f0_1($f0_1($acc0, 0), 1), 2), 3) |
---|
123 | 123 | | } |
---|
124 | 124 | | ((count % 2) == 1) |
---|
125 | 125 | | } |
---|
126 | 126 | | |
---|
127 | 127 | | |
---|
128 | 128 | | func isPointInLine (p1,line) = ((((p1._1 * line._1) + (p1._2 * line._2)) + line._3) == 0) |
---|
129 | 129 | | |
---|
130 | 130 | | |
---|
131 | 131 | | func isPointInDeflection (p1,line) = { |
---|
132 | 132 | | let $t059546056 = line |
---|
133 | 133 | | let a1 = $t059546056._1 |
---|
134 | 134 | | let b1 = $t059546056._2 |
---|
135 | 135 | | let c1 = $t059546056._3 |
---|
136 | 136 | | let $t060596194 = getOrthoLine(p1, $Tuple2(a1, b1)) |
---|
137 | 137 | | let aNorm = $t060596194._1 |
---|
138 | 138 | | let bNorm = $t060596194._2 |
---|
139 | 139 | | let cNorm = $t060596194._3 |
---|
140 | 140 | | let d = mulVectors($Tuple2(a1, b1), $Tuple2(aNorm, bNorm)) |
---|
141 | 141 | | let dx = mulVectors($Tuple2(-(c1), b1), $Tuple2(-(cNorm), bNorm)) |
---|
142 | 142 | | let dy = mulVectors($Tuple2(a1, -(c1)), $Tuple2(aNorm, -(cNorm))) |
---|
143 | 143 | | let xRes = fraction(1, dx, d) |
---|
144 | 144 | | let yRes = fraction(1, dy, d) |
---|
145 | 145 | | let lineLen = getLineLength(p1, $Tuple2(xRes, yRes)) |
---|
146 | 146 | | (getDeflection() >= lineLen) |
---|
147 | 147 | | } |
---|
148 | 148 | | |
---|
149 | 149 | | |
---|
150 | 150 | | func isPointInPolygon (p1,vertex1,vertex2,vertex3) = { |
---|
151 | 151 | | let segment1 = $Tuple2(vertex1, vertex2) |
---|
152 | 152 | | let segment2 = $Tuple2(vertex2, vertex3) |
---|
153 | 153 | | let segment3 = $Tuple2(vertex3, vertex1) |
---|
154 | 154 | | if (if (if ((p1 == vertex1)) |
---|
155 | 155 | | then true |
---|
156 | 156 | | else (p1 == vertex2)) |
---|
157 | 157 | | then true |
---|
158 | 158 | | else (p1 == vertex3)) |
---|
159 | 159 | | then true |
---|
160 | 160 | | else isInside(p1, segment1, segment2, segment3) |
---|
161 | 161 | | } |
---|
162 | 162 | | |
---|
163 | 163 | | |
---|
164 | 164 | | func calcRewardMultiplier (inLine,inPolygon,inDeflection) = if (if (inLine) |
---|
165 | 165 | | then inPolygon |
---|
166 | 166 | | else false) |
---|
167 | 167 | | then 10 |
---|
168 | 168 | | else if (if (inDeflection) |
---|
169 | 169 | | then inPolygon |
---|
170 | 170 | | else false) |
---|
171 | 171 | | then 8 |
---|
172 | 172 | | else if (inLine) |
---|
173 | 173 | | then 6 |
---|
174 | 174 | | else if (inPolygon) |
---|
175 | 175 | | then 4 |
---|
176 | 176 | | else if (inDeflection) |
---|
177 | 177 | | then 2 |
---|
178 | 178 | | else 1 |
---|
179 | 179 | | |
---|
180 | 180 | | |
---|
181 | 181 | | func validatePayment (paymentAsset,paymentAmount) = if (if ((paymentAsset == unit)) |
---|
182 | 182 | | then (getMaxBet() >= paymentAmount) |
---|
183 | 183 | | else false) |
---|
184 | 184 | | then unit |
---|
185 | 185 | | else throw("Incorrect payment") |
---|
186 | 186 | | |
---|
187 | 187 | | |
---|
188 | 188 | | @Callable(i) |
---|
189 | 189 | | func setLotteryParams (goalP1,goalP2,deflection,vertex1,vertex2,vertex3,maxBet) = [StringEntry(kGoalP1, goalP1), StringEntry(kGoalP2, goalP2), IntegerEntry(kDeflection, deflection), StringEntry(kVertex1, vertex1), StringEntry(kVertex2, vertex2), StringEntry(kVertex3, vertex3), IntegerEntry(kMaxBet, maxBet)] |
---|
190 | 190 | | |
---|
191 | 191 | | |
---|
192 | 192 | | |
---|
193 | 193 | | @Callable(i) |
---|
194 | 194 | | func tryToWin (p1x,p1y) = { |
---|
195 | 195 | | let $t089109028 = $Tuple2(i.payments[0].assetId, value(i.payments[0].amount)) |
---|
196 | 196 | | let paymentAsset = $t089109028._1 |
---|
197 | 197 | | let paymentAmount = $t089109028._2 |
---|
198 | 198 | | valueOrElse(validatePayment(paymentAsset, paymentAmount), { |
---|
199 | 199 | | let p1 = $Tuple2(p1x, p1y) |
---|
200 | 200 | | let $t092589379 = getGoalLinePoints() |
---|
201 | 201 | | let p2 = $t092589379._1 |
---|
202 | 202 | | let p3 = $t092589379._2 |
---|
203 | 203 | | let $t093849483 = getPolygonPoints() |
---|
204 | 204 | | let p4 = $t093849483._1 |
---|
205 | 205 | | let p5 = $t093849483._2 |
---|
206 | 206 | | let p6 = $t093849483._3 |
---|
207 | 207 | | let $t094889580 = getLine(p2, p3) |
---|
208 | 208 | | let a1 = $t094889580._1 |
---|
209 | 209 | | let b1 = $t094889580._2 |
---|
210 | 210 | | let c1 = $t094889580._3 |
---|
211 | 211 | | let inLine = isPointInLine(p1, $Tuple3(a1, b1, c1)) |
---|
212 | 212 | | let inPolygon = isPointInPolygon(p1, p4, p5, p6) |
---|
213 | 213 | | let inDeflection = isPointInDeflection(p1, $Tuple3(a1, b1, c1)) |
---|
214 | 214 | | if (if (if (inLine) |
---|
215 | 215 | | then true |
---|
216 | 216 | | else inDeflection) |
---|
217 | 217 | | then true |
---|
218 | 218 | | else inPolygon) |
---|
219 | 219 | | then { |
---|
220 | 220 | | let multiplier = calcRewardMultiplier(inLine, inPolygon, inDeflection) |
---|
221 | 221 | | let reward = (paymentAmount * multiplier) |
---|
222 | 222 | | [IntegerEntry("multiplier", multiplier), ScriptTransfer(i.originCaller, reward, unit)] |
---|
223 | 223 | | } |
---|
224 | 224 | | else nil |
---|
225 | 225 | | }) |
---|
226 | 226 | | } |
---|
227 | 227 | | |
---|
228 | 228 | | |
---|
229 | 229 | | |
---|
230 | 230 | | @Callable(i) |
---|
231 | 231 | | func startGame () = { |
---|
232 | 232 | | let p1 = getPoint(i.transactionId) |
---|
233 | 233 | | let res = invoke(this, "tryToWin", [p1._1, p1._2], [i.payments[0]]) |
---|
234 | 234 | | if ((res == res)) |
---|
235 | 235 | | then nil |
---|
236 | 236 | | else throw("Strict value is not equal to itself.") |
---|
237 | 237 | | } |
---|
238 | 238 | | |
---|
239 | 239 | | |
---|