master
1function(C, A={}) { // press:false, advertise:"0000", show_rules:false, show_winners:false
2 if(C.calling_script) return "`Dtisk, tisk!`";
3
4 let P = C.caller,
5 sn = C.this_script,
6 L = #s.scripts.lib(),
7 F = {_id:"button"},
8 {press=false, advertise="", show_rules=false, show_winners=false} = A,
9
10 {s, l, b, o, u, p, f, w, k, cost, minReward, maxTime, cut, ocut, adw} = #db.f(F, {_id:0}).first(),
11 now = new Date(),
12 n = now.getTime(),
13 m = [],
14 ad=[],
15 fms=(ms)=> {
16 //get min
17 ms = MATH.max(0, ms);
18 let sec = ms / 1000.0,
19 min = MATH.floor(sec / 60.0);
20 sec = (sec - (min * 60.0)).toFixed(2);
21 return min > 0 ? `\`A${min} min\` and \`A${sec} sec\`` : `\`A${sec} sec\``;
22 },
23 fpct=(pct)=>`\`A${pct*100}%\``,
24 bar=`\`H${"#".repeat(70)}\``,
25 write = (msg_arr, rp=2)=>`
26${bar}
27${msg_arr.map(msg => '\t'.repeat(rp) + msg + ' ').join('\n')}
28${bar}
29`,
30
31 //ms since last checked
32 elapsed = n - l,
33 isOver = elapsed > maxTime,
34 score = maxTime - elapsed,
35
36 E = () => #s.escrow.charge({cost, is_unlim:false});
37 e = null;
38 r=()=>p*(1.-(cut+ocut)),
39 payout=()=>{
40 if(f) {
41 let x = MATH.floor(r()/u.length),
42 q = u.map(z=>{u:z, r:x}),
43 or = MATH.floor(p*ocut),
44 f=0;
45
46 //notify winners
47 L.each(q,z=>#s.chats.tell({to:z.u, msg:`\`LCongratulations\`, you won ${L.to_gc_str(z.r)} from ${sn}! Payout will distribute within 24h.`}));
48
49 //notify originator
50 #s.chats.tell({to:o, msg:`You just earned ${L.to_gc_str(or)} for starting a round of ${sn}! Payout will distribute within 24h.`})
51
52 //update db
53 #db.u(F,{
54 $set: {f}
55 $addToSet: {
56 w: { $each: q },
57 k: {u:o, r:or}
58 }
59 });
60 }
61 };
62
63 if(show_rules) {
64 m.concat([
65 `Rules:`,
66 `* Each press of the button costs ${L.to_gc_str(cost)}, and contributes to the reward.`,
67 `* Each press of the button resets the timer back to ${fms(maxTime)}.`,
68 `* The closest press to the zero mark of the time remaining wins the reward.`,
69 `* There are no "tie scores", the first player to register the best score wins.`,
70 `* ${fpct(ocut)} of the pot is reserved to the player that starts a game of the button.`,
71 `* ${fpct(cut)} of the pot is reserved for toy. Rewards are payed out on the following day.`,
72 `* This is keep the script FULLSEC.`,
73 ``
74 ]);
75 }
76
77 if(show_winners) {
78 m.push("Previously paid winners:");
79 m.concat(adw.map(x=>`\`L${adw.u}\`:\t\t\t${L.to_gc_str(adw.r)}`));
80 m.push("");
81 }
82
83 if(press) {
84 if(isOver) {
85 //check the payout
86 payout();
87
88 //check escrow
89 e = E();
90 if(!e) {
91 //reset for a new round
92 s = now;
93 l = n;
94 b = maxTime;
95 o = P;
96 u = [P];
97 p = cost;
98 f = 1;
99
100 //update db
101 #db.u(F, {$set: {s,l,b,o,u,p,f}});
102
103 //update the message
104 m.concat([
105 `Thank you for starting a round of ${sn}! You are guarenteed ${fpct(ocut)} of the pot.`,
106 `Draw in more players by running ${sn}{advertise:"0000"}, or in any other chat channel.`
107 ]);
108 }
109 } else {
110 //check escrow
111 e = E();
112 if(!e) {
113 p += cost;
114 l = n;
115
116 //check if score is new best score
117 if(score < b) {
118 //send message to old best score
119 L.each(u, z=>#s.chats.tell({to:z, msg:`I just beat your ${sn} score with ${fms(score)}. The pot is up to ${L.to_gc_str(r())}!`}));
120
121 //add to the advertisement
122 ad.push(`${P} just set a new best time of ${fms(score)} on ${sn}`);
123
124 //update best score
125 b = score;
126 u = [P];
127
128 //add to the messages
129 m.push(`You just set a new best time of ${fms(b)}`);
130 } else {
131 m.push(`Sorry, but your time of ${fms(score)} did not beat ${fms(b)}.`)
132 }
133 m.push(`The ${sn} pot is now up to ${L.to_gc_str(p)}`);
134 ad.push(`The ${sn} timer is reset to ${fms(maxTime)}`)
135 }
136 }
137 ad.push(`The pot for ${sn} is up to ${L.to_gc_str(r())}!`);
138 } else {
139 if(isOver) {
140 //check the payout
141 payout();
142 }
143 }
144
145 if(advertise)
146 {
147 if(isOver) {
148
149 } else {
150
151 }
152
153 #s.chats.send({channel:advertise, msg:write(ad)});
154 }
155
156 if(!Object.keys(A).length) {
157 let iw = isOver?"was":"is",
158 mm = [
159 `Arguments:`,
160 `press:true\t\t\tBuy an entry in the game`,
161 `advertise:"channel"\trun advertisment in channel`,
162 `show_rules:true\t\tdisplays the rules to the game`,
163 `show_winners:true\tlist previous paid winners`,
164 ``,
165 `The current round is ${isOver?"over":"running"}.`,
166 `The best score ${iw} ${fms(b)}.`,
167 `The reward ${iw} ${L.to_gc_str(r())}`
168 ];
169 if(!isOver)mm.push(`The round time remaining is ${fms(score)}.`);
170 return write(mm);
171 }
172
173 m.push(e?e:"Thank you for playing!");
174 return write(m);
175
176 // //is game over?
177 // if(isOver) {
178 // let x = MATH.floor(r/u.length),
179 // q = u.map(z=>{u:z, x});
180
181 // //reset game stats
182 // s = now;
183 // l = n;
184 // b = maxTime;
185 // o = P;
186 // u = [P];
187 // r = p = cost;
188
189 // //commit new db state
190 // #db.u(F, {
191 // $set: { s, l, b, o, u, p, r },
192 // $addToSet: {
193 // w: { $each: q },
194 // k: {u:o, r:MATH.floor(p*ocut)}
195 // },
196 // }
197 // );
198 // }
199
200 // if(press) {
201 // let escrow = #s.escrow.charge({charge, is_unlim:false});
202 // if(escrow) return escrow;
203
204 // //update the pot
205 // p += cost;
206 // //calculate the reward
207 // r = MATH.floor(p*(1.0-(cut+(p>minReward?ocut:0))));
208 // l = n;
209
210 // let nbs = 0, //new best score (bool)
211 // ou = u,
212 // ob = b;
213
214 // if(score < b) {
215 // //new best score!
216 // u = [P];
217 // b = score;
218 // nbs=1;
219 // }
220
221 // if(nbs) {
222 // L.each(ou, x=>#s.chats.tell({to:x, msg:`I just topped your score of ${fms(ob)} with ${fms(b)} to win ${L.to_gc_str(r)} on ${sn}!`}));
223 // m.push(`Congratulations you have set the new best score of ${fms(b)} to win ${L.to_gc_str(r)}!`);
224 // } else {
225 // m.push(`Sorry but your score of ${fms(score)} did not beat ${fms(b)}.`);
226 // }
227 // } else {
228 // if(!A || Object.keys(A).length == 0) {
229 // return write([
230 // `Arguments:`,
231 // `press:true\t\t\tBuy an entry in the game`,
232 // `advertise:"channel"\trun advertisment in channel`,
233 // `show_rules:true\t\tdisplays the rules to the game`,
234 // `show_winners:true\tlist previous paid winners`,
235 // ``,
236 // `The current round is ${isOver?"over":"running"}.`,
237 // `The best score is ${fms(b)}.`,
238 // `The reward is ${L.to_gc_str(r)}`,
239 // `The time remaining is ${fms(score)}.`
240 // ]);
241 // } else {
242 // return isOver;
243 // if(isOver) {
244 // m.concat([
245 // `The current round of ${sn} is over.`,
246 // `Run ${sn}{press:true, advertise:"0000"} and automatically earn ${fpct(ocut)} of the next game's proceeds!`,
247 // `Run ${sn}{advertise:"0000"} to advertise in channel 0000`
248 // ]);
249 // } else {
250 // m.concat([
251 // `The current round's reward is at ${L.to_gc_str(r)} with ${fms(score)} sec remaining to enter.`,
252 // `Run ${sn}{press:true} for chance to win!`
253 // ]);
254 // }
255 // }
256 // }
257 // m.push("");
258
259 // if(show_rules) {
260 // m.concat([
261 // `Rules:`,
262 // `* Each press of the button costs ${L.to_gc_str(cost)}, and contributes to the reward.`,
263 // `* Each press of the button resets the timer back to ${fms(maxTime)}.`,
264 // `* The closest press to the zero mark of the time remaining wins the reward.`,
265 // `* There are no "tie scores", the first player to register the best score wins.`,
266 // `* ${fpct(ocut)} of the pot is reserved to the player that starts a game of the button.`,
267 // `* ${fpct(cut)} of the pot is reserved for toy. Rewards are payed out on the following day.`,
268 // `* This is keep the script FULLSEC.`,
269 // ``
270 // ]);
271 // }
272
273 // if (show_winners) {
274 // m.push("Previously paid winners:");
275 // L.each(adw, x=>m.push(`\`L${adw.u}\`:\t\t\t${L.to_gc_str(adw.r)}`));
276 // m.push("");
277 // }
278
279 // if(advertise) {
280 // isOver?
281 // ad.concat([
282 // `Run ${sn}{press:true} now and be guarenteed ${fpct(ocut)} of the reward!`
283 // ]):
284 // ad.concat([
285 // `\`DHurry!\` There is only ${fms(score)} remaining!`,
286 // `Run ${sn}{press:true} to enter and win ${L.to_gc_str(r)}!`,
287 // ]);
288 // ad.concat([
289 // `RUN ${sn}{show_rules:true} for more information`,
290 // `Check for yourself, it's \`AFULLSEC\` script.`
291 // ]);
292 // #s.chats.send({channel:advertise, msg:write(ad)});
293 // }
294
295 // m.push("Thank you for playing!");
296 // return write(m);
297}