ThePooN / bancho.js

Interface with Bancho over IRC, made easy and reliable.
https://bancho.js.org
GNU General Public License v3.0
58 stars 6 forks source link

Emit timer events in BanchoLobby class. #31

Closed Froidland closed 12 months ago

Froidland commented 1 year ago

Emit corresponding events related to the !mp timer command for the BanchoLobby class.

timerEnd: image

timerAborted: image

timerTick: image image

Tests were done manually:

import BanchoJs from "bancho.js";

(async () => {
    const bancho = new BanchoJs.BanchoClient({
        username: "Pancho",
        password: "[REDACTED]",
        apiKey: "[REDACTED]",
    });

    await bancho.connect();

    const channel = await bancho.createLobby("Timer test");

    channel.lobby.invitePlayer("Pancho");

    bancho.on("CM", (msg) => {
        console.log(
            `[${msg.channel.name}] ${msg.user.ircUsername}: ${msg.message}`
        );
    });

    channel.lobby.on("timerEnded", () => {
        console.log("[TEST] Timer ended");
    });
    channel.lobby.on("timerAborted", () => {
        console.log("[TEST] Timer aborted");
    });
    channel.lobby.on("timerTick", (seconds) => {
        console.log(`[TEST] Timer tick: ${seconds}`);
    });

    process.on("SIGINT", async () => {
        console.log("Closing lobby and disconnecting...");
        await channel.lobby.closeLobby();
        bancho.disconnect();
    });
})();

/*
[TEST] Timer tick: 61
[#mp_111351854] BanchoBot: Countdown ends in 1 minute and 1 second
[TEST] Timer tick: 60
[#mp_111351854] BanchoBot: Countdown ends in 1 minute
[TEST] Timer tick: 30
[#mp_111351854] BanchoBot: Countdown ends in 30 seconds

[#mp_111351854] BanchoBot: Countdown ends in 1 second
[TEST] Timer ended
[#mp_111351854] BanchoBot: Countdown finished

[#mp_111351854] Pancho: !mp aborttimer
[TEST] Timer aborted
[#mp_111351854] BanchoBot: Countdown aborted
*/
ThePooN commented 1 year ago

Hey, thanks for the PR.

Bancho supports two kinds of timers, but only one can be active at a time: general-purpose timer, and start timer (set using !mp start <seconds>). Shouldn't we be looking to support both?

Froidland commented 12 months ago

Totally forgot about the !mp start timers 😅, latest commit adds them, tho I feel like the name startTimerStarted sounds a bit weird.

Manually tested:

import BanchoJs from "bancho.js";

(async () => {
    const bancho = new BanchoJs.BanchoClient({
        username: "Pancho",
        password: "",
        apiKey: "",
    });

    await bancho.connect();

    const channel = await bancho.createLobby("Timer test");

    bancho.on("CM", (msg) => {
        console.log(
            `[${msg.channel.name}] ${msg.user.ircUsername}: ${msg.message}`
        );
    });

    channel.lobby.on("startTimerStarted", (seconds) => {
        console.log(`[TEST] Start timer started: ${seconds}`);
    });
    channel.lobby.on("startTimerAborted", () => {
        console.log("[TEST] Start timer aborted");
    });
    channel.lobby.on("startTimerTick", (seconds) => {
        console.log(`[TEST] Start timer tick: ${seconds}`);
    });

    await channel.lobby.startMatch(121);

    const timeout = setTimeout(async () => {
        await channel.lobby.abortTimer();
    }, 110 * 1000);

    process.on("SIGINT", async () => {
        console.log("Closing lobby and disconnecting...");
        clearTimeout(timeout);
        await channel.lobby.closeLobby();
        bancho.disconnect();
    });
})();

/*
[#mp_111377983] Pancho: !mp start 121 3zghs9lby1u
[TEST] Start timer tick: 121
[#mp_111377983] BanchoBot: Match starts in 2 minutes and 1 second
[TEST] Start timer started: 121
[#mp_111377983] BanchoBot: Queued the match to start in 2 minutes and 1 second
[TEST] Start timer tick: 120
[#mp_111377983] BanchoBot: Match starts in 2 minutes
[TEST] Start timer tick: 60
[#mp_111377983] BanchoBot: Match starts in 1 minute
[TEST] Start timer tick: 30
[#mp_111377983] BanchoBot: Match starts in 30 seconds
[#mp_111377983] Pancho: !mp aborttimer e7gpz1fwv58
[TEST] Start timer aborted
[#mp_111377983] BanchoBot: Aborted the match start timer
[#mp_111377983] BanchoBot: Countdown aborted
*/

Please let me know if I'm missing something.

ThePooN commented 12 months ago

LGTM, can't think of anything missing.