Mainmenu: Unify favorite servers with main serverlist
[oweals/minetest.git] / builtin / mainmenu / tab_multiplayer.lua
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 --------------------------------------------------------------------------------
19 local function get_formspec(tabview, name, tabdata)
20         -- Update the cached supported proto info,
21         -- it may have changed after a change by the settings menu.
22         common_update_cached_supp_proto()
23         local fav_selected = menudata.favorites[tabdata.fav_selected]
24
25         local retval =
26                 "label[7.75,-0.15;" .. fgettext("Address / Port") .. "]" ..
27                 "label[7.75,1.05;" .. fgettext("Name / Password") .. "]" ..
28                 "field[8,0.75;3.3,0.5;te_address;;" ..
29                         core.formspec_escape(core.setting_get("address")) .. "]" ..
30                 "field[11.15,0.75;1.4,0.5;te_port;;" ..
31                         core.formspec_escape(core.setting_get("remote_port")) .. "]" ..
32                 "button[10.1,4.9;2,0.5;btn_mp_connect;" .. fgettext("Connect") .. "]" ..
33                 "field[8,1.95;2.95,0.5;te_name;;" ..
34                         core.formspec_escape(core.setting_get("name")) .. "]" ..
35                 "pwdfield[10.78,1.95;1.77,0.5;te_pwd;]" ..
36                 "box[7.73,2.35;4.3,2.28;#999999]"
37
38         if tabdata.fav_selected and fav_selected then
39                 if gamedata.fav then
40                         retval = retval .. "button[7.85,4.9;2.3,0.5;btn_delete_favorite;" ..
41                                 fgettext("Del. Favorite") .. "]"
42                 end
43                 if fav_selected.description then
44                         retval = retval .. "textarea[8.1,2.4;4.26,2.6;;" ..
45                                 core.formspec_escape((gamedata.serverdescription or ""), true) .. ";]"
46                 end
47         end
48
49         --favourites
50         retval = retval .. "tablecolumns[" ..
51                 image_column(fgettext("Favorite"), "favorite") .. ";" ..
52                 "color,span=3;" ..
53                 "text,align=right;" ..                -- clients
54                 "text,align=center,padding=0.25;" ..  -- "/"
55                 "text,align=right,padding=0.25;" ..   -- clients_max
56                 image_column(fgettext("Creative mode"), "creative") .. ",padding=1;" ..
57                 image_column(fgettext("Damage enabled"), "damage") .. ",padding=0.25;" ..
58                 image_column(fgettext("PvP enabled"), "pvp") .. ",padding=0.25;" ..
59                 "color,span=1;" ..
60                 "text,padding=1]" ..
61                 "table[-0.15,-0.1;7.75,5.5;favourites;"
62
63         if #menudata.favorites > 0 then
64                 local favs = core.get_favorites("local")
65                 if #favs > 0 then
66                         for i = 1, #favs do
67                         for j = 1, #menudata.favorites do
68                                 if menudata.favorites[j].address == favs[i].address and
69                                                 menudata.favorites[j].port == favs[i].port then
70                                         table.insert(menudata.favorites, i, table.remove(menudata.favorites, j))
71                                 end
72                         end
73                                 if favs[i].address ~= menudata.favorites[i].address then
74                                         table.insert(menudata.favorites, i, favs[i])
75                                 end
76                         end
77                 end
78                 retval = retval .. render_favorite(menudata.favorites[1], (#favs > 0))
79                 for i = 2, #menudata.favorites do
80                         retval = retval .. "," .. render_favorite(menudata.favorites[i], (i <= #favs))
81                 end
82         end
83
84         if tabdata.fav_selected ~= nil then
85                 retval = retval .. ";" .. tabdata.fav_selected .. "]"
86         else
87                 retval = retval .. ";0]"
88         end
89
90         return retval
91 end
92
93 --------------------------------------------------------------------------------
94 local function main_button_handler(tabview, fields, name, tabdata)
95         if fields["te_name"] ~= nil then
96                 gamedata.playername = fields["te_name"]
97                 core.setting_set("name", fields["te_name"])
98         end
99
100         if fields["favourites"] ~= nil then
101                 local event = core.explode_table_event(fields["favourites"])
102                 local fav = menudata.favorites[event.row]
103
104                 if event.type == "DCL" then
105                         if event.row <= #menudata.favorites then
106                                 if menudata.favorites_is_public and
107                                                 not is_server_protocol_compat_or_error(
108                                                         fav.proto_min, fav.proto_max) then
109                                         return true
110                                 end
111                                 gamedata.address    = fav.address
112                                 gamedata.port       = fav.port
113                                 gamedata.playername = fields["te_name"]
114                                 if fields["te_pwd"] ~= nil then
115                                         gamedata.password               = fields["te_pwd"]
116                                 end
117                                 gamedata.selected_world = 0
118
119                                 gamedata.servername        = fav.name
120                                 gamedata.serverdescription = fav.description
121
122                                 if gamedata.address ~= nil and
123                                         gamedata.port ~= nil then
124                                         core.setting_set("address",gamedata.address)
125                                         core.setting_set("remote_port",gamedata.port)
126                                         core.start()
127                                 end
128                         end
129                         return true
130                 end
131
132                 if event.type == "CHG" then
133                         if event.row <= #menudata.favorites then
134                                 gamedata.fav = false
135                                 local favs = core.get_favorites("local")
136                                 local address = fav.address
137                                 local port    = fav.port
138                                 gamedata.serverdescription = fav.description
139
140                                 for i = 1, #favs do
141                                         if fav.address == favs[i].address and
142                                                         fav.port == favs[i].port then
143                                                 gamedata.fav = true
144                                         end
145                                 end
146
147                                 if address ~= nil and
148                                         port ~= nil then
149                                         core.setting_set("address",address)
150                                         core.setting_set("remote_port",port)
151                                 end
152
153                                 tabdata.fav_selected = event.row
154                         end
155                         
156                         return true
157                 end
158         end
159
160         if fields["key_up"] ~= nil or
161                 fields["key_down"] ~= nil then
162
163                 local fav_idx = core.get_table_index("favourites")
164
165                 if fav_idx ~= nil then
166                         if fields["key_up"] ~= nil and fav_idx > 1 then
167                                 fav_idx = fav_idx -1
168                         else if fields["key_down"] and fav_idx < #menudata.favorites then
169                                 fav_idx = fav_idx +1
170                         end end
171                 else
172                         fav_idx = 1
173                 end
174                 
175                 if menudata.favorites == nil or
176                         menudata.favorites[fav_idx] == nil then
177                         tabdata.fav_selected = 0
178                         return true
179                 end
180         
181                 local address = menudata.favorites[fav_idx].address
182                 local port    = menudata.favorites[fav_idx].port
183
184                 if address ~= nil and
185                         port ~= nil then
186                         core.setting_set("address",address)
187                         core.setting_set("remote_port",port)
188                 end
189
190                 tabdata.fav_selected = fav_idx
191                 return true
192         end
193
194         if fields.btn_delete_favorite then
195                 local current_favourite = core.get_table_index("favourites")
196                 if not current_favourite then return end
197
198                 core.delete_favorite(current_favourite)
199                 asyncOnlineFavourites()
200                 tabdata.fav_selected = nil
201
202                 core.setting_set("address", "")
203                 core.setting_set("remote_port", "30000")
204                 return true
205         end
206
207         if (fields["btn_mp_connect"] ~= nil or
208                 fields["key_enter"] ~= nil) and fields["te_address"] ~= nil and
209                 fields["te_port"] ~= nil then
210
211                 gamedata.playername     = fields["te_name"]
212                 gamedata.password       = fields["te_pwd"]
213                 gamedata.address        = fields["te_address"]
214                 gamedata.port           = fields["te_port"]
215
216                 local fav_idx = core.get_table_index("favourites")
217
218                 if fav_idx ~= nil and fav_idx <= #menudata.favorites and
219                         menudata.favorites[fav_idx].address == fields["te_address"] and
220                         menudata.favorites[fav_idx].port    == fields["te_port"] then
221
222                         local fav = menudata.favorites[fav_idx]
223                         gamedata.servername        = fav.name
224                         gamedata.serverdescription = fav.description
225
226                         if menudata.favorites_is_public and
227                                         not is_server_protocol_compat_or_error(
228                                                 fav.proto_min, fav.proto_max) then
229                                 return true
230                         end
231                 else
232                         gamedata.servername        = ""
233                         gamedata.serverdescription = ""
234                 end
235
236                 gamedata.selected_world = 0
237
238                 core.setting_set("address",    fields["te_address"])
239                 core.setting_set("remote_port",fields["te_port"])
240
241                 core.start()
242                 return true
243         end
244         return false
245 end
246
247 local function on_change(type, old_tab, new_tab)
248         if type == "LEAVE" then return end
249         asyncOnlineFavourites()
250 end
251
252 --------------------------------------------------------------------------------
253 return {
254         name = "multiplayer",
255         caption = fgettext("Client"),
256         cbf_formspec = get_formspec,
257         cbf_button_handler = main_button_handler,
258         on_change = on_change
259 }