From 557cef7e041a18a4c16c19d13870ada293c8eeac Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 24 Jun 2019 20:31:12 +0100 Subject: Tidy up some misc functions --- scripts/toy.groovy | 30 ++++++++++++------------------ scripts/toy/misc.groovy | 2 ++ 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/scripts/toy.groovy b/scripts/toy.groovy index 93d231b..bfebfc4 100644 --- a/scripts/toy.groovy +++ b/scripts/toy.groovy @@ -129,6 +129,14 @@ return new Object() { void setAway(String a) { save("toy.owner.away", a) } int randRange(int min, int max) { min + getRandom(1 + max - min) } int randRange(double min, double max) { randRange((int)min, (int)max) } + def setDefault = { prop, val -> + if (loadString(prop) == null) { + save(prop, val); + } + }; + // This exists because calling .wait(t) in a metaClass method + // calls it on the metaClass, not on the original object + void pause(int t) { wait(t); } // Events static class Event { @@ -290,38 +298,24 @@ return new Object() { adjustPunish(5); return false; }; + final activityList = [:]; final addActivity = { String name, func -> activityList[name] = func; } - // Setup - def setDefault = { prop, val -> - if (loadString(prop) == null) { - save(prop, val); - } - }; - def namedEvents = [ - permit: { name, arg, schedTime, rt -> givePermission(arg) } - ]; - def showFaq = { - useUrl("http://toy.randomdan.homeip.net/"); - }; + final namedEvents = [:]; final addNamedEvent = { String name, func -> namedEvents[name] = func; } - def requestables = [ - faq: [ lbl: "FAQ (webpage)", act: showFaq ], - ]; + + final requestables = [:]; final addRequestable = { String name, String label, func, cond = null -> requestables[name] = [lbl: label, act: func, cond: cond]; }; final removeRequestable = { String name -> requestables.remove(name); }; - // This exists because calling .wait(t) in a metaClass method - // calls it on the metaClass, not on the original object - void pause(int t) { wait(t); } String main() diff --git a/scripts/toy/misc.groovy b/scripts/toy/misc.groovy index 92af633..bb88a7f 100644 --- a/scripts/toy/misc.groovy +++ b/scripts/toy/misc.groovy @@ -6,5 +6,7 @@ }; }; + toy.addNamedEvent(toy.PERMIT, { name, arg, schedTime, rt -> toy.givePermission(arg) }); + toy.addRequestable("faq", "FAQ (webpage)", { useUrl("http://toy.randomdan.homeip.net/"); }); toy.addActivity("playNothing", { toy.playNothing() }); } -- cgit v1.2.3