|
| 1 | +-- Testing script for eventful events |
| 2 | + |
| 3 | +--[====[ |
| 4 | +devel/eventful-client |
| 5 | +===================== |
| 6 | +
|
| 7 | +Usage:: |
| 8 | +
|
| 9 | + devel/eventful-client help |
| 10 | + devel/eventful-client add <event type> <frequency> |
| 11 | + devel/eventful-client add all <frequency> |
| 12 | + devel/eventful-client list |
| 13 | + devel/eventful-client clear |
| 14 | +
|
| 15 | +:help: shows this help text and a list of valid event types |
| 16 | +:add: add a handler for the named event type at the requested tick frequency |
| 17 | +:list: lists active handlers and their frequencies |
| 18 | +:clear: unregisters all handlers |
| 19 | +
|
| 20 | +Note this script does not handle the eventful reaction or workshop events. |
| 21 | +]====] |
| 22 | + |
| 23 | +local eventful = require('plugins.eventful') |
| 24 | + |
| 25 | +local utils = require('utils') |
| 26 | + |
| 27 | +rng = rng or dfhack.random.new() |
| 28 | + |
| 29 | +handlers = handlers or {} |
| 30 | + |
| 31 | +local function get_event_fn_name(event_type_name) |
| 32 | + local fn_name = 'on' |
| 33 | + for word in event_type_name:gmatch('[^_]+') do |
| 34 | + fn_name = fn_name .. word:sub(1,1) .. word:sub(2):lower() |
| 35 | + end |
| 36 | + if eventful[fn_name] then return fn_name end |
| 37 | + fn_name = fn_name .. 'CreatedDestroyed' |
| 38 | + if eventful[fn_name] then return fn_name end |
| 39 | + return nil |
| 40 | +end |
| 41 | + |
| 42 | +local function get_event_registry() |
| 43 | + local registry = {} |
| 44 | + local event_list = utils.invert(eventful.eventType) |
| 45 | + for i=0,#event_list do |
| 46 | + local event_type_name = event_list[i] |
| 47 | + if event_type_name == 'EVENT_MAX' then |
| 48 | + return registry |
| 49 | + end |
| 50 | + registry[i+1] = {etype=event_type_name, |
| 51 | + fn=get_event_fn_name(event_type_name)} |
| 52 | + end |
| 53 | +end |
| 54 | + |
| 55 | +local function help() |
| 56 | + print(dfhack.script_help()) |
| 57 | + print() |
| 58 | + print('Event types (and the eventful event functions they map to):') |
| 59 | + for _,v in ipairs(get_event_registry()) do |
| 60 | + print((' %s -> %s'):format(v.etype, v.fn)) |
| 61 | + end |
| 62 | +end |
| 63 | + |
| 64 | +local function make_handler_fn(registry_entry, freq) |
| 65 | + return function() |
| 66 | + print(('eventful-client: %s received %s event (freq=%d)') |
| 67 | + :format(os.date("%X"), registry_entry.etype, freq)) |
| 68 | + end |
| 69 | +end |
| 70 | + |
| 71 | +-- adds a new handler for the specified event type. uses a unique handler name |
| 72 | +-- each time so multiple handlers can be registered for the same event. |
| 73 | +local function add_one(registry_entry, freq) |
| 74 | + if not registry_entry.fn then |
| 75 | + print(('no eventful event function for %s events; skipping') |
| 76 | + :format(registry_entry.etype)) |
| 77 | + return |
| 78 | + end |
| 79 | + eventful.enableEvent(eventful.eventType[registry_entry.etype], freq) |
| 80 | + local handler_name = 'eventful-client.'..tostring(rng:random()) |
| 81 | + local handler = make_handler_fn(registry_entry, freq) |
| 82 | + print(('eventful-client registering new %s handler at freq %d: %s') |
| 83 | + :format(registry_entry.etype, freq, handler_name)) |
| 84 | + eventful[registry_entry.fn][handler_name] = handler |
| 85 | + handlers[handler_name] = freq |
| 86 | +end |
| 87 | + |
| 88 | +local function add(spec, freq) |
| 89 | + for _,v in ipairs(get_event_registry()) do |
| 90 | + if spec == 'all' or spec == v.etype then |
| 91 | + add_one(v, freq) |
| 92 | + end |
| 93 | + end |
| 94 | +end |
| 95 | + |
| 96 | +local function iterate_handlers(fn) |
| 97 | + local count = 0 |
| 98 | + for _,v in ipairs(get_event_registry()) do |
| 99 | + if not eventful[v.fn] then goto continue end |
| 100 | + for k in pairs(eventful[v.fn]) do |
| 101 | + if k:startswith('eventful-client.') then |
| 102 | + count = count + 1 |
| 103 | + fn(v, k) |
| 104 | + end |
| 105 | + end |
| 106 | + ::continue:: |
| 107 | + end |
| 108 | + return count |
| 109 | +end |
| 110 | + |
| 111 | +local function list_fn(registry_entry, handler_name) |
| 112 | + print((' %s -> %s (freq=%s)') |
| 113 | + :format(registry_entry.etype, handler_name, |
| 114 | + handlers[handler_name] or 'unknown')) |
| 115 | +end |
| 116 | + |
| 117 | +local function list() |
| 118 | + print('Active handlers:') |
| 119 | + if 0 == iterate_handlers(list_fn) then |
| 120 | + print(' None') |
| 121 | + end |
| 122 | +end |
| 123 | + |
| 124 | +local function clear_fn(registry_entry, handler_name) |
| 125 | + list_fn(registry_entry, handler_name) |
| 126 | + eventful[registry_entry.fn][handler_name] = nil |
| 127 | + handlers[handler_name] = nil |
| 128 | +end |
| 129 | + |
| 130 | +local function clear() |
| 131 | + print('Clearing handlers:') |
| 132 | + iterate_handlers(clear_fn) |
| 133 | +end |
| 134 | + |
| 135 | +local action_switch = { |
| 136 | + add=add, |
| 137 | + list=list, |
| 138 | + clear=clear, |
| 139 | +} |
| 140 | +setmetatable(action_switch, {__index=function() return help end}) |
| 141 | + |
| 142 | +local args = {...} |
| 143 | +local action = table.remove(args, 1) or 'help' |
| 144 | + |
| 145 | +action_switch[action](table.unpack(args)) |
0 commit comments