From 0c5cb0563a3d09ec0789d72c99c47c1c788bca22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Mon, 8 Nov 2021 16:06:41 +0100 Subject: [PATCH] Implement cancelling and finishing a match --- app.ts | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 1 deletion(-) diff --git a/app.ts b/app.ts index 8356461..19adaf1 100644 --- a/app.ts +++ b/app.ts @@ -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 { +async function request(query: string, variables?: Variables): Promise { const endpoint = process.env.ENDPOINT!; const graphQLClient = new GraphQLClient(endpoint, { headers: {}, @@ -75,6 +81,34 @@ async function request(query: string, variables: Variables): Promise { return data; } +async function is_admin(clid: string, teamspeakID: string): Promise { + 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; + } + } +} -- 2.44.0