Skip to content

Commit 95fd45c

Browse files
committed
feat(context): game context tracks which character is connected
1 parent e1e4e1b commit 95fd45c

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

electron/common/game/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -416,13 +416,13 @@ export enum ExperienceMindState {
416416
export interface GameConnectMessage {
417417
accountName: string;
418418
characterName: string;
419-
gameCode: string;
419+
gameCode: GameCode;
420420
}
421421

422422
export interface GameDisconnectMessage {
423423
accountName: string;
424424
characterName: string;
425-
gameCode: string;
425+
gameCode: GameCode;
426426
}
427427

428428
export interface GameErrorMessage {

electron/preload/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -656,12 +656,12 @@ declare module 'common/game/types' {
656656
export interface GameConnectMessage {
657657
accountName: string;
658658
characterName: string;
659-
gameCode: string;
659+
gameCode: GameCode;
660660
}
661661
export interface GameDisconnectMessage {
662662
accountName: string;
663663
characterName: string;
664-
gameCode: string;
664+
gameCode: GameCode;
665665
}
666666
export interface GameErrorMessage {
667667
error: Error;

electron/renderer/context/game.tsx

+44-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import type { IpcRendererEvent } from 'electron';
22
import { EuiLoadingSpinner, EuiOverlayMask } from '@elastic/eui';
33
import { useRouter } from 'next/router.js';
44
import type { ReactNode } from 'react';
5-
import { createContext, useEffect, useState } from 'react';
5+
import { createContext, useEffect, useMemo, useState } from 'react';
66
import type { Character } from '../../common/account/types.js';
77
import type {
8+
GameCode,
89
GameCommandMessage,
910
GameConnectMessage,
1011
GameDisconnectMessage,
@@ -20,15 +21,31 @@ import { runInBackground } from '../lib/async/run-in-background.js';
2021
* React context for storing Game-related data and callbacks.
2122
*/
2223
export interface GameContextValue {
23-
//
24-
todo?: true;
24+
/**
25+
* Whether the game client is connected.
26+
*/
27+
isConnected: boolean;
28+
/**
29+
* The account of the connected character.
30+
*/
31+
accountName?: string;
32+
/**
33+
* The name of the connected character.
34+
*/
35+
characterName?: string;
36+
/**
37+
* The game code of the connected character.
38+
*/
39+
gameCode?: GameCode;
2540
}
2641

2742
/**
2843
* Defines shape and behavior of the context value
2944
* when no provider is found in the component hierarchy.
3045
*/
31-
export const GameContext = createContext<GameContextValue>({});
46+
export const GameContext = createContext<GameContextValue>({
47+
isConnected: false,
48+
});
3249

3350
GameContext.displayName = 'GameContext';
3451

@@ -48,6 +65,20 @@ export const GameProvider: React.FC<GameProviderProps> = (
4865
const router = useRouter();
4966
const pubsub = usePubSub();
5067

68+
const [isConnected, setIsConnected] = useState<boolean>(false);
69+
const [accountName, setAccountName] = useState<string>();
70+
const [characterName, setCharacterName] = useState<string>();
71+
const [gameCode, setGameCode] = useState<GameCode>();
72+
73+
const contextValue = useMemo<GameContextValue>(() => {
74+
return {
75+
isConnected,
76+
accountName,
77+
characterName,
78+
gameCode,
79+
};
80+
}, [isConnected, accountName, characterName, gameCode]);
81+
5182
const quitCharacter = useQuitCharacter();
5283

5384
// To protect against a user pressing play/stop while the app
@@ -112,6 +143,10 @@ export const GameProvider: React.FC<GameProviderProps> = (
112143
characterName,
113144
gameCode,
114145
});
146+
setIsConnected(true);
147+
setAccountName(accountName);
148+
setCharacterName(characterName);
149+
setGameCode(gameCode);
115150
pubsub.publish('game:connect', {
116151
accountName,
117152
characterName,
@@ -134,6 +169,10 @@ export const GameProvider: React.FC<GameProviderProps> = (
134169
characterName,
135170
gameCode,
136171
});
172+
setIsConnected(false);
173+
setAccountName(accountName);
174+
setCharacterName(characterName);
175+
setGameCode(gameCode);
137176
pubsub.publish('game:disconnect', {
138177
accountName,
139178
characterName,
@@ -198,7 +237,7 @@ export const GameProvider: React.FC<GameProviderProps> = (
198237
}, [pubsub]);
199238

200239
return (
201-
<GameContext.Provider value={{}}>
240+
<GameContext.Provider value={contextValue}>
202241
<>
203242
{(showPlayStartingOverlay || showPlayStoppingOverlay) && (
204243
<EuiOverlayMask>

0 commit comments

Comments
 (0)