Enha: nullable roomID for player [WIP]

This commit is contained in:
Grail Finder
2025-07-03 11:00:33 +03:00
parent c82439d43a
commit 2a593739ae
4 changed files with 21 additions and 13 deletions

View File

@ -135,14 +135,22 @@ func getFullInfoByCtx(ctx context.Context) (*models.FullInfo, error) {
return nil, err
}
resp.State = state
if state.RoomID == "" {
if state.RoomID == nil {
return resp, nil
}
// room, err := getRoomByID(state.RoomID)
room, err := repo.RoomGetByID(ctx, state.RoomID)
room, err := repo.RoomGetByID(ctx, *state.RoomID)
if err != nil {
// room was deleted; remove it from player;
log.Warn("failed to find room despite knowing room_id;",
"room_id", state.RoomID)
state.Team = models.UserTeamNone
state.Role = models.UserRoleNone
if err := repo.PlayerExitRoom(ctx, state.Username); err != nil {
log.Warn("failed to exit room",
"room_id", state.RoomID, "username", state.Username)
return resp, err
}
return nil, err
}
resp.Room = room

View File

@ -1,7 +1,6 @@
package handlers
import (
"context"
"errors"
"fmt"
"gralias/models"
@ -32,8 +31,7 @@ func HandleCreateRoom(w http.ResponseWriter, r *http.Request) {
abortWithError(w, msg)
return
}
ctx := context.WithValue(r.Context(), "current_room", room.ID)
fi, err := getFullInfoByCtx(ctx)
fi, err := getFullInfoByCtx(r.Context())
if err != nil {
msg := "failed to get full info from ctx"
log.Error(msg, "error", err)
@ -43,10 +41,12 @@ func HandleCreateRoom(w http.ResponseWriter, r *http.Request) {
fi.State.RoomID = room.ID
fi.Room = room
if err := repo.RoomCreate(r.Context(), room); err != nil {
log.Error("failed to create a room", "error", err)
abortWithError(w, err.Error())
return
}
if err := repo.PlayerSetRoomID(r.Context(), fi.State.Username, room.ID); err != nil {
log.Error("failed to set room id", "error", err)
abortWithError(w, err.Error())
return
}

View File

@ -104,7 +104,7 @@ type Action struct {
type Player struct {
ID uint32 `json:"id" db:"id"`
RoomID string `json:"room_id" db:"room_id"`
RoomID *string `json:"room_id" db:"room_id"`
Username string `json:"username" db:"username"`
Team UserTeam `json:"team" db:"team"`
Role UserRole `json:"role" db:"role"`

View File

@ -64,7 +64,7 @@ func (p *RepoProvider) PlayerSetRoomID(ctx context.Context, username, roomID str
func (p *RepoProvider) PlayerExitRoom(ctx context.Context, username string) error {
db := getDB(ctx, p.DB)
_, err := db.ExecContext(ctx, "UPDATE players SET room_id = null WHERE username = ?", username)
_, err := db.ExecContext(ctx, "UPDATE players SET room_id='', team='', role='' WHERE username = ?", username)
return err
}