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 | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	resp.State = state | 	resp.State = state | ||||||
| 	if state.RoomID == "" { | 	if state.RoomID == nil { | ||||||
| 		return resp, nil | 		return resp, nil | ||||||
| 	} | 	} | ||||||
| 	// room, err := getRoomByID(state.RoomID) | 	// room, err := getRoomByID(state.RoomID) | ||||||
| 	room, err := repo.RoomGetByID(ctx, state.RoomID) | 	room, err := repo.RoomGetByID(ctx, *state.RoomID) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | 		// room was deleted; remove it from player; | ||||||
| 		log.Warn("failed to find room despite knowing room_id;", | 		log.Warn("failed to find room despite knowing room_id;", | ||||||
| 			"room_id", state.RoomID) | 			"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 | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	resp.Room = room | 	resp.Room = room | ||||||
|   | |||||||
| @@ -1,7 +1,6 @@ | |||||||
| package handlers | package handlers | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"context" |  | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"gralias/models" | 	"gralias/models" | ||||||
| @@ -32,8 +31,7 @@ func HandleCreateRoom(w http.ResponseWriter, r *http.Request) { | |||||||
| 		abortWithError(w, msg) | 		abortWithError(w, msg) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	ctx := context.WithValue(r.Context(), "current_room", room.ID) | 	fi, err := getFullInfoByCtx(r.Context()) | ||||||
| 	fi, err := getFullInfoByCtx(ctx) |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		msg := "failed to get full info from ctx" | 		msg := "failed to get full info from ctx" | ||||||
| 		log.Error(msg, "error", err) | 		log.Error(msg, "error", err) | ||||||
| @@ -43,10 +41,12 @@ func HandleCreateRoom(w http.ResponseWriter, r *http.Request) { | |||||||
| 	fi.State.RoomID = room.ID | 	fi.State.RoomID = room.ID | ||||||
| 	fi.Room = room | 	fi.Room = room | ||||||
| 	if err := repo.RoomCreate(r.Context(), room); err != nil { | 	if err := repo.RoomCreate(r.Context(), room); err != nil { | ||||||
|  | 		log.Error("failed to create a room", "error", err) | ||||||
| 		abortWithError(w, err.Error()) | 		abortWithError(w, err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	if err := repo.PlayerSetRoomID(r.Context(), fi.State.Username, room.ID); err != nil { | 	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()) | 		abortWithError(w, err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -104,7 +104,7 @@ type Action struct { | |||||||
|  |  | ||||||
| type Player struct { | type Player struct { | ||||||
| 	ID       uint32   `json:"id" db:"id"` | 	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"` | 	Username string   `json:"username" db:"username"` | ||||||
| 	Team     UserTeam `json:"team" db:"team"` | 	Team     UserTeam `json:"team" db:"team"` | ||||||
| 	Role     UserRole `json:"role" db:"role"` | 	Role     UserRole `json:"role" db:"role"` | ||||||
|   | |||||||
| @@ -64,7 +64,7 @@ func (p *RepoProvider) PlayerSetRoomID(ctx context.Context, username, roomID str | |||||||
|  |  | ||||||
| func (p *RepoProvider) PlayerExitRoom(ctx context.Context, username string) error { | func (p *RepoProvider) PlayerExitRoom(ctx context.Context, username string) error { | ||||||
| 	db := getDB(ctx, p.DB) | 	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 | 	return err | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder