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);

        if (this.hits.size === Object.keys(game.players).length - 1) {
            this.hits = new Set();
            this.resolver();
        }
    }
}
