-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.lua
306 lines (255 loc) · 7.8 KB
/
util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
local util = {}
local etlua = require "system.etlua"
--- Create a new kind (module/package) which has a handler for messages
--- and supports collecting verbs defined with util.verb
function util.kind(superkind)
local result = {}
if type(superkind) == "string" then
superkind = require(superkind)
end
--- If a message is unhandled, tries sending it to the superkind
--- (this also affects verbs)
function result.handler(name, payload)
local to_call = result[name]
if to_call then
return to_call(payload)
elseif superkind then
return superkind.handler(name, payload)
else
print("Object", orisa.self, "ignoring message", name)
end
end
-- special support for collecting verbs across superkinds
function result.get_verbs()
local verbs = {}
if superkind and superkind.get_verbs then
verbs = superkind.get_verbs()
end
for k, v in pairs(result) do
if type(v) == "table" and v.verb_info then
verbs[k] = v.verb_info
end
end
return verbs
end
return result
end
util.priority = {
normal = 2,
high = 3,
low = 1,
fallback = 0
}
function util.verb(verb)
local patterns, body = table.unpack(verb)
if type(patterns) == "string" then
patterns = {patterns}
end
local result = {
verb_info = {
patterns = patterns,
priority = verb.priority or util.priority.normal
}
};
setmetatable(result, { __call = function(t, ...)
-- we drop the initial argument which is the table itself
body(...)
end })
return result
end
--- Returns a pair of the kind's top-level package and the package name
-- e.g. util.split_kind("system.object") == "system", "object".
-- Returns nil if it does not match the expected pattern.
function util.split_kind(kind)
return string.match(kind, "^([-a-zA-Z0-9_/]+)%.([-a-zA-Z0-9_]+)$")
end
--- A quick description of this object
function util.get_name(object)
local username = orisa.get_username(object)
if username ~= nil then
return username
end
local custom_name = orisa.get_attr(object, "name")
if custom_name ~= nil then
return custom_name
end
-- fall back to "object" or "room" etc
local kind = orisa.get_kind(object)
local _, name = util.split_kind(kind)
return name or "object"
end
-- Quick method for finding an object assuming it's unambiguous
function util.find(query, from)
local all = util.find_all(query, from)
if #all == 0 then
return nil
end
return all[1]
end
--- Find all matching objects in the current location or inside of `from`, defaulting to current user
--- TODO: prefixes, scores/ordering, normalizing whitespace/case (happens in commands right now)
function util.find_all(query, from)
if string.match(query, "^#%d+$") then return {query} end
-- in case someone copy-pastes "something (#13)"
local paren_id = string.match(query, "%((#%d+)%)")
if paren_id then return {paren_id} end
if from == nil then
from = orisa.original_user
end
if query == "me" then
return {from}
end
if query == "here" then
return {orisa.get_parent(from)}
end
local parent = orisa.get_parent(from)
local results = {}
if parent ~= nil then
for _, child in ipairs(orisa.get_children(parent)) do
if util.object_matches(child, query) then
table.insert(results, child)
end
end
if util.object_matches(parent, query) then
table.insert(results, parent)
end
end
for _, child in ipairs(orisa.get_children(from)) do
if util.object_matches(child, query) then
table.insert(results, child)
end
end
return results
end
util.stopwords = {the = true, a = true, an = true, some = true, any = true}
-- Convert some text to lowercase space-separated words, removing stopwords
function util.to_words(query)
local result = {}
for word in string.gmatch(query, "%S+") do
if not util.stopwords[word] then
table.insert(result, string.lower(word))
end
end
return table.concat(result, " ")
end
--- Does the text given describe this object? (Assuming both are lowercase & space-separated.)
function util.object_matches(object, text)
local text = util.to_words(text)
if util.get_name(object) == text then
return true
end
local aliases = orisa.get_attr(object, "aliases") or {}
for _, alias in ipairs(aliases) do
if alias == text then
return true
end
end
return false
end
-- splits the string around the given punctuation character, ignoring doubled separators
-- e.g. util.split_punct("a,b,,c", ",") -> {"a", "b", "c"}
function util.split_punct(string, char)
local result = {}
string.gsub(string, "[^%" .. char .. "]+", function(piece) table.insert(result, piece) end)
return result
end
-- like tostring but prints contents of k-v tables
function util.tostring(v)
local t = type(v)
if t == "table" then
-- TODO: nicer handling for array-like tables
local pieces = {}
for k,v in pairs(v) do
table.insert(pieces, string.format("%s = %s", util.tostring(k), util.tostring(v)))
end
return "{" .. table.concat(pieces, ", ") .. "}"
else
return tostring(v)
end
end
-- Turn the value into a lua code which will produce that value
function util.tocode(v)
local t = type(v)
if t == "table" then
-- TODO: nicer handling for array-like tables
local pieces = {}
for k,v in pairs(v) do
-- TODO: nicer handling for 'normal' strings
table.insert(pieces, string.format("%s = %s", util.tocode(k), util.tocode(v)))
end
return "{" .. table.concat(pieces, ", ") .. "}"
elseif t == "string" then
return string.format("%q", v)
elseif t == "number" or t == "boolean" or t == "nil" then
return tostring(v)
else
error("Unable to convert value of type ".. t .. " to code")
end
end
local log_warned = {}
--- Build a logger which can be turned on/off by user attributes.
--- Returns a function to log which acts just like string.format except all args
--- are util.tostring'ed first. (i.e. use %s or %q to substitute them)
function util.logger(name)
local prefix = string.format("%s:", name)
local attr = "log_" .. name
if not log_warned[orisa.original_user .. ':' .. name] then
log_warned[orisa.original_user .. ':' .. name] = true
if orisa.get_attr(orisa.original_user, attr) then
print(string.format("Logger \"%s\" enabled; use `/set me %s false` to disable.", name, attr))
else
print(string.format("Logger \"%s\" disabled; use `/set me %s true` to enable.", name, attr))
end
end
return function(format, ...)
if orisa.get_attr(orisa.original_user, attr) then
print(prefix, string.format(format, util.tostring_all(...)))
end
end
end
function util.tostring_all(...)
-- This is pretty gross, sorry.
-- This is the only combination I could find which handled nil arguments in the
-- middle of the list.
local inputs = table.pack(...)
local result = {}
for key, raw in pairs(inputs) do
result[key] = util.tostring(raw)
end
return table.unpack(result, 1, select("#", ...))
end
function util.title(t)
return string.gsub(string.gsub(string.lower(t), "^%g", string.upper), "%f[%g]%g", string.upper)
end
function util.current_room(from)
if from == nil then
from = orisa.self
end
local parent = orisa.get_parent(from)
if parent == nil then
return from
else
return util.current_room(parent)
end
end
function util.is_inside(child, parent)
if child == parent or child == nil then
return false
end
local next_child = orisa.get_parent(child)
if next_child == parent then
return true
end
return util.is_inside(next_child, parent)
end
function util.oxford_join(list, sep, sep_last)
local count = #list
if count < 1 then
return ""
elseif count == 1 then
return tostring(list[1])
else
return table.concat({table.unpack(list, 1, #list - 1)}, sep) .. sep_last .. list[#list]
end
end
return util