2
0

common.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. function render_serverlist_row(spec)
  42. local text = ""
  43. if spec.name then
  44. text = text .. core.formspec_escape(spec.name:trim())
  45. elseif spec.address then
  46. text = text .. core.formspec_escape(spec.address:trim())
  47. if spec.port then
  48. text = text .. ":" .. spec.port
  49. end
  50. end
  51. local grey_out = not spec.is_compatible
  52. local details = {}
  53. if spec.lag or spec.ping then
  54. local lag = (spec.lag or 0) * 1000 + (spec.ping or 0) * 250
  55. if lag <= 125 then
  56. table.insert(details, "1")
  57. elseif lag <= 175 then
  58. table.insert(details, "2")
  59. elseif lag <= 250 then
  60. table.insert(details, "3")
  61. else
  62. table.insert(details, "4")
  63. end
  64. else
  65. table.insert(details, "0")
  66. end
  67. table.insert(details, ",")
  68. local color = (grey_out and "#aaaaaa") or ((spec.is_favorite and "#ddddaa") or "#ffffff")
  69. if spec.clients and (spec.clients_max or 0) > 0 then
  70. local clients_percent = 100 * spec.clients / spec.clients_max
  71. -- Choose a color depending on how many clients are connected
  72. -- (relatively to clients_max)
  73. local clients_color
  74. if grey_out then clients_color = '#aaaaaa'
  75. elseif spec.clients == 0 then clients_color = '' -- 0 players: default/white
  76. elseif clients_percent <= 60 then clients_color = '#a1e587' -- 0-60%: green
  77. elseif clients_percent <= 90 then clients_color = '#ffdc97' -- 60-90%: yellow
  78. elseif clients_percent == 100 then clients_color = '#dd5b5b' -- full server: red (darker)
  79. else clients_color = '#ffba97' -- 90-100%: orange
  80. end
  81. table.insert(details, clients_color)
  82. table.insert(details, render_client_count(spec.clients) .. " / " ..
  83. render_client_count(spec.clients_max))
  84. else
  85. table.insert(details, color)
  86. table.insert(details, "?")
  87. end
  88. if spec.creative then
  89. table.insert(details, "1") -- creative icon
  90. else
  91. table.insert(details, "0")
  92. end
  93. if spec.pvp then
  94. table.insert(details, "2") -- pvp icon
  95. elseif spec.damage then
  96. table.insert(details, "1") -- heart icon
  97. else
  98. table.insert(details, "0")
  99. end
  100. table.insert(details, color)
  101. table.insert(details, text)
  102. return table.concat(details, ",")
  103. end
  104. ---------------------------------------------------------------------------------
  105. os.tmpname = function()
  106. error('do not use') -- instead use core.get_temp_path()
  107. end
  108. --------------------------------------------------------------------------------
  109. function menu_render_worldlist(show_gameid)
  110. local retval = {}
  111. local current_worldlist = menudata.worldlist:get_list()
  112. local row
  113. for i, v in ipairs(current_worldlist) do
  114. row = v.name
  115. if show_gameid == nil or show_gameid == true then
  116. row = row .. " [" .. v.gameid .. "]"
  117. end
  118. retval[#retval+1] = core.formspec_escape(row)
  119. end
  120. return table.concat(retval, ",")
  121. end
  122. function menu_handle_key_up_down(fields, textlist, settingname)
  123. local oldidx, newidx = core.get_textlist_index(textlist), 1
  124. if fields.key_up or fields.key_down then
  125. if fields.key_up and oldidx and oldidx > 1 then
  126. newidx = oldidx - 1
  127. elseif fields.key_down and oldidx and
  128. oldidx < menudata.worldlist:size() then
  129. newidx = oldidx + 1
  130. end
  131. core.settings:set(settingname, menudata.worldlist:get_raw_index(newidx))
  132. configure_selected_world_params(newidx)
  133. return true
  134. end
  135. return false
  136. end
  137. function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
  138. local textlines = core.wrap_text(text, textlen, true)
  139. local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
  140. "," .. height .. ";" .. tl_name .. ";"
  141. for i = 1, #textlines do
  142. textlines[i] = textlines[i]:gsub("\r", "")
  143. retval = retval .. core.formspec_escape(textlines[i]) .. ","
  144. end
  145. retval = retval .. ";0;"
  146. if transparency then retval = retval .. "true" end
  147. retval = retval .. "]"
  148. return retval
  149. end
  150. function is_server_protocol_compat(server_proto_min, server_proto_max)
  151. if (not server_proto_min) or (not server_proto_max) then
  152. -- There is no info. Assume the best and act as if we would be compatible.
  153. return true
  154. end
  155. return min_supp_proto <= server_proto_max and max_supp_proto >= server_proto_min
  156. end
  157. function is_server_protocol_compat_or_error(server_proto_min, server_proto_max)
  158. if not is_server_protocol_compat(server_proto_min, server_proto_max) then
  159. local server_prot_ver_info, client_prot_ver_info
  160. local s_p_min = server_proto_min
  161. local s_p_max = server_proto_max
  162. if s_p_min ~= s_p_max then
  163. server_prot_ver_info = fgettext_ne("Server supports protocol versions between $1 and $2. ",
  164. s_p_min, s_p_max)
  165. else
  166. server_prot_ver_info = fgettext_ne("Server enforces protocol version $1. ",
  167. s_p_min)
  168. end
  169. if min_supp_proto ~= max_supp_proto then
  170. client_prot_ver_info= fgettext_ne("We support protocol versions between version $1 and $2.",
  171. min_supp_proto, max_supp_proto)
  172. else
  173. client_prot_ver_info = fgettext_ne("We only support protocol version $1.", min_supp_proto)
  174. end
  175. gamedata.errormessage = fgettext_ne("Protocol version mismatch. ")
  176. .. server_prot_ver_info
  177. .. client_prot_ver_info
  178. return false
  179. end
  180. return true
  181. end
  182. function menu_worldmt(selected, setting, value)
  183. local world = menudata.worldlist:get_list()[selected]
  184. if world then
  185. local filename = world.path .. DIR_DELIM .. "world.mt"
  186. local world_conf = Settings(filename)
  187. if value then
  188. if not world_conf:write() then
  189. core.log("error", "Failed to write world config file")
  190. end
  191. world_conf:set(setting, value)
  192. world_conf:write()
  193. else
  194. return world_conf:get(setting)
  195. end
  196. else
  197. return nil
  198. end
  199. end
  200. function menu_worldmt_legacy(selected)
  201. local modes_names = {"creative_mode", "enable_damage", "server_announce"}
  202. for _, mode_name in pairs(modes_names) do
  203. local mode_val = menu_worldmt(selected, mode_name)
  204. if mode_val then
  205. core.settings:set(mode_name, mode_val)
  206. else
  207. menu_worldmt(selected, mode_name, core.settings:get(mode_name))
  208. end
  209. end
  210. end