Skip to content

Commit

Permalink
fix(adventure): use item callback with state
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jul 5, 2021
1 parent f0d9a61 commit 26621aa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/plugin-adventure/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ namespace Event {

export const lose = <T>(items: Adventurer.Infer<Item.Pack, T>, reason = '$s 失去了$i!'): Visible<T> => (session, state) => {
const itemMap = toItemMap(getValue(items, session.user, state))
const output = [reason.replace('$i', () => {
return Item.format(Array.isArray(items) ? items : itemMap)
})]
const output = reason
? [reason.replace('$i', () => Item.format(Array.isArray(items) ? items : itemMap))]
: []
for (const name in itemMap) {
const result = Item.lose(session, name)
if (result) output.push(result)
Expand Down
7 changes: 4 additions & 3 deletions packages/plugin-adventure/src/phase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace Phase {

export const userSessionMap: Record<string, [Adventurer.Session, NodeJS.Timer]> = {}
export const channelUserMap: Record<string, [string, NodeJS.Timer]> = {}
export const activeUsers = new Set<string>()
export const activeUsers = new Map<string, any>()

export function getBadEndingCount(user: Pick<User, 'endings'>) {
return Object.keys(user.endings).filter(id => badEndings.has(id)).length
Expand Down Expand Up @@ -365,7 +365,7 @@ namespace Phase {
await dispatch(session, phase.events, state)

// resolve next phase
activeUsers.add(user.id)
activeUsers.set(user.id, state)
const action = typeof next === 'function' && next
|| choices && choose(choices, options)
|| items && useItem(items)
Expand Down Expand Up @@ -583,7 +583,8 @@ namespace Phase {
return `你暂未持有物品“${item}”。`
}

const progress = getValue(itemMap[item], user)
const state = activeUsers.get(user.id)
const progress = getValue(itemMap[item], user, state)
if (progress) {
logger.debug('%s use %c', session.user.id, item)
if (activeUsers.has(session.user.id)) {
Expand Down

0 comments on commit 26621aa

Please sign in to comment.