Feat: word loader

This commit is contained in:
Grail Finder
2025-05-13 07:49:16 +03:00
parent f752d2a162
commit 0c94811632
5 changed files with 6043 additions and 31 deletions

5909
assets/words/en_nouns.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -4,9 +4,11 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"golias/broker" "golias/broker"
"golias/models" "golias/models"
"golias/utils" "golias/utils"
"golias/wordloader"
"strings" "strings"
) )
@ -248,3 +250,14 @@ func notify(event, msg string) {
Payload: msg, Payload: msg,
} }
} }
func loadCards(room *models.Room) {
wl := wordloader.InitDefaultLoader("assets/words/en_nouns.txt")
cards, err := wl.Load()
if err != nil {
// no logger
fmt.Println("failed to load cards", "error", err)
}
room.Cards = cards
}

View File

@ -176,7 +176,8 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) {
fi.Room.IsRunning = true fi.Room.IsRunning = true
fi.Room.IsOver = false fi.Room.IsOver = false
fi.Room.TeamTurn = "blue" fi.Room.TeamTurn = "blue"
fi.Room.LoadTestCards() // fi.Room.LoadTestCards()
loadCards(fi.Room)
fi.Room.UpdateCounter() fi.Room.UpdateCounter()
fi.Room.TeamWon = "" fi.Room.TeamWon = ""
if err := saveFullInfo(fi); err != nil { if err := saveFullInfo(fi); err != nil {

View File

@ -103,36 +103,45 @@ func (r *Room) UpdateCounter() {
r.BlueCounter = blueCounter r.BlueCounter = blueCounter
} }
func (r *Room) LoadTestCards() { // func (r *Room) LoadTestCards() {
cards := []WordCard{ // // TODO: pass room settings
{Word: "hamster", Color: "blue"}, // // TODO: map language to path
{Word: "child", Color: "red"}, // wl := wordloader.InitDefaultLoader("assets/words/en_nouns.txt")
{Word: "wheel", Color: "white"}, // cards, err := wl.Load()
{Word: "condition", Color: "black"}, // if err != nil {
{Word: "test", Color: "white"}, // // no logger
{Word: "ball", Color: "blue"}, // fmt.Println("failed to load cards", "error", err)
{Word: "violin", Color: "red"}, // }
{Word: "rat", Color: "white"}, // r.Cards = cards
{Word: "perplexity", Color: "blue"}, // // cards := []WordCard{
{Word: "notion", Color: "red"}, // // {Word: "hamster", Color: "blue"},
{Word: "guitar", Color: "blue"}, // // {Word: "child", Color: "red"},
{Word: "ocean", Color: "blue"}, // // {Word: "wheel", Color: "white"},
{Word: "moon", Color: "blue"}, // // {Word: "condition", Color: "black"},
{Word: "coffee", Color: "blue"}, // // {Word: "test", Color: "white"},
{Word: "mountain", Color: "blue"}, // // {Word: "ball", Color: "blue"},
{Word: "book", Color: "blue"}, // // {Word: "violin", Color: "red"},
{Word: "camera", Color: "blue"}, // // {Word: "rat", Color: "white"},
{Word: "apple", Color: "red"}, // // {Word: "perplexity", Color: "blue"},
{Word: "fire", Color: "red"}, // // {Word: "notion", Color: "red"},
{Word: "rose", Color: "red"}, // // {Word: "guitar", Color: "blue"},
{Word: "sun", Color: "red"}, // // {Word: "ocean", Color: "blue"},
{Word: "cherry", Color: "red"}, // // {Word: "moon", Color: "blue"},
{Word: "heart", Color: "red"}, // // {Word: "coffee", Color: "blue"},
{Word: "tomato", Color: "red"}, // // {Word: "mountain", Color: "blue"},
{Word: "cloud", Color: "white"}, // // {Word: "book", Color: "blue"},
} // // {Word: "camera", Color: "blue"},
r.Cards = cards // // {Word: "apple", Color: "red"},
} // // {Word: "fire", Color: "red"},
// // {Word: "rose", Color: "red"},
// // {Word: "sun", Color: "red"},
// // {Word: "cherry", Color: "red"},
// // {Word: "heart", Color: "red"},
// // {Word: "tomato", Color: "red"},
// // {Word: "cloud", Color: "white"},
// // }
// // r.Cards = cards
// }
func (r *Room) ChangeTurn() { func (r *Room) ChangeTurn() {
switch r.TeamTurn { switch r.TeamTurn {

80
wordloader/wordloader.go Normal file
View File

@ -0,0 +1,80 @@
package wordloader
import (
"golias/models"
"math/rand/v2"
"os"
"strings"
)
type Loader struct {
FilePath string
BlackCount uint8
WhiteCount uint8
RedCount uint8
BlueCount uint8
}
func InitDefaultLoader(fpath string) *Loader {
return &Loader{
FilePath: fpath,
BlackCount: 1,
WhiteCount: 7,
RedCount: 8,
BlueCount: 9,
}
}
func (l *Loader) WholeCount() uint8 {
return l.BlackCount + l.WhiteCount + l.RedCount + l.BlueCount
}
func (l *Loader) Load() ([]models.WordCard, error) {
data, err := os.ReadFile(l.FilePath)
if err != nil {
return nil, err
}
lines := strings.Split(string(data), "\n")
rand.Shuffle(len(lines), func(i, j int) {
lines[i], lines[j] = lines[j], lines[i]
})
words := lines[:int(l.WholeCount())]
cards := make([]models.WordCard, l.WholeCount())
for i, word := range words {
if l.BlackCount > 0 {
cards[i] = models.WordCard{
Word: word,
Color: "black",
}
l.BlackCount--
continue
}
if l.WhiteCount > 0 {
cards[i] = models.WordCard{
Word: word,
Color: "white",
}
l.WhiteCount--
continue
}
if l.RedCount > 0 {
cards[i] = models.WordCard{
Word: word,
Color: "red",
}
l.RedCount--
continue
}
if l.BlueCount > 0 {
cards[i] = models.WordCard{
Word: word,
Color: "blue",
}
l.BlueCount--
}
}
rand.Shuffle(len(cards), func(i, j int) {
cards[i], cards[j] = cards[j], cards[i]
})
return cards, nil
}