From a4b3cc3c4bf09e0d7e78e2b44c2079fe886737ac Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Fri, 2 Jan 2026 14:49:59 +0100 Subject: [PATCH] chore(eslint/format): remformatted all files and fixed eslint issues --- src/pong/src/app.ts | 310 +++++++++++++++++++------------ src/pong/src/broadcast.ts | 34 ++-- src/pong/src/chat_types.ts | 10 +- src/pong/src/routes/broadcast.ts | 6 - src/pong/src/run.ts | 2 +- src/pong/src/setGameLink.ts | 3 +- 6 files changed, 213 insertions(+), 152 deletions(-) diff --git a/src/pong/src/app.ts b/src/pong/src/app.ts index a889aed..d4dc42c 100644 --- a/src/pong/src/app.ts +++ b/src/pong/src/app.ts @@ -10,12 +10,8 @@ import { broadcast } from './broadcast'; import type { ClientProfil, ClientMessage } from './chat_types'; import { sendInvite } from './sendInvite'; import { setGameLink } from './setGameLink'; -import { emit } from 'process'; -import { Boolean, Record } from 'typebox/type'; import { UserId } from '@shared/database/mixin/user'; - - // colors for console.log export const color = { red: '\x1b[31m', @@ -30,8 +26,8 @@ declare const __SERVICE_NAME: string; // Global map of clients // key = socket, value = clientname interface ClientInfo { - user: string; - lastSeen: number; + user: string; + lastSeen: number; } export const clientChat = new Map(); @@ -76,45 +72,61 @@ declare module 'fastify' { io: Server<{ inviteGame: (data: ClientProfil) => void; message: (msg: string) => void; - batmove_Left: (direction: "up" | "down") => void; - batmove_Right: (direction: "up" | "down") => void; - batLeft_update: (y:number) => void; - batRight_update: (y:number) => void; - ballPos_update: (x:number, y:number) => void; + batmove_Left: (direction: 'up' | 'down') => void; + batmove_Right: (direction: 'up' | 'down') => void; + batLeft_update: (y: number) => void; + batRight_update: (y: number) => void; + ballPos_update: (x: number, y: number) => void; MsgObjectServer: (data: { message: ClientMessage }) => void; - queuJoin: (userID : UserId) => void; + queuJoin: (userID: UserId) => void; }>; } } -function isInRange(x : number, low : number, high : number) { - if (x >= low && x <= high) - return (true); - return (false); +function isInRange(x: number, low: number, high: number) { + if (x >= low && x <= high) return true; + return false; } -async function sendScore(socket : Socket, scoreLeft : number, scoreRight : number) { // idk why, sometimes... it fails? - let msg : ClientMessage = {destination : "score-info", command: "", user:"", text:scoreLeft.toString() + ":" + scoreRight.toString(), SenderWindowID:""}; +async function sendScore( + socket: Socket, + scoreLeft: number, + scoreRight: number, +) { + // idk why, sometimes... it fails? + const msg: ClientMessage = { + destination: 'score-info', + command: '', + user: '', + text: scoreLeft.toString() + ':' + scoreRight.toString(), + SenderWindowID: '', + }; - socket.emit('MsgObjectServer', {message : msg}); + socket.emit('MsgObjectServer', { message: msg }); } async function onReady(fastify: FastifyInstance) { - // shows address for connection au server transcendance const session = process.env.SESSION_MANAGER ?? ''; if (session) { const part = session.split('/')[1]; const machineName = part.split('.')[0]; - console.log(color.yellow, 'Connect at : https://' + machineName + ':8888/app/login'); + console.log( + color.yellow, + 'Connect at : https://' + machineName + ':8888/app/login', + ); } // DRAW AREA - const TOP_EDGE = 0; // top edge of the field - const BOTTOM_EDGE = 450; // bottom edge of the field; + // top edge of the field + const TOP_EDGE = 0; + // bottom edge of the field; + const BOTTOM_EDGE = 450; const LEFT_EDGE = 0; const RIGHT_EDGE = 800; + void LEFT_EDGE; + // PADDLEs const PADDLE_HEIGHT = 80; const PADDLE_WIDTH = 12; @@ -122,20 +134,25 @@ async function onReady(fastify: FastifyInstance) { const PADDLE_SPEED = 20; const PADDLE_X_OFFSET = 4; - const MAX_PADDLE_Y = BOTTOM_EDGE - PADDLE_HEIGHT; // 370 - const PADDLE_START = BOTTOM_EDGE / 2 - PADDLE_HEIGHT / 2; // 185 + // 370 + const MAX_PADDLE_Y = BOTTOM_EDGE - PADDLE_HEIGHT; + // 185 + const PADDLE_START = BOTTOM_EDGE / 2 - PADDLE_HEIGHT / 2; // BALL - const BALL_SIZE = 8 * 2 + 4; // widht times 2 bc rounded on moth sides + 4 for border - const START_BALLX = (RIGHT_EDGE / 2) - BALL_SIZE; - const START_BALLY = (BOTTOM_EDGE / 2) - BALL_SIZE; + // widht times 2 bc rounded on moth sides + 4 for border + const BALL_SIZE = 8 * 2 + 4; + const START_BALLX = RIGHT_EDGE / 2 - BALL_SIZE; + const START_BALLY = BOTTOM_EDGE / 2 - BALL_SIZE; const ACCELERATION_FACTOR = 1.15; const ABS_MAX_BALL_SPEED = 3; // val inits - let paddleLeft = PADDLE_START; //shared start bat position - let paddleRight = PADDLE_START; //shared start bat position + // shared start bat position + let paddleLeft = PADDLE_START; + // shared start bat position + let paddleRight = PADDLE_START; let ballPosX = START_BALLX; let ballPosY = START_BALLY; @@ -144,123 +161,174 @@ async function onReady(fastify: FastifyInstance) { let scoreL = 0; let scoreR = 0; - - let games : Record = {}; // uuid, game uid - if not in game empty string + // uuid, game uid - if not in game empty string + const games: Record = {}; fastify.io.on('connection', (socket: Socket) => { - socket.emit("batLeft_update", paddleLeft); - socket.emit("batRight_update", paddleRight); - socket.emit("ballPos_update", ballPosX, ballPosY); + socket.emit('batLeft_update', paddleLeft); + socket.emit('batRight_update', paddleRight); + socket.emit('ballPos_update', ballPosX, ballPosY); sendScore(socket, scoreL, scoreR); // GAME - // paddle handling - socket.on('batmove_Left', (direction: "up" | "down") => { - if (direction === "up") { - paddleLeft -= PADDLE_SPEED; - } - if (direction === "down") { - paddleLeft += PADDLE_SPEED; - } - // position of bat leftplokoplpl - paddleLeft = Math.max(TOP_EDGE, Math.min(MAX_PADDLE_Y, paddleLeft)); - console.log("batLeft_update:", paddleLeft); - socket.emit("batLeft_update", paddleLeft); - }); - socket.on('batmove_Right', (direction: "up" | "down") => { - if (direction === "up") { - paddleRight -= PADDLE_SPEED; - } - if (direction === "down") { - paddleRight += PADDLE_SPEED; - } - // position of bat left - paddleRight = Math.max(TOP_EDGE, Math.min(MAX_PADDLE_Y, paddleRight)); - socket.emit("batRight_update", paddleRight); - }); - // ball handling: - setInterval(async () => { - const new_ballPosX = ballPosX + ballSpeedX; - const new_ballPosY = ballPosY + ballSpeedY; + // paddle handling + socket.on('batmove_Left', (direction: 'up' | 'down') => { + if (direction === 'up') { + paddleLeft -= PADDLE_SPEED; + } + if (direction === 'down') { + paddleLeft += PADDLE_SPEED; + } + // position of bat leftplokoplpl + paddleLeft = Math.max(TOP_EDGE, Math.min(MAX_PADDLE_Y, paddleLeft)); + console.log('batLeft_update:', paddleLeft); + socket.emit('batLeft_update', paddleLeft); + }); + socket.on('batmove_Right', (direction: 'up' | 'down') => { + if (direction === 'up') { + paddleRight -= PADDLE_SPEED; + } + if (direction === 'down') { + paddleRight += PADDLE_SPEED; + } + // position of bat left + paddleRight = Math.max( + TOP_EDGE, + Math.min(MAX_PADDLE_Y, paddleRight), + ); + socket.emit('batRight_update', paddleRight); + }); + // ball handling: + setInterval(async () => { + const new_ballPosX = ballPosX + ballSpeedX; + const new_ballPosY = ballPosY + ballSpeedY; - if (((isInRange(new_ballPosY, paddleLeft, paddleLeft + PADDLE_HEIGHT) || isInRange(new_ballPosY + BALL_SIZE *2, paddleLeft, paddleLeft + PADDLE_HEIGHT)) // y ok ? - && isInRange(new_ballPosX, PADDLE_X_OFFSET, PADDLE_X_OFFSET + PADDLE_WIDTH) && ballSpeedX < 0) || // x ok? && ball going toward paddle? - ((isInRange(new_ballPosY, paddleRight, paddleRight + PADDLE_HEIGHT) || isInRange(new_ballPosY + BALL_SIZE *2, paddleRight, paddleRight + PADDLE_HEIGHT)) // right side equations - && isInRange(new_ballPosX + BALL_SIZE * 2, RIGHT_EDGE - PADDLE_X_OFFSET - PADDLE_WIDTH, RIGHT_EDGE - PADDLE_X_OFFSET)) && ballSpeedX > 0) - { - ballSpeedX *= -1; - ballSpeedX *= ACCELERATION_FACTOR; - ballSpeedY *= ACCELERATION_FACTOR; - console.log('bat colision'); - } - else if (new_ballPosX < 0 || new_ballPosX + BALL_SIZE*2 > RIGHT_EDGE) { - ballPosX = START_BALLX; - ballPosY = START_BALLY; - ballSpeedX = (Math.random() - .5) < 0 ? -1 : 1; + if ( + ((isInRange( + new_ballPosY, + paddleLeft, + paddleLeft + PADDLE_HEIGHT, + ) || + isInRange( + new_ballPosY + BALL_SIZE * 2, + paddleLeft, + paddleLeft + PADDLE_HEIGHT, + )) && + // y ok ? + isInRange( + new_ballPosX, + PADDLE_X_OFFSET, + PADDLE_X_OFFSET + PADDLE_WIDTH, + ) && + ballSpeedX < 0) || + // x ok? && ball going toward paddle? + ((isInRange( + new_ballPosY, + paddleRight, + paddleRight + PADDLE_HEIGHT, + ) || + isInRange( + new_ballPosY + BALL_SIZE * 2, + paddleRight, + paddleRight + PADDLE_HEIGHT, + )) && + // right side equations + isInRange( + new_ballPosX + BALL_SIZE * 2, + RIGHT_EDGE - PADDLE_X_OFFSET - PADDLE_WIDTH, + RIGHT_EDGE - PADDLE_X_OFFSET, + ) && + ballSpeedX > 0) + ) { + ballSpeedX *= -1; + ballSpeedX *= ACCELERATION_FACTOR; + ballSpeedY *= ACCELERATION_FACTOR; + console.log('bat colision'); + } + else if ( + new_ballPosX < 0 || + new_ballPosX + BALL_SIZE * 2 > RIGHT_EDGE + ) { + ballPosX = START_BALLX; + ballPosY = START_BALLY; + ballSpeedX = Math.random() - 0.5 < 0 ? -1 : 1; - if (new_ballPosX < 0) { - scoreR += 1; - ballSpeedY = -1; - } else { - scoreL += 1; - ballSpeedY = 1; - } - if (scoreL >= 5 || scoreR >= 5) - { - console.log('game should stop + board reset'); - ballSpeedX = 0; // temp solution - ballSpeedY = 0; - // reset board :D - } - console.log('point scored'); - sendScore(socket, scoreL, scoreR); - // TODO: score point + ball reset + spd reset + if (new_ballPosX < 0) { + scoreR += 1; + ballSpeedY = -1; } - else if (new_ballPosY < 0 || new_ballPosY + BALL_SIZE*2 > BOTTOM_EDGE) { - ballSpeedY *= -1; - ballSpeedX *= ACCELERATION_FACTOR; - ballSpeedY *= ACCELERATION_FACTOR; + else { + scoreL += 1; + ballSpeedY = 1; } - ballSpeedX = Math.max(-ABS_MAX_BALL_SPEED, Math.min(ballSpeedX, ABS_MAX_BALL_SPEED)); - ballSpeedY = Math.max(-ABS_MAX_BALL_SPEED, Math.min(ballSpeedY, ABS_MAX_BALL_SPEED)); + if (scoreL >= 5 || scoreR >= 5) { + console.log('game should stop + board reset'); + // temp solution + ballSpeedX = 0; + ballSpeedY = 0; + // reset board :D + } + console.log('point scored'); + sendScore(socket, scoreL, scoreR); + // TODO: score point + ball reset + spd reset + } + else if ( + new_ballPosY < 0 || + new_ballPosY + BALL_SIZE * 2 > BOTTOM_EDGE + ) { + ballSpeedY *= -1; + ballSpeedX *= ACCELERATION_FACTOR; + ballSpeedY *= ACCELERATION_FACTOR; + } + ballSpeedX = Math.max( + -ABS_MAX_BALL_SPEED, + Math.min(ballSpeedX, ABS_MAX_BALL_SPEED), + ); + ballSpeedY = Math.max( + -ABS_MAX_BALL_SPEED, + Math.min(ballSpeedY, ABS_MAX_BALL_SPEED), + ); - ballPosX += ballSpeedX; - ballPosY += ballSpeedY; + ballPosX += ballSpeedX; + ballPosY += ballSpeedY; - socket.emit("ballPos_update", ballPosX, ballPosY); - }, 16) + socket.emit('ballPos_update', ballPosX, ballPosY); + }, 16); // QUEUE HANDL - socket.on('queuJoin', async (uuid: UserId) => { - console.log('queu join recieved for : ', uuid); - if (!games.hasOwnProperty(uuid)) { - console.log("new user in game search queu"); - games[uuid] = ""; - } else if (games.hasOwnProperty(uuid) && games[uuid] == "") { - console.log('already searching for game'); - } else { // (games.hasOwnProperty(uuid) && games[uuid] != "") { - console.log('user alredy in game'); - return ; - } - // TODO: step2 : sesrch in record<> find guid w/ "" &/ pair them up - // TODO: step3 : move game logic to lifecycle of queu'ed game - }) + socket.on('queuJoin', async (uuid: UserId) => { + console.log('queu join recieved for : ', uuid); + if (!(uuid in games.hasOwnProperty)) { + console.log('new user in game search queu'); + games[uuid] = ''; + } + else if (uuid in games && games[uuid] == '') { + console.log('already searching for game'); + } + else { + // (games.hasOwnProperty(uuid) && games[uuid] != "") { + console.log('user alredy in game'); + return; + } + // TODO: step2 : sesrch in record<> find guid w/ "" &/ pair them up + // TODO: step3 : move game logic to lifecycle of queu'ed game + }); // other: socket.on('message', (message: string) => { const obj: ClientMessage = JSON.parse(message) as ClientMessage; clientChat.set(socket.id, { user: obj.user, lastSeen: Date.now() }); - socket.emit('welcome', {msg: 'Welcome to the chat! : '}); + socket.emit('welcome', { msg: 'Welcome to the chat! : ' }); broadcast(fastify, obj, obj.SenderWindowID); }); socket.on('inviteGame', async (data: string) => { const clientName: string = clientChat.get(socket.id)?.user || ''; const profilInvite: ClientProfil = JSON.parse(data) || ''; - const inviteHtml: string = 'invites you to a game ' + setGameLink(''); + const inviteHtml: string = + 'invites you to a game ' + setGameLink(''); if (clientName !== null) { sendInvite(fastify, inviteHtml, profilInvite); } }); - }); } diff --git a/src/pong/src/broadcast.ts b/src/pong/src/broadcast.ts index ad04321..ac93f1f 100644 --- a/src/pong/src/broadcast.ts +++ b/src/pong/src/broadcast.ts @@ -3,20 +3,20 @@ import { clientChat, color } from './app'; import { FastifyInstance } from 'fastify'; export function broadcast(fastify: FastifyInstance, data: ClientMessage, sender?: string) { - fastify.io.fetchSockets().then((sockets) => { - for (const socket of sockets) { - // Skip sender's own socket - if (socket.id === sender) continue; - // Get client name from map - const clientInfo = clientChat.get(socket.id); - if (!clientInfo?.user) { - console.log(color.yellow, `Skipping socket ${socket.id} (no user found)`); - continue; - } - // Emit structured JSON object - socket.emit('MsgObjectServer', { message: data }); - // Debug logs - // console.log(color.green, `'DEBUG LOG: Broadcast to:', ${data.command} message: ${data.text}`); - } - }); -} \ No newline at end of file + fastify.io.fetchSockets().then((sockets) => { + for (const socket of sockets) { + // Skip sender's own socket + if (socket.id === sender) continue; + // Get client name from map + const clientInfo = clientChat.get(socket.id); + if (!clientInfo?.user) { + console.log(color.yellow, `Skipping socket ${socket.id} (no user found)`); + continue; + } + // Emit structured JSON object + socket.emit('MsgObjectServer', { message: data }); + // Debug logs + // console.log(color.green, `'DEBUG LOG: Broadcast to:', ${data.command} message: ${data.text}`); + } + }); +} diff --git a/src/pong/src/chat_types.ts b/src/pong/src/chat_types.ts index 5ed14f9..73ceeeb 100644 --- a/src/pong/src/chat_types.ts +++ b/src/pong/src/chat_types.ts @@ -9,15 +9,15 @@ export type ClientMessage = { export type ClientProfil = { command: string, destination: string, - type: string, + type: string, user: string, loginName: string, userID: string, text: string, timestamp: number, - SenderWindowID:string, + SenderWindowID: string, SenderName: string, - Sendertext: string, - innerHtml?: string, + Sendertext: string, + innerHtml?: string, -}; \ No newline at end of file +}; diff --git a/src/pong/src/routes/broadcast.ts b/src/pong/src/routes/broadcast.ts index 8c9118f..8f4b533 100644 --- a/src/pong/src/routes/broadcast.ts +++ b/src/pong/src/routes/broadcast.ts @@ -2,15 +2,12 @@ import { FastifyPluginAsync } from 'fastify'; import { Static, Type } from 'typebox'; import { broadcast } from '../broadcast'; - - export const PongReq = Type.Object({ message: Type.String(), }); export type PongReq = Static; - const route: FastifyPluginAsync = async (fastify): Promise => { fastify.post<{ Body: PongReq }>( '/api/pong/broadcast', @@ -38,9 +35,6 @@ export default route; * send message info to the fronatend via the route '/api/pong/broadcast' */ - - - // const route: FastifyPluginAsync = async (fastify): Promise => { // fastify.post('/api/chat/broadcast', { // schema: { diff --git a/src/pong/src/run.ts b/src/pong/src/run.ts index d9f1e2a..c050473 100644 --- a/src/pong/src/run.ts +++ b/src/pong/src/run.ts @@ -33,4 +33,4 @@ const start = async () => { process.exit(1); }; }; -start(); \ No newline at end of file +start(); diff --git a/src/pong/src/setGameLink.ts b/src/pong/src/setGameLink.ts index c5067fa..e0cb51b 100644 --- a/src/pong/src/setGameLink.ts +++ b/src/pong/src/setGameLink.ts @@ -1,7 +1,6 @@ - export function setGameLink(link: string): string { if (!link) { link = 'Click me'; } return link; -}; \ No newline at end of file +};