Feat: enter room: login with link

This commit is contained in:
Grail Finder
2025-05-21 09:22:29 +03:00
parent 59cccbbe8e
commit ad8f1aaee2
6 changed files with 125 additions and 85 deletions

View File

@ -1,22 +1,26 @@
{{define "main"}}
<!-- user has no username -> login form -->
Start of main temp
{{ if not . }}
{{template "login"}}
<!-- user has name but no room id => suggest to create room -->
login temp
{{template "login"}}
{{ else if ne .LinkLogin "" }}
got to linklogin
{{template "linklogin" .LinkLogin}}
{{ else if eq .State.RoomID "" }}
<div id="hello-user">
<p>Hello {{.State.Username}}</p>
</div>
<div id="create-room" class="create-room-div">
<button button id="create-form-btn" type="submit" class="justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" hx-get="/room/createform" hx-swap="outerHTML">SHOW ROOM CREATE FORM</button>
</div>
<div>
{{template "roomlist" .List}}
</div>
empty state roomid
<div id="hello-user">
<p>Hello {{.State.Username}}</p>
</div>
<div id="create-room" class="create-room-div">
<button button id="create-form-btn" type="submit" class="justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" hx-get="/room/createform" hx-swap="outerHTML">SHOW ROOM CREATE FORM</button>
</div>
<div>
{{template "roomlist" .List}}
</div>
{{else}}
<!-- instead of having room div; better to replace ancestor completely with room -->
<div id="room">
{{template "room" .}}
</div>
else
<div id="room">
{{template "room" .}}
</div>
{{end}}
{{end}}

16
components/linklogin.html Normal file
View File

@ -0,0 +1,16 @@
{{define "linklogin"}}
<div id="logindiv">
You're about to join room#{{.}}; but first!
<form class="space-y-6" hx-post="/login" hx-target="#ancestor">
<label For="username" class="block text-sm font-medium leading-6 text-white-900">tell us your username</label>
<div class="mt-2">
<input id="username" name="username" hx-target="#login_notice" hx-swap="outerHTML" hx-post="/check/name" hx-trigger="input changed delay:400ms" autocomplete="username" required class="block w-full rounded-md border-0 bg-white py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-300 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6 text-center"/>
<input type="hidden" name="room_id" value={{.}}>
</div>
<div id="login_notice">this name looks available</div>
<div>
<button type="submit" class="flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">Sign in</button>
</div>
</form>
</div>
{{end}}

View File

@ -60,6 +60,7 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
abortWithError(w, msg)
return
}
roomID := r.PostFormValue("room_id")
// make sure username does not exists
cleanName := utils.RemoveSpacesFromStr(username)
// login user
@ -75,19 +76,37 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
// state := models.MakeTestState(cleanName)
// state.State.Username = cleanName
userstate := models.InitState(cleanName)
fi := &models.FullInfo{
State: userstate,
}
fi.List = listPublicRooms()
// save state to cache
if err := saveState(cleanName, userstate); err != nil {
// if err := saveFullInfo(fi); err != nil {
log.Error("failed to save state", "error", err)
abortWithError(w, err.Error())
return
// check if room_id provided and exists
if roomID != "" {
log.Debug("got room_id in login", "room_id", roomID)
room, err := getRoomByID(roomID)
if err != nil {
abortWithError(w, err.Error())
return
}
room.PlayerList = append(room.PlayerList, fi.State.Username)
fi.State.RoomID = room.ID
fi.Room = room
fi.List = nil
// save full info instead
if err := saveFullInfo(fi); err != nil {
abortWithError(w, err.Error())
return
}
} else {
log.Debug("no room_id in login")
fi.List = listPublicRooms()
// save state to cache
if err := saveState(cleanName, userstate); err != nil {
// if err := saveFullInfo(fi); err != nil {
log.Error("failed to save state", "error", err)
abortWithError(w, err.Error())
return
}
}
tmpl.ExecuteTemplate(w, "base", fi)
}

View File

