tab_online.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. local function get_sorted_servers()
  18. local servers = {
  19. fav = {},
  20. public = {},
  21. incompatible = {}
  22. }
  23. local favs = serverlistmgr.get_favorites()
  24. local taken_favs = {}
  25. local result = menudata.search_result or serverlistmgr.servers
  26. for _, server in ipairs(result) do
  27. server.is_favorite = false
  28. for index, fav in ipairs(favs) do
  29. if server.address == fav.address and server.port == fav.port then
  30. taken_favs[index] = true
  31. server.is_favorite = true
  32. break
  33. end
  34. end
  35. server.is_compatible = is_server_protocol_compat(server.proto_min, server.proto_max)
  36. if server.is_favorite then
  37. table.insert(servers.fav, server)
  38. elseif server.is_compatible then
  39. table.insert(servers.public, server)
  40. else
  41. table.insert(servers.incompatible, server)
  42. end
  43. end
  44. if not menudata.search_result then
  45. for index, fav in ipairs(favs) do
  46. if not taken_favs[index] then
  47. table.insert(servers.fav, fav)
  48. end
  49. end
  50. end
  51. return servers
  52. end
  53. local function is_selected_fav(server)
  54. local address = core.settings:get("address")
  55. local port = tonumber(core.settings:get("remote_port"))
  56. for _, fav in ipairs(serverlistmgr.get_favorites()) do
  57. if address == fav.address and port == fav.port then
  58. return true
  59. end
  60. end
  61. return false
  62. end
  63. -- Persists the selected server in the "address" and "remote_port" settings
  64. local function set_selected_server(server)
  65. if server == nil then -- reset selection
  66. core.settings:remove("address")
  67. core.settings:remove("remote_port")
  68. return
  69. end
  70. local address = server.address
  71. local port = server.port
  72. gamedata.serverdescription = server.description
  73. if address and port then
  74. core.settings:set("address", address)
  75. core.settings:set("remote_port", port)
  76. end
  77. end
  78. local function find_selected_server()
  79. local address = core.settings:get("address")
  80. local port = tonumber(core.settings:get("remote_port"))
  81. for _, server in ipairs(serverlistmgr.servers) do
  82. if server.address == address and server.port == port then
  83. return server
  84. end
  85. end
  86. for _, server in ipairs(serverlistmgr.get_favorites()) do
  87. if server.address == address and server.port == port then
  88. return server
  89. end
  90. end
  91. end
  92. local function get_formspec(tabview, name, tabdata)
  93. -- Update the cached supported proto info,
  94. -- it may have changed after a change by the settings menu.
  95. common_update_cached_supp_proto()
  96. if not tabdata.search_for then
  97. tabdata.search_for = ""
  98. end
  99. local retval =
  100. -- Search
  101. "field[0.25,0.25;7,0.75;te_search;;" .. core.formspec_escape(tabdata.search_for) .. "]" ..
  102. "field_enter_after_edit[te_search;true]" ..
  103. "container[7.25,0.25]" ..
  104. "image_button[0,0;0.75,0.75;" .. core.formspec_escape(defaulttexturedir .. "search.png") .. ";btn_mp_search;]" ..
  105. "image_button[0.75,0;0.75,0.75;" .. core.formspec_escape(defaulttexturedir .. "clear.png") .. ";btn_mp_clear;]" ..
  106. "image_button[1.5,0;0.75,0.75;" .. core.formspec_escape(defaulttexturedir .. "refresh.png") .. ";btn_mp_refresh;]" ..
  107. "tooltip[btn_mp_clear;" .. fgettext("Clear") .. "]" ..
  108. "tooltip[btn_mp_search;" .. fgettext("Search") .. "]" ..
  109. "tooltip[btn_mp_refresh;" .. fgettext("Refresh") .. "]" ..
  110. "container_end[]" ..
  111. "container[9.75,0]" ..
  112. "box[0,0;5.75,7.1;#666666]" ..
  113. -- Address / Port
  114. "label[0.25,0.35;" .. fgettext("Address") .. "]" ..
  115. "label[4.25,0.35;" .. fgettext("Port") .. "]" ..
  116. "field[0.25,0.5;4,0.75;te_address;;" ..
  117. core.formspec_escape(core.settings:get("address")) .. "]" ..
  118. "field[4.25,0.5;1.25,0.75;te_port;;" ..
  119. core.formspec_escape(core.settings:get("remote_port")) .. "]" ..
  120. -- Description Background
  121. "label[0.25,1.6;" .. fgettext("Server Description") .. "]" ..
  122. "box[0.25,1.85;5.25,2.7;#999999]"..
  123. -- Name / Password
  124. "container[0,4.8]" ..
  125. "label[0.25,0;" .. fgettext("Name") .. "]" ..
  126. "label[2.875,0;" .. fgettext("Password") .. "]" ..
  127. "field[0.25,0.2;2.625,0.75;te_name;;" .. core.formspec_escape(core.settings:get("name")) .. "]" ..
  128. "pwdfield[2.875,0.2;2.625,0.75;te_pwd;]" ..
  129. "container_end[]" ..
  130. -- Connect
  131. "button[3,6;2.5,0.75;btn_mp_login;" .. fgettext("Login") .. "]"
  132. if core.settings:get_bool("enable_split_login_register") then
  133. retval = retval .. "button[0.25,6;2.5,0.75;btn_mp_register;" .. fgettext("Register") .. "]"
  134. end
  135. local selected_server = find_selected_server()
  136. if selected_server then
  137. if gamedata.serverdescription then
  138. retval = retval .. "textarea[0.25,1.85;5.25,2.7;;;" ..
  139. core.formspec_escape(gamedata.serverdescription) .. "]"
  140. end
  141. local clients_list = selected_server.clients_list
  142. local can_view_clients_list = clients_list and #clients_list > 0
  143. if can_view_clients_list then
  144. table.sort(clients_list, function(a, b)
  145. return a:lower() < b:lower()
  146. end)
  147. local max_clients = 5
  148. if #clients_list > max_clients then
  149. retval = retval .. "tooltip[btn_view_clients;" ..
  150. fgettext("Clients:\n$1", table.concat(clients_list, "\n", 1, max_clients)) .. "\n..." .. "]"
  151. else
  152. retval = retval .. "tooltip[btn_view_clients;" ..
  153. fgettext("Clients:\n$1", table.concat(clients_list, "\n")) .. "]"
  154. end
  155. retval = retval .. "style[btn_view_clients;padding=6]"
  156. retval = retval .. "image_button[4.5,1.3;0.5,0.5;" .. core.formspec_escape(defaulttexturedir ..
  157. "server_view_clients.png") .. ";btn_view_clients;]"
  158. end
  159. if is_selected_fav() then
  160. retval = retval .. "tooltip[btn_delete_favorite;" .. fgettext("Remove favorite") .. "]"
  161. retval = retval .. "style[btn_delete_favorite;padding=6]"
  162. retval = retval .. "image_button[5,1.3;0.5,0.5;" ..
  163. core.formspec_escape(defaulttexturedir .. "server_favorite_delete.png") .. ";btn_delete_favorite;]"
  164. else
  165. retval = retval .. "tooltip[btn_add_favorite;" .. fgettext("Add favorite") .. "]"
  166. retval = retval .. "style[btn_add_favorite;padding=6]"
  167. retval = retval .. "image_button[5,1.3;0.5,0.5;" ..
  168. core.formspec_escape(defaulttexturedir .. "server_favorite.png") .. ";btn_add_favorite;]"
  169. end
  170. end
  171. retval = retval .. "container_end[]"
  172. -- Table
  173. retval = retval .. "tablecolumns[" ..
  174. "image,tooltip=" .. fgettext("Ping") .. "," ..
  175. "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
  176. "1=" .. core.formspec_escape(defaulttexturedir .. "server_ping_4.png") .. "," ..
  177. "2=" .. core.formspec_escape(defaulttexturedir .. "server_ping_3.png") .. "," ..
  178. "3=" .. core.formspec_escape(defaulttexturedir .. "server_ping_2.png") .. "," ..
  179. "4=" .. core.formspec_escape(defaulttexturedir .. "server_ping_1.png") .. "," ..
  180. "5=" .. core.formspec_escape(defaulttexturedir .. "server_favorite.png") .. "," ..
  181. "6=" .. core.formspec_escape(defaulttexturedir .. "server_public.png") .. "," ..
  182. "7=" .. core.formspec_escape(defaulttexturedir .. "server_incompatible.png") .. ";" ..
  183. "color,span=1;" ..
  184. "text,align=inline;"..
  185. "color,span=1;" ..
  186. "text,align=inline,width=4.25;" ..
  187. "image,tooltip=" .. fgettext("Creative mode") .. "," ..
  188. "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
  189. "1=" .. core.formspec_escape(defaulttexturedir .. "server_flags_creative.png") .. "," ..
  190. "align=inline,padding=0.25,width=1.5;" ..
  191. --~ PvP = Player versus Player
  192. "image,tooltip=" .. fgettext("Damage / PvP") .. "," ..
  193. "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
  194. "1=" .. core.formspec_escape(defaulttexturedir .. "server_flags_damage.png") .. "," ..
  195. "2=" .. core.formspec_escape(defaulttexturedir .. "server_flags_pvp.png") .. "," ..
  196. "align=inline,padding=0.25,width=1.5;" ..
  197. "color,align=inline,span=1;" ..
  198. "text,align=inline,padding=1]" ..
  199. "table[0.25,1;9.25,5.8;servers;"
  200. local servers = get_sorted_servers()
  201. local dividers = {
  202. fav = "5,#ffff00," .. fgettext("Favorites") .. ",,,0,0,,",
  203. public = "6,#4bdd42," .. fgettext("Public Servers") .. ",,,0,0,,",
  204. incompatible = "7,"..mt_color_grey.."," .. fgettext("Incompatible Servers") .. ",,,0,0,,"
  205. }
  206. local order = {"fav", "public", "incompatible"}
  207. tabdata.lookup = {} -- maps row number to server
  208. local rows = {}
  209. for _, section in ipairs(order) do
  210. local section_servers = servers[section]
  211. if next(section_servers) ~= nil then
  212. rows[#rows + 1] = dividers[section]
  213. for _, server in ipairs(section_servers) do
  214. tabdata.lookup[#rows + 1] = server
  215. rows[#rows + 1] = render_serverlist_row(server)
  216. end
  217. end
  218. end
  219. retval = retval .. table.concat(rows, ",")
  220. local selected_row_idx = 0
  221. if selected_server then
  222. for i, server in pairs(tabdata.lookup) do
  223. if selected_server.address == server.address and
  224. selected_server.port == server.port then
  225. selected_row_idx = i
  226. break
  227. end
  228. end
  229. end
  230. retval = retval .. ";" .. selected_row_idx .. "]"
  231. return retval
  232. end
  233. --------------------------------------------------------------------------------
  234. local function search_server_list(input)
  235. menudata.search_result = nil
  236. if #serverlistmgr.servers < 2 then
  237. return
  238. end
  239. -- setup the keyword list
  240. local keywords = {}
  241. for word in input:gmatch("%S+") do
  242. table.insert(keywords, word:lower())
  243. end
  244. if #keywords == 0 then
  245. return
  246. end
  247. menudata.search_result = {}
  248. -- Search the serverlist
  249. local search_result = {}
  250. for i, server in ipairs(serverlistmgr.servers) do
  251. local name_matches, description_matches = true, true
  252. for _, keyword in ipairs(keywords) do
  253. name_matches = name_matches and not not
  254. (server.name or ""):lower():find(keyword, 1, true)
  255. description_matches = description_matches and not not
  256. (server.description or ""):lower():find(keyword, 1, true)
  257. end
  258. if name_matches or description_matches then
  259. server.points = #serverlistmgr.servers - i
  260. + (name_matches and 50 or 0)
  261. table.insert(search_result, server)
  262. end
  263. end
  264. if #search_result == 0 then
  265. return
  266. end
  267. table.sort(search_result, function(a, b)
  268. return a.points > b.points
  269. end)
  270. menudata.search_result = search_result
  271. end
  272. local function main_button_handler(tabview, fields, name, tabdata)
  273. if fields.te_name then
  274. gamedata.playername = fields.te_name
  275. core.settings:set("name", fields.te_name)
  276. end
  277. if fields.servers then
  278. local event = core.explode_table_event(fields.servers)
  279. local server = tabdata.lookup[event.row]
  280. if server then
  281. if event.type == "DCL" then
  282. if not is_server_protocol_compat_or_error(
  283. server.proto_min, server.proto_max) then
  284. return true
  285. end
  286. gamedata.address = server.address
  287. gamedata.port = server.port
  288. gamedata.playername = fields.te_name
  289. gamedata.selected_world = 0
  290. if fields.te_pwd then
  291. gamedata.password = fields.te_pwd
  292. end
  293. gamedata.servername = server.name
  294. gamedata.serverdescription = server.description
  295. if gamedata.address and gamedata.port then
  296. set_selected_server(server)
  297. core.start()
  298. end
  299. return true
  300. end
  301. if event.type == "CHG" then
  302. set_selected_server(server)
  303. return true
  304. end
  305. end
  306. end
  307. if fields.btn_add_favorite then
  308. serverlistmgr.add_favorite(find_selected_server())
  309. return true
  310. end
  311. if fields.btn_delete_favorite then
  312. local idx = core.get_table_index("servers")
  313. if not idx then return end
  314. local server = tabdata.lookup[idx]
  315. if not server then return end
  316. serverlistmgr.delete_favorite(server)
  317. set_selected_server(server)
  318. return true
  319. end
  320. if fields.btn_view_clients then
  321. local dlg = create_clientslist_dialog(find_selected_server())
  322. dlg:set_parent(tabview)
  323. tabview:hide()
  324. dlg:show()
  325. return true
  326. end
  327. if fields.btn_mp_clear then
  328. tabdata.search_for = ""
  329. menudata.search_result = nil
  330. return true
  331. end
  332. if fields.btn_mp_search or fields.key_enter_field == "te_search" then
  333. tabdata.search_for = fields.te_search
  334. search_server_list(fields.te_search:lower())
  335. if menudata.search_result then
  336. -- Note: This clears the selection if there are no results
  337. set_selected_server(menudata.search_result[1])
  338. end
  339. return true
  340. end
  341. if fields.btn_mp_refresh then
  342. serverlistmgr.sync()
  343. return true
  344. end
  345. local host_filled = (fields.te_address ~= "") and fields.te_port:match("^%s*[1-9][0-9]*%s*$")
  346. local te_port_number = tonumber(fields.te_port)
  347. if (fields.btn_mp_login or fields.key_enter) and host_filled then
  348. gamedata.playername = fields.te_name
  349. gamedata.password = fields.te_pwd
  350. gamedata.address = fields.te_address
  351. gamedata.port = te_port_number
  352. local enable_split_login_register = core.settings:get_bool("enable_split_login_register")
  353. gamedata.allow_login_or_register = enable_split_login_register and "login" or "any"
  354. gamedata.selected_world = 0
  355. local idx = core.get_table_index("servers")
  356. local server = idx and tabdata.lookup[idx]
  357. if server and server.address == gamedata.address and
  358. server.port == gamedata.port then
  359. serverlistmgr.add_favorite(server)
  360. gamedata.servername = server.name
  361. gamedata.serverdescription = server.description
  362. if not is_server_protocol_compat_or_error(
  363. server.proto_min, server.proto_max) then
  364. return true
  365. end
  366. else
  367. gamedata.servername = ""
  368. gamedata.serverdescription = ""
  369. serverlistmgr.add_favorite({
  370. address = gamedata.address,
  371. port = gamedata.port,
  372. })
  373. end
  374. core.settings:set("address", gamedata.address)
  375. core.settings:set("remote_port", gamedata.port)
  376. core.start()
  377. return true
  378. end
  379. if fields.btn_mp_register and host_filled then
  380. local idx = core.get_table_index("servers")
  381. local server = idx and tabdata.lookup[idx]
  382. if server and (server.address ~= fields.te_address or server.port ~= te_port_number) then
  383. server = nil
  384. end
  385. if server and not is_server_protocol_compat_or_error(
  386. server.proto_min, server.proto_max) then
  387. return true
  388. end
  389. local dlg = create_register_dialog(fields.te_address, te_port_number, server)
  390. dlg:set_parent(tabview)
  391. tabview:hide()
  392. dlg:show()
  393. return true
  394. end
  395. return false
  396. end
  397. local function on_change(type)
  398. if type == "ENTER" then
  399. mm_game_theme.set_engine()
  400. serverlistmgr.sync()
  401. end
  402. end
  403. return {
  404. name = "online",
  405. caption = fgettext("Join Game"),
  406. cbf_formspec = get_formspec,
  407. cbf_button_handler = main_button_handler,
  408. on_change = on_change
  409. }