random name generation done

main
Bel LaPointe 2024-12-15 13:41:57 -07:00
parent f7a303168a
commit 0b22ba4bd2
5 changed files with 442 additions and 6 deletions

249
cmd/server/adjectives.txt Normal file
View File

@ -0,0 +1,249 @@
amazing
astounding
adorable
astonishing
awesome
agreeable
admirable
affectionate
artistic
animated
beautiful
brave
brilliant
big
bouncy
bright
busy
bold
blissful
bubbly
caring
clever
courageous
creative
cute
cheerful
charming
confident
curious
content
daring
delightful
determined
diligent
dynamic
decisive
dazzling
dependable
dramatic
dreamy
eager
earnest
effervescent
elegant
energetic
enchanting
enthusiastic
excellent
exciting
expressive
fair
faithful
fantastic
fearless
festive
friendly
funny
feisty
forgiving
focused
gentle
giving
glad
gleeful
glorious
good
graceful
grateful
great
gregarious
happy
helpful
honest
hopeful
hungry
hilarious
honorable
huggable
humble
hardworking
important
impressive
incredible
independent
inquisitive
intelligent
interesting
imaginative
inventive
inspiring
joyful
jolly
jovial
judicious
just
jaunty
jubilant
jazzy
jumpy
joking
kind
knowledgeable
keen
kooky
kindhearted
karate-chopping
krazy
kicking
kissable
kidding
loving
laughing
likable
lucky
lovely
light
little
loud
lazy
lanky
merry
magical
marvellous
mysterious
mischievous
masterful
mindful
melodic
mighty
motivated
nice
nimble
nifty
noisy
nutty
nautical
noteworthy
nosey
neat
nourished
odd
old
obedient
outstanding
opinionated
optimistic
orderly
outgoing
overjoyed
organized
perfect
playful
pleasant
polite
powerful
peaceful
patient
proud
puzzled
perky
quirky
quick
queenly
quaint
qualified
quizzical
quaint
quiet
quirky
quacking
rainy
rambunctious
respectful
right
responsible
ripe
rustic
rotten
rhythmic
righteous
silly
sweet
smart
smiling
strong
super
skillful
sleepy
scented
spotless
thankful
tired
tasty
talented
thoughtful
tremendous
terrific
truthful
tough
trustworthy
unique
understanding
unusual
upbeat
useful
uplifting
unafraid
universal
unlimited
unselfish
victorious
vivacious
valuable
vibrant
versatile
virtuous
visionary
vocal
vivacious
valiant
wonderful
whimsical
welcoming
witty
wise
wild
warm
wacky
willing
watchful
xenial
xeric
yummy
yellow
yippee
yappy
young
yucky
yummy
youthful
yakky
zany
zesty
zen
zealous
zingy
zippy
zonal
zonked

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

@ -0,0 +1,100 @@
lion
tiger
goat
horse
donkey
dog
cat
pig
cow
elephant
giraffe
kangaroo
koala
deer
moose
sheep
zebra
bear
wolf
fox
otter
raccoon
squirrel
bat
chimpanzee
gorilla
orangutan
lemur
panda
red panda
hippopotamus
rhinoceros
camel
llama
alpaca
ferret
hedgehog
skunk
beaver
walrus
seal
dolphin
whale
bat
eagle
hawk
falcon
owl
parrot
crow
raven
pigeon
dove
swan
goose
duck
chicken
turkey
peacock
ostrich
emu
snake
lizard
turtle
crocodile
alligator
chameleon
gecko
shark
dolphin
whale
octopus
lobster
crab
shrimp
clam
oyster
ant
bee
butterfly
caterpillar
cricket
grasshopper
ladybug
mosquito
scorpion
spider
worm
snail
jellyfish
starfish
sponge
platypus
koala
tasmanian devil
kangaroo
wombat
wallaby
meerkat
lemming

View File

