summaryrefslogtreecommitdiff
path: root/client/src/network/MessageProcessor.ts
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-09-05 21:44:37 -0600
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-09-05 21:44:37 -0600
commitd19da30f6dbf316bf89355b9b840a6d77e5435ec (patch)
tree1c4e406683f197c019edbfa249741798aac1b5b4 /client/src/network/MessageProcessor.ts
parentba989bd7f86f823e062cfce7437d89495ef527c6 (diff)
downloadjumpstorm-d19da30f6dbf316bf89355b9b840a6d77e5435ec.tar.gz
jumpstorm-d19da30f6dbf316bf89355b9b840a6d77e5435ec.zip
decrease CSP threshold, move client networking to its own folder
Diffstat (limited to 'client/src/network/MessageProcessor.ts')
-rw-r--r--client/src/network/MessageProcessor.ts74
1 files changed, 74 insertions, 0 deletions
diff --git a/client/src/network/MessageProcessor.ts b/client/src/network/MessageProcessor.ts
new file mode 100644
index 0000000..539937a
--- /dev/null
+++ b/client/src/network/MessageProcessor.ts
@@ -0,0 +1,74 @@
+import type { Game } from '@engine/Game';
+import { ComponentNames, Control, NetworkUpdateable } from '@engine/components';
+import { Entity } from '@engine/entities';
+import {
+ MessageType,
+ type Message,
+ type EntityAddBody,
+ type EntityUpdateBody,
+ type MessageProcessor
+} from '@engine/network';
+import { Input, SystemNames } from '@engine/systems';
+
+export class ClientMessageProcessor implements MessageProcessor {
+ private game: Game;
+
+ constructor(game: Game) {
+ this.game = game;
+ }
+
+ public process(message: Message) {
+ switch (message.type) {
+ case MessageType.NEW_ENTITIES:
+ const entityAdditions = message.body as unknown as EntityAddBody[];
+ entityAdditions.forEach((addBody) => {
+ const entity = Entity.from(
+ addBody.entityName,
+ addBody.id,
+ addBody.args
+ );
+ if (entity.hasComponent(ComponentNames.Control)) {
+ const clientId = this.game.getSystem<Input>(
+ SystemNames.Input
+ ).clientId;
+ const control = entity.getComponent<Control>(
+ ComponentNames.Control
+ );
+
+ if (control.controllableBy === clientId) {
+ entity.addComponent(new NetworkUpdateable());
+ }
+ }
+
+ this.game.addEntity(entity);
+ });
+ break;
+ case MessageType.REMOVE_ENTITIES:
+ const ids = message.body as unknown as string[];
+ ids.forEach((id) => this.game.removeEntity(id));
+ break;
+ case MessageType.UPDATE_ENTITIES:
+ const entityUpdates = message.body as unknown as EntityUpdateBody[];
+ entityUpdates.forEach(({ id, args }) => {
+ const entity = this.game.getEntity(id);
+ if (!entity) return;
+
+ if (entity && entity.hasComponent(ComponentNames.Control)) {
+ const clientId = this.game.getSystem<Input>(
+ SystemNames.Input
+ ).clientId;
+ const control = entity.getComponent<Control>(
+ ComponentNames.Control
+ );
+
+ // don't listen to entities which we control
+ if (control.controllableBy === clientId) return;
+ }
+ entity.setFrom(args);
+ });
+ break;
+ default:
+ break;
+ }
+ }
+}