Fix: session to create player only if does not exist

This commit is contained in:
Grail Finder
2025-07-07 09:21:18 +03:00
parent 7ae255cc04
commit a2c5f17e30
4 changed files with 20 additions and 29 deletions

View File

@ -4,8 +4,7 @@
{{ else if ne .LinkLogin "" }}
{{template "linklogin" .LinkLogin}}
{{ else if not .State.RoomID }}
<div id="hello-user">
<p>data: {{.}} {{.State}} {{.Room}}</p>
<div id="hello-user" class="text-xl py-2">
<p>Hello {{.State.Username}}</p>
</div>
<div id="create-room" class="create-room-div">

View File

@ -90,6 +90,7 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
// userstate, err := loadState(cleanName)
userstate, err := repo.PlayerGetByName(r.Context(), cleanName)
if err != nil || userstate == nil {
log.Debug("making new player", "error", err, "state", userstate)
userstate = models.InitPlayer(cleanName)
makeplayer = true
}
@ -106,7 +107,7 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
return
}
// room.PlayerList = append(room.PlayerList, fi.State.Username)
fi.Room = room
// fi.Room = room
fi.List = nil
fi.State.RoomID = &room.ID
if err := repo.PlayerSetRoomID(r.Context(), room.ID, fi.State.Username); err != nil {
@ -181,11 +182,14 @@ func makeCookie(username string, remote string) (*http.Cookie, error) {
cookie.Secure = false
log.Info("changing cookie domain", "domain", cookie.Domain)
}
// make player first, since username is fk to players table
player := models.InitPlayer(username)
if err := repo.PlayerAdd(context.Background(), player); err != nil {
slog.Error("failed to create player", "username", username)
return nil, err
player, err := repo.PlayerGetByName(context.Background(), username)
if err != nil || player == nil {
// make player first, since username is fk to players table
player = models.InitPlayer(username)
if err := repo.PlayerAdd(context.Background(), player); err != nil {
slog.Error("failed to create player", "username", username)
return nil, err
}
}
if err := repo.SessionCreate(context.Background(), session); err != nil {
return nil, err

View File

@ -25,7 +25,7 @@ var (
// got prompt: control character (\\u0000-\\u001F) found while parsing a string at line 4 column 0
MimePrompt = `we are playing alias;\nyou are a mime (player who gives a clue of one noun word and number of cards you expect them to open) of the %s team (people who would guess by your clue want open the %s cards);\nplease return your clue, number of cards to open and what words you mean them to find using that clue in json like:\n{\n\"clue\": \"one-word-noun\",\n\"number\": \"number-from-0-to-9\",\n\"words_I_mean_my_team_to_open\": [\"this\", \"that\", ...]\n}\nthe team who openes all their cards first wins.\nplease return json only.\nunopen Blue cards left: %d;\nunopen Red cards left: %d;\nhere is the game info in json:\n%s`
GuesserPrompt = `we are playing alias;\nyou are to guess words of the %s team (you want open %s cards) by given clue and a number of meant guesses;\nplease return your guesses and words that could be meant by the clue, but you do not wish to open yet, in json like:\n{\n\"guesses\": [\"word1\", \"word2\", ...],\n\"could_be\": [\"this\", \"that\", ...]\n}\nthe team who openes all their cards first wins.\nplease return json only.\nunopen Blue cards left: %d;\nunopen Red cards left: %d;\nhere is the cards (and other info), you need to choose revealed==false words:\n%s`
GuesserSimplePrompt = `we are playing game of alias;\n you were given a clue: \"%s\";\nplease return your guess and words that could be meant by the clue, but you do not wish to open yet, in json like:\n{\n\"guess\": \"most_relevant_word_to_the_clue\",\n\"could_be\": [\"this\", \"that\", ...]\n}\nhere is the words that left:\n%v`
GuesserSimplePrompt = `we are playing game of alias;\n you were given a clue: \"%s\";\nplease return your guess and words that could be meant by the clue, but you do not wish to open yet, in json like:\n{\n\"guess\": \"most_relevant_word_to_the_clue\",\n\"could_be\": [\"this\", \"that\", ...]\n}\nhere is the words that you can choose from:\n%v`
MimeSimplePrompt = `we are playing alias;\nyou are to give one word clue and a number of words you mean your team to open; your team words: %v;\nhere are the words of opposite team you want to avoid: %v;\nand here is a black word that is critical not to pick: %s;\nplease return your clue, number of cards to open and what words you mean them to find using that clue in json like:\n{\n\"clue\": \"one-word-noun\",\n\"number\": \"number-from-0-to-9-as-string\",\n\"words_I_mean_my_team_to_open\": [\"this\", \"that\", ...]\n}\nplease return json only.\nunopen Blue cards left: %d;\nunopen Red cards left: %d;`
)
@ -50,23 +50,6 @@ func convertToSliceOfStrings(value any) ([]string, error) {
}
}
// nolint: unused
func (b *Bot) checkGuesses(tempMap map[string]any, room *models.Room) error {
guesses, err := convertToSliceOfStrings(tempMap["guesses"])
if err != nil {
b.log.Warn("failed to parse bot given guesses", "mimeResp", tempMap, "bot_name", b.BotName)
return err
}
for _, word := range guesses {
if err := b.checkGuess(word, room); err != nil {
// log error
b.log.Warn("failed to check guess", "mimeResp", tempMap, "bot_name", b.BotName, "guess", word)
return err
}
}
return nil
}
func (b *Bot) checkGuess(word string, room *models.Room) error {
// color, exists := room.WCMap[word]
color, exists := room.FindColor(word)
@ -288,13 +271,17 @@ func (b *Bot) BotMove() {
b.log.Warn("failed to parse guess", "mimeResp", tempMap, "bot_name", b.BotName)
}
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)
b.log.Error("failed to check guess", "mimeResp", tempMap, "bot_name", b.BotName, "guess", guess, "error", err)
entry := fmt.Sprintf("failed to check guess; mimeResp: %v; guess: %s; error: %v", tempMap, guess, err)
room.LogJournal = append(room.LogJournal, models.Journal{
lj := models.Journal{
Entry: entry,
Username: b.BotName,
RoomID: room.ID,
})
}
room.LogJournal = append(room.LogJournal, lj)
if err := repo.JournalCreate(context.Background(), &lj); err != nil {
b.log.Warn("failed to write to journal", "entry", lj)
}
}
b.log.Info("guesser resp log", "guesserResp", tempMap)
couldBe, err := convertToSliceOfStrings(tempMap["could_be"])

View File

@ -85,3 +85,4 @@
- start new game satrted timer for a mime; (feature? in other cases mime has no timer);
- timer ended and went to 300;
- mime sees the clue input out of turn;