games passes test

main
Bel LaPointe 2024-12-15 01:58:40 -07:00
parent 130f8122b6
commit f17328a9ee
3 changed files with 163 additions and 3 deletions

View File

@ -3,11 +3,13 @@ package main
import ( import (
"context" "context"
"database/sql" "database/sql"
_ "embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
"maps" "maps"
"math/rand" "math/rand"
"slices" "slices"
"strings"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
@ -94,7 +96,7 @@ type (
} }
KillWords struct { KillWords struct {
Global string Global KillWord
Assigned time.Time Assigned time.Time
Assignee string 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}) 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 { func (games Games) CreateEventAssignmentRotation(ctx context.Context, id string, killer, victim, word string, points int) error {
state, err := games.GameState(ctx, id) state, err := games.GameState(ctx, id)
if err != nil { if err != nil {
@ -308,6 +313,11 @@ func (games Games) CreateEventAssignmentRotation(ctx context.Context, id string,
Assignment: killerState.KillWords.Assignment, Assignment: killerState.KillWords.Assignment,
} }
toAssign = slices.DeleteFunc(toAssign, func(s string) bool { return s == event.KillWords[killer].Assignee }) 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 { 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) return games.createEvent(ctx, id, event)
} }

View File

@ -91,7 +91,7 @@ func TestGames(t *testing.T) {
} else { } else {
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
p := fmt.Sprintf("p%d", i+1) 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") t.Error(p, "no killwords.global")
} else if v.Players[p].KillWords.Assigned.IsZero() { } else if v.Players[p].KillWords.Assigned.IsZero() {
t.Error(p, "no killwords.assigned") t.Error(p, "no killwords.assigned")

100
cmd/server/holiday.txt Normal file
View File

@ -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