Skip to content

Commit

Permalink
Improve network data for player, objects and events (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelRipoll23 authored Jan 4, 2025
1 parent 30ba022 commit 9fc19c1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 51 deletions.
8 changes: 7 additions & 1 deletion src/models/game-player.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export class GamePlayer {
private id: string;
private displayId: string;
private host: boolean;
private pingTime: number | null = null;
private name: string;
Expand All @@ -11,7 +12,8 @@ export class GamePlayer {
name = "Unknown",
score = 0
) {
this.id = id;
this.id = id.replace(/-/g, "");
this.displayId = id;
this.host = host;
this.name = name;
this.score = score;
Expand All @@ -21,6 +23,10 @@ export class GamePlayer {
return this.id;
}

public getDisplayId(): string {
return this.displayId;
}

public isHost(): boolean {
return this.host;
}
Expand Down
10 changes: 8 additions & 2 deletions src/objects/base/base-multiplayer-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class BaseMultiplayerGameObject
implements MultiplayerGameObject
{
protected id: string | null = null;
protected displayId: string | null = null;
protected typeId: ObjectType | null = null;
protected syncableByHost: boolean = false;
protected owner: GamePlayer | null = null;
Expand All @@ -31,8 +32,13 @@ export class BaseMultiplayerGameObject
return this.id;
}

public setId(syncableId: string): void {
this.id = syncableId;
public setId(id: string): void {
this.id = id.replace(/-/g, "");
this.displayId = id;
}

public getDisplayId(): string | null {
return this.displayId;
}

public getTypeId(): ObjectType | null {
Expand Down
6 changes: 3 additions & 3 deletions src/objects/scoreboard-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,17 @@ export class ScoreboardObject
}

public serialize(): ArrayBuffer {
const arrayBuffer = new ArrayBuffer(4);
const arrayBuffer = new ArrayBuffer(2);
const dataView = new DataView(arrayBuffer);

dataView.setInt32(0, this.elapsedMilliseconds);
dataView.setUint16(0, this.elapsedMilliseconds);

return arrayBuffer;
}

public synchronize(data: ArrayBuffer): void {
const dataView = new DataView(data);
this.elapsedMilliseconds = dataView.getInt32(0);
this.elapsedMilliseconds = dataView.getUint16(0);
}

public sendSyncableData(webrtcPeer: WebRTCPeer, data: ArrayBuffer): void {
Expand Down
31 changes: 23 additions & 8 deletions src/screens/world-screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,12 @@ export class WorldScreen extends BaseCollidingGameScreen {
return console.warn("Array buffer is null");
}

const countdownNumber = new DataView(arrayBuffer).getInt32(0);
// Check if we are receiving a countdown event as host
if (this.gameState.getMatch()?.isHost()) {
return console.warn("Host should not receive countdown event");
}

const countdownNumber = new DataView(arrayBuffer).getInt8(0);

this.countdownCurrentNumber = countdownNumber;
this.showCountdown();
Expand All @@ -331,8 +336,8 @@ export class WorldScreen extends BaseCollidingGameScreen {
}

private sendCountdownEvent() {
const arrayBuffer = new ArrayBuffer(4);
new DataView(arrayBuffer).setInt32(0, this.countdownCurrentNumber);
const arrayBuffer = new ArrayBuffer(1);
new DataView(arrayBuffer).setInt8(0, this.countdownCurrentNumber);

const countdownStartEvent = new RemoteEvent(EventType.Countdown);
countdownStartEvent.setBuffer(arrayBuffer);
Expand Down Expand Up @@ -420,10 +425,10 @@ export class WorldScreen extends BaseCollidingGameScreen {
const playerId: string = player.getId();
const playerScore: number = player.getScore();

const arrayBuffer = new ArrayBuffer(36 + 4);
const arrayBuffer = new ArrayBuffer(32 + 1);

new Uint8Array(arrayBuffer).set(new TextEncoder().encode(playerId), 0);
new DataView(arrayBuffer).setInt32(36, playerScore);
new DataView(arrayBuffer).setUint8(32, playerScore);

const goalEvent = new RemoteEvent(EventType.GoalStart);
goalEvent.setBuffer(arrayBuffer);
Expand Down Expand Up @@ -470,6 +475,11 @@ export class WorldScreen extends BaseCollidingGameScreen {
return console.warn("Array buffer is null");
}

// Check if we are receiving a goal event as host
if (this.gameState.getMatch()?.isHost()) {
return console.warn("Host should not receive goal event");
}

// Pause timer
this.scoreboardObject?.stopTimer();

Expand All @@ -480,8 +490,8 @@ export class WorldScreen extends BaseCollidingGameScreen {
this.gameState.getMatch()?.setState(MatchStateType.GoalScored);

// Score
const playerId = new TextDecoder().decode(arrayBuffer.slice(0, 36));
const playerScore = new DataView(arrayBuffer).getInt32(36);
const playerId = new TextDecoder().decode(arrayBuffer.slice(0, 32));
const playerScore = new DataView(arrayBuffer).getUint8(32);

const player = this.gameState.getMatch()?.getPlayer(playerId) ?? null;
player?.setScore(playerScore);
Expand Down Expand Up @@ -547,7 +557,7 @@ export class WorldScreen extends BaseCollidingGameScreen {
private sendGameOverStartEvent(winner: GamePlayer): void {
const playerId: string = winner.getId();

const arrayBuffer = new ArrayBuffer(36);
const arrayBuffer = new ArrayBuffer(32);
new Uint8Array(arrayBuffer).set(new TextEncoder().encode(playerId), 0);

const gameOverStartEvent = new RemoteEvent(EventType.GameOverStart);
Expand All @@ -563,6 +573,11 @@ export class WorldScreen extends BaseCollidingGameScreen {
return console.warn("Array buffer is null");
}

// Check if we are receiving a game over event as host
if (this.gameState.getMatch()?.isHost()) {
return console.warn("Host should not receive game over event");
}

const playerId = new TextDecoder().decode(arrayBuffer);
const player = this.gameState.getMatch()?.getPlayer(playerId) ?? null;

Expand Down
16 changes: 8 additions & 8 deletions src/services/matchmaking-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export class MatchmakingService {
return this.handleUnavailableSlots(peer);
}

const playerId = new TextDecoder().decode(payload.slice(0, 36));
const playerName = new TextDecoder().decode(payload.slice(36));
const playerId = new TextDecoder().decode(payload.slice(0, 32));
const playerName = new TextDecoder().decode(payload.slice(32));

console.log("Received join request from", playerName);

Expand Down Expand Up @@ -152,10 +152,10 @@ export class MatchmakingService {
const dataView = new DataView(payload);

const state = dataView.getUint8(0);
const id = new TextDecoder().decode(payload.slice(1, 37));
const host = dataView.getUint8(37) === 1;
const score = dataView.getUint8(38);
const nameBytes = payload.slice(39);
const id = new TextDecoder().decode(payload.slice(1, 33));
const host = dataView.getUint8(33) === 1;
const score = dataView.getUint8(34);
const nameBytes = payload.slice(35);

const name = new TextDecoder().decode(nameBytes);

Expand Down Expand Up @@ -239,11 +239,11 @@ export class MatchmakingService {
return console.warn("Invalid player ping payload", payload);
}

const idBytes = payload.slice(0, 36);
const idBytes = payload.slice(0, 32);
const id = new TextDecoder().decode(idBytes);

const dataView = new DataView(payload);
const pingTime = dataView.getUint16(36);
const pingTime = dataView.getUint16(32);

this.gameState.getMatch()?.getPlayer(id)?.setPingTime(pingTime);
}
Expand Down
18 changes: 5 additions & 13 deletions src/services/object-orchestrator-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { MultiplayerScreen } from "../interfaces/screen/multiplayer-screen.js";
import { ObjectStateType } from "../enums/object-state-type.js";
import { ScreenUtils } from "../utils/screen-utils.js";
import { WebRTCType } from "../enums/webrtc-type.js";
import { WebRTCUtils } from "../utils/webrtc-utils.js";

export class ObjectOrchestrator {
private readonly PERIODIC_MILLISECONDS = 500;
Expand Down Expand Up @@ -65,9 +64,7 @@ export class ObjectOrchestrator {
const dataView = new DataView(data);
const screenId = dataView.getInt8(0);
const stateId = dataView.getInt8(1);

let ownerId = new TextDecoder().decode(new Uint8Array(data, 4, 32));
ownerId = WebRTCUtils.addHyphensToUUID(ownerId);
const ownerId = new TextDecoder().decode(new Uint8Array(data, 4, 32));

// Check for owner
if (ObjectUtils.hasInvalidOwner(webrtcPeer, ownerId)) {
Expand Down Expand Up @@ -170,17 +167,14 @@ export class ObjectOrchestrator {
const stateId = multiplayerObject.getState();
const layerId = multiplayerScreen?.getObjectLayer(multiplayerObject);
const typeId = multiplayerObject.getTypeId();
const serializedData = multiplayerObject.serialize();

let ownerId = multiplayerObject.getOwner()?.getId() ?? null;
let objectId = multiplayerObject.getId();
const ownerId = multiplayerObject.getOwner()?.getId() ?? null;
const objectId = multiplayerObject.getId();

if (typeId === null || objectId === null || ownerId === null) {
throw new Error("Invalid object data for object id " + objectId);
}

ownerId = WebRTCUtils.removeHyphensFromUUID(ownerId);
objectId = WebRTCUtils.removeHyphensFromUUID(objectId);
const serializedData = multiplayerObject.serialize();

const ownerIdBytes = new TextEncoder().encode(ownerId);
const objectIdBytes = new TextEncoder().encode(objectId);
Expand Down Expand Up @@ -251,9 +245,7 @@ export class ObjectOrchestrator {
ownerId: string,
data: ArrayBuffer
) {
let objectId = new TextDecoder().decode(new Uint8Array(data, 36, 32));
objectId = WebRTCUtils.addHyphensToUUID(objectId);

const objectId = new TextDecoder().decode(new Uint8Array(data, 36, 32));
const serializedData = data.slice(68);

// Try to find object
Expand Down
16 changes: 0 additions & 16 deletions src/utils/webrtc-utils.ts

This file was deleted.

0 comments on commit 9fc19c1

Please sign in to comment.