2
0

common.lua 8.9 KB

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