2023-03-04 00:25:54 +00:00
|
|
|
import { socket, game } from "./main.js";
|
2023-03-03 17:34:15 +00:00
|
|
|
import { Packet } from "./packet.js";
|
|
|
|
|
2023-02-02 11:27:52 +00:00
|
|
|
/**
|
|
|
|
* Typical barrier type.
|
|
|
|
*
|
|
|
|
* Block all clients until everyone has hit the barrier.
|
|
|
|
*/
|
2023-03-03 17:34:15 +00:00
|
|
|
export class Barrier {
|
2023-02-02 11:27:52 +00:00
|
|
|
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) {
|
2023-02-02 11:27:52 +00:00
|
|
|
this.hits = new Set();
|
|
|
|
this.resolver();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|