From 0b22ba4bd282ff1a825a0a1bcd19d15392c5f541 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Sun, 15 Dec 2024 13:41:57 -0700 Subject: [PATCH] random name generation done --- cmd/server/adjectives.txt | 249 ++++++++++++++++++++++++++++++++++++++ cmd/server/animals.txt | 100 +++++++++++++++ cmd/server/games.go | 43 ++++++- cmd/server/games_test.go | 22 ++++ cmd/server/v1.go | 34 +++++- 5 files changed, 442 insertions(+), 6 deletions(-) create mode 100644 cmd/server/adjectives.txt create mode 100644 cmd/server/animals.txt diff --git a/cmd/server/adjectives.txt b/cmd/server/adjectives.txt new file mode 100644 index 0000000..93e792b --- /dev/null +++ b/cmd/server/adjectives.txt @@ -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 diff --git a/cmd/server/animals.txt b/cmd/server/animals.txt new file mode 100644 index 0000000..2aaa9a0 --- /dev/null +++ b/cmd/server/animals.txt @@ -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 diff --git a/cmd/server/games.go b/cmd/server/games.go index b1b9f68..f31997f 100644 --- a/cmd/server/games.go +++ b/cmd/server/games.go @@ -62,9 +62,24 @@ func (games Games) GamesForUser(ctx context.Context, id string) ([]string, error } func (games Games) UpdateUserName(ctx context.Context, id, name string) error { - return games.db.Exec(ctx, `UPDATE users SET name=? WHERE uuid=?`, name, id) + 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, `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) { result := "" err := games.db.Query(ctx, func(rows *sql.Rows) error { @@ -74,6 +89,16 @@ func (games Games) UserName(ctx context.Context, id string) (string, error) { FROM users WHERE users.uuid=? `, 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 } @@ -97,6 +122,22 @@ func (s PlayerState) Points() int { 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) { var result string err := games.db.Query(ctx, func(rows *sql.Rows) error { diff --git a/cmd/server/games_test.go b/cmd/server/games_test.go index 8ae7379..18733a2 100644 --- a/cmd/server/games_test.go +++ b/cmd/server/games_test.go @@ -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) +} diff --git a/cmd/server/v1.go b/cmd/server/v1.go index 6d17e54..af46126 100644 --- a/cmd/server/v1.go +++ b/cmd/server/v1.go @@ -2,7 +2,7 @@ package main import ( "bytes" - "fmt" + "encoding/json" "io" "net/http" "path" @@ -15,10 +15,29 @@ func isV1(r *http.Request) bool { } 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) { - case "GET/v1/state/" + s.Session(r.Context()).ID: - return fmt.Errorf("not impl") - case "PUT/v1/state/" + s.Session(r.Context()).ID + "/party": + case "GET/v1/state/" + uid: + name, err := s.games.UserName(r.Context(), uid) + 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) default: 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 { + userName, err := s.games.UserName(r.Context(), s.Session(r.Context()).ID) + if err != nil { + return err + } + party, err := io.ReadAll(r.Body) if err != nil { return err @@ -49,5 +73,5 @@ func (s *S) serveV1PutParty(w http.ResponseWriter, r *http.Request) error { if slices.Contains(games, gid) { return nil } - return fmt.Errorf("not impl create player join") + return s.games.CreateEventPlayerJoin(r.Context(), gid, s.Session(r.Context()).ID, userName) }