registration.lua (3846B)
1 require "lib.utils" 2 pprint = require "pprint" 3 4 _M = {} 5 6 reasons = { 7 abandoned = "a", 8 lawless = "l", 9 voluntary = "v", 10 deported = "d", 11 writ = "w", 12 mistake = "k", 13 proposal = "p", 14 destroyed = "e", 15 ratification = "r", 16 uknown = "u", 17 exiled = "x", 18 banned = "b" 19 } 20 21 function _M.deregister(args, players, log) 22 p = players[args.name] 23 if not p then 24 die(string.format("There's no player '%s'.", args.name)) 25 end 26 27 h = table.query(p.history, function (c) return c.reason == "s" end) 28 29 if not h then 30 die(string.format("Player '%s' is not registered.", args.name)) 31 end 32 33 ev = {when = args.date, 34 what = "deregister", 35 who = args.name, 36 where = args.m} 37 38 if not args.p then 39 h.active = nil 40 h.deregistration = os.date("%Y-%m-%d", args.date) 41 h.reason = reasons[args.reason] 42 h.latest = nil 43 io.write(string.format("Please remember to delete any reminders of %s's birthday.\n", args.name)) 44 45 table.insert(log, ev) 46 else 47 io.write(string.format("Deregister player %s.\n", args.name)) 48 pprint.pprint(ev) 49 pprint.pprint(h) 50 io.write("active := nil\n") 51 io.write("latest := d\n") 52 io.write(string.format("deregistration := %s\n", os.date("%Y-%m-%d", args.date))) 53 io.write("reason := a\n") 54 end 55 end 56 57 function _M.register(args, players, log, ts) 58 local birthday = nil 59 local exists = nil 60 -- Check if player name exists 61 for p, h in pairs(players) do 62 if table.query(h.history, function (x) return x.name == args.name end) then 63 exists = p 64 if table.query(h.history, function (x) return x.reason == "s" end) then 65 die(string.format("Error: player '%s' exists and is registered.", p)) 66 end 67 break 68 end 69 end 70 if exists then 71 if yn(string.format("Player exists under name '%s'. Proceed? [yn] ", exists)) and not args.p then 72 -- Change registration name if different 73 if args.name ~= exists then 74 players[args.name] = players[exists] 75 players[exists] = nil 76 end 77 -- Query player's birthday 78 birthday = players[args.name].history[1].registration 79 for _,h in ipairs(players[args.name].history) do 80 if birthday > h.registration then 81 birthday = h.registration 82 end 83 end 84 end 85 else 86 players[args.name] = { history = {} } 87 birthday = os.date("%Y-%m-%d", args.date) 88 end 89 local reg = { 90 name = args.name, 91 registration = os.date("%Y-%m-%d", args.date), 92 active = true, 93 reason = "s", 94 contact = args.contact, 95 latest = os.date("%Y-%m-%d", args.date) 96 } 97 local ev = { 98 what = "register", 99 who = args.name, 100 when = args.date, 101 where = args.m, 102 whence = args.contact 103 } 104 local msg = string.format("Please remember to set a reminder for this player's Agoran birthday! Eir first registration was on %s\n", birthday) 105 if not args.p then 106 table.insert(players[args.name].history, reg) 107 table.insert(log, ev) 108 io.write(msg) 109 else 110 io.write(pprint.pprint(reg)) 111 io.write(pprint.pprint(ev)) 112 end 113 end 114 115 function _M.rename(args, players, log) 116 die(not players[args.who], string.format("No player '%s'.", args.who)) 117 local h = table.query(players[args.who].history, function (h) return h.reason == "s" end) 118 die(not h, "Player is not registered.") 119 h.name = args.whither 120 players[args.whither] = players[args.who] 121 players[args.who] = nil 122 table.insert(log, {what="rename", who=args.who, whither=args.whither, where=args.m, when=os.time()}) 123 end 124 125 function _M.readdress(args, players, log) 126 die(not players[args.who], string.format("No player '%s'.", args.who)) 127 local h = table.query(players[args.who].history, function (h) return h.reason == "s" end) 128 if h then 129 h.contact = args.whither 130 else 131 players[args.who].contact = args.whither 132 end 133 table.insert(log, {what="readdress", who=args.who, whither=args.whither, where=args.m, when=args.when}) 134 end 135 136 _M.reasons = {} 137 for k,_ in pairs(reasons) do 138 table.insert(_M.reasons, k) 139 end 140 141 return _M