Switch sending/state changes to reducer to prep for optimizations

This commit is contained in:
zikaeroh 2020-05-28 18:06:36 -07:00
parent c74e4f8ce0
commit 6a43b5c0dc
2 changed files with 149 additions and 116 deletions

View File

@ -5,41 +5,38 @@ import { v4 } from 'uuid';
import { assertIsDefined, assertNever, noop, reloadOutdatedPage, websocketUrl } from '../common'; import { assertIsDefined, assertNever, noop, reloadOutdatedPage, websocketUrl } from '../common';
import { useServerTime } from '../hooks/useServerTime'; import { useServerTime } from '../hooks/useServerTime';
import { version } from '../metadata.json'; import { version as codiesVersion } from '../metadata.json';
import { ClientNote, ServerNote, State, StatePlayer, TimeResponse, WordPack } from '../protocol'; import { ClientNote, PartialClientNote, ServerNote, State, StatePlayer, TimeResponse, WordPack } from '../protocol';
import { GameView, Sender } from './gameView'; import { GameView, Sender } from './gameView';
import { Loading } from './loading'; import { Loading } from './loading';
const socketUrl = websocketUrl('/api/ws'); const socketUrl = websocketUrl('/api/ws');
function useSender(sendNote: (r: ClientNote) => void, version: number): Sender { function useSender(dispatch: (action: PartialClientNote) => void): Sender {
return React.useMemo<Sender>(() => { return React.useMemo<Sender>(() => {
return { return {
reveal: (row: number, col: number) => reveal: (row: number, col: number) =>
sendNote({ dispatch({
method: 'reveal', method: 'reveal',
version,
params: { params: {
row, row,
col, col,
}, },
}), }),
newGame: () => sendNote({ method: 'newGame', version, params: {} }), newGame: () => dispatch({ method: 'newGame', params: {} }),
endTurn: () => sendNote({ method: 'endTurn', version, params: {} }), endTurn: () => dispatch({ method: 'endTurn', params: {} }),
changeNickname: (nickname: string) => sendNote({ method: 'changeNickname', version, params: { nickname } }), changeNickname: (nickname: string) => dispatch({ method: 'changeNickname', params: { nickname } }),
changeRole: (spymaster: boolean) => sendNote({ method: 'changeRole', version, params: { spymaster } }), changeRole: (spymaster: boolean) => dispatch({ method: 'changeRole', params: { spymaster } }),
changeTeam: (team: number) => sendNote({ method: 'changeTeam', version, params: { team } }), changeTeam: (team: number) => dispatch({ method: 'changeTeam', params: { team } }),
randomizeTeams: () => sendNote({ method: 'randomizeTeams', version, params: {} }), randomizeTeams: () => dispatch({ method: 'randomizeTeams', params: {} }),
changePack: (num: number, enable: boolean) => changePack: (num: number, enable: boolean) => dispatch({ method: 'changePack', params: { num, enable } }),
sendNote({ method: 'changePack', version, params: { num, enable } }), changeTurnMode: (timed: boolean) => dispatch({ method: 'changeTurnMode', params: { timed } }),
changeTurnMode: (timed: boolean) => sendNote({ method: 'changeTurnMode', version, params: { timed } }), changeTurnTime: (seconds: number) => dispatch({ method: 'changeTurnTime', params: { seconds } }),
changeTurnTime: (seconds: number) => sendNote({ method: 'changeTurnTime', version, params: { seconds } }), addPacks: (packs: WordPack[]) => dispatch({ method: 'addPacks', params: { packs } }),
addPacks: (packs: WordPack[]) => sendNote({ method: 'addPacks', version, params: { packs } }), removePack: (num: number) => dispatch({ method: 'removePack', params: { num } }),
removePack: (num: number) => sendNote({ method: 'removePack', version, params: { num } }), changeHideBomb: (hideBomb: boolean) => dispatch({ method: 'changeHideBomb', params: { hideBomb } }),
changeHideBomb: (hideBomb: boolean) =>
sendNote({ method: 'changeHideBomb', version, params: { hideBomb } }),
}; };
}, [sendNote, version]); }, [dispatch]);
} }
function usePlayer(playerID: string, state?: State): { pState: StatePlayer; pTeam: number } | undefined { function usePlayer(playerID: string, state?: State): { pState: StatePlayer; pTeam: number } | undefined {
@ -71,7 +68,7 @@ function useWS(roomID: string, playerID: string, nickname: string, dead: () => v
// //
// X-CODIES-VERSION would be cleaner, but the WS hook doesn't // X-CODIES-VERSION would be cleaner, but the WS hook doesn't
// support anything but query params. // support anything but query params.
queryParams: { roomID: roomID, playerID: playerID, nickname: nickname, codiesVersion: version }, queryParams: { roomID: roomID, playerID: playerID, nickname: nickname, codiesVersion: codiesVersion },
reconnectAttempts, reconnectAttempts,
onMessage: () => { onMessage: () => {
retry.current = 0; retry.current = 0;
@ -99,7 +96,10 @@ function useWS(roomID: string, playerID: string, nickname: string, dead: () => v
}); });
} }
function syncTime(setOffset: (offset: number) => void) { function useSyncedServerTime() {
const { setOffset } = useServerTime();
const syncTime = React.useCallback(() => {
const fn = async () => { const fn = async () => {
let bestRTT: number | undefined; let bestRTT: number | undefined;
let offset = 0; let offset = 0;
@ -128,6 +128,45 @@ function syncTime(setOffset: (offset: number) => void) {
setOffset(offset); setOffset(offset);
}; };
fn().catch(noop); fn().catch(noop);
}, [setOffset]);
React.useEffect(() => {
const interval = window.setInterval(() => {
syncTime();
}, 10 * 60 * 1000);
return () => window.clearInterval(interval);
}, [syncTime]);
return syncTime;
}
type StateAction = { method: 'setState'; state: State } | PartialClientNote;
function useStateReducer(sendNote: (r: ClientNote) => void) {
// TODO: Create a new state which contains the server state.
// TODO: Put sendNote in the state instead of reffing it?
const sendNoteRef = React.useRef(sendNote);
sendNoteRef.current = sendNote;
return React.useCallback(
(state: State | undefined, action: StateAction): State | undefined => {
if (state === undefined) {
if (action.method === 'setState') {
return action.state;
}
return state;
}
switch (action.method) {
case 'setState':
return action.state;
default:
sendNoteRef.current({ ...action, version: state.version });
return state;
}
},
[sendNoteRef]
);
} }
export interface GameProps { export interface GameProps {
@ -139,21 +178,13 @@ export interface GameProps {
export const Game = (props: GameProps) => { export const Game = (props: GameProps) => {
const [playerID] = React.useState(v4); const [playerID] = React.useState(v4);
const nickname = React.useRef(props.nickname); // Preserve a nickname for use in reconnects. const nickname = React.useRef(props.nickname); // Preserve a nickname for use in reconnects.
const [state, setState] = React.useState<State | undefined>();
const { setOffset } = useServerTime();
const { sendJsonMessage, lastJsonMessage } = useWS(props.roomID, playerID, nickname.current, props.leave, () => const syncTime = useSyncedServerTime();
syncTime(setOffset) const { sendJsonMessage, lastJsonMessage } = useWS(props.roomID, playerID, nickname.current, props.leave, syncTime);
);
React.useEffect(() => { const reducer = useStateReducer(sendJsonMessage);
const interval = window.setInterval(() => { const [state, dispatch] = React.useReducer(reducer, undefined);
syncTime(setOffset); const send = useSender(dispatch);
}, 10 * 60 * 1000);
return () => window.clearInterval(interval);
}, [setOffset]);
const send = useSender(sendJsonMessage, state?.version ?? 0);
React.useEffect(() => { React.useEffect(() => {
if (!lastJsonMessage) { if (!lastJsonMessage) {
@ -164,7 +195,7 @@ export const Game = (props: GameProps) => {
switch (note.method) { switch (note.method) {
case 'state': case 'state':
setState(note.params); dispatch({ method: 'setState', state: note.params });
break; break;
default: default:
assertNever(note.method); assertNever(note.method);

View File

@ -11,13 +11,9 @@ const WordPack = myzod.object({
words: myzod.array(myzod.string()), words: myzod.array(myzod.string()),
}); });
export type ClientNote = Infer<typeof ClientNote>; export type PartialClientNote = Infer<typeof PartialClientNote>;
export const ClientNote = myzod export type PartialClientNoteSender = (r: PartialClientNote) => void;
.object({ const PartialClientNote = myzod.union([
version: myzod.number(),
})
.and(
myzod.union([
myzod.object({ myzod.object({
method: myzod.literal('newGame'), method: myzod.literal('newGame'),
params: myzod.object({}), params: myzod.object({}),
@ -72,8 +68,14 @@ export const ClientNote = myzod
method: myzod.literal('changeHideBomb'), method: myzod.literal('changeHideBomb'),
params: myzod.object({ hideBomb: myzod.boolean() }), params: myzod.object({ hideBomb: myzod.boolean() }),
}), }),
]) ]);
);
export type ClientNote = Infer<typeof ClientNote>;
export const ClientNote = myzod
.object({
version: myzod.number(),
})
.and(PartialClientNote);
// Messages sent from server to client. // Messages sent from server to client.