grungnet/src/contexts/PlayerContext.js

136 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-01-01 17:13:26 -05:00
import React, { useEffect, useContext } from "react";
2020-12-07 23:58:01 -05:00
import { useDatabase } from "./DatabaseContext";
import { useAuth } from "./AuthContext";
2020-12-07 23:58:01 -05:00
import { getRandomMonster } from "../helpers/monsters";
import useNetworkedState from "../hooks/useNetworkedState";
2020-12-31 01:56:51 -05:00
export const PlayerStateContext = React.createContext();
export const PlayerUpdaterContext = React.createContext(() => {});
2020-12-07 23:58:01 -05:00
export function PlayerProvider({ session, children }) {
const { userId } = useAuth();
const { database, databaseStatus } = useDatabase();
2020-12-07 23:58:01 -05:00
const [playerState, setPlayerState] = useNetworkedState(
{
nickname: "",
timer: null,
dice: { share: false, rolls: [] },
sessionId: null,
userId,
2020-12-07 23:58:01 -05:00
},
session,
"player_state",
500,
false
2020-12-07 23:58:01 -05:00
);
useEffect(() => {
if (!database || databaseStatus === "loading") {
return;
}
async function loadNickname() {
const storedNickname = await database.table("user").get("nickname");
if (storedNickname !== undefined) {
setPlayerState((prevState) => ({
...prevState,
nickname: storedNickname.value,
}));
} else {
const name = getRandomMonster();
setPlayerState((prevState) => ({ ...prevState, nickname: name }));
database.table("user").add({ key: "nickname", value: name });
}
}
loadNickname();
2020-12-31 01:56:51 -05:00
}, [database, databaseStatus, setPlayerState]);
2020-12-07 23:58:01 -05:00
useEffect(() => {
if (
playerState.nickname &&
database !== undefined &&
databaseStatus !== "loading"
) {
database
.table("user")
.update("nickname", { value: playerState.nickname });
}
}, [playerState, database, databaseStatus]);
useEffect(() => {
2021-04-09 01:13:06 -04:00
if (userId) {
setPlayerState((prevState) => {
if (prevState) {
return {
...prevState,
userId,
};
}
return prevState;
});
}
2020-12-31 01:56:51 -05:00
}, [userId, setPlayerState]);
useEffect(() => {
2021-04-09 01:13:06 -04:00
function updateSessionId() {
setPlayerState((prevState) => {
if (prevState) {
return {
...prevState,
sessionId: session.id,
};
}
return prevState;
});
}
function handleSocketConnect() {
2020-12-07 23:58:01 -05:00
// Set the player state to trigger a sync
2021-04-09 01:13:06 -04:00
updateSessionId();
2020-12-07 23:58:01 -05:00
}
2021-02-20 02:10:00 -05:00
function handleSocketStatus(status) {
if (status === "joined") {
2021-04-09 01:13:06 -04:00
updateSessionId();
2021-02-20 02:10:00 -05:00
}
}
session.on("status", handleSocketStatus);
session.socket?.on("connect", handleSocketConnect);
2021-04-09 01:13:06 -04:00
session.socket?.io.on("reconnect", handleSocketConnect);
2020-12-07 23:58:01 -05:00
return () => {
2021-02-20 02:10:00 -05:00
session.off("status", handleSocketStatus);
session.socket?.off("connect", handleSocketConnect);
2021-04-09 01:13:06 -04:00
session.socket?.io.off("reconnect", handleSocketConnect);
2020-12-07 23:58:01 -05:00
};
});
return (
2020-12-31 01:56:51 -05:00
<PlayerStateContext.Provider value={playerState}>
<PlayerUpdaterContext.Provider value={setPlayerState}>
{children}
2020-12-31 01:56:51 -05:00
</PlayerUpdaterContext.Provider>
</PlayerStateContext.Provider>
2020-12-07 23:58:01 -05:00
);
}
export function usePlayerState() {
const context = useContext(PlayerStateContext);
if (context === undefined) {
throw new Error("usePlayerState must be used within a PlayerProvider");
}
return context;
}
export function usePlayerUpdater() {
const context = useContext(PlayerUpdaterContext);
if (context === undefined) {
throw new Error("usePlayerUpdater must be used within a PlayerProvider");
}
return context;
}