Fix: test; limit changes on player update

This commit is contained in:
Grail Finder
2025-07-04 10:02:39 +03:00
parent 6ca8afd13d
commit 83215f5c14
9 changed files with 40 additions and 92 deletions

View File

@ -24,57 +24,6 @@ func createRoom(ctx context.Context, req *models.RoomReq) (*models.Room, error)
return room, nil
}
// // DEPRECATED
// func saveRoom(room *models.Room) error {
// key := models.CacheRoomPrefix + room.ID
// data, err := json.Marshal(room)
// if err != nil {
// return err
// }
// memcache.Set(key, data)
// // do I need last action here? since room save is kind of an action on itself
// // time.Now().Add(time.Hour).Sub(room.LastActionTS)
// anHour := int64(216000) // 60 * 60 * 60
// memcache.Expire(key, anHour)
// return nil
// }
// func getRoomByID(roomID string) (*models.Room, error) {
// roomBytes, err := memcache.Get(models.CacheRoomPrefix + roomID)
// if err != nil {
// return nil, err
// }
// resp := &models.Room{}
// if err := json.Unmarshal(roomBytes, &resp); err != nil {
// return nil, err
// }
// return resp, nil
// }
// func removeRoom(roomID string) {
// key := models.CacheRoomPrefix + roomID
// memcache.RemoveKey(key)
// }
// context
// func getStateByCtx(ctx context.Context) (*models.UserState, error) {
// username, ok := ctx.Value(models.CtxUsernameKey).(string)
// if !ok {
// log.Debug("no username in ctx")
// return &models.UserState{}, errors.New("no username in ctx")
// }
// us, err := loadState(username)
// if err != nil {
// return &models.UserState{}, err
// }
// return us, nil
// }
// func dbCreate(fi *models.FullInfo) error{
// repo.CreateRoom()
// }
func saveFullInfo(ctx context.Context, fi *models.FullInfo) error {
// INFO: no transactions; so case is possible where first object is updated but the second is not
if err := repo.PlayerUpdate(ctx, fi.State); err != nil {

View File

@ -159,7 +159,6 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
// Initialize transaction
ctx, tx, err := repo.InitTx(r.Context())
if err != nil {
@ -173,7 +172,6 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) {
panic(r)
}
}()
fi.Room.IsRunning = true
fi.Room.IsOver = false
fi.Room.TeamTurn = "blue"
@ -189,24 +187,21 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) {
Action: models.ActionTypeGameStarted,
}
fi.Room.ActionHistory = append(fi.Room.ActionHistory, action)
// Use the new context with transaction
if err := saveFullInfo(ctx, fi); err != nil {
tx.Rollback()
abortWithError(w, err.Error())
return
}
// Save action history
action.RoomID = fi.Room.ID
action.CreatedAtUnix = time.Now().Unix()
action.CreatedAt = time.Now()
if err := repo.CreateAction(ctx, fi.Room.ID, &action); err != nil {
tx.Rollback()
log.Error("failed to save action", "error", err)
abortWithError(w, err.Error())
return
}
// Save word cards
for _, card := range fi.Room.Cards {
card.RoomID = fi.Room.ID // Ensure RoomID is set for each card
@ -273,7 +268,8 @@ func HandleJoinRoom(w http.ResponseWriter, r *http.Request) {
fi.State.RoomID = &room.ID
fi.Room = room
fi.List = nil
if err := saveFullInfo(r.Context(), fi); err != nil {
if err := repo.PlayerSetRoomID(r.Context(), room.ID, fi.State.Username); err != nil {
log.Error("failed to set room_id for player", "error", err, "username", fi.State.Username, "room_id", room.ID)
abortWithError(w, err.Error())
return
}

View File

@ -110,11 +110,11 @@ func HandleExit(w http.ResponseWriter, r *http.Request) {
fiToSave := &models.FullInfo{
Room: exitedRoom,
}
if err := saveFullInfo(r.Context(), fiToSave); err != nil {
if err := repo.PlayerExitRoom(r.Context(), fi.State.Username); err != nil {
abortWithError(w, err.Error())
return
}
if err := repo.PlayerExitRoom(r.Context(), fi.State.Username); err != nil {
if err := saveFullInfo(r.Context(), fiToSave); err != nil {
abortWithError(w, err.Error())
return
}