master
1function(context, args) { // schema:true, state:true, payout:false, set: {}, get: {}, pwd:"!bigfish"
2 var lib = #s.scripts.lib();
3 var scriptn = "toy.button";
4 var run_schema = () =>`
5{
6 s:date "date time started",
7 l:int "date time last set",
8 b:int "lowest score in ms",
9 o:string "username of originator",
10 u:array [string "usernames of best time"],
11 p:int "pot amount",
12 f:int "winners flag, if true then u:array needs to move to w array"
13 k:array [{u: user, r: reward}] "kickoff rewards",
14 w:array [{u: user, r: reward}] "winners payouts",
15 cost:int "how much to play",
16 minReward:int "how much the pot will grow before toy takes a cut",
17 maxTime:int "how long the timer since last press runs for",
18 cut:number "percent the toy takes",
19 ocut:number "percent the originator takes",
20 adw:array [{u: user, r: reward}] "previous payouts since last payout for ad purposes"
21}
22`;
23
24 var run_state = () => #db.f({_id:"button"}, {_id:0}).first();
25
26 var run_set = values => #db.u({_id:"button"},{$set:values});
27
28 var run_get = values => #db.f({_id:"button"}, values).first();
29
30 var run_payout = ()=> {
31 let {k, w} = #db.f({_id:"button"}, {_id:0, w:1, k:1});
32 let adw = w;
33 let po = (a, msg)=>lib.each(a, z=>#s.accts.xfer_gc_to({to:z.u,amount:z.r,memo:msg}));
34 po(w,`Winning ${scriptn}!`);
35 po(k,`Starting ${scriptn}.`)
36
37 //clear the winners list and the kickof winners
38 #db.u(
39 {_id:"button"},
40 {$set: {w: [], k:[], adw}}
41 );
42 }
43
44 if(args) {
45 let {schema=false, state=false, payout=false, set=null, get=null, pwd=""} = args;
46 if(set)return run_set(set);
47 if(get)return run_get(get);
48 if(payout)return run_payout();
49 if(state)return run_state();
50 if(schema)return run_schema();
51 }
52}