common.lua 8.2 KB

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