Riskless/static/js/modules/interface/barrier.js

34 lines
727 B
JavaScript
Raw Permalink Normal View History

2023-03-04 00:25:54 +00:00
import { socket, game } from "./main.js";
import { Packet } from "./packet.js";
/**
* Typical barrier type.
*
* Block all clients until everyone has hit the barrier.
*/
export class Barrier {
constructor() {
let resolver;
this.promise = new Promise((resolve) => {
resolver = resolve;
});
this.resolver = resolver;
this.hits = new Set();
}
wait() {
socket.emit("message", Packet.createBarrierSignal());
return this.promise;
}
resolve(data) {
this.hits.add(data.author);
2023-03-04 00:25:54 +00:00
if (this.hits.size === Object.keys(game.players).length - 1) {
this.hits = new Set();
this.resolver();
}
}
}