retrieved what Maieul had done with my app.ts
This commit is contained in:
parent
bee33f96ff
commit
66aa65c9ef
1 changed files with 172 additions and 76 deletions
|
|
@ -97,93 +97,189 @@ async function onReady(fastify: FastifyInstance, game: TTC) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
declare const __SERVICE_NAME: string;
|
// // TODO: Import the plugins defined for this microservice
|
||||||
|
// // TODO: Import the routes defined for this microservice
|
||||||
|
|
||||||
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
|
// // @brief The microservice app (as a plugin for Fastify), kinda like a main function I guess ???
|
||||||
const plugins = import.meta.glob('./plugins/**/*.ts', { eager: true });
|
// // @param fastify
|
||||||
// @ts-expect-error: import.meta.glob is a vite thing. Typescript doesn't know this...
|
// // @param opts
|
||||||
const routes = import.meta.glob('./routes/**/*.ts', { eager: true });
|
// export const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
|
||||||
|
// // Register all the fastify plugins that this app will use
|
||||||
|
|
||||||
const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
|
// // Once it is done:
|
||||||
void opts;
|
// fastify.ready((err) => {
|
||||||
|
// if (err) {
|
||||||
|
// throw err;
|
||||||
|
// }
|
||||||
|
// // TODO: Supposedly, something should be there I guess
|
||||||
|
// });
|
||||||
|
// };
|
||||||
|
// // Export it as the default for this file.
|
||||||
|
// export default app;
|
||||||
|
|
||||||
await fastify.register(utils.useMonitoring);
|
// // TODO: Understand what is this for in /src/chat/src/app.ts
|
||||||
await fastify.register(utils.useMakeResponse);
|
// // declare module 'fastify' {
|
||||||
await fastify.register(swagger.useSwagger, { service: __SERVICE_NAME });
|
// // interface FastifyInstance {
|
||||||
await fastify.register(db.useDatabase as FastifyPluginAsync, {});
|
// // io: Server<{
|
||||||
await fastify.register(auth.jwtPlugin as FastifyPluginAsync, {});
|
// // hello: (message: string) => string;
|
||||||
await fastify.register(auth.authPlugin as FastifyPluginAsync, {});
|
// // MsgObjectServer: (data: { message: ClientMessage }) => void;
|
||||||
|
// // message: (msg: string) => void;
|
||||||
|
// // testend: (sock_id_client: string) => void;
|
||||||
|
// // }>;
|
||||||
|
// // }
|
||||||
|
// // }
|
||||||
|
|
||||||
// Place here your custom code!
|
// // TODO: Same for this, also in /src/chat/src/app.ts
|
||||||
for (const plugin of Object.values(plugins)) {
|
// // async function onReady(fastify: FastifyInstance) {
|
||||||
void fastify.register(plugin as FastifyPluginAsync, {});
|
// // function connectedUser(io?: Server, target?: string): number {
|
||||||
}
|
// // let count = 0;
|
||||||
for (const route of Object.values(routes)) {
|
// // const seen = new Set<string>();
|
||||||
void fastify.register(route as FastifyPluginAsync, {});
|
// // // <- only log/count unique usernames
|
||||||
}
|
|
||||||
|
|
||||||
void fastify.register(fastifyFormBody, {});
|
// // for (const [socketId, username] of clientChat) {
|
||||||
void fastify.register(fastifyMultipart, {});
|
// // // Basic sanity checks
|
||||||
|
// // if (typeof socketId !== 'string' || socketId.length === 0) {
|
||||||
|
// // clientChat.delete(socketId);
|
||||||
|
// // continue;
|
||||||
|
// // }
|
||||||
|
// // if (typeof username !== 'string' || username.length === 0) {
|
||||||
|
// // clientChat.delete(socketId);
|
||||||
|
// // continue;
|
||||||
|
// // }
|
||||||
|
|
||||||
const game = new TTC();
|
// // // If we have the io instance, attempt to validate the socket is still connected
|
||||||
|
// // if (io && typeof io.sockets?.sockets?.get === 'function') {
|
||||||
|
// // const s = io.sockets.sockets.get(socketId) as
|
||||||
|
// // | Socket
|
||||||
|
// // | undefined;
|
||||||
|
// // // If socket not found or disconnected, remove from map and skip
|
||||||
|
// // if (!s || s.disconnected) {
|
||||||
|
// // clientChat.delete(socketId);
|
||||||
|
// // continue;
|
||||||
|
// // }
|
||||||
|
|
||||||
fastify.ready((err) => {
|
// // // Skip duplicates (DO NOT delete them — just don't count)
|
||||||
if (err) throw err;
|
// // if (seen.has(username)) {
|
||||||
onReady(fastify, game);
|
// // continue;
|
||||||
});
|
// // }
|
||||||
};
|
// // // socket exists and is connected
|
||||||
export default app;
|
// // seen.add(username);
|
||||||
export { app };
|
// // count++;
|
||||||
|
// // // console.log(color.green,"count: ", count);
|
||||||
|
// // console.log(color.yellow, 'Client:', color.reset, username);
|
||||||
|
|
||||||
// When using .decorate you have to specify added properties for Typescript
|
// // const targetSocketId = target;
|
||||||
declare module 'fastify' {
|
// // io.to(targetSocketId!).emit('listObj', username);
|
||||||
interface FastifyInstance {
|
|
||||||
io: Server<{
|
|
||||||
hello: (message: string) => string;
|
|
||||||
// idk you put something
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
gameState: any;
|
|
||||||
makeMove: (idx: number) => void;
|
|
||||||
resetGame: () => void;
|
|
||||||
error: string,
|
|
||||||
}>;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function onReady(fastify: FastifyInstance, game: TTC) {
|
// // console.log(
|
||||||
fastify.io.on('connection', (socket) => {
|
// // color.yellow,
|
||||||
fastify.log.info(`Client connected: ${socket.id}`);
|
// // 'Chat Socket ID:',
|
||||||
|
// // color.reset,
|
||||||
|
// // socketId,
|
||||||
|
// // );
|
||||||
|
// // continue;
|
||||||
|
// // }
|
||||||
|
|
||||||
socket.emit('gameState', {
|
// // // If no io provided, assume entries in the map are valid and count them.
|
||||||
board: game.board,
|
// // count++;
|
||||||
turn: game.currentPlayer,
|
// // console.log(
|
||||||
gameOver: game.isGameOver,
|
// // color.red,
|
||||||
});
|
// // 'Client (unverified):',
|
||||||
|
// // color.reset,
|
||||||
|
// // username,
|
||||||
|
// // );
|
||||||
|
// // console.log(
|
||||||
|
// // color.red,
|
||||||
|
// // 'Chat Socket ID (unverified):',
|
||||||
|
// // color.reset,
|
||||||
|
// // socketId,
|
||||||
|
// // );
|
||||||
|
// // }
|
||||||
|
|
||||||
socket.on('makeMove', (idx: number) => {
|
// // return count;
|
||||||
const result = game.makeMove(idx);
|
// // }
|
||||||
|
|
||||||
if (result === 'invalidMove') {
|
// // function broadcast(data: ClientMessage, sender?: string) {
|
||||||
socket.emit('error', 'Invalid Move');
|
// // fastify.io.fetchSockets().then((sockets) => {
|
||||||
}
|
// // for (const s of sockets) {
|
||||||
else {
|
// // if (s.id !== sender) {
|
||||||
fastify.io.emit('gameState', {
|
// // // Send REAL JSON object
|
||||||
board: game.board,
|
// // const clientName = clientChat.get(s.id) || null;
|
||||||
turn: game.currentPlayer,
|
// // if (clientName !== null) {
|
||||||
lastResult: result,
|
// // s.emit('MsgObjectServer', { message: data });
|
||||||
});
|
// // }
|
||||||
}
|
// // console.log(' Target window socket ID:', s.id);
|
||||||
});
|
// // console.log(' Target window ID:', [...s.rooms]);
|
||||||
|
// // console.log(' Sender window ID:', sender ? sender : 'none');
|
||||||
|
// // }
|
||||||
|
// // }
|
||||||
|
// // });
|
||||||
|
// // }
|
||||||
|
|
||||||
socket.on('resetGame', () => {
|
// // fastify.io.on('connection', (socket: Socket) => {
|
||||||
game.reset();
|
// // socket.on('message', (message: string) => {
|
||||||
fastify.io.emit('gameState', {
|
// // console.info(
|
||||||
board: game.board,
|
// // color.blue,
|
||||||
turn: game.currentPlayer,
|
// // 'Socket connected!',
|
||||||
reset: true,
|
// // color.reset,
|
||||||
});
|
// // socket.id,
|
||||||
});
|
// // );
|
||||||
});
|
// // console.log(
|
||||||
};
|
// // color.blue,
|
||||||
|
// // 'Received message from client',
|
||||||
|
// // color.reset,
|
||||||
|
// // message,
|
||||||
|
// // );
|
||||||
|
|
||||||
export default app;
|
// // const obj: ClientMessage = JSON.parse(message) as ClientMessage;
|
||||||
|
// // clientChat.set(socket.id, obj.user);
|
||||||
|
// // console.log(
|
||||||
|
// // color.green,
|
||||||
|
// // 'Message from client',
|
||||||
|
// // color.reset,
|
||||||
|
// // `Sender: login name: "${obj.user}" - windowID "${obj.SenderWindowID}" - text message: "${obj.text}"`,
|
||||||
|
// // );
|
||||||
|
// // // Send object directly — DO NOT wrap it in a string
|
||||||
|
// // broadcast(obj, obj.SenderWindowID);
|
||||||
|
// // console.log(
|
||||||
|
// // color.red,
|
||||||
|
// // 'connected in the Chat :',
|
||||||
|
// // connectedUser(fastify.io),
|
||||||
|
// // color.reset,
|
||||||
|
// // );
|
||||||
|
// // });
|
||||||
|
|
||||||
|
// // socket.on('testend', (sock_id_cl: string) => {
|
||||||
|
// // console.log('testend received from client socket id:', sock_id_cl);
|
||||||
|
// // });
|
||||||
|
|
||||||
|
// // socket.on('list', () => {
|
||||||
|
// // console.log(color.red, 'list activated', color.reset, socket.id);
|
||||||
|
// // connectedUser(fastify.io, socket.id);
|
||||||
|
// // });
|
||||||
|
|
||||||
|
// // socket.on('disconnecting', (reason) => {
|
||||||
|
// // const clientName = clientChat.get(socket.id) || null;
|
||||||
|
// // console.log(
|
||||||
|
// // color.green,
|
||||||
|
// // `Client disconnecting: ${clientName} (${socket.id}) reason:`,
|
||||||
|
// // reason,
|
||||||
|
// // );
|
||||||
|
// // if (reason === 'transport error') return;
|
||||||
|
|
||||||
|
// // if (clientName !== null) {
|
||||||
|
// // const obj = {
|
||||||
|
// // type: 'chat',
|
||||||
|
// // user: clientName,
|
||||||
|
// // token: '',
|
||||||
|
// // text: 'LEFT the chat',
|
||||||
|
// // timestamp: Date.now(),
|
||||||
|
// // SenderWindowID: socket.id,
|
||||||
|
// // };
|
||||||
|
|
||||||
|
// // broadcast(obj, obj.SenderWindowID);
|
||||||
|
// // // clientChat.delete(obj.user);
|
||||||
|
// // }
|
||||||
|
// // });
|
||||||
|
// // });
|
||||||
|
// // }
|
||||||
Loading…
Add table
Add a link
Reference in a new issue