Enha: removing memcache [WIP]

This commit is contained in:
Grail Finder
2025-07-02 09:17:11 +03:00
parent 86574bf69c
commit 95a55a8213
4 changed files with 43 additions and 21 deletions

View File

@ -158,15 +158,17 @@ func getAllNames() []string {
// can room exists without state? I think no
func getFullInfoByCtx(ctx context.Context) (*models.FullInfo, error) {
resp := &models.FullInfo{}
state, err := getStateByCtx(ctx)
if err != nil {
return nil, err
}
// state, err := getStateByCtx(ctx)
// if err != nil {
// return nil, err
// }
state, err := getPlayerByCtx(ctx)
resp.State = state
if state.RoomID == "" {
return resp, nil
}
room, err := getRoomByID(state.RoomID)
// room, err := getRoomByID(state.RoomID)
room, err := repo.GetRoomByID(ctx, state.RoomID)
if err != nil {
log.Warn("failed to find room despite knowing room_id;",
"room_id", state.RoomID)
@ -176,6 +178,15 @@ func getFullInfoByCtx(ctx context.Context) (*models.FullInfo, error) {
return resp, nil
}
func getPlayerByCtx(ctx context.Context) (*models.Player, error) {
username, ok := ctx.Value(models.CtxUsernameKey).(string)
if !ok {
log.Debug("no username in ctx")
return &models.Player{}, errors.New("no username in ctx")
}
return repo.GetPlayerByName(username)
}
// // DEPRECATED
// func leaveRole(fi *models.FullInfo) {
// fi.Room.RedTeam.Guessers = utils.RemoveFromSlice(fi.State.Username, fi.Room.RedTeam.Guessers)

View File

@ -4,7 +4,7 @@ CREATE TABLE rooms (
id TEXT PRIMARY KEY,
created_at TIMESTAMP NOT NULL,
creator_name TEXT NOT NULL,
team_turn TEXT,
team_turn TEXT NOT NULL DEFAULT '',
this_turn_limit INTEGER,
opened_this_turn INTEGER,
blue_counter INTEGER,
@ -13,19 +13,19 @@ CREATE TABLE rooms (
mime_done BOOLEAN,
is_public BOOLEAN,
is_running BOOLEAN,
language TEXT,
language TEXT NOT NULL DEFAULT '',
round_time INTEGER,
is_over BOOLEAN,
team_won TEXT,
room_pass TEXT
team_won TEXT NOT NULL DEFAULT '',
room_pass TEXT NOT NULL DEFAULT ''
);
CREATE TABLE players (
id INTEGER PRIMARY KEY AUTOINCREMENT,
room_id TEXT NOT NULL,
username TEXT NOT NULL,
team TEXT, -- 'red' or 'blue'
role TEXT, -- 'guesser' or 'mime'
team TEXT NOT NULL DEFAULT '', -- 'red' or 'blue'
role TEXT NOT NULL DEFAULT '', -- 'guesser' or 'mime'
is_bot BOOLEAN DEFAULT FALSE,
FOREIGN KEY (room_id) REFERENCES rooms(id)
);
@ -34,7 +34,7 @@ CREATE TABLE word_cards (
id INTEGER PRIMARY KEY AUTOINCREMENT,
room_id TEXT NOT NULL,
word TEXT NOT NULL,
color TEXT,
color TEXT NOT NULL DEFAULT '',
revealed BOOLEAN DEFAULT FALSE,
mime_view BOOLEAN DEFAULT FALSE,
FOREIGN KEY (room_id) REFERENCES rooms(id)
@ -52,11 +52,21 @@ CREATE TABLE actions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
room_id TEXT NOT NULL,
actor TEXT NOT NULL,
actor_color TEXT,
actor_color TEXT NOT NULL DEFAULT '',
action_type TEXT NOT NULL,
word TEXT,
word_color TEXT,
number_associated TEXT, -- for clues
word TEXT NOT NULL DEFAULT '',
word_color TEXT NOT NULL DEFAULT '',
number_associated TEXT NOT NULL DEFAULT '', -- for clues
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (room_id) REFERENCES rooms(id)
);
CREATE TABLE settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
room_id TEXT NOT NULL,
language TEXT NOT NULL DEFAULT 'en',
room_pass TEXT NOT NULL DEFAULT '',
turn_time INTEGER NOT NULL DEFAULT 60, -- seconds
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (room_id) REFERENCES rooms(id)
);

View File

@ -417,7 +417,8 @@ func (rr *RoomReq) CreateRoom(creator string) *Room {
// ====
type FullInfo struct {
State *UserState
// State *UserState
State *Player
Room *Room
List []*Room
LinkLogin string // room_id
@ -433,7 +434,7 @@ func (f *FullInfo) ExitRoom() *Room {
if f.Room.BlueTeam.Mime == f.State.Username {
f.Room.BlueTeam.Mime = ""
}
f.State.ExitRoom()
// f.State.ExitRoom()
resp := f.Room
f.Room = nil
return resp

View File

@ -6,14 +6,14 @@ import (
)
type PlayersRepo interface {
GetPlayer(roomID, username string) (*models.Player, error)
GetPlayerByName(username string) (*models.Player, error)
AddPlayer(player *models.Player) error
DeletePlayer(roomID, username string) error
}
func (p *RepoProvider) GetPlayer(roomID, username string) (*models.Player, error) {
func (p *RepoProvider) GetPlayerByName(username string) (*models.Player, error) {
var player models.Player
err := p.DB.GetContext(context.Background(), &player, "SELECT id, room_id, username, team, role, is_bot FROM players WHERE room_id = ? AND username = ?", roomID, username)
err := p.DB.GetContext(context.Background(), &player, "SELECT id, room_id, username, team, role, is_bot FROM players WHERE username = ?", username)
if err != nil {
return nil, err
}