Enha: sse update on actions

This commit is contained in:
Grail Finder
2025-05-10 11:03:23 +03:00
parent 416cc63ec0
commit 35e215e26f
12 changed files with 89 additions and 73 deletions

View File

@ -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,
}
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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{