Skip to content

Commit

Permalink
fix(chess): fix puppeteer integration, fix #263
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 6, 2021
1 parent 3dd372d commit 0e9b724
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
19 changes: 9 additions & 10 deletions packages/plugin-chess/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ 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'])
for (const { id, chess } of channels) {
if (chess) {
states[id] = State.from(chess)
states[id].update = rules[chess.rule].update
states[id].imageMode = !!ctx.puppeteer
}
}
})
Expand Down Expand Up @@ -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, '已切换到图片模式。')
}
})
})
Expand Down
4 changes: 3 additions & 1 deletion packages/plugin-chess/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 0e9b724

Please sign in to comment.