Fix: save bot actions

This commit is contained in:
Grail Finder
2025-07-05 11:30:58 +03:00
parent eef4b7941b
commit de2cccf66d
6 changed files with 36 additions and 22 deletions

View File

@ -2725,7 +2725,6 @@ attendance
present
find
lead
wtv
champion
gasoline
national
@ -2745,7 +2744,6 @@ excitement
quote
forehead
wax
mckinley
television
can
voyage
@ -2834,7 +2832,6 @@ least
boot
alien
employer
viscosity
theft
wall
vapor
@ -2847,7 +2844,6 @@ sovereign
smoke
fool
intelligence
indictment
flame
advance
mud
@ -2980,7 +2976,6 @@ agent
motel
punishment
lime
magnification
snap
surgeon
short
@ -3149,7 +3144,6 @@ cope
law
lap
recommendation
patrolman
purple
imagery
offer

View File

@ -208,6 +208,8 @@ func notify(event, msg string) {
}
func loadCards(room *models.Room) {
// remove old cards
room.Cards = []models.WordCard{}
// store it somewhere
wordMap := map[string]string{
"en": "assets/words/en_nouns.txt",

View File

@ -120,7 +120,7 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) {
fi.Room.ClearMarks()
StopTurnTimer(fi.Room.ID)
case string(models.WordColorWhite), string(oppositeColor):
log.Debug("opened opposite color word", "room", fi.Room, "opposite-color", oppositeColor)
log.Debug("opened white or opposite color word", "word", word, "opposite-color", oppositeColor)
// end turn
fi.Room.TeamTurn = oppositeColor
fi.Room.MimeDone = false

View File

@ -163,6 +163,7 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) {
panic(r)
}
}()
fi.Room.MimeDone = false
fi.Room.IsRunning = true
fi.Room.IsOver = false
fi.Room.TeamTurn = "blue"
@ -179,13 +180,13 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) {
}
fi.Room.ActionHistory = append(fi.Room.ActionHistory, action)
// Use the new context with transaction
if err := saveFullInfo(ctx, fi); err != nil {
if err := tx.Rollback(); err != nil {
log.Error("failed to rollback transaction", "error", err)
}
abortWithError(w, err.Error())
return
}
// if err := saveFullInfo(ctx, fi); err != nil {
// if err := tx.Rollback(); err != nil {
// log.Error("failed to rollback transaction", "error", err)
// }
// abortWithError(w, err.Error())
// return
// }
// Save action history
action.RoomID = fi.Room.ID
action.CreatedAt = time.Now()
@ -209,14 +210,18 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) {
return
}
}
if err := repo.RoomUpdate(ctx, fi.Room); err != nil {
log.Error("failed to update room", "error", err)
tx.Rollback()
abortWithError(w, err.Error())
return
}
// Commit the transaction
if err := tx.Commit(); err != nil {
log.Error("failed to commit transaction", "error", err)
abortWithError(w, err.Error())
return
}
// reveal all cards
if fi.State.Role == "mime" {
fi.Room.MimeView()

View File

@ -152,7 +152,18 @@ func (b *Bot) checkGuess(word string, room *models.Room) error {
}
room.ActionHistory = append(room.ActionHistory, action)
}
if err := saveRoom(room); err != nil {
ctx, tx, err := repo.InitTx(context.Background())
defer tx.Commit()
if err != nil {
b.log.Error("failed to init tx", "error", err)
}
if err := repo.ActionCreate(ctx, &action); err != nil {
tx.Rollback()
b.log.Error("failed to create action", "error", err, "action", action)
return err
}
if err := repo.RoomUpdate(ctx, room); err != nil {
tx.Rollback()
b.log.Error("failed to save room", "room", room)
err = fmt.Errorf("fn: checkGuess, failed to save room; err: %w", err)
return err
@ -231,16 +242,15 @@ func (b *Bot) BotMove() {
b.log.Warn("turn limit is 0", "mimeResp", mimeResp)
room.ThisTurnLimit = 9
}
if err := repo.ActionCreate(context.Background(), &action); err != nil {
b.log.Error("failed to create action", "error", err)
return
}
if err := saveRoom(room); err != nil {
b.log.Error("failed to save room", "error", err)
return
}
case models.UserRoleGuesser:
// // deprecated
// if err := b.checkGuesses(tempMap, room); err != nil {
// b.log.Warn("failed to check guess", "mimeResp", tempMap, "bot_name", b.BotName)
// continue
// }
guess, ok := tempMap["guess"].(string)
if !ok || guess == "" {
b.log.Warn("failed to parse guess", "mimeResp", tempMap, "bot_name", b.BotName)

View File

@ -71,3 +71,6 @@
- card marks; +
- on server recover relaunch guess timer if needed;
- start new game: clear last clue; mimedone to false; unload old cards;
- backlog shows white word with opposite color;
- bot actions are not recorder;
- bot recieves opp-color clue because of it ^;