tab_online.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 selected_server.url then
  160. retval = retval .. "tooltip[btn_server_url;" .. fgettext("Open server website") .. "]"
  161. retval = retval .. "style[btn_server_url;padding=6]"
  162. retval = retval .. "image_button[" .. (can_view_clients_list and "4" or "4.5") .. ",1.3;0.5,0.5;" ..
  163. core.formspec_escape(defaulttexturedir .. "server_url.png") .. ";btn_server_url;]"
  164. end
  165. if is_selected_fav() then
  166. retval = retval .. "tooltip[btn_delete_favorite;" .. fgettext("Remove favorite") .. "]"
  167. retval = retval .. "style[btn_delete_favorite;padding=6]"
  168. retval = retval .. "image_button[5,1.3;0.5,0.5;" ..
  169. core.formspec_escape(defaulttexturedir .. "server_favorite_delete.png") .. ";btn_delete_favorite;]"
  170. else
  171. retval = retval .. "tooltip[btn_add_favorite;" .. fgettext("Add favorite") .. "]"
  172. retval = retval .. "style[btn_add_favorite;padding=6]"
  173. retval = retval .. "image_button[5,1.3;0.5,0.5;" ..
  174. core.formspec_escape(defaulttexturedir .. "server_favorite.png") .. ";btn_add_favorite;]"
  175. end
  176. end
  177. retval = retval .. "container_end[]"
  178. -- Table
  179. retval = retval .. "tablecolumns[" ..
  180. "image,tooltip=" .. fgettext("Ping") .. "," ..
  181. "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
  182. "1=" .. core.formspec_escape(defaulttexturedir .. "server_ping_4.png") .. "," ..
  183. "2=" .. core.formspec_escape(defaulttexturedir .. "server_ping_3.png") .. "," ..
  184. "3=" .. core.formspec_escape(defaulttexturedir .. "server_ping_2.png") .. "," ..
  185. "4=" .. core.formspec_escape(defaulttexturedir .. "server_ping_1.png") .. "," ..
  186. "5=" .. core.formspec_escape(defaulttexturedir .. "server_favorite.png") .. "," ..
  187. "6=" .. core.formspec_escape(defaulttexturedir .. "server_public.png") .. "," ..
  188. "7=" .. core.formspec_escape(defaulttexturedir .. "server_incompatible.png") .. ";" ..
  189. "color,span=1;" ..
  190. "text,align=inline;"..
  191. "color,span=1;" ..
  192. "text,align=inline,width=4.25;" ..
  193. "image,tooltip=" .. fgettext("Creative mode") .. "," ..
  194. "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
  195. "1=" .. core.formspec_escape(defaulttexturedir .. "server_flags_creative.png") .. "," ..
  196. "align=inline,padding=0.25,width=1.5;" ..
  197. --~ PvP = Player versus Player
  198. "image,tooltip=" .. fgettext("Damage / PvP") .. "," ..
  199. "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
  200. "1=" .. core.formspec_escape(defaulttexturedir .. "server_flags_damage.png") .. "," ..
  201. "2=" .. core.formspec_escape(defaulttexturedir .. "server_flags_pvp.png") .. "," ..
  202. "align=inline,padding=0.25,width=1.5;" ..
  203. "color,align=inline,span=1;" ..
  204. "text,align=inline,padding=1]" ..
  205. "table[0.25,1;9.25,5.8;servers;"
  206. local servers = get_sorted_servers()
  207. local dividers = {
  208. fav = "5,#ffff00," .. fgettext("Favorites") .. ",,,0,0,,",
  209. public = "6,#4bdd42," .. fgettext("Public Servers") .. ",,,0,0,,",
  210. incompatible = "7,"..mt_color_grey.."," .. fgettext("Incompatible Servers") .. ",,,0,0,,"
  211. }
  212. local order = {"fav", "public", "incompatible"}
  213. tabdata.lookup = {} -- maps row number to server
  214. local rows = {}
  215. for _, section in ipairs(order) do
  216. local section_servers = servers[section]
  217. if next(section_servers) ~= nil then
  218. rows[#rows + 1] = dividers[section]
  219. for _, server in ipairs(section_servers) do
  220. tabdata.lookup[#rows + 1] = server
  221. rows[#rows + 1] = render_serverlist_row(server)
  222. end
  223. end
  224. end
  225. retval = retval .. table.concat(rows, ",")
  226. local selected_row_idx = 0
  227. if selected_server then
  228. for i, server in pairs(tabdata.lookup) do
  229. if selected_server.address == server.address and
  230. selected_server.port == server.port then
  231. selected_row_idx = i
  232. break
  233. end
  234. end
  235. end
  236. retval = retval .. ";" .. selected_row_idx .. "]"
  237. return retval
  238. end
  239. --------------------------------------------------------------------------------
  240. local function search_server_list(input)
  241. menudata.search_result = nil
  242. if #serverlistmgr.servers < 2 then
  243. return
  244. end
  245. -- setup the keyword list
  246. local keywords = {}
  247. for word in input:gmatch("%S+") do
  248. table.insert(keywords, word:lower())
  249. end
  250. if #keywords == 0 then
  251. return
  252. end
  253. menudata.search_result = {}
  254. -- Search the serverlist
  255. local search_result = {}
  256. for i, server in ipairs(serverlistmgr.servers) do
  257. local name_matches, description_matches = true, true
  258. for _, keyword in ipairs(keywords) do
  259. name_matches = name_matches and not not
  260. (server.name or ""):lower():find(keyword, 1, true)
  261. description_matches = description_matches and not not
  262. (server.description or ""):lower():find(keyword, 1, true)
  263. end
  264. if name_matches or description_matches then
  265. server.points = #serverlistmgr.servers - i
  266. + (name_matches and 50 or 0)
  267. table.insert(search_result, server)
  268. end
  269. end
  270. if #search_result == 0 then
  271. return
  272. end
  273. table.sort(search_result, function(a, b)
  274. return a.points > b.points
  275. end)
  276. menudata.search_result = search_result
  277. end
  278. local function main_button_handler(tabview, fields, name, tabdata)
  279. if fields.te_name then
  280. gamedata.playername = fields.te_name
  281. core.settings:set("name", fields.te_name)
  282. end
  283. if fields.servers then
  284. local event = core.explode_table_event(fields.servers)
  285. local server = tabdata.lookup[event.row]
  286. if server then
  287. if event.type == "DCL" then
  288. if not is_server_protocol_compat_or_error(
  289. server.proto_min, server.proto_max) then
  290. return true
  291. end
  292. gamedata.address = server.address
  293. gamedata.port = server.port
  294. gamedata.playername = fields.te_name
  295. gamedata.selected_world = 0
  296. if fields.te_pwd then
  297. gamedata.password = fields.te_pwd
  298. end
  299. gamedata.servername = server.name
  300. gamedata.serverdescription = server.description
  301. if gamedata.address and gamedata.port then
  302. set_selected_server(server)
  303. core.start()
  304. end
  305. return true
  306. end
  307. if event.type == "CHG" then
  308. set_selected_server(server)
  309. return true
  310. end
  311. end
  312. end
  313. if fields.btn_add_favorite then
  314. serverlistmgr.add_favorite(find_selected_server())
  315. return true
  316. end
  317. if fields.btn_delete_favorite then
  318. local idx = core.get_table_index("servers")
  319. if not idx then return end
  320. local server = tabdata.lookup[idx]
  321. if not server then return end
  322. serverlistmgr.delete_favorite(server)
  323. set_selected_server(server)
  324. return true
  325. end
  326. if fields.btn_server_url then
  327. core.open_url_dialog(find_selected_server().url)
  328. return true
  329. end
  330. if fields.btn_view_clients then
  331. local dlg = create_clientslist_dialog(find_selected_server())
  332. dlg:set_parent(tabview)
  333. tabview:hide()
  334. dlg:show()
  335. return true
  336. end
  337. if fields.btn_mp_clear then
  338. tabdata.search_for = ""
  339. menudata.search_result = nil
  340. return true
  341. end
  342. if fields.btn_mp_search or fields.key_enter_field == "te_search" then
  343. tabdata.search_for = fields.te_search
  344. search_server_list(fields.te_search:lower())
  345. if menudata.search_result then
  346. -- Note: This clears the selection if there are no results
  347. set_selected_server(menudata.search_result[1])
  348. end
  349. return true
  350. end
  351. if fields.btn_mp_refresh then
  352. serverlistmgr.sync()
  353. return true
  354. end
  355. local host_filled = (fields.te_address ~= "") and fields.te_port:match("^%s*[1-9][0-9]*%s*$")
  356. local te_port_number = tonumber(fields.te_port)
  357. if (fields.btn_mp_login or fields.key_enter) and host_filled then
  358. gamedata.playername = fields.te_name
  359. gamedata.password = fields.te_pwd
  360. gamedata.address = fields.te_address
  361. gamedata.port = te_port_number
  362. local enable_split_login_register = core.settings:get_bool("enable_split_login_register")
  363. gamedata.allow_login_or_register = enable_split_login_register and "login" or "any"
  364. gamedata.selected_world = 0
  365. local idx = core.get_table_index("servers")
  366. local server = idx and tabdata.lookup[idx]
  367. if server and server.address == gamedata.address and
  368. server.port == gamedata.port then
  369. serverlistmgr.add_favorite(server)
  370. gamedata.servername = server.name
  371. gamedata.serverdescription = server.description
  372. if not is_server_protocol_compat_or_error(
  373. server.proto_min, server.proto_max) then
  374. return true
  375. end
  376. else
  377. gamedata.servername = ""
  378. gamedata.serverdescription = ""
  379. serverlistmgr.add_favorite({
  380. address = gamedata.address,
  381. port = gamedata.port,
  382. })
  383. end
  384. core.settings:set("address", gamedata.address)
  385. core.settings:set("remote_port", gamedata.port)
  386. core.start()
  387. return true
  388. end
  389. if fields.btn_mp_register and host_filled then
  390. local idx = core.get_table_index("servers")
  391. local server = idx and tabdata.lookup[idx]
  392. if server and (server.address ~= fields.te_address or server.port ~= te_port_number) then
  393. server = nil
  394. end
  395. if server and not is_server_protocol_compat_or_error(
  396. server.proto_min, server.proto_max) then
  397. return true
  398. end
  399. local dlg = create_register_dialog(fields.te_address, te_port_number, server)
  400. dlg:set_parent(tabview)
  401. tabview:hide()
  402. dlg:show()
  403. return true
  404. end
  405. return false
  406. end
  407. local function on_change(type)
  408. if type == "ENTER" then
  409. mm_game_theme.set_engine()
  410. serverlistmgr.sync()
  411. end
  412. end
  413. return {
  414. name = "online",
  415. caption = fgettext("Join Game"),
  416. cbf_formspec = get_formspec,
  417. cbf_button_handler = main_button_handler,
  418. on_change = on_change
  419. }