This repository has been archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
GuildInfo.lua
102 lines (94 loc) · 2.55 KB
/
GuildInfo.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
local EPGP = Apollo.GetAddon("EPGP")
local L = EPGP.L
local GS = Apollo.GetPackage("LibGuildStorage-1.0").tPackage
local function strsplit(delim, str, maxNb)
-- Eliminate bad cases...
if string.find(str, delim) == nil then
return { str }
end
if maxNb == nil or maxNb < 1 then
maxNb = 0 -- No limit
end
local result = {}
local pat = "(.-)" .. delim .. "()"
local nb = 0
local lastPos
for part, pos in string.gfind(str, pat) do
nb = nb + 1
result[nb] = part
lastPos = pos
if nb == maxNb then break end
end
-- Handle the last field
if nb ~= maxNb then
result[nb + 1] = string.sub(str, lastPos)
end
return result
end
local tConfigDefs = {
nDecayPerc = {
pattern = "@DECAY_P:(%d+)",
parser = tonumber,
validator = function(v) return v >= 0 and v <= 100 end,
error = L["Decay Percent should be a number between 0 and 100"],
default = 0,
change_message = "DecayPercentChanged",
},
nExtrasPerc = {
pattern = "@EXTRAS_P:(%d+)",
parser = tonumber,
validator = function(v) return v >= 0 and v <= 100 end,
error = L["Extras Percent should be a number between 0 and 100"],
default = 100,
change_message = "ExtrasPercentChanged",
},
nMinEP = {
pattern = "@MIN_EP:(%d+)",
parser = tonumber,
validator = function(v) return v >= 0 end,
error = L["Min EP should be a positive number"],
default = 0,
change_message = "MinEPChanged",
},
nBaseGP = {
pattern = "@BASE_GP:(%d+)",
parser = tonumber,
validator = function(v) return v >= 0 end,
error = L["Base GP should be a positive number"],
default = 1,
change_message = "BaseGPChanged",
},
}
local function ParseGuildInfo(callback, strGuildInfo)
if not strGuildInfo then
return
end
local tLines = strsplit("\n", strGuildInfo)
local bInBlock = false
local tNewConfig = {}
for _,strLine in pairs(tLines) do
if strLine:sub(1,6) == "-EPGP-" then
bInBlock = not bInBlock
elseif bInBlock then
for var, tDef in pairs(tConfigDefs) do
local v = strLine:match(tDef.pattern)
if v then
v = tDef.parser(v)
if v == nil or not tDef.validator(v) then
EPGP.glog:debug(tDef.error)
else
tNewConfig[var] = v
end
end
end
end
end
for var, tDef in pairs(tConfigDefs) do
local nOldValue = EPGP.db.profile[var]
EPGP.db.profile[var] = tNewConfig[var] or tDef.default
if nOldValue ~= EPGP.db.profile[var] then
EPGP.callbacks:Fire(tDef.change_message, EPGP.db.profile[var])
end
end
end
GS.RegisterCallback(EPGP, "GuildInfoChanged", ParseGuildInfo)