Skip to content

Commit

Permalink
fix: better room name generation
Browse files Browse the repository at this point in the history
See #173
  • Loading branch information
jmattheis committed Nov 20, 2023
1 parent 9fd6626 commit 6bc29f9
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 26 deletions.
2 changes: 2 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type UIConfig struct {
User string `json:"user"`
LoggedIn bool `json:"loggedIn"`
Version string `json:"version"`
RoomName string `json:"roomName"`
CloseRoomWhenOwnerLeaves bool `json:"closeRoomWhenOwnerLeaves"`
}

Expand All @@ -42,6 +43,7 @@ func Router(conf config.Config, rooms *ws.Rooms, users *auth.Users, version stri
LoggedIn: loggedIn,
User: user,
Version: version,
RoomName: rooms.RandRoomName(),
CloseRoomWhenOwnerLeaves: conf.CloseRoomWhenOwnerLeaves,
})
})
Expand Down
1 change: 0 additions & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"react-dom": "^18.2.0",
"react-hotkeys-hook": "^4.4.1",
"typescript": "~5.2.2",
"unique-names-generator": "^4.7.1",
"use-http": "^1.0.28",
"vite": "^4.4.9",
"vite-plugin-svgr": "^4.0.0",
Expand Down
3 changes: 1 addition & 2 deletions ui/src/RoomManage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import {
} from '@mui/material';
import {FCreateRoom, UseRoom} from './useRoom';
import {UIConfig} from './message';
import {randomRoomName} from './name';
import {getRoomFromURL} from './useRoomID';
import {authModeToRoomMode, UseConfig} from './useConfig';
import {LoginForm} from './LoginForm';

const CreateRoom = ({room, config}: Pick<UseRoom, 'room'> & {config: UIConfig}) => {
const [id, setId] = React.useState(() => getRoomFromURL() ?? randomRoomName());
const [id, setId] = React.useState(() => getRoomFromURL() ?? config.roomName);
const mode = authModeToRoomMode(config.authMode, config.loggedIn);
const [ownerLeave, setOwnerLeave] = React.useState(config.closeRoomWhenOwnerLeaves);
const submit = () =>
Expand Down
1 change: 1 addition & 0 deletions ui/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface UIConfig {
user: string;
loggedIn: boolean;
version: string;
roomName: string;
closeRoomWhenOwnerLeaves: boolean;
}

Expand Down
8 changes: 0 additions & 8 deletions ui/src/name.ts

This file was deleted.

1 change: 1 addition & 0 deletions ui/src/useConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const useConfig = (): UseConfig => {
loggedIn: false,
loading: true,
version: 'unknown',
roomName: 'unknown',
closeRoomWhenOwnerLeaves: true,
});

Expand Down
5 changes: 0 additions & 5 deletions ui/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1456,11 +1456,6 @@ typescript@~5.2.2:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.2.2.tgz#5ebb5e5a5b75f085f22bc3f8460fba308310fa78"
integrity sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==

unique-names-generator@^4.7.1:
version "4.7.1"
resolved "https://registry.yarnpkg.com/unique-names-generator/-/unique-names-generator-4.7.1.tgz#966407b12ba97f618928f77322cfac8c80df5597"
integrity sha512-lMx9dX+KRmG8sq6gulYYpKWZc9RlGsgBR6aoO8Qsm3qvkSJ+3rAymr+TnV8EDMrIrwuFJ4kruzMWM/OpYzPoow==

update-browserslist-db@^1.0.11:
version "1.0.13"
resolved "https://registry.yarnpkg.com/update-browserslist-db/-/update-browserslist-db-1.0.13.tgz#3c5e4f5c083661bd38ef64b6328c26ed6c8248c4"
Expand Down
34 changes: 25 additions & 9 deletions util/sillyname.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,35 @@ var adjectives = []string{
}

var animals = []string{
"Dog", "Puppy", "Turtle", "Rabbit", "Parrot", "Cat", "Kitten", "Goldfish",
"Mouse", "Hamster", "Fish", "Cow", "Rabbit", "Duck", "Shrimp", "Pig",
"Goat", "Crab", "Deer", "Bee", "Sheep", "Fish", "Turkey", "Dove",
"Chicken", "Horse", "Squirrel", "Dog", "Chimpanzee", "Ox", "Lion", "Panda",
"Walrus", "Otter", "Mouse", "Kangaroo", "Goat", "Horse", "Monkey", "Cow",
"Koala", "Mole", "Elephant", "Leopard", "Hippopotamus", "Giraffe", "Fox",
"Coyote", "Hedgehong", "Sheep", "Deer",
"dog", "puppy", "turtle", "rabbit", "parrot", "cat", "kitten", "goldfish",
"mouse", "hamster", "fish", "cow", "rabbit", "duck", "shrimp", "pig",
"goat", "crab", "deer", "bee", "sheep", "fish", "turkey", "dove",
"chicken", "horse", "squirrel", "dog", "chimpanzee", "ox", "lion", "panda",
"walrus", "otter", "mouse", "kangaroo", "goat", "horse", "monkey", "cow",
"koala", "mole", "elephant", "leopard", "hippopotamus", "giraffe", "fox",
"coyote", "hedgehong", "sheep", "deer",
}

var colors = []string{
"amaranth", "amber", "amethyst", "apricot", "aqua", "aquamarine", "azure",
"beige", "black", "blue", "blush", "bronze", "brown", "chocolate",
"coffee", "copper", "coral", "crimson", "cyan", "emerald", "fuchsia",
"gold", "gray", "green", "harlequin", "indigo", "ivory", "jade",
"lavender", "lime", "magenta", "maroon", "moccasin", "olive", "orange",
"peach", "pink", "plum", "purple", "red", "rose", "salmon", "sapphire",
"scarlet", "silver", "tan", "teal", "tomato", "turquoise", "violet",
"white", "yellow",
}

func r(r *rand.Rand, l []string) string {
return l[r.Intn(len(l)-1)]
}

func NewName(s *rand.Rand) string {
return cases.Title(language.English).String(r(s, adjectives) + " " + r(s, animals))
func NewUserName(s *rand.Rand) string {
title := cases.Title(language.English)
return title.String(r(s, adjectives)) + " " + title.String(r(s, animals))
}

func NewRoomName(s *rand.Rand) string {
return r(s, adjectives) + "-" + r(s, colors) + "-" + r(s, animals)
}
6 changes: 5 additions & 1 deletion ws/rooms.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ type Rooms struct {
}

func (r *Rooms) RandUserName() string {
return util.NewName(r.r)
return util.NewUserName(r.r)
}

func (r *Rooms) RandRoomName() string {
return util.NewRoomName(r.r)
}

func (r *Rooms) Upgrade(w http.ResponseWriter, req *http.Request) {
Expand Down

0 comments on commit 6bc29f9

Please sign in to comment.