2
0

dlg_register.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. --Minetest
  2. --Copyright (C) 2022 rubenwardy
  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 register_formspec(dialogdata)
  19. local title = fgettext("Joining $1", dialogdata.server and dialogdata.server.name or dialogdata.address)
  20. local buttons_y = 4 + 1.3
  21. if dialogdata.error then
  22. buttons_y = buttons_y + 0.8
  23. end
  24. local retval = {
  25. "formspec_version[4]",
  26. "size[8,", tostring(buttons_y + 1.175), "]",
  27. "set_focus[", (dialogdata.name ~= "" and "password" or "name"), "]",
  28. "label[0.375,0.8;", title, "]",
  29. "field[0.375,1.575;7.25,0.8;name;", core.formspec_escape(fgettext("Name")), ";",
  30. core.formspec_escape(dialogdata.name), "]",
  31. "pwdfield[0.375,2.875;7.25,0.8;password;", core.formspec_escape(fgettext("Password")), "]",
  32. "pwdfield[0.375,4.175;7.25,0.8;password_2;", core.formspec_escape(fgettext("Confirm Password")), "]"
  33. }
  34. if dialogdata.error then
  35. table.insert_all(retval, {
  36. "box[0.375,", tostring(buttons_y - 0.9), ";7.25,0.6;darkred]",
  37. "label[0.625,", tostring(buttons_y - 0.6), ";", core.formspec_escape(dialogdata.error), "]",
  38. })
  39. end
  40. table.insert_all(retval, {
  41. "container[0.375,", tostring(buttons_y), "]",
  42. "button[0,0;2.5,0.8;dlg_register_confirm;", fgettext("Register"), "]",
  43. "button[4.75,0;2.5,0.8;dlg_register_cancel;", fgettext("Cancel"), "]",
  44. "container_end[]",
  45. })
  46. return table.concat(retval, "")
  47. end
  48. --------------------------------------------------------------------------------
  49. local function register_buttonhandler(this, fields)
  50. this.data.name = fields.name
  51. this.data.error = nil
  52. if fields.dlg_register_confirm or fields.key_enter then
  53. if fields.name == "" then
  54. this.data.error = fgettext("Missing name")
  55. return true
  56. end
  57. if fields.password ~= fields.password_2 then
  58. this.data.error = fgettext("Passwords do not match")
  59. return true
  60. end
  61. gamedata.playername = fields.name
  62. gamedata.password = fields.password
  63. gamedata.address = this.data.address
  64. gamedata.port = this.data.port
  65. gamedata.allow_login_or_register = "register"
  66. gamedata.selected_world = 0
  67. assert(gamedata.address and gamedata.port)
  68. local server = this.data.server
  69. if server then
  70. serverlistmgr.add_favorite(server)
  71. gamedata.servername = server.name
  72. gamedata.serverdescription = server.description
  73. else
  74. gamedata.servername = ""
  75. gamedata.serverdescription = ""
  76. serverlistmgr.add_favorite({
  77. address = gamedata.address,
  78. port = gamedata.port,
  79. })
  80. end
  81. core.settings:set("name", fields.name)
  82. core.settings:set("address", gamedata.address)
  83. core.settings:set("remote_port", gamedata.port)
  84. core.start()
  85. end
  86. if fields["dlg_register_cancel"] then
  87. this:delete()
  88. return true
  89. end
  90. return false
  91. end
  92. --------------------------------------------------------------------------------
  93. function create_register_dialog(address, port, server)
  94. assert(address)
  95. assert(type(port) == "number")
  96. local retval = dialog_create("dlg_register",
  97. register_formspec,
  98. register_buttonhandler,
  99. nil)
  100. retval.data.address = address
  101. retval.data.port = port
  102. retval.data.server = server
  103. retval.data.name = core.settings:get("name") or ""
  104. return retval
  105. end