From d68b48083df7c4f8233183a9850257eb5916ff42 Mon Sep 17 00:00:00 2001 From: Alexejhero <32238504+Alexejhero@users.noreply.github.com> Date: Sat, 15 Jun 2024 05:34:04 +0200 Subject: [PATCH] start working on ebs-side redeems --- ebs/src/modules/transactions.ts | 4 +++- ebs/src/util/pishock.ts | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 ebs/src/util/pishock.ts diff --git a/ebs/src/modules/transactions.ts b/ebs/src/modules/transactions.ts index 0654a14..9540fc3 100644 --- a/ebs/src/modules/transactions.ts +++ b/ebs/src/modules/transactions.ts @@ -218,6 +218,8 @@ app.post( }; } try { + // TODO: special handling for different types of redeems + const resMsg = await connection.redeem(redeem, cart, userInfo, transaction.token); if (resMsg?.success) { console.log(`[${resMsg.guid}] Redeem succeeded: ${JSON.stringify(resMsg)}`); @@ -230,7 +232,7 @@ app.post( logContext.important = false; logMessage.header = "Redeem did not succeed"; logMessage.content = resMsg; - sendToLogger(logContext); + sendToLogger(logContext).then(); res.status(500).send(resMsg?.message ?? "Redeem failed"); } } catch (error) { diff --git a/ebs/src/util/pishock.ts b/ebs/src/util/pishock.ts new file mode 100644 index 0000000..20d509b --- /dev/null +++ b/ebs/src/util/pishock.ts @@ -0,0 +1,38 @@ +const apiUrl: string = "https://do.pishock.com/api/apioperate/"; + +async function sendOperation(op: number, intensity: number, duration: number) { + try { + const data = { + Username: process.env.PISHOCK_USERNAME, + Apikey: process.env.PISHOCK_APIKEY, + Code: process.env.PISHOCK_CODE, + Name: "Swarm Control", + + Op: op, + Intensity: intensity, + Duration: duration, + }; + + console.log(`Sending PiShock operation: ${op} ${intensity} ${duration}`); + + const response = await fetch(apiUrl, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(data), + }); + + if (!response.ok) { + console.error("Failed to send PiShock operation"); + console.error(response.status, await response.text()); + } + } catch (e: any) { + console.error("Failed to send PiShock operation"); + console.error(e); + } +} + +export async function sendShock(intensity: number, duration: number) { + await sendOperation(0, intensity, duration); +}