Enha: nullable roomID for player [WIP]
This commit is contained in:
		| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
| 	} | ||||
|   | ||||
| @@ -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"` | ||||
| @@ -386,12 +386,12 @@ type WordCard struct { | ||||
|  | ||||
| // table: settings | ||||
| type GameSettings struct { | ||||
| 	ID              uint32 `json:"id" db:"id"` | ||||
| 	RoomID          string `db:"room_id"` | ||||
| 	Language        string `json:"language" example:"en" form:"language" db:"language"` | ||||
| 	RoomPass        string `json:"room_pass" db:"room_pass"` | ||||
| 	TurnSecondsLeft uint32 `db:"-"` | ||||
| 	RoundTime       uint32 `json:"round_time" db:"turn_time"` | ||||
| 	ID              uint32    `json:"id" db:"id"` | ||||
| 	RoomID          string    `db:"room_id"` | ||||
| 	Language        string    `json:"language" example:"en" form:"language" db:"language"` | ||||
| 	RoomPass        string    `json:"room_pass" db:"room_pass"` | ||||
| 	TurnSecondsLeft uint32    `db:"-"` | ||||
| 	RoundTime       uint32    `json:"round_time" db:"turn_time"` | ||||
| 	CreatedAt       time.Time `db:"created_at"` | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder