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 -- Menu helper functions
24 --------------------------------------------------------------------------------
26 --------------------------------------------------------------------------------
27 local function render_client_count(n)
37 --------------------------------------------------------------------------------
38 function image_column(tooltip, flagname)
40 "tooltip=" .. core.formspec_escape(tooltip) .. "," ..
41 "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
42 "1=" .. core.formspec_escape(defaulttexturedir .. "server_flags_" .. flagname .. ".png")
45 --------------------------------------------------------------------------------
46 function render_favorite(spec,render_details)
49 if spec.name ~= nil then
50 text = text .. core.formspec_escape(spec.name:trim())
52 -- if spec.description ~= nil and
53 -- core.formspec_escape(spec.description):trim() ~= "" then
54 -- text = text .. " (" .. core.formspec_escape(spec.description) .. ")"
57 if spec.address ~= nil then
58 text = text .. spec.address:trim()
60 if spec.port ~= nil then
61 text = text .. ":" .. spec.port
66 if not render_details then
72 if spec.clients ~= nil and spec.clients_max ~= nil then
73 local clients_color = ''
74 local clients_percent = 100 * spec.clients / spec.clients_max
76 -- Choose a color depending on how many clients are connected
77 -- (relatively to clients_max)
78 if spec.clients == 0 then
79 clients_color = '' -- 0 players: default/white
80 elseif spec.clients == spec.clients_max then
81 clients_color = '#dd5b5b' -- full server: red (darker)
82 elseif clients_percent <= 60 then
83 clients_color = '#a1e587' -- 0-60%: green
84 elseif clients_percent <= 90 then
85 clients_color = '#ffdc97' -- 60-90%: yellow
87 clients_color = '#ffba97' -- 90-100%: orange
91 clients_color .. ',' ..
92 render_client_count(spec.clients) .. ',' ..
94 render_client_count(spec.clients_max) .. ','
96 details = details .. ',?,/,?,'
100 details = details .. "1,"
102 details = details .. "0,"
106 details = details .. "1,"
108 details = details .. "0,"
112 details = details .. "1,"
114 details = details .. "0,"
117 return details .. text
120 --------------------------------------------------------------------------------
121 os.tempfolder = function()
122 if core.setting_get("TMPFolder") then
123 return core.setting_get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
126 local filetocheck = os.tmpname()
127 os.remove(filetocheck)
129 local randname = "MTTempModFolder_" .. math.random(0,10000)
130 if DIR_DELIM == "\\" then
131 local tempfolder = os.getenv("TEMP")
132 return tempfolder .. filetocheck
134 local backstring = filetocheck:reverse()
135 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
140 --------------------------------------------------------------------------------
141 function menu_render_worldlist()
144 local current_worldlist = menudata.worldlist:get_list()
146 for i,v in ipairs(current_worldlist) do
148 retval = retval ..","
151 retval = retval .. core.formspec_escape(v.name) ..
152 " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
158 --------------------------------------------------------------------------------
159 function menu_handle_key_up_down(fields,textlist,settingname)
161 if fields["key_up"] then
162 local oldidx = core.get_textlist_index(textlist)
164 if oldidx ~= nil and oldidx > 1 then
165 local newidx = oldidx -1
166 core.setting_set(settingname,
167 menudata.worldlist:get_raw_index(newidx))
172 if fields["key_down"] then
173 local oldidx = core.get_textlist_index(textlist)
175 if oldidx ~= nil and oldidx < menudata.worldlist:size() then
176 local newidx = oldidx + 1
177 core.setting_set(settingname,
178 menudata.worldlist:get_raw_index(newidx))
187 --------------------------------------------------------------------------------
188 function asyncOnlineFavourites()
190 menudata.favorites = {}
193 return core.get_favorites("online")
197 if core.setting_getbool("public_serverlist") then
198 menudata.favorites = result
199 core.event_handler("Refresh")
205 --------------------------------------------------------------------------------
206 function text2textlist(xpos,ypos,width,height,tl_name,textlen,text,transparency)
207 local textlines = core.splittext(text,textlen)
209 local retval = "textlist[" .. xpos .. "," .. ypos .. ";"
210 .. width .. "," .. height .. ";"
213 for i=1, #textlines, 1 do
214 textlines[i] = textlines[i]:gsub("\r","")
215 retval = retval .. core.formspec_escape(textlines[i]) .. ","
218 retval = retval .. ";0;"
221 retval = retval .. "true"
224 retval = retval .. "]"