133 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			133 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package repos
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"fmt"
 | |
| 	"gralias/models"
 | |
| 
 | |
| 	"github.com/jmoiron/sqlx"
 | |
| )
 | |
| 
 | |
| type PlayersRepo interface {
 | |
| 	PlayerGetByName(ctx context.Context, username string) (*models.Player, error)
 | |
| 	PlayerAdd(ctx context.Context, player *models.Player) error
 | |
| 	PlayerUpdate(ctx context.Context, player *models.Player) error
 | |
| 	PlayerDelete(ctx context.Context, username string) error
 | |
| 	PlayerSetRoomID(ctx context.Context, roomID, username string) error
 | |
| 	PlayerExitRoom(ctx context.Context, username string) error
 | |
| 	PlayerListNames(ctx context.Context) ([]string, error)
 | |
| 	PlayerList(ctx context.Context, isBot bool) ([]models.Player, error)
 | |
| 	PlayerListAll(ctx context.Context) ([]models.Player, error)
 | |
| 	PlayerListByRoom(ctx context.Context, roomID string) ([]models.Player, error)
 | |
| 	PlayerGetMaxID(ctx context.Context) (uint32, error)
 | |
| }
 | |
| 
 | |
| func (p *RepoProvider) PlayerListNames(ctx context.Context) ([]string, error) {
 | |
| 	var names []string
 | |
| 	err := sqlx.SelectContext(ctx, p.DB, &names, "SELECT username FROM players;")
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	return names, nil
 | |
| }
 | |
| 
 | |
| func (p *RepoProvider) PlayerGetByName(ctx context.Context, username string) (*models.Player, error) {
 | |
| 	var player models.Player
 | |
| 	err := sqlx.GetContext(ctx, p.DB, &player, "SELECT id, room_id, username, team, role, is_bot, password FROM players WHERE username=?;", username)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	if player.RoomID != nil && *player.RoomID == "" {
 | |
| 		player.RoomID = nil
 | |
| 	}
 | |
| 	return &player, nil
 | |
| }
 | |
| 
 | |
| func (p *RepoProvider) PlayerAdd(ctx context.Context, player *models.Player) error {
 | |
| 	db := getDB(ctx, p.DB)
 | |
| 	_, err := db.ExecContext(ctx, "INSERT INTO players (room_id, username, team, role, is_bot, password) VALUES (?, ?, ?, ?, ?, ?)",
 | |
| 		player.RoomID, player.Username, player.Team, player.Role, player.IsBot, player.Password)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	if !player.IsBot {
 | |
| 		return p.CreatePlayerStats(ctx, player.Username)
 | |
| 	}
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func (p *RepoProvider) PlayerUpdate(ctx context.Context, player *models.Player) error {
 | |
| 	db := getDB(ctx, p.DB)
 | |
| 	_, err := db.ExecContext(ctx, "UPDATE players SET team = ?, role = ? WHERE username = ?;",
 | |
| 		player.Team, player.Role, player.Username)
 | |
| 	return err
 | |
| }
 | |
| 
 | |
| func (p *RepoProvider) PlayerDelete(ctx context.Context, username string) error {
 | |
| 	db := getDB(ctx, p.DB)
 | |
| 	_, err := db.ExecContext(ctx, "DELETE FROM players WHERE username = ?", username)
 | |
| 	return err
 | |
| }
 | |
| 
 | |
| func (p *RepoProvider) PlayerSetRoomID(ctx context.Context, roomID, username string) error {
 | |
| 	db := getDB(ctx, p.DB)
 | |
| 	res, err := db.ExecContext(ctx, "UPDATE players SET room_id = ? WHERE username = ?", roomID, username)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	affected, _ := res.RowsAffected()
 | |
| 	if affected == 0 {
 | |
| 		return fmt.Errorf("failed to set room_id (%s) for player (%s)", roomID, username)
 | |
| 	}
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| 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, team='', role='' WHERE username = ?", username)
 | |
| 	return err
 | |
| }
 | |
| 
 | |
| func (p *RepoProvider) PlayerList(ctx context.Context, isBot bool) ([]models.Player, error) {
 | |
| 	var players []models.Player
 | |
| 	var query string
 | |
| 	if isBot {
 | |
| 		query = "SELECT id, room_id, username, team, role, is_bot FROM players WHERE is_bot = true;"
 | |
| 	} else {
 | |
| 		query = "SELECT id, room_id, username, team, role, is_bot FROM players WHERE is_bot = false;"
 | |
| 	}
 | |
| 	err := sqlx.SelectContext(ctx, p.DB, &players, query)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	return players, nil
 | |
| }
 | |
| 
 | |
| func (p *RepoProvider) PlayerGetMaxID(ctx context.Context) (uint32, error) {
 | |
| 	var maxID uint32
 | |
| 	err := sqlx.GetContext(ctx, p.DB, &maxID, "SELECT COALESCE(MAX(id), 0) FROM players")
 | |
| 	if err != nil {
 | |
| 		return 0, err
 | |
| 	}
 | |
| 	return maxID, nil
 | |
| }
 | |
| 
 | |
| func (p *RepoProvider) PlayerListAll(ctx context.Context) ([]models.Player, error) {
 | |
| 	var players []models.Player
 | |
| 	query := "SELECT id, room_id, username, team, role, is_bot FROM players;"
 | |
| 	err := sqlx.SelectContext(ctx, p.DB, &players, query)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	return players, nil
 | |
| }
 | |
| 
 | |
| func (p *RepoProvider) PlayerListByRoom(ctx context.Context, roomID string) ([]models.Player, error) {
 | |
| 	var players []models.Player
 | |
| 	err := sqlx.SelectContext(ctx, p.DB, &players, "SELECT id, room_id, username, team, role, is_bot FROM players WHERE room_id = ?", roomID)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	return players, nil
 | |
| }
 | 
