2
0

tab_online.lua 14 KB

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