Move nickname generation to db populate

This commit is contained in:
Mitchell McCaffrey 2021-07-18 09:13:10 +10:00
parent ac99c9af4a
commit 3dfb1186f9
2 changed files with 3 additions and 6 deletions

View File

@ -3,8 +3,6 @@ import React, { useEffect, useContext } from "react";
import { useDatabase } from "./DatabaseContext";
import { useUserId } from "./UserIdContext";
import { getRandomMonster } from "../helpers/monsters";
import useNetworkedState, {
SetNetworkedState,
} from "../hooks/useNetworkedState";
@ -50,10 +48,6 @@ export function PlayerProvider({ session, children }: PlayerProviderProps) {
...prevState,
nickname: storedNickname.value,
}));
} else {
const name = getRandomMonster();
setPlayerState((prevState) => ({ ...prevState, nickname: name }));
database?.table("user").add({ key: "nickname", value: name });
}
}

View File

@ -5,6 +5,7 @@ import { v4 as uuid } from "uuid";
import { loadVersions, UpgradeEventHandler } from "./upgrade";
import { getDefaultMaps } from "./maps";
import { getDefaultTokens } from "./tokens";
import { getRandomMonster } from "./helpers/monsters";
/**
* Populate DB with initial data
@ -14,6 +15,8 @@ function populate(db: Dexie) {
db.on("populate", () => {
const userId = uuid();
db.table("user").add({ key: "userId", value: userId });
const nickname = getRandomMonster();
db.table("user").add({ key: "nickname", value: nickname });
const { maps, mapStates } = getDefaultMaps(userId);
db.table("maps").bulkAdd(maps);
db.table("states").bulkAdd(mapStates);