common.lua 8.1 KB

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