Skip to content

Commit

Permalink
channel: Add utility function for triggering events from the frontend.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuv2707 committed Jun 20, 2024
1 parent 964ef30 commit 5bd107a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
2 changes: 2 additions & 0 deletions backend/src/controllers/gameControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export async function handleGameEvent(req: AuthRequest, res: Response) {
res.status(404).send({ message: 'Game not found' });
return;
}
console.log('handling event ', event);
//todo: When game data is retrieved from database, it is not an instance of GameEngine
// so we would need to convert it to an instance of GameEngine
const result = game.dispatchEvent(event);
Expand Down Expand Up @@ -46,6 +47,7 @@ export async function handleGameJoin(req: AuthRequest, res: Response) {
}
//note: when retrieving game from database, it is not an instance of GameEngine
// we'd need to add these functions to the mongodb game schema
// this should be sent once the joining player receives the game state
game.dispatchEvent({ type: 'JOIN_GAME', playerId: req.user.id });
propagateChanges(game);
req.user.activeGameId = gameCode;
Expand Down
39 changes: 35 additions & 4 deletions frontend/src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const GAME_EVENTS = {
STATE_SYNC: 'STATE_SYNC',
};

let jwt: string = '';
let authCreds: { jwt: string; playerId: string } | null = null;
let polling = false;

let gameEventsDispatcher: (event: types.AppEvent) => void = () => {};
Expand All @@ -24,6 +24,9 @@ export function setGameEventsDispatcher(
}
let abortController: AbortController | null = null;
async function poll() {
if (!authCreds) {
throw new Error('Auth credentials not set');
}
if (abortController) {
abortController.abort();
}
Expand All @@ -35,7 +38,7 @@ async function poll() {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: jwt,
Authorization: authCreds.jwt,
},
signal: abortController.signal,
}
Expand Down Expand Up @@ -73,8 +76,14 @@ function pollLoop() {
});
}

export function startPolling(_jwt: string) {
jwt = _jwt;
export function setAuthCreds(jwt: string, playerId: string) {
authCreds = { jwt, playerId };
}

export function startPolling() {
if (!authCreds) {
throw new Error('Auth credentials not set');
}
polling = true;
pollLoop();
}
Expand All @@ -85,3 +94,25 @@ export function stopPolling() {
abortController.abort();
}
}

export function triggerEvent(type: types.GameEventType, data?: unknown) {
if (!authCreds) {
throw new Error('Auth credentials not set');
}
fetch(`${process.env.REACT_APP_BACKEND_URL}/game/events`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: authCreds.jwt,
},
body: JSON.stringify({
event: {
type,
playerId: authCreds.playerId,
data,
},
}),
}).catch((e) => {
console.error('Error triggering event:', e);
});
}
6 changes: 4 additions & 2 deletions frontend/src/contexts/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export function AuthProvider({ children }: { children: ReactElement }) {
name: data.user.username,
});
setJwt(localToken!);
channel.startPolling(localToken!);
channel.setAuthCreds(data.token, data.user.id);
channel.startPolling();
}
} catch (e) {
console.info('deleting existing jwt');
Expand Down Expand Up @@ -105,7 +106,8 @@ export function AuthProvider({ children }: { children: ReactElement }) {
});
setJwt(data.token);
localStorage.setItem('jwt', data.token);
channel.startPolling(data.token);
channel.setAuthCreds(data.token, data.user.id);
channel.startPolling();
},
[setUser, toast]
);
Expand Down

0 comments on commit 5bd107a

Please sign in to comment.