From 95a55a8213d6e0f791e412011ea2d10568221a48 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Wed, 2 Jul 2025 09:17:11 +0300 Subject: [PATCH] Enha: removing memcache [WIP] --- handlers/actions.go | 21 +++++++++++++----- migrations/001_initial_schema.up.sql | 32 ++++++++++++++++++---------- models/main.go | 5 +++-- repos/players.go | 6 +++--- 4 files changed, 43 insertions(+), 21 deletions(-) diff --git a/handlers/actions.go b/handlers/actions.go index ce77b8e..acc6dfd 100644 --- a/handlers/actions.go +++ b/handlers/actions.go @@ -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) diff --git a/migrations/001_initial_schema.up.sql b/migrations/001_initial_schema.up.sql index e7b1d74..ed67577 100644 --- a/migrations/001_initial_schema.up.sql +++ b/migrations/001_initial_schema.up.sql @@ -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) ); diff --git a/models/main.go b/models/main.go index 1876186..e594103 100644 --- a/models/main.go +++ b/models/main.go @@ -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 diff --git a/repos/players.go b/repos/players.go index 5453adc..3c14e10 100644 --- a/repos/players.go +++ b/repos/players.go @@ -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 }