Skip to content

Commit

Permalink
Move PING code into GameWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
rameshvarun committed Apr 4, 2023
1 parent 37680b4 commit be46fc6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 46 deletions.
28 changes: 24 additions & 4 deletions netplayjs-client/src/wrappers/gamewrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { PeerConnection } from "../matchmaking/peerconnection";
import * as utils from "../utils";
import * as lit from "lit-html";
import { GameMenu } from "../ui/gamemenu";
import EWMASD from "../ewmasd";

const PING_INTERVAL = 100;

export abstract class GameWrapper {
gameClass: GameClass;
Expand Down Expand Up @@ -174,9 +177,10 @@ export abstract class GameWrapper {
];

this.watchRTCStats(conn.peerConnection);
this.startClient(players, conn);

this.startPing(conn);
this.startVisibilityWatcher(conn);

this.startClient(players, conn);
});

gameMenu.onHostStart.once((conn) => {
Expand All @@ -187,9 +191,10 @@ export abstract class GameWrapper {
];

this.watchRTCStats(conn.peerConnection);
this.startHost(players, conn);

this.startPing(conn);
this.startVisibilityWatcher(conn);

this.startHost(players, conn);
});
}

Expand All @@ -215,6 +220,21 @@ export abstract class GameWrapper {
});
}

pingMeasure: EWMASD = new EWMASD(0.2);
startPing(conn: PeerConnection) {
setInterval(() => {
conn.send({ type: "ping-req", sent_time: performance.now() });
}, PING_INTERVAL);

conn.on("data", (data) => {
if (data.type == "ping-req") {
conn.send({ type: "ping-resp", sent_time: data.sent_time });
} else if (data.type == "ping-resp") {
this.pingMeasure.update(performance.now() - data.sent_time);
}
});
}

renderRTCStats(stats: RTCStatsReport): lit.TemplateResult {
return lit.html`
<details>
Expand Down
20 changes: 0 additions & 20 deletions netplayjs-client/src/wrappers/lockstepwrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import { Game, GameClass } from "../game";
import { PeerConnection } from "../matchmaking/peerconnection";
import { assert } from "chai";

const PING_INTERVAL = 100;

export class LockstepWrapper extends GameWrapper {
pingMeasure: EWMASD = new EWMASD(0.2);
game?: Game;
lockstepNetcode?: LockstepNetcode<Game, DefaultInput>;

Expand Down Expand Up @@ -56,19 +53,11 @@ export class LockstepWrapper extends GameWrapper {
input.deserialize(data.input);

this.lockstepNetcode!.onRemoteInput(data.frame, players![1], input);
} else if (data.type == "ping-req") {
conn.send({ type: "ping-resp", sent_time: data.sent_time });
} else if (data.type == "ping-resp") {
this.pingMeasure.update(Date.now() - data.sent_time);
}
});

console.log("Client has connected... Starting game...");

setInterval(() => {
conn.send({ type: "ping-req", sent_time: Date.now() });
}, PING_INTERVAL);

this.startGameLoop();
}

Expand Down Expand Up @@ -101,19 +90,10 @@ export class LockstepWrapper extends GameWrapper {
this.lockstepNetcode!.onRemoteInput(data.frame, players![0], input);
} else if (data.type === "state") {
this.lockstepNetcode!.onStateSync(data.frame, data.state);
} else if (data.type == "ping-req") {
conn.send({ type: "ping-resp", sent_time: data.sent_time });
} else if (data.type == "ping-resp") {
this.pingMeasure.update(Date.now() - data.sent_time);
}
});

console.log("Successfully connected to server... Starting game...");

setInterval(() => {
conn.send({ type: "ping-req", sent_time: Date.now() });
}, PING_INTERVAL);

this.startGameLoop();
}

Expand Down
22 changes: 0 additions & 22 deletions netplayjs-client/src/wrappers/rollbackwrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ import { PeerConnection } from "../matchmaking/peerconnection";

import * as lit from "lit-html";

const PING_INTERVAL = 100;

export class RollbackWrapper extends GameWrapper {
pingMeasure: EWMASD = new EWMASD(0.2);

game?: Game;

rollbackNetcode?: RollbackNetcode<Game, DefaultInput>;
Expand Down Expand Up @@ -67,19 +63,10 @@ export class RollbackWrapper extends GameWrapper {
let input = new DefaultInput();
input.deserialize(data.input);
this.rollbackNetcode!.onRemoteInput(data.frame, players![1], input);
} else if (data.type == "ping-req") {
conn.send({ type: "ping-resp", sent_time: data.sent_time });
} else if (data.type == "ping-resp") {
this.pingMeasure.update(Date.now() - data.sent_time);
}
});

console.log("Client has connected... Starting game...");

setInterval(() => {
conn.send({ type: "ping-req", sent_time: Date.now() });
}, PING_INTERVAL);

this.startGameLoop();
}

Expand Down Expand Up @@ -113,19 +100,10 @@ export class RollbackWrapper extends GameWrapper {
this.rollbackNetcode!.onRemoteInput(data.frame, players![0], input);
} else if (data.type === "state") {
this.rollbackNetcode!.onStateSync(data.frame, data.state);
} else if (data.type == "ping-req") {
conn.send({ type: "ping-resp", sent_time: data.sent_time });
} else if (data.type == "ping-resp") {
this.pingMeasure.update(Date.now() - data.sent_time);
}
});

console.log("Successfully connected to server... Starting game...");

setInterval(() => {
conn.send({ type: "ping-req", sent_time: Date.now() });
}, PING_INTERVAL);

this.startGameLoop();
}

Expand Down

0 comments on commit be46fc6

Please sign in to comment.