Skip to content

Commit

Permalink
fix(chess): fix skip behavior, fix #169
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 21, 2021
1 parent 16c7059 commit e814fc9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
2 changes: 0 additions & 2 deletions packages/plugin-chess/src/go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,4 @@ export function update(this: State, x: number, y: number, value: -1 | 1) {
this.wBoard = wBoard
return '全局同形'
}

this.save()
}
33 changes: 20 additions & 13 deletions packages/plugin-chess/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function apply(ctx: Context) {
}
}

if (ctx.app.browser) {
if (!ctx.app.browser) {
chess.removeOption('imageMode')
chess.removeOption('textMode')
}
Expand Down Expand Up @@ -108,6 +108,7 @@ export function apply(ctx: Context) {
}
state.update = rule.update
states[cid] = state
state.save()

return state.draw(session, `${session.username} 发起了游戏!`)
}
Expand Down Expand Up @@ -137,14 +138,15 @@ export function apply(ctx: Context) {
}

if (options.skip) {
if (!state.next) return '尚未有人行棋。'
if (state.next !== userId) return '当前不是你的回合。'
state.next = state.p1 === userId ? state.p2 : state.p1
channel.chess = state.serial()
return `${session.username} 选择跳过其回合,下一手轮到 ${segment.at(state.next)}。`
}

if (options.repent) {
if (!state.p2) return '尚未有人行棋。'
if (!state.next) return '尚未有人行棋。'
const last = state.p1 === state.next ? state.p2 : state.p1
if (last !== userId) return '上一手棋不是你所下。'
state.history.pop()
Expand Down Expand Up @@ -178,14 +180,19 @@ export function apply(ctx: Context) {
if (state.get(x, y)) return '此处已有落子。'

let message = ''
if (!state.p2 && userId !== state.p1) {
state.p2 = userId
message = `${session.username} 加入了游戏并落子于 ${position.toUpperCase()},`
} else {
if (state.next || userId === state.p1) {
message = `${session.username} 落子于 ${position.toUpperCase()},`
} else {
if (state.history.length === 1) {
state.p2 = state.p1
state.p1 = userId
} else {
state.p2 = userId
}
message = `${session.username} 加入了游戏并落子于 ${position.toUpperCase()},`
}

const value = state.history.length % 2 ? -1 : 1
const value = userId === state.p1 ? 1 : -1
const result = state.update(x, y, value)

switch (result) {
Expand All @@ -196,31 +203,31 @@ export function apply(ctx: Context) {
message += `下一手依然轮到 ${segment.at(userId)}。`
break
case MoveResult.p1Win:
message += `恭喜 ${segment.at(state.p1)} 获胜!`
delete states[cid]
channel.chess = null
break
return message + `恭喜 ${segment.at(state.p1)} 获胜!`
case MoveResult.p2Win:
message += `恭喜 ${segment.at(state.p2)} 获胜!`
delete states[cid]
channel.chess = null
break
return message + `恭喜 ${segment.at(state.p2)} 获胜!`
case MoveResult.draw:
message += '本局游戏平局。'
delete states[cid]
channel.chess = null
break
return message + '本局游戏平局。'
case undefined:
// eslint-disable-next-line no-cond-assign
if (state.next = userId === state.p1 ? state.p2 : state.p1) {
message += `下一手轮到 ${segment.at(state.next)}。`
} else {
message = message.slice(0, -1) + '。'
}
break
default:
state.next = userId
return `非法落子(${result})。`
}

state.save()
channel.chess = state.serial()
return state.draw(session, message, x, y)
})
Expand Down
5 changes: 2 additions & 3 deletions packages/plugin-chess/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const numbers = '①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳'
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

export enum MoveResult {
p1Win = -1,
p2Win = 1,
p1Win = 1,
p2Win = -1,
draw = -2,
skip = 2,
illegal = 3,
Expand Down Expand Up @@ -192,7 +192,6 @@ export class State {
this.wBoard &= ~chess
this.bBoard &= ~chess
}
this.save()
return board
}

Expand Down

0 comments on commit e814fc9

Please sign in to comment.