Files
gralias/repos/rooms.go
Grail Finder 130ed3763b Fix: unittests
2025-07-02 19:00:39 +03:00

134 lines
4.7 KiB
Go

package repos
import (
"context"
"gralias/models"
"github.com/jmoiron/sqlx"
)
type RoomsRepo interface {
RoomList(ctx context.Context) ([]*models.Room, error)
RoomGetByID(ctx context.Context, id string) (*models.Room, error)
RoomGetExtended(ctx context.Context, id string) (*models.Room, error)
RoomCreate(ctx context.Context, room *models.Room) error
RoomDeleteByID(ctx context.Context, id string) error
RoomUpdate(ctx context.Context, room *models.Room) error
}
func (p *RepoProvider) RoomList(ctx context.Context) ([]*models.Room, error) {
rooms := []*models.Room{}
err := sqlx.SelectContext(ctx, p.DB, &rooms, `SELECT * FROM rooms`)
if err != nil {
return nil, err
}
return rooms, nil
}
func (p *RepoProvider) RoomGetByID(ctx context.Context, id string) (*models.Room, error) {
room := &models.Room{}
err := sqlx.GetContext(ctx, p.DB, room, `SELECT * FROM rooms WHERE id = ?`, id)
if err != nil {
return nil, err
}
settings := &models.GameSettings{}
err = sqlx.GetContext(ctx, p.DB, settings, `SELECT * FROM settings WHERE room_id = ?`, id)
if err != nil {
return nil, err
}
room.Settings = *settings
return room, nil
}
func (p *RepoProvider) RoomCreate(ctx context.Context, r *models.Room) error {
db := getDB(ctx, p.DB)
_, err := db.ExecContext(ctx, `INSERT INTO rooms (id, created_at, creator_name, team_turn, this_turn_limit, opened_this_turn, blue_counter, red_counter, red_turn, mime_done, is_running, is_over, team_won, room_link) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, r.ID, r.CreatedAt, r.CreatorName, r.TeamTurn, r.ThisTurnLimit, r.OpenedThisTurn, r.BlueCounter, r.RedCounter, r.RedTurn, r.MimeDone, r.IsRunning, r.IsOver, r.TeamWon, r.RoomLink)
if err != nil {
return err
}
_, err = db.ExecContext(ctx, `INSERT INTO settings (room_id, language, room_pass, turn_time) VALUES (?, ?, ?, ?)`, r.ID, r.Settings.Language, r.Settings.RoomPass, r.Settings.RoundTime)
return err
}
func (p *RepoProvider) RoomDeleteByID(ctx context.Context, id string) error {
db := getDB(ctx, p.DB)
_, err := db.ExecContext(ctx, `DELETE FROM rooms WHERE id = ?`, id)
if err != nil {
return err
}
_, err = db.ExecContext(ctx, `DELETE FROM settings WHERE room_id = ?`, id)
return err
}
func (p *RepoProvider) RoomUpdate(ctx context.Context, r *models.Room) error {
db := getDB(ctx, p.DB)
_, err := db.ExecContext(ctx, `UPDATE rooms SET team_turn = ?, this_turn_limit = ?, opened_this_turn = ?, blue_counter = ?, red_counter = ?, red_turn = ?, mime_done = ?, is_running = ?, is_over = ?, team_won = ?, room_link = ? WHERE id = ?`, r.TeamTurn, r.ThisTurnLimit, r.OpenedThisTurn, r.BlueCounter, r.RedCounter, r.RedTurn, r.MimeDone, r.IsRunning, r.IsOver, r.TeamWon, r.RoomLink, r.ID)
if err != nil {
return err
}
_, err = db.ExecContext(ctx, `UPDATE settings SET language = ?, room_pass = ?, turn_time = ? WHERE room_id = ?`, r.Settings.Language, r.Settings.RoomPass, r.Settings.RoundTime, r.ID)
return err
}
func (p *RepoProvider) RoomGetExtended(ctx context.Context, id string) (*models.Room, error) {
room := &models.Room{}
err := sqlx.GetContext(ctx, p.DB, room, `SELECT * FROM rooms WHERE id = ?`, id)
if err != nil {
return nil, err
}
// Get players
players := []*models.Player{}
err = sqlx.SelectContext(ctx, p.DB, &players, `SELECT * FROM players WHERE room_id = ?`, id)
if err != nil {
return nil, err
}
room.RedTeam.Color = string(models.UserTeamRed)
room.BlueTeam.Color = string(models.UserTeamBlue)
for _, player := range players {
if player.Team == models.UserTeamRed {
if player.Role == models.UserRoleMime {
room.RedTeam.Mime = player.Username
} else {
room.RedTeam.Guessers = append(room.RedTeam.Guessers, player.Username)
}
} else if player.Team == models.UserTeamBlue {
if player.Role == models.UserRoleMime {
room.BlueTeam.Mime = player.Username
} else {
room.BlueTeam.Guessers = append(room.BlueTeam.Guessers, player.Username)
}
}
if player.IsBot {
if room.BotMap == nil {
room.BotMap = make(map[string]models.BotPlayer)
}
room.BotMap[player.Username] = models.BotPlayer{
Role: player.Role,
Team: player.Team,
}
}
}
// Get word cards
wordCards := []models.WordCard{}
err = sqlx.SelectContext(ctx, p.DB, &wordCards, `SELECT * FROM word_cards WHERE room_id = ?`, id)
if err != nil {
return nil, err
}
room.Cards = wordCards
// Get actions
actions := []models.Action{}
err = sqlx.SelectContext(ctx, p.DB, &actions, `SELECT * FROM actions WHERE room_id = ? ORDER BY created_at ASC`, id)
if err != nil {
return nil, err
}
room.ActionHistory = actions
// Get settings
settings := &models.GameSettings{}
err = sqlx.GetContext(ctx, p.DB, settings, `SELECT * FROM settings WHERE room_id = ?`, id)
if err != nil {
return nil, err
}
room.Settings = *settings
return room, nil
}