diff --git a/components/base.html b/components/base.html index d2ca24f..babec29 100644 --- a/components/base.html +++ b/components/base.html @@ -41,7 +41,7 @@ -
+
{{template "main" .}}
diff --git a/components/cardword.html b/components/cardword.html index 7d8e91a..cd58b72 100644 --- a/components/cardword.html +++ b/components/cardword.html @@ -22,7 +22,7 @@ color: white; text-shadow: 0 2px 4px rgba(0,0,0,0.8); cursor: pointer;" - hx-get="/word/show-color?word={{.Word}}" hx-trigger="click" hx-swap="outerHTML transition:true swap:.1s"> + hx-get="/word/show-color?word={{.Word}}" hx-trigger="click" hx-swap="outerHTML transition:true swap:.05s"> {{.Word}}
{{end}} diff --git a/components/index.html b/components/index.html index 79b16f6..4b0f94a 100644 --- a/components/index.html +++ b/components/index.html @@ -1,8 +1,4 @@ {{define "main"}} -
- Contents of this box will be updated in real time - with every SSE message received from the chatroom. -
{{ if not . }} {{template "login"}} diff --git a/components/playerlist.html b/components/playerlist.html index 2443c64..3a4684d 100644 --- a/components/playerlist.html +++ b/components/playerlist.html @@ -1,12 +1,12 @@ {{define "teamlist"}} -
+

Guessers

{{range .Guessers}}

{{.}}

{{end}}

-
+

Mime

{{.Mime}}

diff --git a/components/room.html b/components/room.html index 9369d1e..a39409a 100644 --- a/components/room.html +++ b/components/room.html @@ -1,44 +1,46 @@ {{define "room"}} -
-

Hello {{.State.Username}};

-

Room created by {{.Room.CreatorName}};

-

Game is running: {{.Room.IsRunning}}

-

- {{if and (eq .State.Username .Room.CreatorName) (not .Room.IsRunning)}} - +

+
+

Hello {{.State.Username}};

+

Room created by {{.Room.CreatorName}};

+

Game is running: {{.Room.IsRunning}}

+

+ {{if and (eq .State.Username .Room.CreatorName) (not .Room.IsRunning)}} + + {{end}} +

+

Turn of the {{.Room.TeamTurn}} team

+

+ {{if eq .State.Team ""}} + join the team! + {{else}} + you're on the team {{.State.Team}}! + {{end}} +

+
+
+
+ + {{template "teamlist" .Room.BlueTeam}} + {{if and (ne .State.Team "blue") (not .Room.IsRunning)}} + {{template "teampew" "blue"}} {{end}} -

-

Turn of the {{.Room.TeamTurn}} team

-

- {{if eq .State.Team ""}} - join the team! - {{else}} - you're on the team {{.State.Team}}! + + {{if and (ne .State.Team "red") (not .Room.IsRunning)}} + {{template "teampew" "red"}} {{end}} -

