Files
golias/models/main.go
2025-05-09 14:39:33 +03:00

154 lines
3.3 KiB
Go

package models
import (
"time"
"github.com/rs/xid"
)
type WordColor string
const (
WordColorWhite = "white"
WordColorBlue = "blue"
WordColorRed = "red"
WordColorBlack = "black"
WordColorUknown = "beige"
)
func StrToWordColor(s string) WordColor {
switch s {
case "white":
return WordColorWhite
case "blue":
return WordColorBlue
case "red":
return WordColorRed
case "black":
return WordColorBlack
default:
return WordColorUknown
}
}
type Team struct {
Guessers []string
Mime string
}
type Room struct {
ID string `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
// RoomName string `json:"room_name"`
RoomPass string `json:"room_pass"`
RoomLink string
CreatorName string `json:"creator_name"`
PlayerList []string `json:"player_list"`
TeamTurn string
RedTeam Team
BlueTeam Team
Cards []WordCard
Result uint8 // 0 for unknown; 1 is win for red; 2 if for blue;
BlueCounter uint8
RedCounter uint8
RedTurn bool // false is blue turn
IsPublic bool
// GameSettings *GameSettings `json:"settings"`
IsRunning bool `json:"is_running"`
Language string `json:"language" example:"en" form:"language"`
RoundTime int32 `json:"round_time"`
// ProgressPct uint32 `json:"progress_pct"`
IsOver bool
}
func (r *Room) LoadTestCards() {
cards := []WordCard{
{Word: "hamster", Color: "blue"},
{Word: "child", Color: "red"},
{Word: "wheel", Color: "white"},
{Word: "condition", Color: "black"},
{Word: "test", Color: "white"},
{Word: "ball", Color: "blue"},
{Word: "violin", Color: "red"},
{Word: "rat", Color: "white"},
{Word: "perplexity", Color: "blue"},
{Word: "notion", Color: "red"},
{Word: "guitar", Color: "blue"},
{Word: "ocean", Color: "blue"},
{Word: "moon", Color: "blue"},
{Word: "coffee", Color: "blue"},
{Word: "mountain", Color: "blue"},
{Word: "book", Color: "blue"},
{Word: "camera", Color: "blue"},
{Word: "apple", Color: "red"},
{Word: "fire", Color: "red"},
{Word: "rose", Color: "red"},
{Word: "sun", Color: "red"},
{Word: "cherry", Color: "red"},
{Word: "heart", Color: "red"},
{Word: "tomato", Color: "red"},
{Word: "cloud", Color: "white"},
}
r.Cards = cards
}
func (r *Room) ChangeTurn() {
switch r.TeamTurn {
case "blue":
r.TeamTurn = "red"
case "red":
r.TeamTurn = "blue"
default:
r.TeamTurn = "blue"
}
}
func (r *Room) RevealAllCards() {
for i := range r.Cards {
r.Cards[i].Revealed = true
}
}
type WordCard struct {
Word string
Color WordColor
Revealed bool
}
type GameSettings struct {
IsRunning bool `json:"is_running"`
Language string `json:"language" example:"en" form:"language"`
RoundTime int32 `json:"round_time"`
ProgressPct uint32 `json:"progress_pct"`
IsOver bool
}
// =====
type RoomReq struct {
// is not user or not unique
RoomPass string `json:"room_pass" form:"room_pass"`
RoomName string `json:"room_name" form:"room_name"`
// GameSettings
}
func (rr *RoomReq) CreateRoom(creator string) *Room {
roomID := xid.New().String()
return &Room{
// RoomName: ,
RoomPass: rr.RoomPass,
ID: roomID,
CreatedAt: time.Now(),
PlayerList: []string{creator},
CreatorName: creator,
}
}
// ====
type FullInfo struct {
State *UserState
Room *Room
List []*Room
}