fix(pong-end messages): added pong usernames instead of ids

This commit is contained in:
bgoulard 2026-01-09 10:26:22 +01:00 committed by Nigel
parent 0a9c727e92
commit 317c8b62bf
2 changed files with 9 additions and 22 deletions

View file

@ -8,15 +8,12 @@ import { isNullish } from "@app/utils";
import client from "@app/api"; import client from "@app/api";
import "./pong.css"; import "./pong.css";
// get the name of the machine used to connect
declare module 'ft_state' { declare module 'ft_state' {
interface State { interface State {
pongSock?: CSocket; pongSock?: CSocket;
} }
} }
// GameRdyDown = "Ready Up?"
// GameRdyUp = "Ready down?"
enum QueueState { enum QueueState {
InQueu = "In Queue", InQueu = "In Queue",
InGame = "In Game", InGame = "In Game",
@ -44,9 +41,6 @@ export function getSocket(): CSocket {
function pongClient(_url: string, _args: RouteHandlerParams): RouteHandlerReturn { function pongClient(_url: string, _args: RouteHandlerParams): RouteHandlerReturn {
setTitle('Pong Game Page'); setTitle('Pong Game Page');
// MAYBE: "queue up" btn : adds timer to page for duration of queue
// TODO: "local play" btn : emit "local new game" evt to server; play game on single computer (maybe need to change keys-handling logic)
return { return {
html: authHtml, postInsert: async (app) => { html: authHtml, postInsert: async (app) => {
const DEFAULT_COLOR = "white"; const DEFAULT_COLOR = "white";
@ -281,11 +275,10 @@ function pongClient(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
} }
resetBoard(batLeft, batRight, playerL, playerR); resetBoard(batLeft, batRight, playerL, playerR);
}) })
// pretty info for queue :3
socket.on('updateInformation', (e) => { socket.on('updateInformation', (e) => {
queue_infos.innerText = `${e.totalUser}👤 ${e.inQueue}${e.totalGames}▮•▮`; queue_infos.innerText = `${e.totalUser}👤 ${e.inQueue}${e.totalGames}▮•▮`;
}); });
socket.on('queueEvent', (e) => showInfo(`QueueEvent: ${e}`)); socket.on('queueEvent', (e) => showInfo(`QueueEvent: ${e}`)); // MAYBE: play a sound? to notify user that smthing happend
// --- // ---
// queue evt end // queue evt end
// --- // ---
@ -295,8 +288,6 @@ function pongClient(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
queueBtn.innerText = QueueState.Iddle; queueBtn.innerText = QueueState.Iddle;
rdy_btn.innerText = ReadyState.readyUp; rdy_btn.innerText = ReadyState.readyUp;
resetBoard(batLeft, batRight, playerL, playerR); resetBoard(batLeft, batRight, playerL, playerR);
showInfo("butter");
showInfo("butter-toast");
} }
} }
}; };

View file

@ -5,7 +5,6 @@ import { Pong } from './game';
import { GameMove, GameUpdate, SSocket } from './socket'; import { GameMove, GameUpdate, SSocket } from './socket';
import { isNullish } from '@shared/utils'; import { isNullish } from '@shared/utils';
import { PongGameId, PongGameOutcome } from '@shared/database/mixin/pong'; import { PongGameId, PongGameOutcome } from '@shared/database/mixin/pong';
import https from 'https';
type PUser = { type PUser = {
id: UserId; id: UserId;
@ -188,12 +187,14 @@ class StateI {
const winner = game.checkWinner() ?? 'left'; const winner = game.checkWinner() ?? 'left';
let player: PUser | undefined = undefined; let player: PUser | undefined = undefined;
if ((player = this.users.get(game.userLeft)) !== undefined) { if ((player = this.users.get(game.userLeft)) !== undefined) {
chat_text += player.id + ' and '; chat_text += (this.fastify.db.getUser(player.id)?.name ?? player.id) + ' and ';
// chat_text += player.id + ' and ';
player.currentGame = null; player.currentGame = null;
player.socket.emit('gameEnd', winner); player.socket.emit('gameEnd', winner);
} }
if ((player = this.users.get(game.userRight)) !== undefined) { if ((player = this.users.get(game.userRight)) !== undefined) {
chat_text += player.id ; chat_text += (this.fastify.db.getUser(player.id)?.name ?? player.id);
// chat_text += player.id;
player.currentGame = null; player.currentGame = null;
player.socket.emit('gameEnd', winner); player.socket.emit('gameEnd', winner);
} }
@ -204,7 +205,7 @@ 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.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 !'); this.fastify.log.info('SetGameOutcome !');
if (!game.local) { if (!game.local) {
let payload = {'nextGame':chat_text}; // TODO: add names of ppl let payload = {'nextGame':chat_text};
try { try {
const resp = await fetch("http://app-chat/api/chat/broadcast", { const resp = await fetch("http://app-chat/api/chat/broadcast", {
method:'POST', method:'POST',
@ -212,18 +213,13 @@ class StateI {
body: JSON.stringify(payload), body: JSON.stringify(payload),
}); });
if (!resp.ok) { if (!resp.ok)
console.log(`fail :( ${resp}`);
console.log('resp:'+resp.body);
throw(resp); throw(resp);
}
else else
console.log("success"); this.fastify.log.info("game-end info to chat success");
} catch (e : any) { } catch (e : any) {
console.log(`error gotten: ${e}`); this.fastify.log.error(`game-end info to chat failed: ${e}`);
throw (e);
} }
// announce to chat
} }
} }