From 0e9b724cb7b93566469fbe2eb1052235afc9f5d8 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Thu, 6 May 2021 20:09:32 +0800 Subject: [PATCH] fix(chess): fix puppeteer integration, fix #263 --- packages/plugin-chess/src/index.ts | 19 +++++++++---------- packages/plugin-chess/src/state.ts | 4 +++- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/plugin-chess/src/index.ts b/packages/plugin-chess/src/index.ts index 90f4c0a0bb..4bba5bec07 100644 --- a/packages/plugin-chess/src/index.ts +++ b/packages/plugin-chess/src/index.ts @@ -41,6 +41,9 @@ export const name = 'chess' export function apply(ctx: Context) { ctx = ctx.group() + State.imageMode = !!ctx.puppeteer + ctx.on('delegate/puppeteer', () => State.imageMode = true) + ctx.on('connect', async () => { if (!ctx.database) return const channels = await ctx.database.getAssignedChannels(['id', 'chess']) @@ -48,7 +51,6 @@ export function apply(ctx: Context) { if (chess) { states[id] = State.from(chess) states[id].update = rules[chess.rule].update - states[id].imageMode = !!ctx.puppeteer } } }) @@ -221,15 +223,12 @@ export function apply(ctx: Context) { .action(({ session, options }) => { const state = states[session.cid] if (!state) return - - if (ctx.app.puppeteer) { - if (options.textMode) { - state.imageMode = false - return state.draw(session, '已切换到文本模式。') - } else if (options.imageMode) { - state.imageMode = true - return state.draw(session, '已切换到图片模式。') - } + if (options.textMode) { + state.imageMode = false + return state.draw(session, '已切换到文本模式。') + } else if (options.imageMode) { + state.imageMode = true + return state.draw(session, '已切换到图片模式。') } }) }) diff --git a/packages/plugin-chess/src/state.ts b/packages/plugin-chess/src/state.ts index 474b15a1c1..fdb66efe11 100644 --- a/packages/plugin-chess/src/state.ts +++ b/packages/plugin-chess/src/state.ts @@ -26,6 +26,8 @@ export class State { imageMode: boolean update: (this: State, x: number, y: number, value: 1 | -1) => MoveResult | string + static imageMode: boolean + constructor(public readonly rule: string, public readonly size: number, public readonly placement: 'cross' | 'grid') { this.area = BigInt(size * size) this.full = (1n << this.area) - 1n @@ -161,7 +163,7 @@ export class State { } async draw(session: Session, message: string = '', x?: number, y?: number) { - if (this.imageMode) { + if (this.imageMode ?? State.imageMode) { const [image] = await Promise.all([ this.drawSvg(x, y).render(session.app), message && session.send(message),