2
0

tab_multiplayer.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. local render_details = core.is_yes(core.setting_getbool("public_serverlist"))
  20. local retval =
  21. "label[7.75,-0.15;" .. fgettext("Address / Port :") .. "]" ..
  22. "label[7.75,1.05;" .. fgettext("Name / Password :") .. "]" ..
  23. "field[8,0.75;3.4,0.5;te_address;;" ..
  24. core.formspec_escape(core.setting_get("address")) .. "]" ..
  25. "field[11.25,0.75;1.3,0.5;te_port;;" ..
  26. core.formspec_escape(core.setting_get("remote_port")) .. "]" ..
  27. "checkbox[0,4.85;cb_public_serverlist;" .. fgettext("Public Serverlist") .. ";" ..
  28. dump(core.setting_getbool("public_serverlist")) .. "]"
  29. if not core.setting_getbool("public_serverlist") then
  30. retval = retval ..
  31. "button[8,4.9;2,0.5;btn_delete_favorite;" .. fgettext("Delete") .. "]"
  32. end
  33. retval = retval ..
  34. "button[10,4.9;2,0.5;btn_mp_connect;" .. fgettext("Connect") .. "]" ..
  35. "field[8,1.95;2.95,0.5;te_name;;" ..
  36. core.formspec_escape(core.setting_get("name")) .. "]" ..
  37. "pwdfield[10.78,1.95;1.77,0.5;te_pwd;]" ..
  38. "box[7.73,2.35;4.3,2.28;#999999]" ..
  39. "textarea[8.1,2.4;4.26,2.6;;"
  40. if tabdata.fav_selected ~= nil and
  41. menudata.favorites[tabdata.fav_selected] ~= nil and
  42. menudata.favorites[tabdata.fav_selected].description ~= nil then
  43. retval = retval ..
  44. core.formspec_escape(menudata.favorites[tabdata.fav_selected].description,true)
  45. end
  46. retval = retval ..
  47. ";]"
  48. --favourites
  49. if render_details then
  50. retval = retval .. "tablecolumns[" ..
  51. "color,span=3;" ..
  52. "text,align=right;" .. -- clients
  53. "text,align=center,padding=0.25;" .. -- "/"
  54. "text,align=right,padding=0.25;" .. -- clients_max
  55. image_column(fgettext("Creative mode"), "creative") .. ",padding=1;" ..
  56. image_column(fgettext("Damage enabled"), "damage") .. ",padding=0.25;" ..
  57. image_column(fgettext("PvP enabled"), "pvp") .. ",padding=0.25;" ..
  58. "color,span=1;" ..
  59. "text,padding=1]" -- name
  60. else
  61. retval = retval .. "tablecolumns[text]"
  62. end
  63. retval = retval ..
  64. "table[-0.15,-0.1;7.75,5;favourites;"
  65. if #menudata.favorites > 0 then
  66. retval = retval .. render_favorite(menudata.favorites[1],render_details)
  67. for i=2,#menudata.favorites,1 do
  68. retval = retval .. "," .. render_favorite(menudata.favorites[i],render_details)
  69. end
  70. end
  71. if tabdata.fav_selected ~= nil then
  72. retval = retval .. ";" .. tabdata.fav_selected .. "]"
  73. else
  74. retval = retval .. ";0]"
  75. end
  76. return retval
  77. end
  78. --------------------------------------------------------------------------------
  79. local function main_button_handler(tabview, fields, name, tabdata)
  80. if fields["te_name"] ~= nil then
  81. gamedata.playername = fields["te_name"]
  82. core.setting_set("name", fields["te_name"])
  83. end
  84. if fields["favourites"] ~= nil then
  85. local event = core.explode_table_event(fields["favourites"])
  86. if event.type == "DCL" then
  87. if event.row <= #menudata.favorites then
  88. if not is_server_protocol_compat_or_error(menudata.favorites[event.row].proto_min,
  89. menudata.favorites[event.row].proto_max) then
  90. return true
  91. end
  92. gamedata.address = menudata.favorites[event.row].address
  93. gamedata.port = menudata.favorites[event.row].port
  94. gamedata.playername = fields["te_name"]
  95. if fields["te_pwd"] ~= nil then
  96. gamedata.password = fields["te_pwd"]
  97. end
  98. gamedata.selected_world = 0
  99. if menudata.favorites ~= nil then
  100. gamedata.servername = menudata.favorites[event.row].name
  101. gamedata.serverdescription = menudata.favorites[event.row].description
  102. end
  103. if gamedata.address ~= nil and
  104. gamedata.port ~= nil then
  105. core.setting_set("address",gamedata.address)
  106. core.setting_set("remote_port",gamedata.port)
  107. core.start()
  108. end
  109. end
  110. return true
  111. end
  112. if event.type == "CHG" then
  113. if event.row <= #menudata.favorites then
  114. local address = menudata.favorites[event.row].address
  115. local port = menudata.favorites[event.row].port
  116. if address ~= nil and
  117. port ~= nil then
  118. core.setting_set("address",address)
  119. core.setting_set("remote_port",port)
  120. end
  121. tabdata.fav_selected = event.row
  122. end
  123. return true
  124. end
  125. end
  126. if fields["key_up"] ~= nil or
  127. fields["key_down"] ~= nil then
  128. local fav_idx = core.get_table_index("favourites")
  129. if fav_idx ~= nil then
  130. if fields["key_up"] ~= nil and fav_idx > 1 then
  131. fav_idx = fav_idx -1
  132. else if fields["key_down"] and fav_idx < #menudata.favorites then
  133. fav_idx = fav_idx +1
  134. end end
  135. else
  136. fav_idx = 1
  137. end
  138. if menudata.favorites == nil or
  139. menudata.favorites[fav_idx] == nil then
  140. tabdata.fav_selected = 0
  141. return true
  142. end
  143. local address = menudata.favorites[fav_idx].address
  144. local port = menudata.favorites[fav_idx].port
  145. if address ~= nil and
  146. port ~= nil then
  147. core.setting_set("address",address)
  148. core.setting_set("remote_port",port)
  149. end
  150. tabdata.fav_selected = fav_idx
  151. return true
  152. end
  153. if fields["cb_public_serverlist"] ~= nil then
  154. core.setting_set("public_serverlist", fields["cb_public_serverlist"])
  155. if core.setting_getbool("public_serverlist") then
  156. asyncOnlineFavourites()
  157. else
  158. menudata.favorites = core.get_favorites("local")
  159. end
  160. tabdata.fav_selected = nil
  161. return true
  162. end
  163. if fields["btn_delete_favorite"] ~= nil then
  164. local current_favourite = core.get_table_index("favourites")
  165. if current_favourite == nil then return end
  166. core.delete_favorite(current_favourite)
  167. menudata.favorites = order_favorite_list(core.get_favorites())
  168. tabdata.fav_selected = nil
  169. core.setting_set("address","")
  170. core.setting_set("remote_port","30000")
  171. return true
  172. end
  173. if (fields["btn_mp_connect"] ~= nil or
  174. fields["key_enter"] ~= nil) and fields["te_address"] ~= nil and
  175. fields["te_port"] ~= nil then
  176. gamedata.playername = fields["te_name"]
  177. gamedata.password = fields["te_pwd"]
  178. gamedata.address = fields["te_address"]
  179. gamedata.port = fields["te_port"]
  180. local fav_idx = core.get_table_index("favourites")
  181. if fav_idx ~= nil and fav_idx <= #menudata.favorites and
  182. menudata.favorites[fav_idx].address == fields["te_address"] and
  183. menudata.favorites[fav_idx].port == fields["te_port"] then
  184. gamedata.servername = menudata.favorites[fav_idx].name
  185. gamedata.serverdescription = menudata.favorites[fav_idx].description
  186. if not is_server_protocol_compat_or_error(menudata.favorites[fav_idx].proto_min,
  187. menudata.favorites[fav_idx].proto_max)then
  188. return true
  189. end
  190. else
  191. gamedata.servername = ""
  192. gamedata.serverdescription = ""
  193. end
  194. gamedata.selected_world = 0
  195. core.setting_set("address", fields["te_address"])
  196. core.setting_set("remote_port",fields["te_port"])
  197. core.start()
  198. return true
  199. end
  200. return false
  201. end
  202. local function on_change(type,old_tab,new_tab)
  203. if type == "LEAVE" then
  204. return
  205. end
  206. if core.setting_getbool("public_serverlist") then
  207. asyncOnlineFavourites()
  208. else
  209. menudata.favorites = core.get_favorites("local")
  210. end
  211. end
  212. --------------------------------------------------------------------------------
  213. tab_multiplayer = {
  214. name = "multiplayer",
  215. caption = fgettext("Client"),
  216. cbf_formspec = get_formspec,
  217. cbf_button_handler = main_button_handler,
  218. on_change = on_change
  219. }