From 26165d701fcb745ab9df09975af059c530a30467 Mon Sep 17 00:00:00 2001 From: Tchips46 Date: Tue, 17 Feb 2026 22:48:13 +0900 Subject: [PATCH] fix(pong-network): terrain and paddle out security --- example/pong-network/client/main.ts | 13 +++++++++++++ example/pong-network/client/systems.ts | 6 +++--- example/pong-network/server/systems.ts | 25 ++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/example/pong-network/client/main.ts b/example/pong-network/client/main.ts index 276c063a..b08dafb6 100644 --- a/example/pong-network/client/main.ts +++ b/example/pong-network/client/main.ts @@ -36,6 +36,19 @@ export const main = async (options: IRunOptions) => { graphics.stage.add(layer); + const terrain = registry.spawnEntity(); + registry.addComponent(terrain, new Position(0, 0)); + registry.addComponent( + terrain, + new RectangleComponent(new Rect({ fill: "rgb(58,99,39)", width: 1920, height: 1080 })), + ); + const terrainLine = registry.spawnEntity(); + registry.addComponent(terrainLine, new Position(955, 0)); + registry.addComponent( + terrainLine, + new RectangleComponent(new Rect({ fill: "rgb(148,204,117)", width: 10, height: 1080 })), + ); + const ball = registry.spawnEntity(); registry.addComponent(ball, new Velocity(0, 0)); registry.addComponent(ball, new Position(0, 0)); diff --git a/example/pong-network/client/systems.ts b/example/pong-network/client/systems.ts index 14bd43da..06155e54 100644 --- a/example/pong-network/client/systems.ts +++ b/example/pong-network/client/systems.ts @@ -82,11 +82,11 @@ export function packetHandler(registry: Registry, ctx: Context) { it.Velocity.y = packet.velocity.y; } else if (type === "assignId") { if (packet.assigned === "ball") { - registry.addComponent(registry.entityFromIndex(0), new NetworkId(packet.id)); - } else if (packet.assigned === "paddle1") { registry.addComponent(registry.entityFromIndex(2), new NetworkId(packet.id)); + } else if (packet.assigned === "paddle1") { + registry.addComponent(registry.entityFromIndex(4), new NetworkId(packet.id)); } else if (packet.assigned === "paddle2") { - registry.addComponent(registry.entityFromIndex(3), new NetworkId(packet.id)); + registry.addComponent(registry.entityFromIndex(5), new NetworkId(packet.id)); } } }); diff --git a/example/pong-network/server/systems.ts b/example/pong-network/server/systems.ts index 60ee736f..3d676e2d 100644 --- a/example/pong-network/server/systems.ts +++ b/example/pong-network/server/systems.ts @@ -64,9 +64,17 @@ function handleClientInput(clientId: number, key: string, network: NetworkServer if (key === "up") { paddle.Velocity.y = -paddleSpeed; + if (paddle.Position.y < 0) { + paddle.Position.y = 0; + paddle.Velocity.y = 0; + } } if (key === "down") { paddle.Velocity.y = paddleSpeed; + if (paddle.Position.y > 780) { + paddle.Position.y = 780; + paddle.Velocity.y = 0; + } } if (key === "stop") { paddle.Velocity.y = 0; @@ -102,13 +110,28 @@ export function packetHandler(registry: Registry, ctx: Context) { }); } +function checkOutOfTerrain(id: number, paddle: any, network: NetworkServerLibrary) { + if (paddle.Position.y < 0) { + paddle.Position.y = 0; + paddle.Velocity.y = 0; + sendMoveAll(id, paddle.Velocity, paddle.Position, network); + } + if (paddle.Position.y > 780) { + paddle.Position.y = 780; + paddle.Velocity.y = 0; + sendMoveAll(id, paddle.Velocity, paddle.Position, network); + } +} + export const bounce = (registry: Registry, ctx: Context) => { const network = ctx.libs.getNetwork(); + const zip = registry.getZipper([Position, Velocity]); + checkOutOfTerrain(1, zip[1], network); + checkOutOfTerrain(2, zip[2], network); if (roundStart < 3000 && roundStart != -1) { roundStart += ctx.app.delta; return; } - const zip = registry.getZipper([Position, Velocity]); if (roundStart >= 3000) { roundStart = -1; zip[0].Velocity.x = 1;