From c0c45f75accb9d0aaddafe0e6694a830bdbd7257 Mon Sep 17 00:00:00 2001 From: DaInfLoop Date: Thu, 20 Jun 2024 02:03:30 +0100 Subject: [PATCH] feat: battle intro --- index.js | 98 ++++++++++++++++++++++++++++++++++++----- opponents/beginner.js | 23 ++++++++++ opponents/beginner.json | 11 ----- 3 files changed, 110 insertions(+), 22 deletions(-) create mode 100644 opponents/beginner.js delete mode 100644 opponents/beginner.json diff --git a/index.js b/index.js index 88e0f98..2ea17ed 100644 --- a/index.js +++ b/index.js @@ -3,15 +3,16 @@ const postgres = require('postgres'); require('dotenv').config() const sql = postgres({ - host: 'hackclub.app', - port: 5432, + host: '/var/run/postgresql', database: 'haroon_slackmaster', - username: 'haroon', - password: process.env.PGSQL_PASSWORD, - ssl: 'require' + username: 'haroon' }) -const BeginnerOpponents = require('./opponents/beginner.json'); +const BeginnerOpponents = require('./opponents/beginner'); + +const AllOpponents = [ + ...BeginnerOpponents +] const app = new App({ token: process.env.SLACK_BOT_TOKEN, @@ -29,7 +30,7 @@ async function initializeUser(slackUserId) { } app.use(async (ctx) => { - await initializeUser(ctx.body.user_id) + await initializeUser(ctx.context.userId) await ctx.next() }) @@ -37,9 +38,31 @@ app.use(async (ctx) => { app.command('/chooseopponent', async (ctx) => { await ctx.ack(); + const user = await initializeUser(ctx.body.user_id); + + if (user.currentopponent != "None") { + const opponent = AllOpponents.find(x => x.rawId == user.currentopponent) + + return await ctx.respond({ + response_type: 'ephemeral', + text: `You are already in a battle with ${opponent.name}.`, + blocks: [ + { + type: 'section', + text: { + type: 'mrkdwn', + text: `*Battle Master:* You are already in a battle with *${opponent.name}*. Please finish your battle with them before proceeding. +${user.battlemessage}` + } + } + ] + }) + } + await ctx.client.views.open({ trigger_id: ctx.body.trigger_id, view: { + "private_metadata": ctx.payload.channel_id, "type": "modal", "callback_id": "chooseopponent", "title": { @@ -234,6 +257,7 @@ app.view("chooseopponent", async (ctx) => { await ctx.ack({ response_action: 'update', view: { + "private_metadata": ctx.payload.private_metadata, "type": "modal", "callback_id": "chooseopponent-" + rank, "title": { @@ -292,11 +316,63 @@ app.view("chooseopponent", async (ctx) => { app.view("chooseopponent-BEGINNER", async (ctx) => { await ctx.ack(); - // const userId = + const channelId = ctx.view.private_metadata; + const userId = ctx.context.userId; - // ctx.client.chat.postMessage({ - // channel: "" - // }) + const slackUser = (await ctx.client.users.info({ user: userId })).user.profile; + + const opponent = BeginnerOpponents.find(o => o.rawId == Object.values(ctx.payload.state.values)[0].opponents.selected_option.value); + + const player = await initializeUser(userId); + + await sql`UPDATE users + SET playerhealth = ${player.health}, + playermin = ${player.mindmg}, + playermax = ${player.maxdmg}, + + currentOpponent = ${opponent.rawId}, + + opponenthealth = ${opponent.stats.health}, + opponentmin = ${opponent.stats.min}, + opponentmax = ${opponent.stats.max} + + WHERE slack_id = ${userId};` + + const msg = await ctx.client.chat.postMessage({ + channel: channelId, + text: `${slackUser.display_name_normalized} started a battle against ${opponent.name}.`, + blocks: [ + { + type: 'section', + text: { + type: 'mrkdwn', + text: opponent.intro.replaceAll("{player}", slackUser.display_name_normalized) + }, + "accessory": { + "type": "image", + "image_url": opponent.image, + "alt_text": opponent.name + } + }, + { + "type": "actions", + "elements": [ + { + "type": "button", + "text": { + "type": "plain_text", + "text": "Continue", + "emoji": true + }, + "value": "continue", + "action_id": "continue" + } + ] + } + ] + }); + + await sql`UPDATE users SET battlemessage = ${`https://hackclub.slack.com/archives/${channelId}/p${msg.ts.replace('.', '')}`} WHERE slack_id = ${userId};` }) app.command('/bm-eval', async (ctx) => { diff --git a/opponents/beginner.js b/opponents/beginner.js new file mode 100644 index 0000000..2532d85 --- /dev/null +++ b/opponents/beginner.js @@ -0,0 +1,23 @@ +module.exports = [ + { + "rawId": "rapStar", + "name": "Rap Star", + "stats": { + "health": 25, + "min": 2, + "max": 5 + }, + "image": "https://media.discordapp.net/attachments/1108683335347212389/1118119440702242816/BM_-_Rap_Star_1.png?ex=6674a820&is=667356a0&hm=520cf408d3cadad838b0d2350fb212470949257cbd2768bfea64523feb33b494&", + "intro": `*_{player} walks down the streets of Vibe City minding their own business until suddenly Rap Star appears with his hand pointing directly at {player}_* + +*Rap Star:* Beep! +*{player}:* Erm... Hi? +*Rap Star:* Beep boop bop +*{player}:* Is this some kind of joke? Do you speak English? + +*_Rap Star throws his microphone at {player}'s head. They get knocked down._* + +*{player}:* Is that how you're gonna be? Fine then, let's battle! +*Rap Star:* BEEP!` + } +] \ No newline at end of file diff --git a/opponents/beginner.json b/opponents/beginner.json deleted file mode 100644 index 858883a..0000000 --- a/opponents/beginner.json +++ /dev/null @@ -1,11 +0,0 @@ -[ - { - "rawId": "beginnerOpponent1", - "name": "Beginner Opponent #1", - "stats": { - "health": 30, - "min": 10, - "max": 15 - } - } -] \ No newline at end of file