@ -62,8 +62,23 @@ func (games Games) GamesForUser(ctx context.Context, id string) ([]string, error
} }
func (games Games) UpdateUserName(ctx context.Context, id, name string) error { func (games Games) UpdateUserName(ctx context.Context, id, name string) error {
var n int
if err := games.db.Query(ctx, func(rows *sql.Rows) error {
return rows.Scan(&n)
}, `SELECT COUNT(uuid) FROM users WHERE uuid=?`, id); err != nil {
return err
}
if n > 0 {
return games.db.Exec(ctx, `UPDATE users SET name=? WHERE uuid=?`, name, id) return games.db.Exec(ctx, `UPDATE users SET name=? WHERE uuid=?`, name, id)
} }
return games.db.Exec(ctx, `INSERT INTO users (uuid, name) VALUES (?, ?)`, id, name)
}
//go:embed adjectives.txt
var namesAdjectives string
//go:embed animals.txt
var namesAnimals string
func (games Games) UserName(ctx context.Context, id string) (string, error) { func (games Games) UserName(ctx context.Context, id string) (string, error) {
result := "" result := ""
@ -74,6 +89,16 @@ func (games Games) UserName(ctx context.Context, id string) (string, error) {
FROM users FROM users
WHERE users.uuid=? WHERE users.uuid=?
`, id) `, id)
if result == "" {
adjectives := strings.Fields(namesAdjectives)
animals := strings.Split(namesAnimals, "\n")
animals = slices.DeleteFunc(animals, func(s string) bool { return s == "" })
name := strings.Title(fmt.Sprintf("%s %s", adjectives[rand.Intn(len(adjectives))], animals[rand.Intn(len(animals))]))
if err := games.UpdateUserName(ctx, id, name); err != nil {
return "", err
}
return games.UserName(ctx, id)
}
return result, err return result, err
} }
@ -97,6 +122,22 @@ func (s PlayerState) Points() int {
return points return points
} }
func (games Games) GameName(ctx context.Context, id string) (string, error) {
var result string
err := games.db.Query(ctx, func(rows *sql.Rows) error {
return rows.Scan(&result)
}, `
SELECT
games.name
FROM
games
WHERE games.uuid=?
ORDER BY games.updated DESC
LIMIT 1
`, id)
return result, err
}
func (games Games) GameByName(ctx context.Context, name string) (string, error) { func (games Games) GameByName(ctx context.Context, name string) (string, error) {
var result string var result string
err := games.db.Query(ctx, func(rows *sql.Rows) error { err := games.db.Query(ctx, func(rows *sql.Rows) error {

View File

@ -382,3 +382,25 @@ func TestAllKillWordsUnused(t *testing.T) {
}) })
}) })
} }
func TestGenerateUserName(t *testing.T) {
games := newTestGames(t)
name, err := games.UserName(context.Background(), "id")
if err != nil {
t.Fatal(err)
}
if name == "" {
t.Fatal(name)
}
name2, err := games.UserName(context.Background(), "id")
if err != nil {
t.Fatal(err)
}
if name2 != name {
t.Fatal(name2)
}
t.Log(name)
}

View File

@ -2,7 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"fmt" "encoding/json"
"io" "io"
"net/http" "net/http"
"path" "path"
@ -15,10 +15,29 @@ func isV1(r *http.Request) bool {
} }
func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error { func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error {
uid := s.Session(r.Context()).ID
switch path.Join(r.Method, r.URL.Path) { switch path.Join(r.Method, r.URL.Path) {
case "GET/v1/state/" + s.Session(r.Context()).ID: case "GET/v1/state/" + uid:
return fmt.Errorf("not impl") name, err := s.games.UserName(r.Context(), uid)
case "PUT/v1/state/" + s.Session(r.Context()).ID + "/party": if err != nil {
return err
}
gids, err := s.games.GamesForUser(r.Context(), uid)
if err != nil {
return err
}
msg := map[string]any{
"name": name,
}
if len(gids) > 0 {
party, err := s.games.GameName(r.Context(), gids[0])
if err != nil {
return err
}
msg["party"] = party
}
return json.NewEncoder(w).Encode(msg)
case "PUT/v1/state/" + uid + "/party":
return s.serveV1PutParty(w, r) return s.serveV1PutParty(w, r)
default: default:
http.NotFound(w, r) http.NotFound(w, r)
@ -27,6 +46,11 @@ func (s *S) serveV1(w http.ResponseWriter, r *http.Request) error {
} }
func (s *S) serveV1PutParty(w http.ResponseWriter, r *http.Request) error { func (s *S) serveV1PutParty(w http.ResponseWriter, r *http.Request) error {
userName, err := s.games.UserName(r.Context(), s.Session(r.Context()).ID)
if err != nil {
return err
}
party, err := io.ReadAll(r.Body) party, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
return err return err
@ -49,5 +73,5 @@ func (s *S) serveV1PutParty(w http.ResponseWriter, r *http.Request) error {
if slices.Contains(games, gid) { if slices.Contains(games, gid) {
return nil return nil
} }
return fmt.Errorf("not impl create player join") return s.games.CreateEventPlayerJoin(r.Context(), gid, s.Session(r.Context()).ID, userName)
} }