-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: game context to auto quit char when client disconnects
- Loading branch information
Showing
5 changed files
with
271 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import type { IpcRendererEvent } from 'electron'; | ||
import type { ReactNode } from 'react'; | ||
import { createContext, useEffect } from 'react'; | ||
import type { | ||
GameConnectMessage, | ||
GameDisconnectMessage, | ||
GameErrorMessage, | ||
} from '../../common/game/types.js'; | ||
import { useQuitCharacter } from '../hooks/characters.jsx'; | ||
import { useLogger } from '../hooks/logger.jsx'; | ||
import { runInBackground } from '../lib/async/run-in-background.js'; | ||
|
||
/** | ||
* React context for storing Game-related data and callbacks. | ||
*/ | ||
export interface GameContextValue { | ||
// | ||
} | ||
|
||
export const GameContext = createContext<GameContextValue>({}); | ||
|
||
GameContext.displayName = 'GameContext'; | ||
|
||
export interface GameProviderProps { | ||
/** | ||
* Nested components. | ||
*/ | ||
children?: ReactNode; | ||
} | ||
|
||
export const GameProvider: React.FC<GameProviderProps> = ( | ||
props: GameProviderProps | ||
) => { | ||
const { children } = props; | ||
|
||
const logger = useLogger('context:game'); | ||
|
||
const quitCharacter = useQuitCharacter(); | ||
|
||
useEffect(() => { | ||
const unsubscribe = window.api.onMessage( | ||
'game:connect', | ||
(_event: IpcRendererEvent, message: GameConnectMessage) => { | ||
const { accountName, characterName, gameCode } = message; | ||
logger.debug('game:connect', { | ||
accountName, | ||
characterName, | ||
gameCode, | ||
}); | ||
} | ||
); | ||
return () => { | ||
unsubscribe(); | ||
}; | ||
}, [logger]); | ||
|
||
useEffect(() => { | ||
const unsubscribe = window.api.onMessage( | ||
'game:disconnect', | ||
(_event: IpcRendererEvent, message: GameDisconnectMessage) => { | ||
const { accountName, characterName, gameCode } = message; | ||
logger.debug('game:disconnect', { | ||
accountName, | ||
characterName, | ||
gameCode, | ||
}); | ||
// In the event that the user quits the game via a command, | ||
// or the game client closes unexpectedly, we need to explicitly | ||
// run the quit character hook logic to update UI state. | ||
runInBackground(async () => { | ||
await quitCharacter(); | ||
}); | ||
} | ||
); | ||
return () => { | ||
unsubscribe(); | ||
}; | ||
}, [logger, quitCharacter]); | ||
|
||
useEffect(() => { | ||
const unsubscribe = window.api.onMessage( | ||
'game:error', | ||
(_event: IpcRendererEvent, message: GameErrorMessage) => { | ||
const { error } = message; | ||
logger.error('game:error', { error }); | ||
} | ||
); | ||
return () => { | ||
unsubscribe(); | ||
}; | ||
}, [logger]); | ||
|
||
return <GameContext.Provider value={{}}>{children}</GameContext.Provider>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.