-
Notifications
You must be signed in to change notification settings - Fork 23
/
storage.lua
479 lines (435 loc) · 12 KB
/
storage.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
-- storage getter/setter
local STORAGE_PREFIX = "mail/"
-- create or populate empty fields on an entry
local function populate_entry(e)
e = e or {}
e.contacts = e.contacts or {}
e.inbox = e.inbox or {}
e.outbox = e.outbox or {}
e.drafts = e.drafts or {}
e.trash = e.trash or {}
e.lists = e.lists or {}
e.settings = e.settings or {}
return e
end
local cache = {}
-- retrieve the storage entry for the player
function mail.get_storage_entry(playername)
local key = STORAGE_PREFIX .. playername
if cache[key] then
-- use cached entry
return cache[key]
end
local str = mail.storage:get_string(key)
local entry
if str == "" then
-- new entry
entry = populate_entry()
else
-- deserialize existing entry
local e = core.parse_json(str)
entry = populate_entry(e)
end
-- cache for next time
cache[key] = entry
return entry
end
-- entries queued for saving
local save_queued_entries = {}
-- save the storage entry for the player
function mail.set_storage_entry(playername, entry)
local key = STORAGE_PREFIX .. playername
-- cache
cache[key] = entry
-- enqueue for writing
save_queued_entries[key] = entry
end
local function save_worker()
for key, entry in pairs(save_queued_entries) do
-- write to backend
mail.storage:set_string(key, core.write_json(entry))
end
-- clear queue
save_queued_entries = {}
-- clear cached entries
cache = {}
-- save every second
core.after(1, save_worker)
end
-- start save-worker loop
save_worker()
-- save on shutdown
core.register_on_shutdown(save_worker)
-- get a mail by id from the players in- or outbox
function mail.get_message(playername, msg_id)
local entry = mail.get_storage_entry(playername)
for _, msg in ipairs(entry.inbox) do
if msg.id == msg_id then
return msg
end
end
for _, msg in ipairs(entry.outbox) do
if msg.id == msg_id then
return msg
end
end
for _, msg in ipairs(entry.drafts) do
if msg.id == msg_id then
return msg
end
end
for _, msg in ipairs(entry.trash) do
if msg.id == msg_id then
return msg
end
end
end
-- get player boxes where a message appears
function mail.get_message_boxes(playername, msg_id)
local entry = mail.get_storage_entry(playername)
local boxes = {}
for _, msg in ipairs(entry.inbox) do
if msg.id == msg_id then
table.insert(boxes, "inbox")
end
end
for _, msg in ipairs(entry.outbox) do
if msg.id == msg_id then
table.insert(boxes, "outbox")
end
end
for _, msg in ipairs(entry.drafts) do
if msg.id == msg_id then
table.insert(boxes, "drafts")
end
end
for _, msg in ipairs(entry.trash) do
if msg.id == msg_id then
table.insert(boxes, "trash")
end
end
return boxes
end
local function safe_find(str, sub)
return str and sub and str:find(sub, 1, true) or nil
end
function mail.sort_messages(messages, sortfield, descending, filter)
local results = {}
-- Filtering
if filter and filter ~= "" then
for _, msg in ipairs(messages) do
if safe_find(msg.from, filter) or safe_find(msg.to, filter) or safe_find(msg.subject, filter) then
table.insert(results, msg)
end
end
else
for i = 1, #messages do
results[i] = messages[i]
end
end
-- Sorting
if sortfield ~= nil then
if descending then
table.sort(results, function(a, b) return a[sortfield] > b[sortfield] end)
else
table.sort(results, function(a, b) return a[sortfield] < b[sortfield] end)
end
end
return results
end
local function mark_property(playername, property, msg_ids, value, hud_update)
local entry = mail.get_storage_entry(playername)
if type(msg_ids) ~= "table" then -- if this is not a table
msg_ids = { msg_ids }
end
for _, property_msg_id in ipairs(msg_ids) do
for _, entry_msg in ipairs(entry.inbox) do
if entry_msg.id == property_msg_id then
entry_msg[property] = value
end
end
end
mail.set_storage_entry(playername, entry)
if hud_update then
mail.hud_update(playername, entry.inbox)
end
return
end
-- marks a mail read by its id
function mail.mark_read(playername, msg_ids)
mark_property(playername, "read", msg_ids, true, true)
return
end
-- marks a mail unread by its id
function mail.mark_unread(playername, msg_ids)
mark_property(playername, "read", msg_ids, false, true)
return
end
-- marks a mail as a spam
function mail.mark_spam(playername, msg_ids)
mark_property(playername, "spam", msg_ids, true)
return
end
-- marks a mail as a non-spam
function mail.unmark_spam(playername, msg_ids)
mark_property(playername, "spam", msg_ids, false)
return
end
-- deletes a mail by its id
function mail.delete_mail(playername, msg_ids, delete_in_trash)
local entry = mail.get_storage_entry(playername)
if type(msg_ids) ~= "table" then -- if this is not a table
msg_ids = { msg_ids }
end
for i = #entry.inbox, 1, -1 do
for _, deleted_msg in ipairs(msg_ids) do
if entry.inbox[i].id == deleted_msg then
table.remove(entry.inbox, i)
break
end
end
end
for i = #entry.outbox, 1, -1 do
for _, deleted_msg in ipairs(msg_ids) do
if entry.outbox[i].id == deleted_msg then
table.remove(entry.outbox, i)
break
end
end
end
for i = #entry.drafts, 1, -1 do
for _, deleted_msg in ipairs(msg_ids) do
if entry.drafts[i].id == deleted_msg then
table.remove(entry.drafts, i)
break
end
end
end
if delete_in_trash then
for i = #entry.trash, 1, -1 do
for _, deleted_msg in ipairs(msg_ids) do
if entry.trash[i].id == deleted_msg then
table.remove(entry.trash, i)
break
end
end
end
end
mail.set_storage_entry(playername, entry)
mail.hud_update(playername, entry.inbox)
return
end
-- move to trash mails by id
function mail.trash_mail(playername, msg_ids)
local entry = mail.get_storage_entry(playername)
if type(msg_ids) ~= "table" then -- if this is not a table
msg_ids = { msg_ids }
end
for _, id in ipairs(msg_ids) do
local msg = mail.get_message(playername, id)
msg.previous_boxes = mail.get_message_boxes(playername, id)
table.insert(entry.trash, 1, msg)
end
mail.set_storage_entry(playername, entry)
mail.delete_mail(playername, msg_ids)
return
end
-- restore a mail from trash
function mail.restore_mail(playername, msg_id)
local entry = mail.get_storage_entry(playername)
for i, msg in ipairs(entry.trash) do
if msg.id == msg_id then
-- not anymore store previous boxes in json
local previous_boxes = msg.previous_boxes
msg.previous_boxes = nil
-- restore it in all previous boxes
for _, box in ipairs(previous_boxes) do
table.insert(entry[box], msg)
end
-- then delete it from trash
table.remove(entry.trash, i)
end
end
mail.set_storage_entry(playername, entry)
return
end
-- clear the trash
function mail.empty_trash(playername)
local entry = mail.get_storage_entry(playername)
entry.trash = {}
mail.set_storage_entry(playername, entry)
return
end
-- add or update a contact
function mail.update_contact(playername, contact)
local entry = mail.get_storage_entry(playername)
local existing_updated = false
for i, existing_contact in ipairs(entry.contacts) do
if existing_contact.name == contact.name then
-- update
entry.contacts[i] = contact
existing_updated = true
break
end
end
if not existing_updated then
-- insert
table.insert(entry.contacts, contact)
end
mail.set_storage_entry(playername, entry)
end
-- deletes a contact
function mail.delete_contact(playername, contactname)
local entry = mail.get_storage_entry(playername)
for i, existing_contact in ipairs(entry.contacts) do
if existing_contact.name == contactname then
-- delete
table.remove(entry.contacts, i)
mail.set_storage_entry(playername, entry)
return
end
end
end
-- get all contacts
function mail.get_contacts(playername)
local entry = mail.get_storage_entry(playername)
return entry.contacts
end
-- get a contact
function mail.get_contact(playername, contactname)
local entry = mail.get_storage_entry(playername)
for _, existing_contact in ipairs(entry.contacts) do
if existing_contact.name == contactname then
return existing_contact
end
end
return false
end
-- returns the maillists of a player
function mail.get_maillists(playername)
local entry = mail.get_storage_entry(playername)
return entry.lists
end
-- returns the maillists of a player
function mail.get_maillist_by_name(playername, listname)
local entry = mail.get_storage_entry(playername)
for _, list in ipairs(entry.lists) do
if list.name == listname then
if not list.players then
list.players = {}
end
return list
end
end
end
-- updates or creates a maillist
function mail.update_maillist(playername, list, old_list_name)
local entry = mail.get_storage_entry(playername)
for i, existing_list in ipairs(entry.lists) do
if existing_list.name == old_list_name then
-- delete
table.remove(entry.lists, i)
break
end
end
-- insert
if not list.players then
list.players = {}
end
table.insert(entry.lists, list)
mail.set_storage_entry(playername, entry)
end
function mail.delete_maillist(playername, listname)
local entry = mail.get_storage_entry(playername)
for i, list in ipairs(entry.lists) do
if list.name == listname then
-- delete
table.remove(entry.lists, i)
mail.set_storage_entry(playername, entry)
return
end
end
end
local function extract_maillists_main(receivers, maillists_owner, expanded_receivers, seen)
if type(receivers) == "string" then
receivers = mail.parse_player_list(receivers)
end
for _, receiver in pairs(receivers) do
if seen[receiver] then
-- Do not add/expand this receiver as it is already seen
core.log("verbose", ("mail: ignoring duplicate receiver %q during maillist expansion"):format(receiver))
elseif string.find(receiver, "^@") then
seen[receiver] = true
local listname = string.sub(receiver, 2)
local maillist = mail.get_maillist_by_name(maillists_owner, listname)
if maillist then
core.log("verbose", ("mail: expanding maillist %q"):format(listname))
for _, entry in ipairs(maillist.players) do
extract_maillists_main(entry, maillists_owner, expanded_receivers, seen)
end
end
else
seen[receiver] = true
core.log("verbose", ("mail: adding %q to receiver list during maillist expansion"):format(receiver))
table.insert(expanded_receivers, receiver)
end
end
end
function mail.extract_maillists(receivers, maillists_owner)
local expanded_receivers = {}
extract_maillists_main(receivers, maillists_owner, expanded_receivers, {})
return expanded_receivers
end
function mail.get_setting_default_value(key)
return mail.settings[key].default
end
function mail.get_setting(playername, key)
local entry = mail.get_storage_entry(playername)
local value = (entry.settings[key] == nil
and {mail.get_setting_default_value(key)}
or {entry.settings[key]})[1]
if mail.settings[key].sync then -- in case this setting is shared with another mod
local sync_value = mail.settings[key].sync(playername) -- get new value
if sync_value then
value = sync_value
mail.set_setting(playername, key, value, true) -- update the setting in mail storage and don't transfer it again
end
end
return value
end
-- add or update a setting
function mail.set_setting(playername, key, value, not_transfer)
local entry = mail.get_storage_entry(playername)
local valid_value = value
if mail.settings[key].check then
valid_value = mail.settings[key].check(playername, value)
end
entry.settings[key] = valid_value
mail.set_storage_entry(playername, entry)
if not not_transfer and mail.settings[key].transfer then -- in case this setting is shared with another mod
mail.settings[key].transfer(playername, valid_value)
end
end
function mail.reset_settings(playername)
local entry = mail.get_storage_entry(playername)
entry.settings = {}
mail.set_storage_entry(playername, entry)
end
function mail.pairs_by_keys(t, f)
-- http://www.lua.org/pil/19.3.html
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function() -- iterator function
i = i + 1
if a[i] == nil then
return nil
else
--return a[i], t[a[i]]
-- add the current position and the length for convenience
return a[i], t[a[i]], i, #a
end
end
return iter
end