Chore: model update

This commit is contained in:
Grail Finder
2025-07-01 16:07:36 +03:00
parent e989590e74
commit 86574bf69c
3 changed files with 59 additions and 130 deletions

View File

@ -79,15 +79,10 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
return return
} }
http.SetCookie(w, cookie) http.SetCookie(w, cookie)
// tmpl, err := template.ParseGlob("components/*.html")
// if err != nil {
// abortWithError(w, err.Error())
// return
// }
// check if that user was already in db // check if that user was already in db
userstate, err := loadState(cleanName) userstate, err := loadState(cleanName)
if err != nil || userstate == nil { if err != nil || userstate == nil {
userstate = models.InitState(cleanName) userstate = models.InitPlayer(cleanName)
} }
fi := &models.FullInfo{ fi := &models.FullInfo{
State: userstate, State: userstate,

View File

@ -9,6 +9,44 @@ import (
"github.com/rs/xid" "github.com/rs/xid"
) )
type (
UserTeam string
UserRole string
)
const (
// UserTeam
UserTeamBlue = "blue"
UserTeamRed = "red"
UserTeamNone = ""
//UserRole
UserRoleMime = "mime"
UserRoleGuesser = "guesser"
UserRoleNone = ""
)
func StrToUserTeam(s string) UserTeam {
switch s {
case "blue":
return UserTeamBlue
case "red":
return UserTeamRed
default:
return UserTeamNone
}
}
func StrToUserRole(s string) UserRole {
switch s {
case "mime":
return UserRoleMime
case "guesser":
return UserRoleGuesser
default:
return UserRoleNone
}
}
type WordColor string type WordColor string
const ( const (
@ -50,20 +88,20 @@ type Team struct {
} }
type Action struct { type Action struct {
ID int `json:"id" db:"id"` ID uint32 `json:"id" db:"id"`
RoomID string `json:"room_id" db:"room_id"` RoomID string `json:"room_id" db:"room_id"`
Actor string `json:"actor" db:"actor"` Actor string `json:"actor" db:"actor"`
ActorColor string `json:"actor_color" db:"actor_color"` ActorColor string `json:"actor_color" db:"actor_color"`
Action string `json:"action_type" db:"action_type"` Action string `json:"action_type" db:"action_type"`
Word string `json:"word" db:"word"` Word string `json:"word" db:"word"`
WordColor string `json:"word_color" db:"word_color"` WordColor string `json:"word_color" db:"word_color"`
Number string `json:"number_associated" db:"number_associated"` Number string `json:"number_associated" db:"number_associated"`
CreatedAt time.Time `json:"created_at" db:"-"` CreatedAt time.Time `json:"created_at" db:"-"`
CreatedAtUnix int64 `db:"created_at"` CreatedAtUnix int64 `db:"created_at"`
} }
type Player struct { type Player struct {
ID int `json:"id" db:"id"` ID uint32 `json:"id" db:"id"`
RoomID string `json:"room_id" db:"room_id"` RoomID string `json:"room_id" db:"room_id"`
Username string `json:"username" db:"username"` Username string `json:"username" db:"username"`
Team UserTeam `json:"team" db:"team"` Team UserTeam `json:"team" db:"team"`
@ -71,6 +109,15 @@ type Player struct {
IsBot bool `json:"is_bot" db:"is_bot"` IsBot bool `json:"is_bot" db:"is_bot"`
} }
func InitPlayer(username string) *Player {
return &Player{
// last id + 1?
Username: username,
Team: UserTeamNone,
Role: UserRoleNone,
}
}
type BotPlayer struct { type BotPlayer struct {
Role UserRole // gueeser | mime Role UserRole // gueeser | mime
Team UserTeam Team UserTeam

View File

@ -1,113 +0,0 @@
package models
import "time"
type (
UserTeam string
UserRole string
)
const (
// UserTeam
UserTeamBlue = "blue"
UserTeamRed = "red"
UserTeamNone = ""
//UserRole
UserRoleMime = "mime"
UserRoleGuesser = "guesser"
UserRoleNone = ""
)
func StrToUserTeam(s string) UserTeam {
switch s {
case "blue":
return UserTeamBlue
case "red":
return UserTeamRed
default:
return UserTeamNone
}
}
func StrToUserRole(s string) UserRole {
switch s {
case "mime":
return UserRoleMime
case "guesser":
return UserRoleGuesser
default:
return UserRoleNone
}
}
type UserState struct {
Username string
RoomID string
Team UserTeam
Role UserRole
}
func (u *UserState) ExitRoom() {
u.RoomID = ""
u.Team = UserTeamNone
u.Role = UserRoleNone
}
func MakeTestState(creatorName string) *FullInfo {
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"},
}
redTeam := Team{Guessers: []string{"Adam", "Eve"}, Mime: "Serpent", Color: "red"}
blueTeam := Team{Guessers: []string{"Abel", "Kain"}, Color: "blue"}
room := &Room{
ID: "test-id",
CreatedAt: time.Now(),
CreatorName: creatorName,
Cards: cards,
RedTeam: redTeam,
BlueTeam: blueTeam,
TeamTurn: "blue",
}
us := &UserState{
Username: creatorName,
Team: UserTeamNone,
Role: UserRoleNone,
RoomID: "test-id",
}
return &FullInfo{
State: us,
Room: room,
}
}
func InitState(username string) *UserState {
return &UserState{
Username: username,
Team: UserTeamNone,
Role: UserRoleNone,
}
}