From 62a50978f0211f454e870188cd5388cacbc83602 Mon Sep 17 00:00:00 2001 From: JC Date: Thu, 18 Apr 2024 00:55:22 -0400 Subject: [PATCH] Crafting works --- lib/queue/use.ts | 5 ++-- lib/slack/routes/use.ts | 55 +++++++++++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/lib/queue/use.ts b/lib/queue/use.ts index 9c690e6..c70cead 100644 --- a/lib/queue/use.ts +++ b/lib/queue/use.ts @@ -366,6 +366,7 @@ export class Results { if (node.break) break this.thread = result[result.length - 1].thread } + console.log('runSequence', prev) return prev } @@ -393,8 +394,8 @@ export class Results { ...this.thread, blocks: showAction( this.action, - description.slice(0, description.length - 1), - this.thread + description.slice(0, description.length - 2), + true ) }) const { permalink } = await props.client.chat.getPermalink({ diff --git a/lib/slack/routes/use.ts b/lib/slack/routes/use.ts index fbac524..e5d563a 100644 --- a/lib/slack/routes/use.ts +++ b/lib/slack/routes/use.ts @@ -1,18 +1,15 @@ import { log } from '../../analytics' import { type IdentityWithInventory, prisma } from '../../db' import { mappedPermissionValues } from '../../permissions' -import { - scheduler, - random, - Results, - showAction, - runNode -} from '../../queue/use' +import { scheduler, random, Results, showAction } from '../../queue/use' import { inMaintainers } from '../../utils' import slack, { execute, CommandMiddleware } from '../slack' const ACTION_TEST = ['action-test', 'test888'] +const sleep = async (seconds: number) => + new Promise(r => setTimeout(r, seconds * 1000)) + const canBeUsed = async ( user: IdentityWithInventory, instance: { itemId: string; quantity: number } @@ -212,18 +209,40 @@ slack.command('/use', async props => { node.action = action node.user = props.body.user_id if (i === trees.length) node.delay = 0 - let timestamp = new Date().getTime() - if (node.await) - timestamp += Array.isArray(node.await) - ? random(...node.await) - : node.await - if (i > 0 && trees[i - 1].delay) { - const prev = trees[i - 1] - timestamp += Array.isArray(prev.delay) - ? random(...prev.delay) - : prev.delay - } + if (!tag.startsWith('--')) + await sleep( + Array.isArray(node.await) ? random(...node.await) : node.await + ) await node.run(slack, trees.slice(0, i), description, summary) + if (!tag.startsWith('--')) + await sleep( + Array.isArray(node.delay) ? random(...node.delay) : node.delay + ) + } + + // Stop if last branch + if (!trees[trees.length - 1].branch.length) { + if (summary.outputs.length) + description.push( + `\n*What you got*: ${summary.outputs + .map(output => `x${output[0]} ${output[1]}`) + .join(', ')}` + ) + if (summary.losses.length) + description.push( + `\n*What you lost*: ${summary.losses + .map(loss => `x${loss[0]} ${loss[1]}`) + .join(', ')}` + ) + + await prisma.actionInstance.update({ + where: { id: action.id }, + data: { done: true } + }) + return await props.client.chat.update({ + ...trees[trees.length - 1].thread, + blocks: showAction(action, description, true) + }) } } else { ts = (