2 --Copyright (C) 2022 rubenwardy
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.
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.
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.
18 --------------------------------------------------------------------------------
20 local function register_formspec(dialogdata)
21 local title = fgettext("Joining $1", dialogdata.server and dialogdata.server.name or dialogdata.address)
22 local buttons_y = 4 + 1.3
23 if dialogdata.error then
24 buttons_y = buttons_y + 0.8
28 "formspec_version[4]",
29 "size[8,", tostring(buttons_y + 1.175), "]",
30 "set_focus[", (dialogdata.name ~= "" and "password" or "name"), "]",
31 "label[0.375,0.8;", title, "]",
32 "field[0.375,1.575;7.25,0.8;name;", core.formspec_escape(fgettext("Name")), ";",
33 core.formspec_escape(dialogdata.name), "]",
34 "pwdfield[0.375,2.875;7.25,0.8;password;", core.formspec_escape(fgettext("Password")), "]",
35 "pwdfield[0.375,4.175;7.25,0.8;password_2;", core.formspec_escape(fgettext("Confirm Password")), "]"
38 if dialogdata.error then
39 table.insert_all(retval, {
40 "box[0.375,", tostring(buttons_y - 0.9), ";7.25,0.6;darkred]",
41 "label[0.625,", tostring(buttons_y - 0.6), ";", core.formspec_escape(dialogdata.error), "]",
45 table.insert_all(retval, {
46 "container[0.375,", tostring(buttons_y), "]",
47 "button[0,0;2.5,0.8;dlg_register_confirm;", fgettext("Register"), "]",
48 "button[4.75,0;2.5,0.8;dlg_register_cancel;", fgettext("Cancel"), "]",
52 return table.concat(retval, "")
55 --------------------------------------------------------------------------------
56 local function register_buttonhandler(this, fields)
57 this.data.name = fields.name
60 if fields.dlg_register_confirm or fields.key_enter then
61 if fields.name == "" then
62 this.data.error = fgettext("Missing name")
65 if fields.password ~= fields.password_2 then
66 this.data.error = fgettext("Passwords do not match")
70 gamedata.playername = fields.name
71 gamedata.password = fields.password
72 gamedata.address = this.data.address
73 gamedata.port = this.data.port
74 gamedata.allow_login_or_register = "register"
75 gamedata.selected_world = 0
77 assert(gamedata.address and gamedata.port)
79 local server = this.data.server
81 serverlistmgr.add_favorite(server)
82 gamedata.servername = server.name
83 gamedata.serverdescription = server.description
85 gamedata.servername = ""
86 gamedata.serverdescription = ""
88 serverlistmgr.add_favorite({
89 address = gamedata.address,
94 core.settings:set("name", fields.name)
95 core.settings:set("address", gamedata.address)
96 core.settings:set("remote_port", gamedata.port)
101 if fields["dlg_register_cancel"] then
109 --------------------------------------------------------------------------------
110 function create_register_dialog(address, port, server)
112 assert(type(port) == "number")
114 local retval = dialog_create("dlg_register",
116 register_buttonhandler,
118 retval.data.address = address
119 retval.data.port = port
120 retval.data.server = server
121 retval.data.name = core.settings:get("name") or ""