tab_online.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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.fav then
  98. retval = retval .. "tooltip[btn_delete_favorite;" .. fgettext("Remove favorite") .. "]"
  99. retval = retval .. "style[btn_delete_favorite;padding=6]"
  100. retval = retval .. "image_button[5,1.3;0.5,0.5;" .. core.formspec_escape(defaulttexturedir ..
  101. "server_favorite_delete.png") .. ";btn_delete_favorite;]"
  102. end
  103. if gamedata.serverdescription then
  104. retval = retval .. "textarea[0.25,1.85;5.25,2.7;;;" ..
  105. core.formspec_escape(gamedata.serverdescription) .. "]"
  106. end
  107. end
  108. retval = retval .. "container_end[]"
  109. -- Table
  110. retval = retval .. "tablecolumns[" ..
  111. "image,tooltip=" .. fgettext("Ping") .. "," ..
  112. "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
  113. "1=" .. core.formspec_escape(defaulttexturedir .. "server_ping_4.png") .. "," ..
  114. "2=" .. core.formspec_escape(defaulttexturedir .. "server_ping_3.png") .. "," ..
  115. "3=" .. core.formspec_escape(defaulttexturedir .. "server_ping_2.png") .. "," ..
  116. "4=" .. core.formspec_escape(defaulttexturedir .. "server_ping_1.png") .. "," ..
  117. "5=" .. core.formspec_escape(defaulttexturedir .. "server_favorite.png") .. "," ..
  118. "6=" .. core.formspec_escape(defaulttexturedir .. "server_public.png") .. "," ..
  119. "7=" .. core.formspec_escape(defaulttexturedir .. "server_incompatible.png") .. ";" ..
  120. "color,span=1;" ..
  121. "text,align=inline;"..
  122. "color,span=1;" ..
  123. "text,align=inline,width=4.25;" ..
  124. "image,tooltip=" .. fgettext("Creative mode") .. "," ..
  125. "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
  126. "1=" .. core.formspec_escape(defaulttexturedir .. "server_flags_creative.png") .. "," ..
  127. "align=inline,padding=0.25,width=1.5;" ..
  128. --~ PvP = Player versus Player
  129. "image,tooltip=" .. fgettext("Damage / PvP") .. "," ..
  130. "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
  131. "1=" .. core.formspec_escape(defaulttexturedir .. "server_flags_damage.png") .. "," ..
  132. "2=" .. core.formspec_escape(defaulttexturedir .. "server_flags_pvp.png") .. "," ..
  133. "align=inline,padding=0.25,width=1.5;" ..
  134. "color,align=inline,span=1;" ..
  135. "text,align=inline,padding=1]" ..
  136. "table[0.25,1;9.25,5.8;servers;"
  137. local servers = get_sorted_servers()
  138. local dividers = {
  139. fav = "5,#ffff00," .. fgettext("Favorites") .. ",,,0,0,,",
  140. public = "6,#4bdd42," .. fgettext("Public Servers") .. ",,,0,0,,",
  141. incompatible = "7,"..mt_color_grey.."," .. fgettext("Incompatible Servers") .. ",,,0,0,,"
  142. }
  143. local order = {"fav", "public", "incompatible"}
  144. tabdata.lookup = {} -- maps row number to server
  145. local rows = {}
  146. for _, section in ipairs(order) do
  147. local section_servers = servers[section]
  148. if next(section_servers) ~= nil then
  149. rows[#rows + 1] = dividers[section]
  150. for _, server in ipairs(section_servers) do
  151. tabdata.lookup[#rows + 1] = server
  152. rows[#rows + 1] = render_serverlist_row(server)
  153. end
  154. end
  155. end
  156. retval = retval .. table.concat(rows, ",")
  157. if tabdata.selected then
  158. retval = retval .. ";" .. tabdata.selected .. "]"
  159. else
  160. retval = retval .. ";0]"
  161. end
  162. return retval
  163. end
  164. --------------------------------------------------------------------------------
  165. local function search_server_list(input)
  166. menudata.search_result = nil
  167. if #serverlistmgr.servers < 2 then
  168. return
  169. end
  170. -- setup the keyword list
  171. local keywords = {}
  172. for word in input:gmatch("%S+") do
  173. table.insert(keywords, word:lower())
  174. end
  175. if #keywords == 0 then
  176. return
  177. end
  178. menudata.search_result = {}
  179. -- Search the serverlist
  180. local search_result = {}
  181. for i, server in ipairs(serverlistmgr.servers) do
  182. local name_matches, description_matches = true, true
  183. for _, keyword in ipairs(keywords) do
  184. name_matches = name_matches and not not
  185. (server.name or ""):lower():find(keyword, 1, true)
  186. description_matches = description_matches and not not
  187. (server.description or ""):lower():find(keyword, 1, true)
  188. end
  189. if name_matches or description_matches then
  190. server.points = #serverlistmgr.servers - i
  191. + (name_matches and 50 or 0)
  192. table.insert(search_result, server)
  193. end
  194. end
  195. if #search_result == 0 then
  196. return
  197. end
  198. table.sort(search_result, function(a, b)
  199. return a.points > b.points
  200. end)
  201. menudata.search_result = search_result
  202. end
  203. local function set_selected_server(tabdata, idx, server)
  204. -- reset selection
  205. if idx == nil or server == nil then
  206. tabdata.selected = nil
  207. core.settings:set("address", "")
  208. core.settings:set("remote_port", "30000")
  209. return
  210. end
  211. local address = server.address
  212. local port = server.port
  213. gamedata.serverdescription = server.description
  214. gamedata.fav = false
  215. for _, fav in ipairs(serverlistmgr.get_favorites()) do
  216. if address == fav.address and port == fav.port then
  217. gamedata.fav = true
  218. break
  219. end
  220. end
  221. if address and port then
  222. core.settings:set("address", address)
  223. core.settings:set("remote_port", port)
  224. end
  225. tabdata.selected = idx
  226. end
  227. local function main_button_handler(tabview, fields, name, tabdata)
  228. if fields.te_name then
  229. gamedata.playername = fields.te_name
  230. core.settings:set("name", fields.te_name)
  231. end
  232. if fields.servers then
  233. local event = core.explode_table_event(fields.servers)
  234. local server = tabdata.lookup[event.row]
  235. if server then
  236. if event.type == "DCL" then
  237. if not is_server_protocol_compat_or_error(
  238. server.proto_min, server.proto_max) then
  239. return true
  240. end
  241. gamedata.address = server.address
  242. gamedata.port = server.port
  243. gamedata.playername = fields.te_name
  244. gamedata.selected_world = 0
  245. if fields.te_pwd then
  246. gamedata.password = fields.te_pwd
  247. end
  248. gamedata.servername = server.name
  249. gamedata.serverdescription = server.description
  250. if gamedata.address and gamedata.port then
  251. core.settings:set("address", gamedata.address)
  252. core.settings:set("remote_port", gamedata.port)
  253. core.start()
  254. end
  255. return true
  256. end
  257. if event.type == "CHG" then
  258. set_selected_server(tabdata, event.row, server)
  259. return true
  260. end
  261. end
  262. end
  263. if fields.btn_delete_favorite then
  264. local idx = core.get_table_index("servers")
  265. if not idx then return end
  266. local server = tabdata.lookup[idx]
  267. if not server then return end
  268. serverlistmgr.delete_favorite(server)
  269. -- the server at [idx+1] will be at idx once list is refreshed
  270. set_selected_server(tabdata, idx, tabdata.lookup[idx+1])
  271. return true
  272. end
  273. if fields.btn_mp_clear then
  274. tabdata.search_for = ""
  275. menudata.search_result = nil
  276. return true
  277. end
  278. if fields.btn_mp_search or fields.key_enter_field == "te_search" then
  279. tabdata.search_for = fields.te_search
  280. search_server_list(fields.te_search:lower())
  281. if menudata.search_result then
  282. -- first server in row 2 due to header
  283. set_selected_server(tabdata, 2, menudata.search_result[1])
  284. end
  285. return true
  286. end
  287. if fields.btn_mp_refresh then
  288. serverlistmgr.sync()
  289. return true
  290. end
  291. local host_filled = (fields.te_address ~= "") and fields.te_port:match("^%s*[1-9][0-9]*%s*$")
  292. local te_port_number = tonumber(fields.te_port)
  293. if (fields.btn_mp_login or fields.key_enter) and host_filled then
  294. gamedata.playername = fields.te_name
  295. gamedata.password = fields.te_pwd
  296. gamedata.address = fields.te_address
  297. gamedata.port = te_port_number
  298. local enable_split_login_register = core.settings:get_bool("enable_split_login_register")
  299. gamedata.allow_login_or_register = enable_split_login_register and "login" or "any"
  300. gamedata.selected_world = 0
  301. local idx = core.get_table_index("servers")
  302. local server = idx and tabdata.lookup[idx]
  303. set_selected_server(tabdata)
  304. if server and server.address == gamedata.address and
  305. server.port == gamedata.port then
  306. serverlistmgr.add_favorite(server)
  307. gamedata.servername = server.name
  308. gamedata.serverdescription = server.description
  309. if not is_server_protocol_compat_or_error(
  310. server.proto_min, server.proto_max) then
  311. return true
  312. end
  313. else
  314. gamedata.servername = ""
  315. gamedata.serverdescription = ""
  316. serverlistmgr.add_favorite({
  317. address = gamedata.address,
  318. port = gamedata.port,
  319. })
  320. end
  321. core.settings:set("address", gamedata.address)
  322. core.settings:set("remote_port", gamedata.port)
  323. core.start()
  324. return true
  325. end
  326. if fields.btn_mp_register and host_filled then
  327. local idx = core.get_table_index("servers")
  328. local server = idx and tabdata.lookup[idx]
  329. if server and (server.address ~= fields.te_address or server.port ~= te_port_number) then
  330. server = nil
  331. end
  332. if server and not is_server_protocol_compat_or_error(
  333. server.proto_min, server.proto_max) then
  334. return true
  335. end
  336. local dlg = create_register_dialog(fields.te_address, te_port_number, server)
  337. dlg:set_parent(tabview)
  338. tabview:hide()
  339. dlg:show()
  340. return true
  341. end
  342. return false
  343. end
  344. local function on_change(type)
  345. if type == "ENTER" then
  346. mm_game_theme.set_engine()
  347. serverlistmgr.sync()
  348. end
  349. end
  350. return {
  351. name = "online",
  352. caption = fgettext("Join Game"),
  353. cbf_formspec = get_formspec,
  354. cbf_button_handler = main_button_handler,
  355. on_change = on_change
  356. }