-
-
-
- - {{template "teamlist" .Room.BlueTeam}} - {{if ne .State.Team "blue"}} - {{template "teampew" "blue"}} - {{end}} - - {{if ne .State.Team "red"}} - {{template "teampew" "red"}} - {{end}} - {{template "teamlist" .Room.RedTeam}} -
-
-
- {{template "cardtable" .Room}} -
-
- {{if eq .State.Role "guesser"}} - - {{else if eq .State.Role "mime"}} - {{template "mimeclue"}} - {{end}} + {{template "teamlist" .Room.RedTeam}} +
+
+
+ {{template "cardtable" .Room}} +
+
+ {{if and (eq .State.Role "guesser") (eq .State.Team .Room.TeamTurn)}} + + {{else if eq .State.Role "mime"}} + {{template "mimeclue"}} + {{end}} +
{{end}} diff --git a/handlers/actions.go b/handlers/actions.go index a3342e0..e83b771 100644 --- a/handlers/actions.go +++ b/handlers/actions.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "golias/broker" "golias/models" "golias/utils" "strings" @@ -168,6 +169,7 @@ func joinTeam(ctx context.Context, role, team string) (*models.FullInfo, error) return fi, err } fi.Room.BlueTeam.Mime = fi.State.Username + fi.Room.BlueTeam.Color = "blue" fi.State.Team = "blue" fi.State.Role = "mime" if fi.Room.RedTeam.Mime == fi.State.Username { @@ -180,6 +182,7 @@ func joinTeam(ctx context.Context, role, team string) (*models.FullInfo, error) return fi, err } fi.Room.RedTeam.Mime = fi.State.Username + fi.Room.RedTeam.Color = "red" fi.State.Team = "red" fi.State.Role = "mime" if fi.Room.BlueTeam.Mime == fi.State.Username { @@ -192,10 +195,12 @@ func joinTeam(ctx context.Context, role, team string) (*models.FullInfo, error) } else if role == "guesser" { if team == "blue" { fi.Room.BlueTeam.Guessers = append(fi.Room.BlueTeam.Guessers, fi.State.Username) + fi.Room.BlueTeam.Color = "blue" fi.State.Team = "blue" fi.State.Role = "guesser" } else if team == "red" { fi.Room.RedTeam.Guessers = append(fi.Room.RedTeam.Guessers, fi.State.Username) + fi.Room.RedTeam.Color = "red" fi.State.Team = "red" fi.State.Role = "guesser" } else { @@ -232,3 +237,10 @@ func listPublicRooms() []*models.Room { } return publicRooms } + +func notify(event, msg string) { + Notifier.Notifier <- broker.NotificationEvent{ + EventName: event, + Payload: msg, + } +} diff --git a/handlers/elements.go b/handlers/elements.go index 8d96225..46f2acf 100644 --- a/handlers/elements.go +++ b/handlers/elements.go @@ -35,24 +35,23 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } - session, ok := ctx.Value(models.CtxSessionKey).(*models.Session) - if !ok { - // trying to get color without a session -> error - http.Redirect(w, r, "/", 302) - return - } - state, err := loadState(session.Username) + fi, err := getFullInfoByCtx(ctx) if err != nil { abortWithError(w, err.Error()) return } - // TODO: whos move it is? - if state.Role != "guesser" { + if fi.State.Role != "guesser" { err = errors.New("need to guesser to open the card") abortWithError(w, err.Error()) return } - log.Debug("got state", "state", state) + // whos move it is? + if fi.State.Team != models.UserTeam(fi.Room.TeamTurn) { + err = errors.New("not your team's move") + abortWithError(w, err.Error()) + return + } + log.Debug("got state", "state", fi) // TODO: update room score color, exists := roundWords[word] log.Debug("got show-color request", "word", word, "color", color) @@ -65,5 +64,11 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { Color: models.StrToWordColor(color), Revealed: true, } + fi.Room.RevealSpecificWord(word) + if err := saveFullInfo(fi); err != nil { + abortWithError(w, err.Error()) + return + } + notify(models.NotifyRoomUpdatePrefix+fi.Room.ID, "") tmpl.ExecuteTemplate(w, "cardword", cardword) } diff --git a/handlers/game.go b/handlers/game.go index 526dcdd..a882e90 100644 --- a/handlers/game.go +++ b/handlers/game.go @@ -149,6 +149,7 @@ func HandleJoinTeam(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } + notify(models.NotifyRoomUpdatePrefix+fi.Room.ID, "") tmpl.ExecuteTemplate(w, "base", fi) } @@ -176,6 +177,7 @@ func HandleEndTurn(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } + notify(models.NotifyRoomUpdatePrefix+fi.Room.ID, "") tmpl.ExecuteTemplate(w, "base", fi) } @@ -204,6 +206,8 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } + // to update only the room that should be updated + notify(models.NotifyRoomUpdatePrefix+fi.Room.ID, "") tmpl.ExecuteTemplate(w, "room", fi) } diff --git a/handlers/handlers.go b/handlers/handlers.go index 87eb978..96ac54a 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -1,7 +1,6 @@ package handlers import ( - "fmt" "golias/broker" "golias/config" "golias/pkg/cache" @@ -9,7 +8,6 @@ import ( "log/slog" "net/http" "os" - "time" ) var ( @@ -28,18 +26,6 @@ func init() { cfg = config.LoadConfigOrDefault("") Notifier = broker.NewBroker() go Notifier.Listen() - ticker := time.NewTicker(2 * time.Second) - go func() { - counter := 0 - for { - <-ticker.C - Notifier.Notifier <- broker.NotificationEvent{ - EventName: "test", - Payload: fmt.Sprintf("%v test call of notifier", counter), - } - counter++ - } - }() } var roundWords = map[string]string{ diff --git a/models/keys.go b/models/keys.go index 400886e..a87a399 100644 --- a/models/keys.go +++ b/models/keys.go @@ -7,4 +7,6 @@ var ( // cache CacheRoomPrefix = "room#" CacheStatePrefix = "state-" + // sse + NotifyRoomUpdatePrefix = "roomupdate_" ) diff --git a/models/main.go b/models/main.go index fd5712a..4fc215e 100644 --- a/models/main.go +++ b/models/main.go @@ -34,6 +34,7 @@ func StrToWordColor(s string) WordColor { type Team struct { Guessers []string Mime string + Color string } type Room struct { @@ -109,6 +110,14 @@ func (r *Room) RevealAllCards() { } } +func (r *Room) RevealSpecificWord(word string) { + for i, card := range r.Cards { + if card.Word == word { + r.Cards[i].Revealed = true + } + } +} + type WordCard struct { Word string Color WordColor diff --git a/models/state.go b/models/state.go index 4c4bc5c..c21376d 100644 --- a/models/state.go +++ b/models/state.go @@ -75,8 +75,8 @@ func MakeTestState(creatorName string) *FullInfo { {Word: "tomato", Color: "red"}, {Word: "cloud", Color: "white"}, } - redTeam := Team{Guessers: []string{"Adam", "Eve"}, Mime: "Serpent"} - blueTeam := Team{Guessers: []string{"Abel", "Kain"}} + 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(),