Skip to content

Commit

Permalink
Simplify popping words
Browse files Browse the repository at this point in the history
  • Loading branch information
Bios-Marcel committed Aug 24, 2023
1 parent 52efedb commit 726a894
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 45 deletions.
21 changes: 15 additions & 6 deletions internal/game/lobby_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,26 @@ func getUnexportedField(field reflect.Value) any {
}

func Test_wordSelectionEvent(t *testing.T) {
firstWordChoice := "abc"
lobby := &Lobby{
mutex: &sync.Mutex{},
EditableLobbySettings: &EditableLobbySettings{
DrawingTime: 10,
Rounds: 10,
},
words: []string{firstWordChoice, "def", "ghi"},
words: []string{"abc", "def", "ghi"},
}
wordHintEvents := make(map[uuid.UUID][]*WordHint)
lobby.WriteObject = noOpWriteObject
var wordChoice []string
lobby.WriteObject = func(player *Player, message easyjson.Marshaler) error {
event, ok := message.(*Event)
if ok {
if event.Type == EventTypeYourTurn {
wordChoice = event.Data.([]string)
}
}

return nil
}
lobby.WritePreparedMessage = func(player *Player, message *websocket.PreparedMessage) error {
data := getUnexportedField(reflect.ValueOf(message).Elem().FieldByName("data")).([]byte)
type event struct {
Expand All @@ -234,14 +243,14 @@ func Test_wordSelectionEvent(t *testing.T) {
t.Fatal("error unmarshalling message", err)
}

t.Log(e.Type)
if e.Type == "update-wordhint" {
var wordHints []*WordHint
if err := json.Unmarshal(e.Data, &wordHints); err != nil {
t.Fatal(err)
t.Fatal("error unmarshalling word hints:", err)
}
wordHintEvents[player.ID] = wordHints
}

return nil
}

Expand Down Expand Up @@ -276,7 +285,7 @@ func Test_wordSelectionEvent(t *testing.T) {
t.Error("Word hints for drawer contained not underlined character")
}

expectedRune := rune(firstWordChoice[index])
expectedRune := rune(wordChoice[0][index])
if wordHint.Character != expectedRune {
t.Errorf("Character at index %d was %c instead of %c", index, wordHint.Character, expectedRune)
}
Expand Down
62 changes: 23 additions & 39 deletions internal/game/words.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,59 +84,43 @@ func GetRandomWords(wordCount int, lobby *Lobby) []string {
// numbers. This can be used for predictability in unit tests.
// See GetRandomWords for functionality documentation.
func getRandomWordsCustomRng(wordCount int, lobby *Lobby, rng func() int) []string {
if lobby.CustomWordsChance > 0 && len(lobby.CustomWords) > 0 {
// Always get custom words
if lobby.CustomWordsChance == 100 {
if len(lobby.CustomWords) >= wordCount {
return popCustomWords(wordCount, lobby)
}

leftOverCustomWords := len(lobby.CustomWords)
return append(
popCustomWords(len(lobby.CustomWords), lobby),
popWordpackWords(wordCount-leftOverCustomWords, lobby)...)
words := make([]string, wordCount)
for i := 0; i < wordCount; i++ {
if lobby.CustomWordsChance > 0 && len(lobby.CustomWords) > 0 && rng() <= lobby.CustomWordsChance {
words[i] = popCustomWord(lobby)
} else {
words[i] = popWordpackWord(lobby)
}

words := make([]string, 0, wordCount)
for i := 0; i < wordCount; i++ {
if len(lobby.CustomWords) >= 1 && rng() <= lobby.CustomWordsChance {
words = append(words, popCustomWords(1, lobby)...)
} else {
words = append(words, popWordpackWords(1, lobby)...)
}
}

return words
}

return popWordpackWords(wordCount, lobby)
return words
}

func popCustomWords(wordCount int, lobby *Lobby) []string {
wordIndex := len(lobby.CustomWords) - wordCount
lastWords := lobby.CustomWords[wordIndex:]
lobby.CustomWords = lobby.CustomWords[:wordIndex]
return lastWords
func popCustomWord(lobby *Lobby) string {
lastIndex := len(lobby.CustomWords) - 1
lastWord := lobby.CustomWords[lastIndex]
lobby.CustomWords = lobby.CustomWords[:lastIndex]
return lastWord
}

// popWordpackWords gets X words from the wordpack. The major difference to
// popWordpackWord gets X words from the wordpack. The major difference to
// popCustomWords is, that the wordlist gets reset and reshuffeled once every
// item has been popped.
func popWordpackWords(wordCount int, lobby *Lobby) []string {
if len(lobby.words) < wordCount {
var readError error
lobby.words, readError = readWordList(lobby.lowercaser, lobby.Wordpack)
if readError != nil {
func popWordpackWord(lobby *Lobby) string {
if len(lobby.words) == 0 {
var err error
lobby.words, err = readWordList(lobby.lowercaser, lobby.Wordpack)
if err != nil {
// Since this list should've been successfully read once before, we
// can "safely" panic if this happens, assuming that there's a
// deeper problem.
panic(readError)
panic(err)
}
}
wordIndex := len(lobby.words) - wordCount
lastThreeWords := lobby.words[wordIndex:]
lobby.words = lobby.words[:wordIndex]
return lastThreeWords
lastIndex := len(lobby.words) - 1
lastWord := lobby.words[lastIndex]
lobby.words = lobby.words[:lastIndex]
return lastWord
}

func shuffleWordList(wordlist []string) {
Expand Down

0 comments on commit 726a894

Please sign in to comment.