Fix: notify bot

This commit is contained in:
Grail Finder
2025-05-22 16:47:49 +03:00
parent 31f721cd43
commit c155654d5f
7 changed files with 84 additions and 90 deletions

View File

@ -51,53 +51,6 @@ func HandleCreateRoom(w http.ResponseWriter, r *http.Request) {
}
}
// DEPRACATED: duplication of HandleJoinRoom
// func HandleRoomEnter(w http.ResponseWriter, r *http.Request) {
// // parse payload
// roomID := r.URL.Query().Get("id")
// if roomID == "" {
// msg := "room id not provided"
// log.Error(msg)
// abortWithError(w, msg)
// return
// }
// tmpl, err := template.ParseGlob("components/*.html")
// if err != nil {
// abortWithError(w, err.Error())
// return
// }
// // create a room
// room, err := getRoomByID(roomID)
// if err != nil {
// msg := "failed to find the room"
// log.Error(msg, "error", err, "room_id", roomID)
// abortWithError(w, msg)
// return
// }
// state, err := getStateByCtx(r.Context())
// // INFO: if non-loggined user join: prompt to login
// if err != nil {
// log.Error("failed to get state", "error", err)
// // abortWithError(w, err.Error())
// tmpl.ExecuteTemplate(w, "login", nil)
// return
// }
// state.RoomID = room.ID
// // update state
// if err := saveStateByCtx(r.Context(), state); err != nil {
// log.Error("failed to update state", "error", err)
// abortWithError(w, err.Error())
// return
// }
// // send msg of created room
// // h.Broker.Notifier <- broker.NotificationEvent{
// // EventName: models.MsgRoomListUpdate,
// // Payload: fmt.Sprintf("%s created a room named %s", r.CreatorName, r.RoomName),
// // }
// // return html
// tmpl.ExecuteTemplate(w, "base", room)
// }
func HandleJoinTeam(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
log.Error("failed to parse form", "error", err)
@ -158,6 +111,7 @@ func HandleEndTurn(w http.ResponseWriter, r *http.Request) {
return
}
fi.Room.ChangeTurn()
fi.Room.MimeDone = false
if err := saveFullInfo(fi); err != nil {
abortWithError(w, err.Error())
return
@ -168,17 +122,7 @@ func HandleEndTurn(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
if botName := fi.Room.WhichBotToMove(); botName != "" {
// get bot from memcache
bot, err := loadBot(botName, fi.Room.ID)
if err != nil {
log.Error("failed to load bot", "bot_name", botName, "room_id", fi.Room.ID)
abortWithError(w, err.Error())
return
}
// send signal to bot
bot.SignalsCh <- true
}
notifyBotIfNeeded(fi)
notify(models.NotifyRoomUpdatePrefix+fi.Room.ID, "")
if err := tmpl.ExecuteTemplate(w, "base", fi); err != nil {
log.Error("failed to execute base template", "error", err)
@ -222,17 +166,7 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
if botName := fi.Room.WhichBotToMove(); botName != "" {
// get bot from memcache
bot, err := loadBot(botName, fi.Room.ID)
if err != nil {
log.Error("failed to load bot", "bot_name", botName, "room_id", fi.Room.ID)
abortWithError(w, err.Error())
return
}
// send signal to bot
bot.SignalsCh <- true
}
notifyBotIfNeeded(fi)
// to update only the room that should be updated
notify(models.NotifyRoomUpdatePrefix+fi.Room.ID, "")
// notify(models.NotifyBacklogPrefix+fi.Room.ID, "game started")