~xenrox/10man-ts3

0c5cb0563a3d09ec0789d72c99c47c1c788bca22 — Thorben Günther 2 years ago a2151dd
Implement cancelling and finishing a match
1 files changed, 102 insertions(+), 1 deletions(-)

M app.ts
M app.ts => app.ts +102 -1
@@ 48,6 48,12 @@ teamspeak.on("textmessage", (ev) => {
        case "!cancel":
            cancel(clid, teamspeakID);
            break;
        case "!cancelMatch":
            cancel_match(clid, teamspeakID);
            break;
        case "!finish":
            finish_match(clid, teamspeakID, argv[1]);
            break;
    }
});



@@ 65,7 71,7 @@ function get_error(e: unknown): string {
    return JSON.parse(JSON.stringify(e)).response.errors[0].message;
}

async function request(query: string, variables: Variables): Promise<any> {
async function request(query: string, variables?: Variables): Promise<any> {
    const endpoint = process.env.ENDPOINT!;
    const graphQLClient = new GraphQLClient(endpoint, {
        headers: {},


@@ 75,6 81,34 @@ async function request(query: string, variables: Variables): Promise<any> {
    return data;
}

async function is_admin(clid: string, teamspeakID: string): Promise<boolean> {
    const query = gql`
        query ($teamspeakID: String!) {
            userByTS(teamspeakID: $teamspeakID) {
                admin
            }
        }
    `;
    const variables = { teamspeakID: teamspeakID };

    try {
        const data = await request(query, variables);
        const is_admin = data.userByTS.admin;
        return is_admin;
    } catch (e) {
        const error = get_error(e);
        switch (error) {
            case "no result":
                message(clid, "You are not registered with this teamspeakID.");
                break;
            default:
                console.error(error);
                break;
        }
        return false;
    }
}

async function elo(clid: string, teamspeakID: string) {
    const query = gql`
        query ($teamspeakID: String!) {


@@ 148,6 182,9 @@ async function play(clid: string, teamspeakID: string) {
            case "already in queue":
                message(clid, "Already in queue.");
                break;
            case "queue is full":
                message(clid, "Queue is full.");
                break;
            default:
                console.error(error);
                break;


@@ 178,3 215,67 @@ async function cancel(clid: string, teamspeakID: string) {
        }
    }
}

async function cancel_match(clid: string, teamspeakID: string) {
    const admin = await is_admin(clid, teamspeakID);
    if (!admin) {
        message(clid, "Only an admin can do that.");
        return;
    }

    const mutation = gql`
        mutation {
            cancelMatch
        }
    `;

    try {
        const data = await request(mutation);
        message_channel("Match cancelled.");
    } catch (e) {
        const error = get_error(e);
        switch (error) {
            case "no match ongoing":
                message(clid, "No ongoing match.");
                break;
            default:
                console.error(error);
                break;
        }
    }
}

async function finish_match(clid: string, teamspeakID: string, winner: string) {
    if (winner !== "TEAM1" && winner !== "TEAM2") {
        message(clid, "Illegal winning team. Only 'TEAM1' or 'TEAM2' allowed.");
        return;
    }

    const admin = await is_admin(clid, teamspeakID);
    if (!admin) {
        message(clid, "Only an admin can do that.");
        return;
    }

    const mutation = gql`
        mutation ($winner: String!) {
            finishMatch(winner: $winner)
        }
    `;
    const variables = { winner: winner };

    try {
        const data = await request(mutation, variables);
        message_channel("Match finished.");
    } catch (e) {
        const error = get_error(e);
        switch (error) {
            case "no match ongoing":
                message(clid, "No ongoing match.");
                break;
            default:
                console.error(error);
                break;
        }
    }
}