@ -49,55 +49,52 @@ func HandleCreateRoom(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "base", fi)
}
func HandleRoomEnter(w http.ResponseWriter, r *http.Request) {
// parse payload
roomID := r.URL.Query().Get("id")
if roomID == "" {
// error
return
}
// create a room
room, err := getRoomByID(roomID)
if err != nil {
msg := "failed to find the room"
log.Error(msg, "error", err, "room_id", roomID)
abortWithError(w, msg)
return
}
ctx := context.WithValue(r.Context(), "current_room", room.ID)
ctx, err = updateRoomInSession(ctx, room.ID)
if err != nil {
msg := "failed to set current room to session"
log.Error(msg, "error", err)
abortWithError(w, msg)
return
}
state, err := getStateByCtx(ctx)
if err != nil {
log.Error("failed to get state", "error", err)
abortWithError(w, err.Error())
return
}
state.RoomID = room.ID
// update state
if err := saveStateByCtx(ctx, state); err != nil {
log.Error("failed to update state", "error", err)
abortWithError(w, err.Error())
return
}
// send msg of created room
// h.Broker.Notifier <- broker.NotificationEvent{
// EventName: models.MsgRoomListUpdate,
// Payload: fmt.Sprintf("%s created a room named %s", r.CreatorName, r.RoomName),
// }
// return html
tmpl, err := template.ParseGlob("components/*.html")
if err != nil {
abortWithError(w, err.Error())
return
}
tmpl.ExecuteTemplate(w, "base", room)
}
// DEPRACATED: duplication of HandleJoinRoom
// func HandleRoomEnter(w http.ResponseWriter, r *http.Request) {
// // parse payload
// roomID := r.URL.Query().Get("id")
// if roomID == "" {
// msg := "room id not provided"
// log.Error(msg)
// abortWithError(w, msg)
// return
// }
// tmpl, err := template.ParseGlob("components/*.html")
// if err != nil {
// abortWithError(w, err.Error())
// return
// }
// // create a room
// room, err := getRoomByID(roomID)
// if err != nil {
// msg := "failed to find the room"
// log.Error(msg, "error", err, "room_id", roomID)
// abortWithError(w, msg)
// return
// }
// state, err := getStateByCtx(r.Context())
// // INFO: if non-loggined user join: prompt to login
// if err != nil {
// log.Error("failed to get state", "error", err)
// // abortWithError(w, err.Error())
// tmpl.ExecuteTemplate(w, "login", nil)
// return
// }
// state.RoomID = room.ID
// // update state
// if err := saveStateByCtx(r.Context(), state); err != nil {
// log.Error("failed to update state", "error", err)
// abortWithError(w, err.Error())
// return
// }
// // send msg of created room
// // h.Broker.Notifier <- broker.NotificationEvent{
// // EventName: models.MsgRoomListUpdate,
// // Payload: fmt.Sprintf("%s created a room named %s", r.CreatorName, r.RoomName),
// // }
// // return html
// tmpl.ExecuteTemplate(w, "base", room)
// }
func HandleJoinTeam(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
@ -239,11 +236,20 @@ func HandleJoinRoom(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
fi, err := getFullInfoByCtx(r.Context())
tmpl, err := template.ParseGlob("components/*.html")
if err != nil {
abortWithError(w, err.Error())
return
}
fi, err := getFullInfoByCtx(r.Context())
if err != nil {
// INFO: if non-loggined user join: prompt to login
fi = &models.FullInfo{}
fi.LinkLogin = roomID
tmpl.ExecuteTemplate(w, "base", fi)
// abortWithError(w, err.Error())
return
}
room.PlayerList = append(room.PlayerList, fi.State.Username)
fi.State.RoomID = room.ID
fi.Room = room
@ -252,12 +258,6 @@ func HandleJoinRoom(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
// return html
tmpl, err := template.ParseGlob("components/*.html")
if err != nil {
abortWithError(w, err.Error())
return
}
tmpl.ExecuteTemplate(w, "room", fi)
}

View File

@ -23,7 +23,7 @@ func ListenToRequests(port string) error {
mux.HandleFunc("GET /ping", handlers.HandlePing)
mux.HandleFunc("GET /", handlers.HandleHome)
mux.HandleFunc("POST /login", handlers.HandleFrontLogin)
mux.HandleFunc("GET /room", handlers.HandleRoomEnter)
// mux.HandleFunc("GET /room", handlers.HandleRoomEnter)
mux.HandleFunc("POST /join-team", handlers.HandleJoinTeam)
mux.HandleFunc("GET /end-turn", handlers.HandleEndTurn)
mux.HandleFunc("POST /room-create", handlers.HandleCreateRoom)

View File

@ -242,9 +242,10 @@ func (rr *RoomReq) CreateRoom(creator string) *Room {
// ====
type FullInfo struct {
State *UserState
Room *Room
List []*Room
State *UserState
Room *Room
List []*Room
LinkLogin string // room_id
}
func (f *FullInfo) ExitRoom() *Room {