From f17328a9ee800d84899ca4e7eaccb00381b37d8e Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Sun, 15 Dec 2024 01:58:40 -0700 Subject: [PATCH] games passes test --- cmd/server/games.go | 64 ++++++++++++++++++++++++- cmd/server/games_test.go | 2 +- cmd/server/holiday.txt | 100 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 cmd/server/holiday.txt diff --git a/cmd/server/games.go b/cmd/server/games.go index 4b2211a..6f257a0 100644 --- a/cmd/server/games.go +++ b/cmd/server/games.go @@ -3,11 +3,13 @@ package main import ( "context" "database/sql" + _ "embed" "encoding/json" "fmt" "maps" "math/rand" "slices" + "strings" "time" "github.com/google/uuid" @@ -94,7 +96,7 @@ type ( } KillWords struct { - Global string + Global KillWord Assigned time.Time Assignee string @@ -264,6 +266,9 @@ func (games Games) CreateEventPlayerLeave(ctx context.Context, id string, player return games.createEvent(ctx, id, EventPlayerLeave{Type: PlayerLeave, ID: player}) } +//go:embed holiday.txt +var wordsHoliday string + func (games Games) CreateEventAssignmentRotation(ctx context.Context, id string, killer, victim, word string, points int) error { state, err := games.GameState(ctx, id) if err != nil { @@ -308,6 +313,11 @@ func (games Games) CreateEventAssignmentRotation(ctx context.Context, id string, Assignment: killerState.KillWords.Assignment, } toAssign = slices.DeleteFunc(toAssign, func(s string) bool { return s == event.KillWords[killer].Assignee }) + + if killerState.KillWords.Global.Word != word { + victimState.KillWords.Assignment = Assignment{} + state.Players[victim] = victimState + } } for !func() bool { @@ -337,7 +347,57 @@ func (games Games) CreateEventAssignmentRotation(ctx context.Context, id string, }() { } - // TODO generate .Global, .Assignments.Public, .Assignments.Private and + globalsInUse := map[string]any{} + publicsInUse := map[string]any{} + privatesInUse := map[string]any{} + for _, v := range event.KillWords { + globalsInUse[v.Global.Word] = nil + for _, public := range v.Assignment.Public { + publicsInUse[public.Word] = nil + } + for _, private := range v.Assignment.Private { + privatesInUse[private.Word] = nil + } + } + + randWord := func(words string, taken map[string]any) string { + wordsList := strings.Fields(words) + for { + got := wordsList[rand.Intn(len(wordsList))] + if _, ok := taken[got]; !ok { + taken[got] = nil + return got + } + } + } + randGlobal := func() string { + return randWord(wordsHoliday, globalsInUse) + } + randPublic := func() string { + return randWord(wordsHoliday, publicsInUse) + } + randPrivate := func() string { + return randWord(wordsHoliday, privatesInUse) + } + + // TODO generate .Global=...us major cities?, .Assignments.Public=...?, .Assignments.Private=holiday + for k, v := range event.KillWords { + if v.Global.Word == "" { + v.Global = KillWord{Word: randGlobal(), Points: 1} + } + if len(v.Assignment.Public) == 0 { + v.Assignment.Public = []KillWord{ + KillWord{Word: randPublic(), Points: 50}, + KillWord{Word: randPublic(), Points: 50}, + } + } + if len(v.Assignment.Private) == 0 { + v.Assignment.Private = []KillWord{ + KillWord{Word: randPrivate(), Points: 100}, + } + } + event.KillWords[k] = v + } return games.createEvent(ctx, id, event) } diff --git a/cmd/server/games_test.go b/cmd/server/games_test.go index b1acace..6fa07e4 100644 --- a/cmd/server/games_test.go +++ b/cmd/server/games_test.go @@ -91,7 +91,7 @@ func TestGames(t *testing.T) { } else { for i := 0; i < 4; i++ { p := fmt.Sprintf("p%d", i+1) - if v.Players[p].KillWords.Global == "" { + if v.Players[p].KillWords.Global.Word == "" { t.Error(p, "no killwords.global") } else if v.Players[p].KillWords.Assigned.IsZero() { t.Error(p, "no killwords.assigned") diff --git a/cmd/server/holiday.txt b/cmd/server/holiday.txt new file mode 100644 index 0000000..a1ee6b5 --- /dev/null +++ b/cmd/server/holiday.txt @@ -0,0 +1,100 @@ +Advent +Angels +Announcement +Bells +Bethlehem +Blitzen +Candles +Candy +Candy canes +Cards +Cedar +Celebrate +Ceremonies +Chimney +Christmas cookies +Christmas tree +Cold +Comet +Cranberry sauce +Crowds +Cupid +Dancer +Dasher +December +Decorations +Dolls +Donner +Dressing +Eggnog +Elves +Family reunion +Festival +Fir +Frosty +Fruitcake +Gift boxes +Gifts +Goodwill +Greetings +Ham +Happy +Holiday +Holly +Holy +Icicles +Jolly +Lights +Lists +Merry +Miracle +Mistletoe +New Year +Noel +North Pole +Pageant +Parades +Party +Pie +Pine +Plum pudding +Poinsettia +Prancer +Presents +Pumpkin pie +Punch +Red/green +Reindeer +Ribbon +Rudolph +Sacred +Sales +Sauce +Scrooge +Season +Sled +Sleighbells +Snowflakes +Spirit +St. Nick +Stand +Star +Stickers +Stocking stuffers +Sweet potato +Tidings +Tinsel +Togetherness +Toys +Tradition +Traffic +Trips +Turkey +Vacation +Vixen +Winter +Worship +Wrapping paper +Wreath +Yule +Yuletide