Ignore downloaded public serverlist if public_serverlist is false
[oweals/minetest.git] / builtin / mainmenu / common.lua
1 --Minetest
2 --Copyright (C) 2014 sapier
3 --
4 --This program is free software; you can redistribute it and/or modify
5 --it under the terms of the GNU Lesser General Public License as published by
6 --the Free Software Foundation; either version 2.1 of the License, or
7 --(at your option) any later version.
8 --
9 --This program is distributed in the hope that it will be useful,
10 --but WITHOUT ANY WARRANTY; without even the implied warranty of
11 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 --GNU Lesser General Public License for more details.
13 --
14 --You should have received a copy of the GNU Lesser General Public License along
15 --with this program; if not, write to the Free Software Foundation, Inc.,
16 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 --------------------------------------------------------------------------------
18 -- Global menu data
19 ---------------------------------------------------------------------------------
20 menudata = {}
21
22 --------------------------------------------------------------------------------
23 -- Menu helper functions
24 --------------------------------------------------------------------------------
25
26 --------------------------------------------------------------------------------
27 local function render_client_count(n)
28         if n > 99 then
29                 return '99+'
30         elseif n >= 0 then
31                 return tostring(n)
32         else
33                 return '?'
34         end
35 end
36
37 --------------------------------------------------------------------------------
38 function render_favorite(spec,render_details)
39         local text = ""
40
41         if spec.name ~= nil then
42                 text = text .. core.formspec_escape(spec.name:trim())
43
44 --              if spec.description ~= nil and
45 --                      core.formspec_escape(spec.description):trim() ~= "" then
46 --                      text = text .. " (" .. core.formspec_escape(spec.description) .. ")"
47 --              end
48         else
49                 if spec.address ~= nil then
50                         text = text .. spec.address:trim()
51
52                         if spec.port ~= nil then
53                                 text = text .. ":" .. spec.port
54                         end
55                 end
56         end
57
58         if not render_details then
59                 return text
60         end
61
62         local details = ""
63
64         if spec.clients ~= nil and spec.clients_max ~= nil then
65                 local clients_color = ''
66                 local clients_percent = 100 * spec.clients / spec.clients_max
67
68                 -- Choose a color depending on how many clients are connected
69                 -- (relatively to clients_max)
70                 if spec.clients == 0 then
71                         clients_color = ''        -- 0 players: default/white
72                 elseif spec.clients == spec.clients_max then
73                         clients_color = '#dd5b5b' -- full server: red (darker)
74                 elseif clients_percent <= 60 then
75                         clients_color = '#a1e587' -- 0-60%: green
76                 elseif clients_percent <= 90 then
77                         clients_color = '#ffdc97' -- 60-90%: yellow
78                 else
79                         clients_color = '#ffba97' -- 90-100%: orange
80                 end
81
82                 details = details ..
83                                 clients_color .. ',' ..
84                                 render_client_count(spec.clients) .. ',' ..
85                                 '/,' ..
86                                 render_client_count(spec.clients_max) .. ','
87         else
88                 details = details .. ',?,/,?,'
89         end
90
91         if spec.creative then
92                 details = details .. "1,"
93         else
94                 details = details .. "0,"
95         end
96
97         if spec.damage then
98                 details = details .. "1,"
99         else
100                 details = details .. "0,"
101         end
102
103         if spec.pvp then
104                 details = details .. "1,"
105         else
106                 details = details .. "0,"
107         end
108
109         return details .. text
110 end
111
112 --------------------------------------------------------------------------------
113 os.tempfolder = function()
114         if core.setting_get("TMPFolder") then
115                 return core.setting_get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
116         end
117
118         local filetocheck = os.tmpname()
119         os.remove(filetocheck)
120
121         local randname = "MTTempModFolder_" .. math.random(0,10000)
122         if DIR_DELIM == "\\" then
123                 local tempfolder = os.getenv("TEMP")
124                 return tempfolder .. filetocheck
125         else
126                 local backstring = filetocheck:reverse()
127                 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
128         end
129
130 end
131
132 --------------------------------------------------------------------------------
133 function menu_render_worldlist()
134         local retval = ""
135
136         local current_worldlist = menudata.worldlist:get_list()
137
138         for i,v in ipairs(current_worldlist) do
139                 if retval ~= "" then
140                         retval = retval ..","
141                 end
142
143                 retval = retval .. core.formspec_escape(v.name) ..
144                                         " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
145         end
146
147         return retval
148 end
149
150 --------------------------------------------------------------------------------
151 function menu_handle_key_up_down(fields,textlist,settingname)
152
153         if fields["key_up"] then
154                 local oldidx = core.get_textlist_index(textlist)
155
156                 if oldidx ~= nil and oldidx > 1 then
157                         local newidx = oldidx -1
158                         core.setting_set(settingname,
159                                 menudata.worldlist:get_raw_index(newidx))
160                 end
161                 return true
162         end
163
164         if fields["key_down"] then
165                 local oldidx = core.get_textlist_index(textlist)
166
167                 if oldidx ~= nil and oldidx < menudata.worldlist:size() then
168                         local newidx = oldidx + 1
169                         core.setting_set(settingname,
170                                 menudata.worldlist:get_raw_index(newidx))
171                 end
172                 
173                 return true
174         end
175         
176         return false
177 end
178
179 --------------------------------------------------------------------------------
180 function asyncOnlineFavourites()
181
182         menudata.favorites = {}
183         core.handle_async(
184                 function(param)
185                         return core.get_favorites("online")
186                 end,
187                 nil,
188                 function(result)
189                         if core.setting_getbool("public_serverlist") then
190                                 menudata.favorites = result
191                                 core.event_handler("Refresh")
192                         end
193                 end
194                 )
195 end
196
197 --------------------------------------------------------------------------------
198 function text2textlist(xpos,ypos,width,height,tl_name,textlen,text,transparency)
199         local textlines = core.splittext(text,textlen)
200         
201         local retval = "textlist[" .. xpos .. "," .. ypos .. ";"
202                                                                 .. width .. "," .. height .. ";"
203                                                                 .. tl_name .. ";"
204         
205         for i=1, #textlines, 1 do
206                 textlines[i] = textlines[i]:gsub("\r","")
207                 retval = retval .. core.formspec_escape(textlines[i]) .. ","
208         end
209         
210         retval = retval .. ";0;"
211         
212         if transparency then
213                 retval = retval .. "true"
214         end
215         
216         retval = retval .. "]"
217
218         return retval
219 end