Skip to content

Commit

Permalink
fix: Add 100ms delay which is accurate to tetrio.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Sup3rFire committed Feb 7, 2024
1 parent e1cf7ac commit 0ab6acf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
6 changes: 4 additions & 2 deletions src/game/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ export default class Game extends EventEmitter {

public ended = false;

public replaySaved?: Promise<void>;
public endData?: any;
public replayData: any[] = [];

public me?: ClientPlayer;

public players: Map<string, Player>;

public saveReplay() {
if (!this.endData.leaderboard) throw new Error("The game has not ended!");
public async saveReplay() {
if (!this.endData.leaderboard || !this.replaySaved) throw new Error("The game has not ended!");
await this.replaySaved;
return JSON.stringify({
ismulti: !0,
data: this.replayData,
Expand Down
28 changes: 15 additions & 13 deletions src/ws/commands/game.advance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import WebSocketManager from "../WebSocketManager";
export default async function (ws: WebSocketManager, { data }: any) {
if (!ws.client.room?.game) return;

ws.client.room.game.replayData.push({
board: data.currentboard,
replays: (data.currentboard as any[]).map((x) => {
let player = [...(ws.client.room.game?.players as Map<string, Player>).values()].find(
(k) => k.user.id === x.id
) as Player;
return { frames: player.replayFrames.at(-1).frame, events: player.replayFrames };
}),
});
ws.client.room.game.players.forEach((v, k) => {
v.replayFrames = [];
ws.client.room.game?.players.set(k, v);
});
setTimeout(() => {
ws.client.room.game?.replayData.push({
board: data.currentboard,
replays: (data.currentboard as any[]).map((x) => {
let player = [...(ws.client.room.game?.players as Map<string, Player>).values()].find(
(k) => k.user.id === x.id
) as Player;
return { frames: player.replayFrames.at(-1).frame, events: player.replayFrames };
}),
});
ws.client.room.game?.players.forEach((v, k) => {
v.replayFrames = [];
ws.client.room.game?.players.set(k, v);
});
}, 100);
}
29 changes: 17 additions & 12 deletions src/ws/commands/game.end.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,23 @@ export default async function (ws: WebSocketManager, { data }: any) {

ws.client.room.game.ended = true;
ws.client.room.game.endData = data;
ws.client.room.game.replayData.push({
board: data.currentboard,
replays: (data.currentboard as any[]).map((x) => {
let player = [...(ws.client.room.game?.players as Map<string, Player>).values()].find(
(k) => k.user.id === x.id
) as Player;
return { frames: player.replayFrames.at(-1).frame, events: player.replayFrames };
}),
});
ws.client.room.game.players.forEach((v, k) => {
v.replayFrames = [];
ws.client.room.game?.players.set(k, v);
ws.client.room.game.replaySaved = new Promise((resolve) => {
setTimeout(() => {
ws.client.room.game?.replayData.push({
board: data.currentboard,
replays: (data.currentboard as any[]).map((x) => {
let player = [...(ws.client.room.game?.players as Map<string, Player>).values()].find(
(k) => k.user.id === x.id
) as Player;
return { frames: player.replayFrames.at(-1).frame, events: player.replayFrames };
}),
});
ws.client.room.game?.players.forEach((v, k) => {
v.replayFrames = [];
ws.client.room.game?.players.set(k, v);
});
resolve();
}, 100);
});
ws.client.room.game.me?.end();

Expand Down

0 comments on commit 0ab6acf

Please sign in to comment.