2 --Copyright (C) 2014 sapier
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.
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.
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 --------------------------------------------------------------------------------
19 --------------------------------------------------------------------------------
22 --------------------------------------------------------------------------------
23 -- Local cached values
24 --------------------------------------------------------------------------------
25 local min_supp_proto = core.get_min_supp_proto()
26 local max_supp_proto = core.get_max_supp_proto()
28 --------------------------------------------------------------------------------
29 -- Menu helper functions
30 --------------------------------------------------------------------------------
32 --------------------------------------------------------------------------------
33 local function render_client_count(n)
43 --------------------------------------------------------------------------------
44 function image_column(tooltip, flagname)
46 "tooltip=" .. core.formspec_escape(tooltip) .. "," ..
47 "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
48 "1=" .. core.formspec_escape(defaulttexturedir .. "server_flags_" .. flagname .. ".png")
51 --------------------------------------------------------------------------------
52 function order_favorite_list(list)
54 --orders the favorite list after support
57 if is_server_protocol_compat(fav.proto_min, fav.proto_max) then
58 table.insert(res, fav)
63 if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then
64 table.insert(res, fav)
70 --------------------------------------------------------------------------------
71 function render_favorite(spec,render_details)
74 if spec.name ~= nil then
75 text = text .. core.formspec_escape(spec.name:trim())
77 -- if spec.description ~= nil and
78 -- core.formspec_escape(spec.description):trim() ~= "" then
79 -- text = text .. " (" .. core.formspec_escape(spec.description) .. ")"
82 if spec.address ~= nil then
83 text = text .. spec.address:trim()
85 if spec.port ~= nil then
86 text = text .. ":" .. spec.port
91 if not render_details then
96 local grey_out = not is_server_protocol_compat(spec.proto_max, spec.proto_min)
98 if spec.clients ~= nil and spec.clients_max ~= nil then
99 local clients_color = ''
100 local clients_percent = 100 * spec.clients / spec.clients_max
102 -- Choose a color depending on how many clients are connected
103 -- (relatively to clients_max)
104 if spec.clients == 0 then
105 clients_color = '' -- 0 players: default/white
106 elseif spec.clients == spec.clients_max then
107 clients_color = '#dd5b5b' -- full server: red (darker)
108 elseif clients_percent <= 60 then
109 clients_color = '#a1e587' -- 0-60%: green
110 elseif clients_percent <= 90 then
111 clients_color = '#ffdc97' -- 60-90%: yellow
113 clients_color = '#ffba97' -- 90-100%: orange
117 clients_color = '#aaaaaa'
121 clients_color .. ',' ..
122 render_client_count(spec.clients) .. ',' ..
124 render_client_count(spec.clients_max) .. ','
126 details = details .. '#aaaaaa,?,/,?,'
128 details = details .. ',?,/,?,'
131 if spec.creative then
132 details = details .. "1,"
134 details = details .. "0,"
138 details = details .. "1,"
140 details = details .. "0,"
144 details = details .. "1,"
146 details = details .. "0,"
149 return details .. (grey_out and '#aaaaaa,' or ',') .. text
152 --------------------------------------------------------------------------------
153 os.tempfolder = function()
154 if core.setting_get("TMPFolder") then
155 return core.setting_get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
158 local filetocheck = os.tmpname()
159 os.remove(filetocheck)
161 local randname = "MTTempModFolder_" .. math.random(0,10000)
162 if DIR_DELIM == "\\" then
163 local tempfolder = os.getenv("TEMP")
164 return tempfolder .. filetocheck
166 local backstring = filetocheck:reverse()
167 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
172 --------------------------------------------------------------------------------
173 function menu_render_worldlist()
176 local current_worldlist = menudata.worldlist:get_list()
178 for i,v in ipairs(current_worldlist) do
180 retval = retval ..","
183 retval = retval .. core.formspec_escape(v.name) ..
184 " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
190 --------------------------------------------------------------------------------
191 function menu_handle_key_up_down(fields,textlist,settingname)
193 if fields["key_up"] then
194 local oldidx = core.get_textlist_index(textlist)
196 if oldidx ~= nil and oldidx > 1 then
197 local newidx = oldidx -1
198 core.setting_set(settingname,
199 menudata.worldlist:get_raw_index(newidx))
204 if fields["key_down"] then
205 local oldidx = core.get_textlist_index(textlist)
207 if oldidx ~= nil and oldidx < menudata.worldlist:size() then
208 local newidx = oldidx + 1
209 core.setting_set(settingname,
210 menudata.worldlist:get_raw_index(newidx))
219 --------------------------------------------------------------------------------
220 function asyncOnlineFavourites()
222 menudata.favorites = {}
225 return core.get_favorites("online")
229 if core.setting_getbool("public_serverlist") then
230 menudata.favorites = order_favorite_list(result)
231 core.event_handler("Refresh")
237 --------------------------------------------------------------------------------
238 function text2textlist(xpos,ypos,width,height,tl_name,textlen,text,transparency)
239 local textlines = core.splittext(text,textlen)
241 local retval = "textlist[" .. xpos .. "," .. ypos .. ";"
242 .. width .. "," .. height .. ";"
245 for i=1, #textlines, 1 do
246 textlines[i] = textlines[i]:gsub("\r","")
247 retval = retval .. core.formspec_escape(textlines[i]) .. ","
250 retval = retval .. ";0;"
253 retval = retval .. "true"
256 retval = retval .. "]"
261 --------------------------------------------------------------------------------
262 function is_server_protocol_compat(proto_min, proto_max)
263 return not ((min_supp_proto > (proto_max or 24)) or (max_supp_proto < (proto_min or 13)))
265 --------------------------------------------------------------------------------
266 function is_server_protocol_compat_or_error(proto_min, proto_max)
267 if not is_server_protocol_compat(proto_min, proto_max) then
268 gamedata.errormessage = fgettext_ne("Protocol version mismatch, server " ..
269 ((proto_min ~= proto_max) and "supports protocols between $1 and $2" or "enforces protocol version $1") ..
271 ((min_supp_proto ~= max_supp_proto) and "support protocols between version $3 and $4." or "only support protocol version $3"),
272 proto_min or 13, proto_max or 24, min_supp_proto, max_supp_proto)