tab_online.lua 10.0 KB

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