feat: battle intro

This commit is contained in:
DaInfLoop 2024-06-20 02:03:30 +01:00
parent 2cc16e68c4
commit c0c45f75ac
3 changed files with 110 additions and 22 deletions

View file

@ -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) => {

23
opponents/beginner.js Normal file
View file

@ -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!`
}
]

View file

@ -1,11 +0,0 @@
[
{
"rawId": "beginnerOpponent1",
"name": "Beginner Opponent #1",
"stats": {
"health": 30,
"min": 10,
"max": 15
}
}
]