This commit is contained in:
bgoulard 2026-01-09 11:14:16 +01:00 committed by Nigel
parent a65b8a9067
commit 8564088a3b
2 changed files with 26 additions and 25 deletions

View file

@ -1,6 +1,6 @@
import { FastifyPluginAsync } from 'fastify';
import { Static, Type } from 'typebox';
import { broadcastNextGame} from '../broadcastNextGame';
import { broadcastNextGame } from '../broadcastNextGame';
export const ChatReq = Type.Object({
message: Type.String(),
@ -28,22 +28,22 @@ export type ChatReq = Static<typeof ChatReq>;
const route: FastifyPluginAsync = async (fastify): Promise<void> => {
fastify.post('/api/chat/broadcast', {
schema: {
body: {
type: 'object',
required: ['nextGame'],
properties: {
nextGame: { type: 'string' }
}
}
}
}, async (req, reply) => {
const gameLink: Promise<string> = Promise.resolve(req.body as string );
if (gameLink)
schema: {
body: {
type: 'object',
required: ['nextGame'],
properties: {
nextGame: { type: 'string' },
},
},
},
}, async (req, reply) => {
const gameLink: Promise<string> = Promise.resolve(req.body as string);
if (gameLink) {
broadcastNextGame(fastify, gameLink);
return reply.send({ status: 'ok' });
});
}
return reply.send({ status: 'ok' });
});
};
export default route;

View file

@ -205,19 +205,20 @@ class StateI {
this.fastify.db.setPongGameOutcome(gameId, { id: game.userLeft, score: game.score[0] }, { id: game.userRight, score: game.score[1] }, outcome, game.local);
this.fastify.log.info('SetGameOutcome !');
if (!game.local) {
let payload = {'nextGame':chat_text};
const payload = { 'nextGame':chat_text };
try {
const resp = await fetch("http://app-chat/api/chat/broadcast", {
const resp = await fetch('http://app-chat/api/chat/broadcast', {
method:'POST',
headers:{'Content-type':'application/json'},
headers:{ 'Content-type':'application/json' },
body: JSON.stringify(payload),
});
});
if (!resp.ok)
throw(resp);
else
this.fastify.log.info("game-end info to chat success");
} catch (e : any) {
if (!resp.ok) { throw (resp); }
else { this.fastify.log.info('game-end info to chat success'); }
}
// disable eslint for err catching
// eslint-disable-next-line @typescript-eslint/no-explicit-any
catch (e : any) {
this.fastify.log.error(`game-end info to chat failed: ${e}`);
}
}