-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCharacterStore.lua
More file actions
166 lines (131 loc) · 3.29 KB
/
CharacterStore.lua
File metadata and controls
166 lines (131 loc) · 3.29 KB
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
local _, namespace = ...
local CharacterStore = {}
namespace.CharacterStore = CharacterStore
local Util = namespace.Util
local Character = namespace.Character
local WAPI = {
GetServerTime = GetServerTime,
}
local Cache = {
instance = nil, -- Singleton
sortOrder = "lastUpdate",
secondarySortOrder = "name",
ascending = true,
secondaryAscending = true,
sortField = "",
}
local function CharacterComparator(lhs, rhs, field)
if lhs == rhs then
return 0
elseif lhs == nil then
return -1
elseif rhs == nil then
return -1
end
if Cache.instance:CurrentPlayer()[field] then
if lhs[field] == rhs[field] then
return 0
elseif lhs[field] == nil then
return -1
elseif rhs[field] == nil then
return 1
end
return lhs[field] < rhs[field] and -1 or 1
end
local subField = Cache.sortField
if lhs[subField][field] == rhs[subField][field] then
return 0
elseif lhs[subField][field] == nil then
return -1
elseif rhs[subField][field] == nil then
return 1
end
return lhs[subField][field] < rhs[subField][field] and -1 or 1
end
local function CreateCharacterSorter(primary, secondary, ascending, secondaryAscending)
return function(a, b)
local result = NegateIf(CharacterComparator(a, b, primary), ascending)
if result == 0 then
return NegateIf(CharacterComparator(a, b, secondary), secondaryAscending) < 0
else
return result < 0
end
end
end
function CharacterStore.Get()
return Cache.instance
end
function CharacterStore.Load(characters)
local store = CharacterStore.Get()
if store == nil then
return CharacterStore:New(characters)
end
for k, v in pairs(characters) do
store[k] = Character:New(v)
end
end
-- Load
function CharacterStore:New(o)
if Cache.instance ~= nil then
Util:Debug("CharacterStore RESET")
end
o = o or {}
self.__index = self
setmetatable(o, self)
for k, v in pairs(o) do
o[k] = Character:New(v)
end
Cache.instance = o
return o
end
function CharacterStore:CurrentPlayer()
local id = UnitGUID("player")
if self[id] == nil then
Util:Debug("Populating new player")
self[id] = Character:New()
end
return self[id]
end
function CharacterStore.IsCurrentPlayer(character)
return character.GUID == UnitGUID("player")
end
function CharacterStore:RemoveCharacter(id)
if not self[id] then
return false
end
Util:Debug("Removed character: ", self[id].name)
self[id] = nil
return true
end
function CharacterStore:GetSortOrder()
return Cache.sortOrder, Cache.ascending
end
function CharacterStore:SetSortOrder(field)
if Cache.sortOrder == field then
Cache.ascending = not Cache.ascending
else
Cache.secondarySortOrder = Cache.sortOrder
Cache.secondaryAscending = Cache.ascending
end
Cache.sortOrder = field
Util:Debug("Sorting:", Cache.sortOrder, Cache.secondarySortOrder, Cache.ascending, Cache.secondaryAscending)
end
function CharacterStore:SetSortField(field)
Cache.sortField = field
end
function CharacterStore:ForEach(callback, filter)
local characters = {}
filter = filter or function(x)
return x.enabled
end
for _, character in pairs(self) do
if filter(character) then
table.insert(characters, 1, character)
end
end
table.sort(characters, CreateCharacterSorter(Cache.sortOrder, Cache.secondarySortOrder, Cache.ascending))
for _, character in ipairs(characters) do
callback(character)
end
return characters
end