diff --git a/main.go b/main.go index 3013511..b0df47a 100644 --- a/main.go +++ b/main.go @@ -142,7 +142,14 @@ func main() { c.Session().Set("user_id", user.ID) c.Session().Set("username", user.Username) c.Session().Set("nickname", user.Username) - c.Redirect("/") + + returnURL := c.Session().GetString("return_url") + if returnURL != "" { + c.Session().Set("return_url", "") + c.Redirect(returnURL) + } else { + c.Redirect("/") + } }) c.View(func() h.H { @@ -203,7 +210,14 @@ func main() { c.Session().Set("user_id", user.ID) c.Session().Set("username", user.Username) c.Session().Set("nickname", user.Username) - c.Redirect("/") + + returnURL := c.Session().GetString("return_url") + if returnURL != "" { + c.Session().Set("return_url", "") + c.Redirect(returnURL) + } else { + c.Redirect("/") + } }) c.View(func() h.H { @@ -226,6 +240,22 @@ func main() { nickname := c.Signal(sessionNickname) colSignal := c.Signal(0) + showGuestPrompt := c.Signal(false) + + goToLogin := c.Action(func() { + c.Session().Set("return_url", "/game/"+gameID) + c.Redirect("/login") + }) + + goToRegister := c.Action(func() { + c.Session().Set("return_url", "/game/"+gameID) + c.Redirect("/register") + }) + + continueAsGuest := c.Action(func() { + showGuestPrompt.SetValue(true) + c.Sync() + }) var gi *game.GameInstance var gameExists bool @@ -326,6 +356,14 @@ func main() { // Need nickname first / not joined yet if myColor == 0 { + // Unauthenticated user who hasn't chosen to continue as guest + if sessionUserID == "" && !showGuestPrompt.Bool() { + return ui.GameJoinPrompt( + goToLogin.OnClick(), + continueAsGuest.OnClick(), + goToRegister.OnClick(), + ) + } return ui.NicknamePrompt( nickname.Bind(), setNickname.OnKeyDown("Enter"), @@ -643,4 +681,16 @@ const gameCSS = ` font-size: 0.75rem; color: var(--pico-muted-color); } + + .join-options { + display: flex; + flex-direction: column; + gap: 0.5rem; + margin: 1rem 0; + } + + .register-link { + font-size: 0.875rem; + color: var(--pico-muted-color); + } ` diff --git a/ui/lobby.go b/ui/lobby.go index 8f121f5..c7bf904 100644 --- a/ui/lobby.go +++ b/ui/lobby.go @@ -67,3 +67,29 @@ func NicknamePrompt(nicknameBind, setNicknameKeyDown, setNicknameClick h.H) h.H ), ) } + +func GameJoinPrompt(loginClick, guestClick, registerClick h.H) h.H { + return h.Main(h.Class("container"), + h.Div(h.Class("lobby"), + h.H1(h.Text("Join Game")), + h.P(h.Text("Log in to track your game history, or continue as a guest.")), + h.Div(h.Class("join-options"), + h.Button( + h.Type("button"), + h.Text("Login"), + loginClick, + ), + h.Button( + h.Type("button"), + h.Class("secondary"), + h.Text("Continue as Guest"), + guestClick, + ), + ), + h.P(h.Class("register-link"), + h.Text("Don't have an account? "), + h.A(h.Href("#"), h.Text("Register"), registerClick), + ), + ), + ) +}