common.lua 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. -- Local cached values
  23. --------------------------------------------------------------------------------
  24. local min_supp_proto, max_supp_proto
  25. function common_update_cached_supp_proto()
  26. min_supp_proto = core.get_min_supp_proto()
  27. max_supp_proto = core.get_max_supp_proto()
  28. end
  29. common_update_cached_supp_proto()
  30. --------------------------------------------------------------------------------
  31. -- Menu helper functions
  32. --------------------------------------------------------------------------------
  33. --------------------------------------------------------------------------------
  34. local function render_client_count(n)
  35. if n > 99 then return '99+'
  36. elseif n >= 0 then return tostring(n)
  37. else return '?' end
  38. end
  39. local function configure_selected_world_params(idx)
  40. local worldconfig = pkgmgr.get_worldconfig(menudata.worldlist:get_list()[idx].path)
  41. if worldconfig.creative_mode then
  42. core.settings:set("creative_mode", worldconfig.creative_mode)
  43. end
  44. if worldconfig.enable_damage then
  45. core.settings:set("enable_damage", worldconfig.enable_damage)
  46. end
  47. end
  48. --------------------------------------------------------------------------------
  49. function image_column(tooltip, flagname)
  50. return "image,tooltip=" .. core.formspec_escape(tooltip) .. "," ..
  51. "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
  52. "1=" .. core.formspec_escape(defaulttexturedir ..
  53. (flagname and "server_flags_" .. flagname .. ".png" or "blank.png")) .. "," ..
  54. "2=" .. core.formspec_escape(defaulttexturedir .. "server_ping_4.png") .. "," ..
  55. "3=" .. core.formspec_escape(defaulttexturedir .. "server_ping_3.png") .. "," ..
  56. "4=" .. core.formspec_escape(defaulttexturedir .. "server_ping_2.png") .. "," ..
  57. "5=" .. core.formspec_escape(defaulttexturedir .. "server_ping_1.png")
  58. end
  59. --------------------------------------------------------------------------------
  60. function render_serverlist_row(spec, is_favorite)
  61. local text = ""
  62. if spec.name then
  63. text = text .. core.formspec_escape(spec.name:trim())
  64. elseif spec.address then
  65. text = text .. core.formspec_escape(spec.address:trim())
  66. if spec.port then
  67. text = text .. ":" .. spec.port
  68. end
  69. end
  70. local grey_out = not is_server_protocol_compat(spec.proto_min, spec.proto_max)
  71. local details
  72. if is_favorite then
  73. details = "1,"
  74. else
  75. details = "0,"
  76. end
  77. if spec.ping then
  78. local ping = spec.ping * 1000
  79. if ping <= 50 then
  80. details = details .. "2,"
  81. elseif ping <= 100 then
  82. details = details .. "3,"
  83. elseif ping <= 250 then
  84. details = details .. "4,"
  85. else
  86. details = details .. "5,"
  87. end
  88. else
  89. details = details .. "0,"
  90. end
  91. if spec.clients and spec.clients_max then
  92. local clients_percent = 100 * spec.clients / spec.clients_max
  93. -- Choose a color depending on how many clients are connected
  94. -- (relatively to clients_max)
  95. local clients_color
  96. if grey_out then clients_color = '#aaaaaa'
  97. elseif spec.clients == 0 then clients_color = '' -- 0 players: default/white
  98. elseif clients_percent <= 60 then clients_color = '#a1e587' -- 0-60%: green
  99. elseif clients_percent <= 90 then clients_color = '#ffdc97' -- 60-90%: yellow
  100. elseif clients_percent == 100 then clients_color = '#dd5b5b' -- full server: red (darker)
  101. else clients_color = '#ffba97' -- 90-100%: orange
  102. end
  103. details = details .. clients_color .. ',' ..
  104. render_client_count(spec.clients) .. ',/,' ..
  105. render_client_count(spec.clients_max) .. ','
  106. elseif grey_out then
  107. details = details .. '#aaaaaa,?,/,?,'
  108. else
  109. details = details .. ',?,/,?,'
  110. end
  111. if spec.creative then
  112. details = details .. "1,"
  113. else
  114. details = details .. "0,"
  115. end
  116. if spec.damage then
  117. details = details .. "1,"
  118. else
  119. details = details .. "0,"
  120. end
  121. if spec.pvp then
  122. details = details .. "1,"
  123. else
  124. details = details .. "0,"
  125. end
  126. return details .. (grey_out and '#aaaaaa,' or ',') .. text
  127. end
  128. --------------------------------------------------------------------------------
  129. os.tempfolder = function()
  130. local temp = core.get_temp_path()
  131. return temp .. DIR_DELIM .. "MT_" .. math.random(0, 10000)
  132. end
  133. --------------------------------------------------------------------------------
  134. os.tmpname = function()
  135. local path = os.tempfolder()
  136. io.open(path, "w"):close()
  137. return path
  138. end
  139. --------------------------------------------------------------------------------
  140. function menu_render_worldlist()
  141. local retval = ""
  142. local current_worldlist = menudata.worldlist:get_list()
  143. for i, v in ipairs(current_worldlist) do
  144. if retval ~= "" then retval = retval .. "," end
  145. retval = retval .. core.formspec_escape(v.name) ..
  146. " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
  147. end
  148. return retval
  149. end
  150. --------------------------------------------------------------------------------
  151. function menu_handle_key_up_down(fields, textlist, settingname)
  152. local oldidx, newidx = core.get_textlist_index(textlist), 1
  153. if fields.key_up or fields.key_down then
  154. if fields.key_up and oldidx and oldidx > 1 then
  155. newidx = oldidx - 1
  156. elseif fields.key_down and oldidx and
  157. oldidx < menudata.worldlist:size() then
  158. newidx = oldidx + 1
  159. end
  160. core.settings:set(settingname, menudata.worldlist:get_raw_index(newidx))
  161. configure_selected_world_params(newidx)
  162. return true
  163. end
  164. return false
  165. end
  166. --------------------------------------------------------------------------------
  167. function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
  168. local textlines = core.wrap_text(text, textlen, true)
  169. local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
  170. "," .. height .. ";" .. tl_name .. ";"
  171. for i = 1, #textlines do
  172. textlines[i] = textlines[i]:gsub("\r", "")
  173. retval = retval .. core.formspec_escape(textlines[i]) .. ","
  174. end
  175. retval = retval .. ";0;"
  176. if transparency then retval = retval .. "true" end
  177. retval = retval .. "]"
  178. return retval
  179. end
  180. --------------------------------------------------------------------------------
  181. function is_server_protocol_compat(server_proto_min, server_proto_max)
  182. if (not server_proto_min) or (not server_proto_max) then
  183. -- There is no info. Assume the best and act as if we would be compatible.
  184. return true
  185. end
  186. return min_supp_proto <= server_proto_max and max_supp_proto >= server_proto_min
  187. end
  188. --------------------------------------------------------------------------------
  189. function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
  190. if not is_server_protocol_compat(server_proto_min, server_proto_max) then
  191. local server_prot_ver_info, client_prot_ver_info
  192. local s_p_min = server_proto_min
  193. local s_p_max = server_proto_max
  194. if s_p_min ~= s_p_max then
  195. server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
  196. s_p_min, s_p_max)
  197. else
  198. server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
  199. s_p_min)
  200. end
  201. if min_supp_proto ~= max_supp_proto then
  202. client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
  203. min_supp_proto, max_supp_proto)
  204. else
  205. client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
  206. end
  207. gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
  208. .. server_prot_ver_info
  209. .. client_prot_ver_info
  210. return false
  211. end
  212. return true
  213. end
  214. --------------------------------------------------------------------------------
  215. function menu_worldmt(selected, setting, value)
  216. local world = menudata.worldlist:get_list()[selected]
  217. if world then
  218. local filename = world.path .. DIR_DELIM .. "world.mt"
  219. local world_conf = Settings(filename)
  220. if value then
  221. if not world_conf:write() then
  222. core.log("error", "Failed to write world config file")
  223. end
  224. world_conf:set(setting, value)
  225. world_conf:write()
  226. else
  227. return world_conf:get(setting)
  228. end
  229. else
  230. return nil
  231. end
  232. end
  233. function menu_worldmt_legacy(selected)
  234. local modes_names = {"creative_mode", "enable_damage", "server_announce"}
  235. for _, mode_name in pairs(modes_names) do
  236. local mode_val = menu_worldmt(selected, mode_name)
  237. if mode_val then
  238. core.settings:set(mode_name, mode_val)
  239. else
  240. menu_worldmt(selected, mode_name, core.settings:get(mode_name))
  241. end
  242. end
  243. end