diff --git a/handlers/auth.go b/handlers/auth.go index b487a05..9a3150b 100644 --- a/handlers/auth.go +++ b/handlers/auth.go @@ -79,15 +79,10 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) { return } 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 userstate, err := loadState(cleanName) if err != nil || userstate == nil { - userstate = models.InitState(cleanName) + userstate = models.InitPlayer(cleanName) } fi := &models.FullInfo{ State: userstate, diff --git a/models/main.go b/models/main.go index de5de91..1876186 100644 --- a/models/main.go +++ b/models/main.go @@ -9,6 +9,44 @@ import ( "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 const ( @@ -50,20 +88,20 @@ type Team struct { } type Action struct { - ID int `json:"id" db:"id"` - RoomID string `json:"room_id" db:"room_id"` - Actor string `json:"actor" db:"actor"` - ActorColor string `json:"actor_color" db:"actor_color"` - Action string `json:"action_type" db:"action_type"` - Word string `json:"word" db:"word"` - WordColor string `json:"word_color" db:"word_color"` - Number string `json:"number_associated" db:"number_associated"` - CreatedAt time.Time `json:"created_at" db:"-"` - CreatedAtUnix int64 `db:"created_at"` + ID uint32 `json:"id" db:"id"` + RoomID string `json:"room_id" db:"room_id"` + Actor string `json:"actor" db:"actor"` + ActorColor string `json:"actor_color" db:"actor_color"` + Action string `json:"action_type" db:"action_type"` + Word string `json:"word" db:"word"` + WordColor string `json:"word_color" db:"word_color"` + Number string `json:"number_associated" db:"number_associated"` + CreatedAt time.Time `json:"created_at" db:"-"` + CreatedAtUnix int64 `db:"created_at"` } type Player struct { - ID int `json:"id" db:"id"` + ID uint32 `json:"id" db:"id"` RoomID string `json:"room_id" db:"room_id"` Username string `json:"username" db:"username"` Team UserTeam `json:"team" db:"team"` @@ -71,6 +109,15 @@ type Player struct { 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 { Role UserRole // gueeser | mime Team UserTeam diff --git a/models/state.go b/models/state.go deleted file mode 100644 index 9d09b5d..0000000 --- a/models/state.go +++ /dev/null @@ -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, - } -}