Compare commits

..

3 Commits

Author SHA1 Message Date
f46cbff602 Fix: buildable 2025-07-05 14:40:42 +03:00
6ad251fc47 Enha: journal repo [wip] 2025-07-05 14:32:48 +03:00
27e31603da Feat: add journal [wip] 2025-07-05 14:15:31 +03:00
4 changed files with 82 additions and 8 deletions

View File

@ -202,13 +202,21 @@ func (b *Bot) BotMove() {
// call llm
llmResp, err := b.CallLLM(prompt)
if err != nil {
room.LogJournal = append(room.LogJournal, b.BotName+" send call got error: "+err.Error())
room.LogJournal = append(room.LogJournal, models.Journal{
Entry: "send call got error: " + err.Error(),
Username: b.BotName,
RoomID: room.ID,
})
b.log.Error("bot loop", "error", err)
return
}
tempMap, err := b.LLMParser.ParseBytes(llmResp)
if err != nil {
room.LogJournal = append(room.LogJournal, b.BotName+" parse resp got error: "+err.Error())
room.LogJournal = append(room.LogJournal, models.Journal{
Entry: "parse resp got error: " + err.Error(),
Username: b.BotName,
RoomID: room.ID,
})
b.log.Error("bot loop", "error", err, "resp", string(llmResp))
return
}
@ -233,8 +241,12 @@ func (b *Bot) BotMove() {
}
room.ActionHistory = append(room.ActionHistory, action)
room.MimeDone = true
meant := fmt.Sprintf(b.BotName+" meant to open: %v", tempMap["words_I_mean_my_team_to_open"])
room.LogJournal = append(room.LogJournal, meant)
entry := fmt.Sprintf("meant to open: %v", tempMap["words_I_mean_my_team_to_open"])
room.LogJournal = append(room.LogJournal, models.Journal{
Entry: entry,
Username: b.BotName,
RoomID: room.ID,
})
eventPayload = mimeResp.Clue + mimeResp.Number
guessLimitU64, err := strconv.ParseUint(mimeResp.Number, 10, 8)
if err != nil {
@ -260,15 +272,24 @@ func (b *Bot) BotMove() {
}
if err := b.checkGuess(guess, room); err != nil {
b.log.Warn("failed to check guess", "mimeResp", tempMap, "bot_name", b.BotName, "guess", guess, "error", err)
msg := fmt.Sprintf("failed to check guess; mimeResp: %v; bot_name: %s; guess: %s; error: %v", tempMap, b.BotName, guess, err)
room.LogJournal = append(room.LogJournal, msg)
entry := fmt.Sprintf("failed to check guess; mimeResp: %v; guess: %s; error: %v", tempMap, guess, err)
room.LogJournal = append(room.LogJournal, models.Journal{
Entry: entry,
Username: b.BotName,
RoomID: room.ID,
})
}
b.log.Info("guesser resp log", "guesserResp", tempMap)
couldBe, err := convertToSliceOfStrings(tempMap["could_be"])
if err != nil {
b.log.Warn("failed to parse could_be", "bot_resp", tempMap, "bot_name", b.BotName)
}
room.LogJournal = append(room.LogJournal, fmt.Sprintf("%s also considered this: %v", b.BotName, couldBe))
entry := fmt.Sprintf("also considered this: %v", couldBe)
room.LogJournal = append(room.LogJournal, models.Journal{
Entry: entry,
Username: b.BotName,
RoomID: room.ID,
})
eventName = models.NotifyRoomUpdatePrefix + room.ID
eventPayload = ""
// TODO: needs to decide if it wants to open the next cardword or end turn

View File

@ -76,3 +76,13 @@ CREATE TABLE sessions(
username TEXT NOT NULL,
FOREIGN KEY (username) REFERENCES players(username) ON DELETE CASCADE
);
CREATE TABLE journal(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
entry TEXT NOT NULL DEFAULT '',
username TEXT NOT NULL,
room_id TEXT NOT NULL,
FOREIGN KEY (username) REFERENCES players(username) ON DELETE CASCADE,
FOREIGN KEY (room_id) REFERENCES rooms(id) ON DELETE CASCADE
);

View File

@ -130,6 +130,14 @@ type CardMark struct {
Username string `db:"username"`
}
type Journal struct {
ID uint32 `db:"id"`
Username string `db:"username"`
RoomID string `db:"room_id"`
Entry string `db:"entry"`
CreatedAt time.Time `db:"created_at"`
}
type Room struct {
ID string `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
@ -151,7 +159,7 @@ type Room struct {
BlueTeam Team `db:"-"`
Cards []WordCard `db:"-"`
BotMap map[string]BotPlayer `db:"-"`
LogJournal []string `db:"-"`
LogJournal []Journal `db:"-"`
Settings GameSettings `db:"-"`
}

35
repos/journal.go Normal file
View File

@ -0,0 +1,35 @@
package repos
import (
"context"
"gralias/models"
"github.com/jmoiron/sqlx"
)
type JournalRepo interface {
JournalByRoomID(ctx context.Context, roomID string) ([]models.Journal, error)
JournalCreate(ctx context.Context, j *models.Journal) error
JournalDeleteByRoomID(ctx context.Context, roomID string) error
}
func (p *RepoProvider) JournalByRoomID(ctx context.Context, roomID string) ([]models.Journal, error) {
journals := []models.Journal{}
err := sqlx.SelectContext(ctx, p.DB, &journals, `SELECT id, created_at, entry, username, room_id FROM journal WHERE room_id = ? ORDER BY created_at ASC`, roomID)
if err != nil {
return nil, err
}
return journals, nil
}
func (p *RepoProvider) JournalCreate(ctx context.Context, j *models.Journal) error {
db := getDB(ctx, p.DB)
_, err := db.ExecContext(ctx, `INSERT INTO journal (entry, username, room_id) VALUES (?, ?, ?)`, j.Entry, j.Username, j.RoomID)
return err
}
func (p *RepoProvider) JournalDeleteByRoomID(ctx context.Context, roomID string) error {
db := getDB(ctx, p.DB)
_, err := db.ExecContext(ctx, `DELETE FROM journal WHERE room_id = ?`, roomID)
return err
}