tab_online.lua 14 KB

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