From 0476622fa53ec6cafceb6a09644bdf9ac2527e1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Thu, 11 Nov 2021 13:25:33 +0100 Subject: [PATCH] Add function to manipulate user data For now only elo is possible. --- app.ts | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/app.ts b/app.ts index 4c49097..69e9cbb 100644 --- a/app.ts +++ b/app.ts @@ -31,6 +31,7 @@ let commands = new Map([ let admin_commands = new Map([ ["!cancelMatch", "Cancel the ongoing match"], ["!finish ", "Finish the ongoing match with WINNER (TEAM1, TEAM2)"], + ["!set ", "Set ATTRIBUTE (elo) to VALUE"], ]); teamspeak.on("ready", async () => { @@ -71,6 +72,9 @@ teamspeak.on("textmessage", (ev) => { case "!finish": finish_match(clid, teamspeakID, argv[1]); break; + case "!set": + set(clid, teamspeakID, argv[1], argv[2], argv[3]); + break; case "!help": help(clid, teamspeakID); break; @@ -101,6 +105,34 @@ async function request(query: string, variables?: Variables): Promise { return data; } +async function get_user_id(clid: string, steamID64: string): Promise { + const query = gql` + query ($steamID: String!) { + userBySteam(steamID: $steamID) { + id + } + } + `; + const variables = { steamID: steamID64 }; + + try { + const data = await request(query, variables); + const id = data.userBySteam.id; + return id; + } catch (e) { + const error = get_error(e); + switch (error) { + case "no result": + message(clid, "This user is not registered."); + break; + default: + console.error(error); + break; + } + return -1n; + } +} + async function is_admin(clid: string, teamspeakID: string): Promise { const query = gql` query ($teamspeakID: String!) { @@ -421,3 +453,51 @@ async function help(clid: string, teamspeakID: string) { teamspeak.sendTextMessage(clid, 1, msg); } + +async function set( + clid: string, + teamspeakID: string, + steamID64: string, + attribute: string, + value: string +) { + const admin = await is_admin(clid, teamspeakID); + if (!admin) { + message(clid, "Only an admin can do that."); + return; + } + + switch (attribute) { + case "elo": + set_elo(clid, steamID64, value); + break; + default: + message(clid, "Attribute not found."); + break; + } +} + +async function set_elo(clid: string, steamID64: string, elo: string) { + const id = await get_user_id(clid, steamID64); + if (id == -1n) { + return; + } + + const mutation = gql` + mutation ($id: Int!, $elo: Int!) { + updateUser(id: $id, input: { elo: $elo }) { + elo + } + } + `; + + try { + // converting can fail, so put it in the try block + const variables = { id: id, elo: Number(BigInt(elo)) }; + const data = await request(mutation, variables); + message(clid, "Elo updated successfully."); + } catch (e) { + console.error(e); + message(clid, "Elo update failed."); + } +} -- 2.44.0