Skip to content

Commit 018e848

Browse files
committed
Allow using custom words only; Fixes #384
1 parent 41bc4a4 commit 018e848

5 files changed

Lines changed: 35 additions & 3 deletions

File tree

internal/api/v1.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ func (handler *V1Handler) postLobby(writer http.ResponseWriter, request *http.Re
114114
}
115115

116116
scoreCalculation, scoreCalculationInvalid := ParseScoreCalculation(request.Form.Get("score_calculation"))
117-
languageData, languageKey, languageInvalid := ParseLanguage(request.Form.Get("language"))
117+
languageRawValue := strings.ToLower(strings.TrimSpace(request.Form.Get("language")))
118+
languageData, languageKey, languageInvalid := ParseLanguage(languageRawValue)
118119
drawingTime, drawingTimeInvalid := ParseDrawingTime(handler.cfg, request.Form.Get("drawing_time"))
119120
rounds, roundsInvalid := ParseRounds(handler.cfg, request.Form.Get("rounds"))
120121
maxPlayers, maxPlayersInvalid := ParseMaxPlayers(handler.cfg, request.Form.Get("max_players"))
@@ -133,6 +134,7 @@ func (handler *V1Handler) postLobby(writer http.ResponseWriter, request *http.Re
133134
} else {
134135
lowercaser = languageData.Lowercaser()
135136
}
137+
136138
customWords, customWordsInvalid := ParseCustomWords(lowercaser, request.Form.Get("custom_words"))
137139

138140
if scoreCalculationInvalid != nil {
@@ -155,6 +157,10 @@ func (handler *V1Handler) postLobby(writer http.ResponseWriter, request *http.Re
155157
}
156158
if customWordsPerTurnInvalid != nil {
157159
requestErrors = append(requestErrors, customWordsPerTurnInvalid.Error())
160+
} else {
161+
if languageRawValue == "custom" && len(customWords) == 0 {
162+
requestErrors = append(requestErrors, "custom words must be provided when using custom language")
163+
}
158164
}
159165
if clientsPerIPLimitInvalid != nil {
160166
requestErrors = append(requestErrors, clientsPerIPLimitInvalid.Error())

internal/frontend/index.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ func (handler *SSRHandler) ssrCreateLobby(writer http.ResponseWriter, request *h
185185
}
186186

187187
scoreCalculation, scoreCalculationInvalid := api.ParseScoreCalculation(request.Form.Get("score_calculation"))
188-
languageData, languageKey, languageInvalid := api.ParseLanguage(request.Form.Get("language"))
188+
languageRawValue := request.Form.Get("language")
189+
languageData, languageKey, languageInvalid := api.ParseLanguage(languageRawValue)
189190
drawingTime, drawingTimeInvalid := api.ParseDrawingTime(handler.cfg, request.Form.Get("drawing_time"))
190191
rounds, roundsInvalid := api.ParseRounds(handler.cfg, request.Form.Get("rounds"))
191192
maxPlayers, maxPlayersInvalid := api.ParseMaxPlayers(handler.cfg, request.Form.Get("max_players"))
@@ -247,6 +248,10 @@ func (handler *SSRHandler) ssrCreateLobby(writer http.ResponseWriter, request *h
247248
}
248249
if customWordsPerTurnInvalid != nil {
249250
pageData.Errors = append(pageData.Errors, customWordsPerTurnInvalid.Error())
251+
} else {
252+
if languageRawValue == "custom" && len(customWords) == 0 {
253+
pageData.Errors = append(pageData.Errors, "custom words must be provided when using custom language")
254+
}
250255
}
251256
if clientsPerIPLimitInvalid != nil {
252257
pageData.Errors = append(pageData.Errors, clientsPerIPLimitInvalid.Error())

internal/game/data.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ type Lobby struct {
3636
DrawingTimeNew int
3737

3838
CustomWords []string
39-
words []string
39+
// customWordIndex is used to keep track of the next word to be drawn from
40+
// the custom word stack. Only used if exclusive custom word mode is active.
41+
customWordIndex int
42+
words []string
4043

4144
// players references all participants of the Lobby.
4245
players []*Player

internal/game/lobby.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var SupportedScoreCalculations = []string{
2626
}
2727

2828
var SupportedLanguages = map[string]string{
29+
"custom": "Custom words only",
2930
"english_gb": "English (GB)",
3031
"english": "English (US)",
3132
"italian": "Italian",

internal/game/words.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ type LanguageData struct {
2222
var (
2323
ErrUnknownWordList = errors.New("wordlist unknown")
2424
WordlistData = map[string]LanguageData{
25+
"custom": {
26+
LanguageCode: "en_gb",
27+
Lowercaser: func() cases.Caser { return cases.Lower(language.BritishEnglish) },
28+
},
2529
"english_gb": {
2630
LanguageCode: "en_gb",
2731
Lowercaser: func() cases.Caser { return cases.Lower(language.BritishEnglish) },
@@ -142,6 +146,19 @@ func GetRandomWords(wordCount int, lobby *Lobby) []string {
142146
func getRandomWords(wordCount int, lobby *Lobby, reloadWords func(lobby *Lobby) ([]string, error)) []string {
143147
words := make([]string, wordCount)
144148

149+
// If we have custom words only, we don't want to pop them off the stack.
150+
// We want to keep going in circles, worstcase returning the same word 3 times.
151+
if lobby.Wordpack == "custom" && len(lobby.CustomWords) > 0 {
152+
for i := range wordCount {
153+
if lobby.customWordIndex >= len(lobby.CustomWords) {
154+
lobby.customWordIndex = 0
155+
}
156+
words[i] = lobby.CustomWords[lobby.customWordIndex]
157+
lobby.customWordIndex++
158+
}
159+
return words
160+
}
161+
145162
for customWordsLeft, i := lobby.CustomWordsPerTurn, 0; i < wordCount; i++ {
146163
if customWordsLeft > 0 && len(lobby.CustomWords) > 0 {
147164
customWordsLeft--

0 commit comments

Comments
 (0)