diff options
Diffstat (limited to 'scripts/toy.groovy')
-rw-r--r-- | scripts/toy.groovy | 70 |
1 files changed, 50 insertions, 20 deletions
diff --git a/scripts/toy.groovy b/scripts/toy.groovy index e85b83e..00e7652 100644 --- a/scripts/toy.groovy +++ b/scripts/toy.groovy @@ -216,28 +216,52 @@ 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 loadEvents = { - return (loadMap("toy.events") ?: [:]); + + // Events + static class Event { + int time; + String func; + Object arg; + def map = { + [ + time: time, + func: func, + arg: arg + ] + } + } + + static class NamedEvent { + String name; + Event event; } - def saveEvents = { events -> - save("toy.events", events); + + Map<String, Event> loadEvents() { + return (loadMap("toy.events") ?: [:]) + .collectEntries { k, v -> [ k, new Event(v) ] }; } - def nextEvent = { events -> - def first = null; - events.each{ k, v -> + + void saveEvents(Map<String, Event> events) { + save("toy.events", events.collectEntries { k, v -> [ k, v.map() ] }); + } + + NamedEvent nextEvent(Map<String, Event> events) { + NamedEvent first = null; + events.each{ String k, Event v -> if (!first || first.event.time > v.time) { - first = [ name: k, event: v ]; + first = new NamedEvent(name: k, event: v); } }; return first; - }; - def setEvent = { events, name, time, func, arg = null -> + } + + boolean setEvent(Map<String, Event> events, String name, int time, String func, Object arg = null) { if (namedEvents.containsKey(func)) { - events[name] = [ + events[name] = new Event( time: time, func: func, arg: arg - ]; + ); saveEvents(events); return true; } @@ -245,23 +269,28 @@ return new Object() { showPopup("No such event $func"); return false; } - }; - def addEvent = { name, time, func, arg = null -> + } + + boolean addEvent(String name, int time, String func, Object arg = null) { def events = loadEvents(); return setEvent(events, name, time, func, arg); } - def addEventIfMissing = { name, time, func, arg = null -> + + boolean addEventIfMissing(String name, int time, String func, Object arg = null) { def events = loadEvents(); if (!events.containsKey(name)) { return setEvent(events, name, time, func, arg); } - }; - def removeEvent = { name -> + return false; + } + + void removeEvent(String name) { def events = loadEvents(); events.remove(name); saveEvents(events); - }; - def execEvents = { rt -> + } + + void execEvents(boolean rt) { for (def e = nextEvent(loadEvents()); e && e.event.time <= getTime(); e = nextEvent(loadEvents())) { def f = namedEvents[e.event.func]; removeEvent(e.name); @@ -269,7 +298,8 @@ return new Object() { f(e.name, e.event.arg, e.event.time, rt); } } - }; + } + def sessionAborted = null; def sessionToys = [:]; def gagText = { t, p -> |