Enha: stop timer if turn ended

This commit is contained in:
Grail Finder
2025-07-01 09:38:03 +03:00
parent e6177df5d8
commit 036def3819
7 changed files with 50 additions and 25 deletions

View File

@ -303,7 +303,12 @@ func notify(event, msg string) {
} }
func loadCards(room *models.Room) { func loadCards(room *models.Room) {
wl := wordloader.InitDefaultLoader("assets/words/en_nouns.txt") // store it somewhere
wordMap := map[string]string{
"en": "assets/words/en_nouns.txt",
"ru": "assets/words/ru_nouns.txt",
}
wl := wordloader.InitDefaultLoader(wordMap[room.Settings.Language])
cards, err := wl.Load() cards, err := wl.Load()
if err != nil { if err != nil {
// no logger // no logger

View File

@ -84,6 +84,7 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) {
fi.Room.OpenedThisTurn = 0 fi.Room.OpenedThisTurn = 0
fi.Room.ThisTurnLimit = 0 fi.Room.ThisTurnLimit = 0
fi.Room.ClearMarks() fi.Room.ClearMarks()
StopTurnTimer(fi.Room.ID)
} }
switch string(color) { switch string(color) {
case string(models.WordColorBlack): case string(models.WordColorBlack):
@ -102,6 +103,7 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) {
fi.Room.ThisTurnLimit = 0 fi.Room.ThisTurnLimit = 0
fi.Room.ActionHistory = append(fi.Room.ActionHistory, action) fi.Room.ActionHistory = append(fi.Room.ActionHistory, action)
fi.Room.ClearMarks() fi.Room.ClearMarks()
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 opposite color word", "room", fi.Room, "opposite-color", oppositeColor)
// end turn // end turn
@ -109,6 +111,7 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) {
fi.Room.MimeDone = false fi.Room.MimeDone = false
fi.Room.OpenedThisTurn = 0 fi.Room.OpenedThisTurn = 0
fi.Room.ThisTurnLimit = 0 fi.Room.ThisTurnLimit = 0
StopTurnTimer(fi.Room.ID)
// check if no cards left => game over // check if no cards left => game over
if fi.Room.BlueCounter == 0 { if fi.Room.BlueCounter == 0 {
// blue won // blue won

View File

@ -1,13 +1,19 @@
package handlers package handlers
import ( import (
"fmt"
"gralias/models" "gralias/models"
"sync" "sync"
"time" "time"
) )
type roomTimer struct {
ticker *time.Ticker
done chan bool
}
var ( var (
timers = make(map[string]*time.Ticker) timers = make(map[string]*roomTimer)
mu sync.Mutex mu sync.Mutex
) )
@ -18,24 +24,31 @@ func StartTurnTimer(roomID string, duration time.Duration) {
if _, exists := timers[roomID]; exists { if _, exists := timers[roomID]; exists {
return // Timer already running return // Timer already running
} }
ticker := time.NewTicker(1 * time.Second) ticker := time.NewTicker(1 * time.Second)
timers[roomID] = ticker done := make(chan bool)
timers[roomID] = &roomTimer{ticker: ticker, done: done}
go func() { go func() {
for range ticker.C { for {
select {
case <-done:
return
case <-ticker.C:
room, err := getRoomByID(roomID) room, err := getRoomByID(roomID)
if err != nil { if err != nil {
log.Error("failed to get room by id", "error", err) log.Error("failed to get room by id", "error", err)
StopTurnTimer(roomID) StopTurnTimer(roomID)
return return
} }
if room.Settings.TurnSecondsLeft == 0 { if room.Settings.TurnSecondsLeft <= 0 {
log.Info("turn time is over", "room_id", roomID) log.Info("turn time is over", "room_id", roomID)
room.ChangeTurn() room.ChangeTurn()
room.MimeDone = false room.MimeDone = false
if err := saveRoom(room); err != nil { if err := saveRoom(room); err != nil {
log.Error("failed to save room", "error", err) log.Error("failed to save room", "error", err)
} }
notify(models.NotifyRoomUpdatePrefix+room.ID, "") notify(models.NotifyTurnTimerPrefix+room.ID, fmt.Sprintf("%d", room.Settings.TurnSecondsLeft))
notifyBotIfNeeded(room) notifyBotIfNeeded(room)
StopTurnTimer(roomID) StopTurnTimer(roomID)
return return
@ -46,15 +59,17 @@ func StartTurnTimer(roomID string, duration time.Duration) {
} }
notify(models.NotifyRoomUpdatePrefix+room.ID, "") notify(models.NotifyRoomUpdatePrefix+room.ID, "")
} }
}
}() }()
} }
func StopTurnTimer(roomID string) { func StopTurnTimer(roomID string) {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
if timer, exists := timers[roomID]; exists {
if ticker, exists := timers[roomID]; exists { timer.ticker.Stop()
ticker.Stop() close(timer.done)
delete(timers, roomID) delete(timers, roomID)
} }
} }

View File

@ -25,7 +25,7 @@ var (
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` 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` 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 left:\n%v`
MimeSimplePrompt = `we are playing alias;\nyou are to give a 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;` 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;`
) )
func convertToSliceOfStrings(value any) ([]string, error) { func convertToSliceOfStrings(value any) ([]string, error) {

View File

@ -14,4 +14,5 @@ var (
NotifyRoomUpdatePrefix = "roomupdate_" NotifyRoomUpdatePrefix = "roomupdate_"
NotifyBacklogPrefix = "backlog_" NotifyBacklogPrefix = "backlog_"
NotifyJournalPrefix = "journal_" NotifyJournalPrefix = "journal_"
NotifyTurnTimerPrefix = "turntimer_"
) )

View File

@ -62,3 +62,4 @@
- bot team does not loses their turn after white card (or limit); - bot team does not loses their turn after white card (or limit);
- name check does not work; - name check does not work;
- game did not end when all blue cards were open; - game did not end when all blue cards were open;
- bot ends a turn after guessing one word only;