Enha: stop timer if turn ended
This commit is contained in:
		| @@ -303,7 +303,12 @@ func notify(event, msg string) { | ||||
| } | ||||
|  | ||||
| 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() | ||||
| 	if err != nil { | ||||
| 		// no logger | ||||
|   | ||||
| @@ -84,6 +84,7 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { | ||||
| 		fi.Room.OpenedThisTurn = 0 | ||||
| 		fi.Room.ThisTurnLimit = 0 | ||||
| 		fi.Room.ClearMarks() | ||||
| 		StopTurnTimer(fi.Room.ID) | ||||
| 	} | ||||
| 	switch string(color) { | ||||
| 	case string(models.WordColorBlack): | ||||
| @@ -102,6 +103,7 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { | ||||
| 		fi.Room.ThisTurnLimit = 0 | ||||
| 		fi.Room.ActionHistory = append(fi.Room.ActionHistory, action) | ||||
| 		fi.Room.ClearMarks() | ||||
| 		StopTurnTimer(fi.Room.ID) | ||||
| 	case string(models.WordColorWhite), string(oppositeColor): | ||||
| 		log.Debug("opened opposite color word", "room", fi.Room, "opposite-color", oppositeColor) | ||||
| 		// end turn | ||||
| @@ -109,6 +111,7 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { | ||||
| 		fi.Room.MimeDone = false | ||||
| 		fi.Room.OpenedThisTurn = 0 | ||||
| 		fi.Room.ThisTurnLimit = 0 | ||||
| 		StopTurnTimer(fi.Room.ID) | ||||
| 		// check if no cards left => game over | ||||
| 		if fi.Room.BlueCounter == 0 { | ||||
| 			// blue won | ||||
|   | ||||
| @@ -1,13 +1,19 @@ | ||||
| package handlers | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"gralias/models" | ||||
| 	"sync" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| type roomTimer struct { | ||||
| 	ticker *time.Ticker | ||||
| 	done   chan bool | ||||
| } | ||||
|  | ||||
| var ( | ||||
| 	timers = make(map[string]*time.Ticker) | ||||
| 	timers = make(map[string]*roomTimer) | ||||
| 	mu     sync.Mutex | ||||
| ) | ||||
|  | ||||
| @@ -18,33 +24,41 @@ func StartTurnTimer(roomID string, duration time.Duration) { | ||||
| 	if _, exists := timers[roomID]; exists { | ||||
| 		return // Timer already running | ||||
| 	} | ||||
|  | ||||
| 	ticker := time.NewTicker(1 * time.Second) | ||||
| 	timers[roomID] = ticker | ||||
| 	done := make(chan bool) | ||||
| 	timers[roomID] = &roomTimer{ticker: ticker, done: done} | ||||
|  | ||||
| 	go func() { | ||||
| 		for range ticker.C { | ||||
| 			room, err := getRoomByID(roomID) | ||||
| 			if err != nil { | ||||
| 				log.Error("failed to get room by id", "error", err) | ||||
| 				StopTurnTimer(roomID) | ||||
| 		for { | ||||
| 			select { | ||||
| 			case <-done: | ||||
| 				return | ||||
| 			} | ||||
| 			if room.Settings.TurnSecondsLeft == 0 { | ||||
| 				log.Info("turn time is over", "room_id", roomID) | ||||
| 				room.ChangeTurn() | ||||
| 				room.MimeDone = false | ||||
| 			case <-ticker.C: | ||||
| 				room, err := getRoomByID(roomID) | ||||
| 				if err != nil { | ||||
| 					log.Error("failed to get room by id", "error", err) | ||||
| 					StopTurnTimer(roomID) | ||||
| 					return | ||||
| 				} | ||||
| 				if room.Settings.TurnSecondsLeft <= 0 { | ||||
| 					log.Info("turn time is over", "room_id", roomID) | ||||
| 					room.ChangeTurn() | ||||
| 					room.MimeDone = false | ||||
| 					if err := saveRoom(room); err != nil { | ||||
| 						log.Error("failed to save room", "error", err) | ||||
| 					} | ||||
| 					notify(models.NotifyTurnTimerPrefix+room.ID, fmt.Sprintf("%d", room.Settings.TurnSecondsLeft)) | ||||
| 					notifyBotIfNeeded(room) | ||||
| 					StopTurnTimer(roomID) | ||||
| 					return | ||||
| 				} | ||||
| 				room.Settings.TurnSecondsLeft-- | ||||
| 				if err := saveRoom(room); err != nil { | ||||
| 					log.Error("failed to save room", "error", err) | ||||
| 				} | ||||
| 				notify(models.NotifyRoomUpdatePrefix+room.ID, "") | ||||
| 				notifyBotIfNeeded(room) | ||||
| 				StopTurnTimer(roomID) | ||||
| 				return | ||||
| 			} | ||||
| 			room.Settings.TurnSecondsLeft-- | ||||
| 			if err := saveRoom(room); err != nil { | ||||
| 				log.Error("failed to save room", "error", err) | ||||
| 			} | ||||
| 			notify(models.NotifyRoomUpdatePrefix+room.ID, "") | ||||
| 		} | ||||
| 	}() | ||||
| } | ||||
| @@ -52,9 +66,10 @@ func StartTurnTimer(roomID string, duration time.Duration) { | ||||
| func StopTurnTimer(roomID string) { | ||||
| 	mu.Lock() | ||||
| 	defer mu.Unlock() | ||||
|  | ||||
| 	if ticker, exists := timers[roomID]; exists { | ||||
| 		ticker.Stop() | ||||
| 	if timer, exists := timers[roomID]; exists { | ||||
| 		timer.ticker.Stop() | ||||
| 		close(timer.done) | ||||
| 		delete(timers, roomID) | ||||
| 	} | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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` | ||||
| 	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` | ||||
| 	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) { | ||||
|   | ||||
| @@ -14,4 +14,5 @@ var ( | ||||
| 	NotifyRoomUpdatePrefix = "roomupdate_" | ||||
| 	NotifyBacklogPrefix    = "backlog_" | ||||
| 	NotifyJournalPrefix    = "journal_" | ||||
| 	NotifyTurnTimerPrefix  = "turntimer_" | ||||
| ) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder