2
0

tab_online.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. --Minetest
  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. word = word:gsub("(%W)", "%%%1")
  174. table.insert(keywords, word)
  175. end
  176. if #keywords == 0 then
  177. return
  178. end
  179. menudata.search_result = {}
  180. -- Search the serverlist
  181. local search_result = {}
  182. for i = 1, #serverlistmgr.servers do
  183. local server = serverlistmgr.servers[i]
  184. local found = 0
  185. for k = 1, #keywords do
  186. local keyword = keywords[k]
  187. if server.name then
  188. local sername = server.name:lower()
  189. local _, count = sername:gsub(keyword, keyword)
  190. found = found + count * 4
  191. end
  192. if server.description then
  193. local desc = server.description:lower()
  194. local _, count = desc:gsub(keyword, keyword)
  195. found = found + count * 2
  196. end
  197. end
  198. if found > 0 then
  199. local points = (#serverlistmgr.servers - i) / 5 + found
  200. server.points = points
  201. table.insert(search_result, server)
  202. end
  203. end
  204. if #search_result == 0 then
  205. return
  206. end
  207. table.sort(search_result, function(a, b)
  208. return a.points > b.points
  209. end)
  210. menudata.search_result = search_result
  211. end
  212. local function set_selected_server(tabdata, idx, server)
  213. -- reset selection
  214. if idx == nil or server == nil then
  215. tabdata.selected = nil
  216. core.settings:set("address", "")
  217. core.settings:set("remote_port", "30000")
  218. return
  219. end
  220. local address = server.address
  221. local port = server.port
  222. gamedata.serverdescription = server.description
  223. gamedata.fav = false
  224. for _, fav in ipairs(serverlistmgr.get_favorites()) do
  225. if address == fav.address and port == fav.port then
  226. gamedata.fav = true
  227. break
  228. end
  229. end
  230. if address and port then
  231. core.settings:set("address", address)
  232. core.settings:set("remote_port", port)
  233. end
  234. tabdata.selected = idx
  235. end
  236. local function main_button_handler(tabview, fields, name, tabdata)
  237. if fields.te_name then
  238. gamedata.playername = fields.te_name
  239. core.settings:set("name", fields.te_name)
  240. end
  241. if fields.servers then
  242. local event = core.explode_table_event(fields.servers)
  243. local server = tabdata.lookup[event.row]
  244. if server then
  245. if event.type == "DCL" then
  246. if not is_server_protocol_compat_or_error(
  247. server.proto_min, server.proto_max) then
  248. return true
  249. end
  250. gamedata.address = server.address
  251. gamedata.port = server.port
  252. gamedata.playername = fields.te_name
  253. gamedata.selected_world = 0
  254. if fields.te_pwd then
  255. gamedata.password = fields.te_pwd
  256. end
  257. gamedata.servername = server.name
  258. gamedata.serverdescription = server.description
  259. if gamedata.address and gamedata.port then
  260. core.settings:set("address", gamedata.address)
  261. core.settings:set("remote_port", gamedata.port)
  262. core.start()
  263. end
  264. return true
  265. end
  266. if event.type == "CHG" then
  267. set_selected_server(tabdata, event.row, server)
  268. return true
  269. end
  270. end
  271. end
  272. if fields.btn_delete_favorite then
  273. local idx = core.get_table_index("servers")
  274. if not idx then return end
  275. local server = tabdata.lookup[idx]
  276. if not server then return end
  277. serverlistmgr.delete_favorite(server)
  278. -- the server at [idx+1] will be at idx once list is refreshed
  279. set_selected_server(tabdata, idx, tabdata.lookup[idx+1])
  280. return true
  281. end
  282. if fields.btn_mp_clear then
  283. tabdata.search_for = ""
  284. menudata.search_result = nil
  285. return true
  286. end
  287. if fields.btn_mp_search or fields.key_enter_field == "te_search" then
  288. tabdata.search_for = fields.te_search
  289. search_server_list(fields.te_search:lower())
  290. if menudata.search_result then
  291. -- first server in row 2 due to header
  292. set_selected_server(tabdata, 2, menudata.search_result[1])
  293. end
  294. return true
  295. end
  296. if fields.btn_mp_refresh then
  297. serverlistmgr.sync()
  298. return true
  299. end
  300. local host_filled = (fields.te_address ~= "") and fields.te_port:match("^%s*[1-9][0-9]*%s*$")
  301. local te_port_number = tonumber(fields.te_port)
  302. if (fields.btn_mp_login or fields.key_enter) and host_filled then
  303. gamedata.playername = fields.te_name
  304. gamedata.password = fields.te_pwd
  305. gamedata.address = fields.te_address
  306. gamedata.port = te_port_number
  307. local enable_split_login_register = core.settings:get_bool("enable_split_login_register")
  308. gamedata.allow_login_or_register = enable_split_login_register and "login" or "any"
  309. gamedata.selected_world = 0
  310. local idx = core.get_table_index("servers")
  311. local server = idx and tabdata.lookup[idx]
  312. set_selected_server(tabdata)
  313. if server and server.address == gamedata.address and
  314. server.port == gamedata.port then
  315. serverlistmgr.add_favorite(server)
  316. gamedata.servername = server.name
  317. gamedata.serverdescription = server.description
  318. if not is_server_protocol_compat_or_error(
  319. server.proto_min, server.proto_max) then
  320. return true
  321. end
  322. else
  323. gamedata.servername = ""
  324. gamedata.serverdescription = ""
  325. serverlistmgr.add_favorite({
  326. address = gamedata.address,
  327. port = gamedata.port,
  328. })
  329. end
  330. core.settings:set("address", gamedata.address)
  331. core.settings:set("remote_port", gamedata.port)
  332. core.start()
  333. return true
  334. end
  335. if fields.btn_mp_register and host_filled then
  336. local idx = core.get_table_index("servers")
  337. local server = idx and tabdata.lookup[idx]
  338. if server and (server.address ~= fields.te_address or server.port ~= te_port_number) then
  339. server = nil
  340. end
  341. if server and not is_server_protocol_compat_or_error(
  342. server.proto_min, server.proto_max) then
  343. return true
  344. end
  345. local dlg = create_register_dialog(fields.te_address, te_port_number, server)
  346. dlg:set_parent(tabview)
  347. tabview:hide()
  348. dlg:show()
  349. return true
  350. end
  351. return false
  352. end
  353. local function on_change(type)
  354. if type == "ENTER" then
  355. mm_game_theme.set_engine()
  356. serverlistmgr.sync()
  357. end
  358. end
  359. return {
  360. name = "online",
  361. caption = fgettext("Join Game"),
  362. cbf_formspec = get_formspec,
  363. cbf_button_handler = main_button_handler,
  364. on_change = on_change
  365. }