Fix: save bot actions
This commit is contained in:
		| @@ -2725,7 +2725,6 @@ attendance | |||||||
| present | present | ||||||
| find | find | ||||||
| lead | lead | ||||||
| wtv |  | ||||||
| champion | champion | ||||||
| gasoline | gasoline | ||||||
| national | national | ||||||
| @@ -2745,7 +2744,6 @@ excitement | |||||||
| quote | quote | ||||||
| forehead | forehead | ||||||
| wax | wax | ||||||
| mckinley |  | ||||||
| television | television | ||||||
| can | can | ||||||
| voyage | voyage | ||||||
| @@ -2834,7 +2832,6 @@ least | |||||||
| boot | boot | ||||||
| alien | alien | ||||||
| employer | employer | ||||||
| viscosity |  | ||||||
| theft | theft | ||||||
| wall | wall | ||||||
| vapor | vapor | ||||||
| @@ -2847,7 +2844,6 @@ sovereign | |||||||
| smoke | smoke | ||||||
| fool | fool | ||||||
| intelligence | intelligence | ||||||
| indictment |  | ||||||
| flame | flame | ||||||
| advance | advance | ||||||
| mud | mud | ||||||
| @@ -2980,7 +2976,6 @@ agent | |||||||
| motel | motel | ||||||
| punishment | punishment | ||||||
| lime | lime | ||||||
| magnification |  | ||||||
| snap | snap | ||||||
| surgeon | surgeon | ||||||
| short | short | ||||||
| @@ -3149,7 +3144,6 @@ cope | |||||||
| law | law | ||||||
| lap | lap | ||||||
| recommendation | recommendation | ||||||
| patrolman |  | ||||||
| purple | purple | ||||||
| imagery | imagery | ||||||
| offer | offer | ||||||
|   | |||||||
| @@ -208,6 +208,8 @@ func notify(event, msg string) { | |||||||
| } | } | ||||||
|  |  | ||||||
| func loadCards(room *models.Room) { | func loadCards(room *models.Room) { | ||||||
|  | 	// remove old cards | ||||||
|  | 	room.Cards = []models.WordCard{} | ||||||
| 	// store it somewhere | 	// store it somewhere | ||||||
| 	wordMap := map[string]string{ | 	wordMap := map[string]string{ | ||||||
| 		"en": "assets/words/en_nouns.txt", | 		"en": "assets/words/en_nouns.txt", | ||||||
|   | |||||||
| @@ -120,7 +120,7 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { | |||||||
| 		fi.Room.ClearMarks() | 		fi.Room.ClearMarks() | ||||||
| 		StopTurnTimer(fi.Room.ID) | 		StopTurnTimer(fi.Room.ID) | ||||||
| 	case string(models.WordColorWhite), string(oppositeColor): | 	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 | 		// end turn | ||||||
| 		fi.Room.TeamTurn = oppositeColor | 		fi.Room.TeamTurn = oppositeColor | ||||||
| 		fi.Room.MimeDone = false | 		fi.Room.MimeDone = false | ||||||
|   | |||||||
| @@ -163,6 +163,7 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) { | |||||||
| 			panic(r) | 			panic(r) | ||||||
| 		} | 		} | ||||||
| 	}() | 	}() | ||||||
|  | 	fi.Room.MimeDone = false | ||||||
| 	fi.Room.IsRunning = true | 	fi.Room.IsRunning = true | ||||||
| 	fi.Room.IsOver = false | 	fi.Room.IsOver = false | ||||||
| 	fi.Room.TeamTurn = "blue" | 	fi.Room.TeamTurn = "blue" | ||||||
| @@ -179,13 +180,13 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) { | |||||||
| 	} | 	} | ||||||
| 	fi.Room.ActionHistory = append(fi.Room.ActionHistory, action) | 	fi.Room.ActionHistory = append(fi.Room.ActionHistory, action) | ||||||
| 	// Use the new context with transaction | 	// Use the new context with transaction | ||||||
| 	if err := saveFullInfo(ctx, fi); err != nil { | 	// if err := saveFullInfo(ctx, fi); err != nil { | ||||||
| 		if err := tx.Rollback(); err != nil { | 	// 	if err := tx.Rollback(); err != nil { | ||||||
| 			log.Error("failed to rollback transaction", "error", err) | 	// 		log.Error("failed to rollback transaction", "error", err) | ||||||
| 		} | 	// 	} | ||||||
| 		abortWithError(w, err.Error()) | 	// 	abortWithError(w, err.Error()) | ||||||
| 		return | 	// 	return | ||||||
| 	} | 	// } | ||||||
| 	// Save action history | 	// Save action history | ||||||
| 	action.RoomID = fi.Room.ID | 	action.RoomID = fi.Room.ID | ||||||
| 	action.CreatedAt = time.Now() | 	action.CreatedAt = time.Now() | ||||||
| @@ -209,14 +210,18 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) { | |||||||
| 			return | 			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 | 	// Commit the transaction | ||||||
| 	if err := tx.Commit(); err != nil { | 	if err := tx.Commit(); err != nil { | ||||||
| 		log.Error("failed to commit transaction", "error", err) | 		log.Error("failed to commit transaction", "error", err) | ||||||
| 		abortWithError(w, err.Error()) | 		abortWithError(w, err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// reveal all cards | 	// reveal all cards | ||||||
| 	if fi.State.Role == "mime" { | 	if fi.State.Role == "mime" { | ||||||
| 		fi.Room.MimeView() | 		fi.Room.MimeView() | ||||||
|   | |||||||
| @@ -152,7 +152,18 @@ func (b *Bot) checkGuess(word string, room *models.Room) error { | |||||||
| 		} | 		} | ||||||
| 		room.ActionHistory = append(room.ActionHistory, action) | 		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) | 		b.log.Error("failed to save room", "room", room) | ||||||
| 		err = fmt.Errorf("fn: checkGuess, failed to save room; err: %w", err) | 		err = fmt.Errorf("fn: checkGuess, failed to save room; err: %w", err) | ||||||
| 		return err | 		return err | ||||||
| @@ -231,16 +242,15 @@ func (b *Bot) BotMove() { | |||||||
| 			b.log.Warn("turn limit is 0", "mimeResp", mimeResp) | 			b.log.Warn("turn limit is 0", "mimeResp", mimeResp) | ||||||
| 			room.ThisTurnLimit = 9 | 			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 { | 		if err := saveRoom(room); err != nil { | ||||||
| 			b.log.Error("failed to save room", "error", err) | 			b.log.Error("failed to save room", "error", err) | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 	case models.UserRoleGuesser: | 	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) | 		guess, ok := tempMap["guess"].(string) | ||||||
| 		if !ok || guess == "" { | 		if !ok || guess == "" { | ||||||
| 			b.log.Warn("failed to parse guess", "mimeResp", tempMap, "bot_name", b.BotName) | 			b.log.Warn("failed to parse guess", "mimeResp", tempMap, "bot_name", b.BotName) | ||||||
|   | |||||||
							
								
								
									
										3
									
								
								todos.md
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								todos.md
									
									
									
									
									
								
							| @@ -71,3 +71,6 @@ | |||||||
| - card marks; + | - card marks; + | ||||||
| - on server recover relaunch guess timer if needed; | - on server recover relaunch guess timer if needed; | ||||||
| - start new game: clear last clue; mimedone to false; unload old cards; | - 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 ^; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder