]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu_worldlist.lua
Don't show flags in local favorites
[dragonfireclient.git] / builtin / mainmenu_worldlist.lua
1 worldlist = {}
2
3 --------------------------------------------------------------------------------
4 function worldlist.refresh()
5         worldlist.m_raw_worldlist = engine.get_worlds()
6         worldlist.process()
7 end
8
9 --------------------------------------------------------------------------------
10 function worldlist.init()
11         worldlist.m_gamefilter = nil
12         worldlist.m_sortmode = "alphabetic"
13
14         worldlist.m_processed_worldlist = nil
15         worldlist.m_raw_worldlist = engine.get_worlds()
16
17         worldlist.process()
18 end
19
20 --------------------------------------------------------------------------------
21 function worldlist.set_gamefilter(gameid)
22         if gameid == worldlist.m_gamefilter then
23                 return
24         end
25         worldlist.m_gamefilter = gameid
26         worldlist.process()
27 end
28
29 --------------------------------------------------------------------------------
30 function worldlist.get_gamefilter()
31         return worldlist.m_gamefilter
32 end
33
34 --------------------------------------------------------------------------------
35 --supported sort mode "alphabetic|none"
36 function worldlist.set_sortmode(mode)
37         if (mode == worldlist.m_sortmode) then
38                 return
39         end
40         worldlist.m_sortmode = mode
41         worldlist.process()
42 end
43
44 --------------------------------------------------------------------------------
45 function worldlist.get_list()
46         return worldlist.m_processed_worldlist
47 end
48
49 --------------------------------------------------------------------------------
50 function worldlist.get_raw_list()
51         return worldlist.m_raw_worldlist
52 end
53
54 --------------------------------------------------------------------------------
55 function worldlist.get_raw_world(idx)
56         if type(idx) ~= "number" then
57                 idx = tonumber(idx)
58         end
59         
60         if idx ~= nil and idx > 0 and idx < #worldlist.m_raw_worldlist then
61                 return worldlist.m_raw_worldlist[idx]
62         end
63         
64         return nil
65 end
66
67 --------------------------------------------------------------------------------
68 function worldlist.get_engine_index(worldlistindex)
69         assert(worldlist.m_processed_worldlist ~= nil)
70         
71         if worldlistindex ~= nil and worldlistindex > 0 and
72                 worldlistindex <= #worldlist.m_processed_worldlist then
73                 local entry = worldlist.m_processed_worldlist[worldlistindex]
74                 
75                 for i,v in ipairs(worldlist.m_raw_worldlist) do
76                 
77                         if worldlist.compare(v,entry) then
78                                 return i
79                         end
80                 end
81         end
82         
83         return 0
84 end
85
86 --------------------------------------------------------------------------------
87 function worldlist.get_current_index(worldlistindex)
88         assert(worldlist.m_processed_worldlist ~= nil)
89         
90         if worldlistindex ~= nil and worldlistindex > 0 and
91                 worldlistindex <= #worldlist.m_raw_worldlist then
92                 local entry = worldlist.m_raw_worldlist[worldlistindex]
93                 
94                 for i,v in ipairs(worldlist.m_processed_worldlist) do
95                 
96                         if worldlist.compare(v,entry) then
97                                 return i
98                         end
99                 end
100         end
101         
102         return 0
103 end
104
105 --------------------------------------------------------------------------------
106 function worldlist.process()
107         assert(worldlist.m_raw_worldlist ~= nil)
108
109         if worldlist.m_sortmode == "none" and 
110            worldlist.m_gamefilter == nil then
111                 worldlist.m_processed_worldlist = worldlist.m_raw_worldlist
112                 return
113         end
114         
115         worldlist.m_processed_worldlist = {}
116         
117         for i,v in ipairs(worldlist.m_raw_worldlist) do
118         
119                 if worldlist.m_gamefilter == nil or 
120                         v.gameid == worldlist.m_gamefilter then
121                         table.insert(worldlist.m_processed_worldlist,v)
122                 end
123         end
124         
125         if worldlist.m_sortmode == "none" then
126                 return
127         end
128         
129         if worldlist.m_sortmode == "alphabetic" then
130                 worldlist.sort_alphabetic()
131         end
132
133 end
134
135 --------------------------------------------------------------------------------
136 function worldlist.compare(world1,world2)
137
138         if world1.path ~= world2.path then
139                 return false
140         end
141         
142         if world1.name ~= world2.name then
143                 return false
144         end
145         
146         if world1.gameid ~= world2.gameid then
147                 return false
148         end
149
150         return true
151 end
152
153 --------------------------------------------------------------------------------
154 function worldlist.size()
155         if worldlist.m_processed_worldlist == nil then
156                 return 0
157         end
158         
159         return #worldlist.m_processed_worldlist
160 end
161
162 --------------------------------------------------------------------------------
163 function worldlist.exists(worldname)
164         for i,v in ipairs(worldlist.m_raw_worldlist) do
165                 if v.name == worldname then
166                         return true
167                 end
168         end
169         return false
170 end
171
172
173 --------------------------------------------------------------------------------
174 function worldlist.engine_index_by_name(worldname)
175         local worldcount = 0
176         local worldidx = 0
177         for i,v in ipairs(worldlist.m_raw_worldlist) do
178                 if v.name == worldname then
179                         worldcount = worldcount +1
180                         worldidx = i
181                 end
182         end
183         
184         
185         -- If there are more worlds than one with same name we can't decide which
186         -- one is meant. This shouldn't be possible but just for sure.
187         if worldcount > 1 then
188                 worldidx=0
189         end
190
191         return worldidx
192 end
193
194 --------------------------------------------------------------------------------
195 function worldlist.sort_alphabetic() 
196
197         table.sort(worldlist.m_processed_worldlist, function(a, b) 
198                         local n1 = a.name 
199                         local n2 = b.name 
200                         local count = math.min(#n1, #n2) 
201                         
202                         for i=1,count do 
203                                 if n1:sub(i, i):lower() < n2:sub(i, i):lower() then 
204                                         return true 
205                                 elseif n1:sub(i, i):lower() > n2:sub(i, i):lower() then 
206                                         return false 
207                                 end 
208                         end 
209                         return (#n1 <= #n2) 
210                 end) 
211 end