tab_online.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. --------------------------------------------------------------------------------
  18. local function get_formspec(tabview, name, tabdata)
  19. -- Update the cached supported proto info,
  20. -- it may have changed after a change by the settings menu.
  21. common_update_cached_supp_proto()
  22. local selected
  23. if menudata.search_result then
  24. selected = menudata.search_result[tabdata.selected]
  25. else
  26. selected = serverlistmgr.servers[tabdata.selected]
  27. end
  28. if not tabdata.search_for then
  29. tabdata.search_for = ""
  30. end
  31. local retval =
  32. -- Search
  33. "field[0.15,0.075;5.91,1;te_search;;" .. core.formspec_escape(tabdata.search_for) .. "]" ..
  34. "image_button[5.63,-.165;.83,.83;" .. core.formspec_escape(defaulttexturedir .. "search.png") .. ";btn_mp_search;]" ..
  35. "image_button[6.3,-.165;.83,.83;" .. core.formspec_escape(defaulttexturedir .. "clear.png") .. ";btn_mp_clear;]" ..
  36. "image_button[6.97,-.165;.83,.83;" .. core.formspec_escape(defaulttexturedir .. "refresh.png")
  37. .. ";btn_mp_refresh;]" ..
  38. -- Address / Port
  39. "label[7.75,-0.25;" .. fgettext("Address / Port") .. "]" ..
  40. "field[8,0.65;3.25,0.5;te_address;;" ..
  41. core.formspec_escape(core.settings:get("address")) .. "]" ..
  42. "field[11.1,0.65;1.4,0.5;te_port;;" ..
  43. core.formspec_escape(core.settings:get("remote_port")) .. "]" ..
  44. -- Name / Password
  45. "label[7.75,0.95;" .. fgettext("Name / Password") .. "]" ..
  46. "field[8,1.85;2.9,0.5;te_name;;" ..
  47. core.formspec_escape(core.settings:get("name")) .. "]" ..
  48. "pwdfield[10.73,1.85;1.77,0.5;te_pwd;]" ..
  49. -- Description Background
  50. "box[7.73,2.25;4.25,2.6;#999999]"..
  51. -- Connect
  52. "button[9.88,4.9;2.3,1;btn_mp_connect;" .. fgettext("Connect") .. "]"
  53. if tabdata.selected and selected then
  54. if gamedata.fav then
  55. retval = retval .. "button[7.73,4.9;2.3,1;btn_delete_favorite;" ..
  56. fgettext("Del. Favorite") .. "]"
  57. end
  58. if selected.description then
  59. retval = retval .. "textarea[8.1,2.3;4.23,2.9;;;" ..
  60. core.formspec_escape((gamedata.serverdescription or ""), true) .. "]"
  61. end
  62. end
  63. --favorites
  64. retval = retval .. "tablecolumns[" ..
  65. image_column(fgettext("Favorite"), "favorite") .. ";" ..
  66. image_column(fgettext("Ping")) .. ",padding=0.25;" ..
  67. "color,span=3;" ..
  68. "text,align=right;" .. -- clients
  69. "text,align=center,padding=0.25;" .. -- "/"
  70. "text,align=right,padding=0.25;" .. -- clients_max
  71. image_column(fgettext("Creative mode"), "creative") .. ",padding=1;" ..
  72. image_column(fgettext("Damage enabled"), "damage") .. ",padding=0.25;" ..
  73. --~ PvP = Player versus Player
  74. image_column(fgettext("PvP enabled"), "pvp") .. ",padding=0.25;" ..
  75. "color,span=1;" ..
  76. "text,padding=1]" ..
  77. "table[-0.15,0.6;7.75,5.15;favorites;"
  78. if menudata.search_result then
  79. local favs = serverlistmgr.get_favorites()
  80. for i = 1, #menudata.search_result do
  81. local server = menudata.search_result[i]
  82. for fav_id = 1, #favs do
  83. if server.address == favs[fav_id].address and
  84. server.port == favs[fav_id].port then
  85. server.is_favorite = true
  86. end
  87. end
  88. if i ~= 1 then
  89. retval = retval .. ","
  90. end
  91. retval = retval .. render_serverlist_row(server, server.is_favorite)
  92. end
  93. elseif #serverlistmgr.servers > 0 then
  94. local favs = serverlistmgr.get_favorites()
  95. if #favs > 0 then
  96. for i = 1, #favs do
  97. for j = 1, #serverlistmgr.servers do
  98. if serverlistmgr.servers[j].address == favs[i].address and
  99. serverlistmgr.servers[j].port == favs[i].port then
  100. table.insert(serverlistmgr.servers, i, table.remove(serverlistmgr.servers, j))
  101. end
  102. end
  103. if favs[i].address ~= serverlistmgr.servers[i].address then
  104. table.insert(serverlistmgr.servers, i, favs[i])
  105. end
  106. end
  107. end
  108. retval = retval .. render_serverlist_row(serverlistmgr.servers[1], (#favs > 0))
  109. for i = 2, #serverlistmgr.servers do
  110. retval = retval .. "," .. render_serverlist_row(serverlistmgr.servers[i], (i <= #favs))
  111. end
  112. end
  113. if tabdata.selected then
  114. retval = retval .. ";" .. tabdata.selected .. "]"
  115. else
  116. retval = retval .. ";0]"
  117. end
  118. return retval
  119. end
  120. --------------------------------------------------------------------------------
  121. local function main_button_handler(tabview, fields, name, tabdata)
  122. local serverlist = menudata.search_result or serverlistmgr.servers
  123. if fields.te_name then
  124. gamedata.playername = fields.te_name
  125. core.settings:set("name", fields.te_name)
  126. end
  127. if fields.favorites then
  128. local event = core.explode_table_event(fields.favorites)
  129. local fav = serverlist[event.row]
  130. if event.type == "DCL" then
  131. if event.row <= #serverlist then
  132. if not is_server_protocol_compat_or_error(
  133. fav.proto_min, fav.proto_max) then
  134. return true
  135. end
  136. gamedata.address = fav.address
  137. gamedata.port = fav.port
  138. gamedata.playername = fields.te_name
  139. gamedata.selected_world = 0
  140. if fields.te_pwd then
  141. gamedata.password = fields.te_pwd
  142. end
  143. gamedata.servername = fav.name
  144. gamedata.serverdescription = fav.description
  145. if gamedata.address and gamedata.port then
  146. core.settings:set("address", gamedata.address)
  147. core.settings:set("remote_port", gamedata.port)
  148. core.start()
  149. end
  150. end
  151. return true
  152. end
  153. if event.type == "CHG" then
  154. if event.row <= #serverlist then
  155. gamedata.fav = false
  156. local favs = serverlistmgr.get_favorites()
  157. local address = fav.address
  158. local port = fav.port
  159. gamedata.serverdescription = fav.description
  160. for i = 1, #favs do
  161. if fav.address == favs[i].address and
  162. fav.port == favs[i].port then
  163. gamedata.fav = true
  164. end
  165. end
  166. if address and port then
  167. core.settings:set("address", address)
  168. core.settings:set("remote_port", port)
  169. end
  170. tabdata.selected = event.row
  171. end
  172. return true
  173. end
  174. end
  175. if fields.key_up or fields.key_down then
  176. local fav_idx = core.get_table_index("favorites")
  177. local fav = serverlist[fav_idx]
  178. if fav_idx then
  179. if fields.key_up and fav_idx > 1 then
  180. fav_idx = fav_idx - 1
  181. elseif fields.key_down and fav_idx < #serverlistmgr.servers then
  182. fav_idx = fav_idx + 1
  183. end
  184. else
  185. fav_idx = 1
  186. end
  187. if not serverlistmgr.servers or not fav then
  188. tabdata.selected = 0
  189. return true
  190. end
  191. local address = fav.address
  192. local port = fav.port
  193. gamedata.serverdescription = fav.description
  194. if address and port then
  195. core.settings:set("address", address)
  196. core.settings:set("remote_port", port)
  197. end
  198. tabdata.selected = fav_idx
  199. return true
  200. end
  201. if fields.btn_delete_favorite then
  202. local current_favorite = core.get_table_index("favorites")
  203. if not current_favorite then return end
  204. serverlistmgr.delete_favorite(serverlistmgr.servers[current_favorite])
  205. serverlistmgr.sync()
  206. tabdata.selected = nil
  207. core.settings:set("address", "")
  208. core.settings:set("remote_port", "30000")
  209. return true
  210. end
  211. if fields.btn_mp_clear then
  212. tabdata.search_for = ""
  213. menudata.search_result = nil
  214. return true
  215. end
  216. if fields.btn_mp_search or fields.key_enter_field == "te_search" then
  217. tabdata.selected = 1
  218. local input = fields.te_search:lower()
  219. tabdata.search_for = fields.te_search
  220. if #serverlistmgr.servers < 2 then
  221. return true
  222. end
  223. menudata.search_result = {}
  224. -- setup the keyword list
  225. local keywords = {}
  226. for word in input:gmatch("%S+") do
  227. word = word:gsub("(%W)", "%%%1")
  228. table.insert(keywords, word)
  229. end
  230. if #keywords == 0 then
  231. menudata.search_result = nil
  232. return true
  233. end
  234. -- Search the serverlist
  235. local search_result = {}
  236. for i = 1, #serverlistmgr.servers do
  237. local server = serverlistmgr.servers[i]
  238. local found = 0
  239. for k = 1, #keywords do
  240. local keyword = keywords[k]
  241. if server.name then
  242. local sername = server.name:lower()
  243. local _, count = sername:gsub(keyword, keyword)
  244. found = found + count * 4
  245. end
  246. if server.description then
  247. local desc = server.description:lower()
  248. local _, count = desc:gsub(keyword, keyword)
  249. found = found + count * 2
  250. end
  251. end
  252. if found > 0 then
  253. local points = (#serverlistmgr.servers - i) / 5 + found
  254. server.points = points
  255. table.insert(search_result, server)
  256. end
  257. end
  258. if #search_result > 0 then
  259. table.sort(search_result, function(a, b)
  260. return a.points > b.points
  261. end)
  262. menudata.search_result = search_result
  263. local first_server = search_result[1]
  264. core.settings:set("address", first_server.address)
  265. core.settings:set("remote_port", first_server.port)
  266. gamedata.serverdescription = first_server.description
  267. end
  268. return true
  269. end
  270. if fields.btn_mp_refresh then
  271. serverlistmgr.sync()
  272. return true
  273. end
  274. if (fields.btn_mp_connect or fields.key_enter)
  275. and fields.te_address ~= "" and fields.te_port then
  276. gamedata.playername = fields.te_name
  277. gamedata.password = fields.te_pwd
  278. gamedata.address = fields.te_address
  279. gamedata.port = tonumber(fields.te_port)
  280. gamedata.selected_world = 0
  281. local fav_idx = core.get_table_index("favorites")
  282. local fav = serverlist[fav_idx]
  283. if fav_idx and fav_idx <= #serverlist and
  284. fav.address == gamedata.address and
  285. fav.port == gamedata.port then
  286. serverlistmgr.add_favorite(fav)
  287. gamedata.servername = fav.name
  288. gamedata.serverdescription = fav.description
  289. if not is_server_protocol_compat_or_error(
  290. fav.proto_min, fav.proto_max) then
  291. return true
  292. end
  293. else
  294. gamedata.servername = ""
  295. gamedata.serverdescription = ""
  296. serverlistmgr.add_favorite({
  297. address = gamedata.address,
  298. port = gamedata.port,
  299. })
  300. end
  301. core.settings:set("address", gamedata.address)
  302. core.settings:set("remote_port", gamedata.port)
  303. core.start()
  304. return true
  305. end
  306. return false
  307. end
  308. local function on_change(type, old_tab, new_tab)
  309. if type == "LEAVE" then return end
  310. serverlistmgr.sync()
  311. end
  312. --------------------------------------------------------------------------------
  313. return {
  314. name = "online",
  315. caption = fgettext("Join Game"),
  316. cbf_formspec = get_formspec,
  317. cbf_button_handler = main_button_handler,
  318. on_change = on_change
  319. }