Skip to content

Commit

Permalink
fix(adventure): fix repeated counting ending rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 16, 2021
1 parent 66cae53 commit dfd683c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-adventure/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-adventure",
"version": "0.2.0",
"description": "Adventure Game for Koishi",
"description": "Adventure game framework for Koishi.js",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-adventure/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ export function apply(ctx: Context, config?: Config) {
const endingReward = [300, 200, 100]
ctx.on('adventure/ending', ({ app, user, username }, id) => {
if (user.flag & User.Flag.noLeading) return
const count = Phase.endingCount[id], reward = endingReward[count]
if (reward) {
const set = Phase.endingCount[id]
const count = set.size, reward = endingReward[count]
if (reward && set.add(user.id).size > count) {
app.broadcast(`恭喜 ${username} 达成了结局「${Phase.endingMap[id]}」的全服${leadingOrder[count]},将获得 ${reward}¥ 的奖励!`).catch()
user.money += reward
user.wealth += reward
}
Phase.endingCount[id] += 1
})
}
19 changes: 8 additions & 11 deletions packages/plugin-adventure/src/phase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export namespace Phase {
}

export const endingMap: Record<string, string> = {}
export const endingCount: Record<string, number> = {}
export const endingCount: Record<string, Set<string>> = {}
export const reversedEndingMap: Record<string, string> = {}
/** 键:prefix,值:[剧情线名,结局数] */
export const lines: Record<string, [string, number]> = {}
Expand All @@ -114,7 +114,7 @@ export namespace Phase {
for (const id in map) {
const name = `${prefix}-${id}`
endingMap[name] = map[id]
endingCount[name] = 0
endingCount[name] = new Set()
Show.redirect(map[id], 'ending', checkEnding)
reversedEndingMap[map[id]] = name
if (bad.includes(id)) {
Expand Down Expand Up @@ -565,15 +565,12 @@ export namespace Phase {
})

ctx.on('connect', async () => {
const endings = Object.keys(endingCount)
if (!endings.length) return
let sql = 'SELECT'
for (const id of endings) {
sql += ` find_ending('${id}') AS '${id}',`
}
const [data] = await ctx.database.mysql.query<[Record<string, number>]>(sql.slice(0, -1))
for (const key in data) {
endingCount[key] = data[key]
if (!Object.keys(endingCount).length) return
const data = await ctx.database.mysql.query<Pick<User, 'id' | 'endings'>[]>('select id, endings from user where json_length(endings)')
for (const { id, endings } of data) {
for (const name in endings) {
endingCount[name]?.add(id)
}
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-assets/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koishi-plugin-assets",
"description": "Asset provider plugin for Koishi",
"description": "A classic assets provider for Koishi.js",
"version": "1.1.0",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-chat/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koishi-plugin-chat",
"description": "Chat plugin for Koishi",
"description": "Display and respond to messages for Koishi.js",
"version": "0.5.0",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down

0 comments on commit dfd683c

Please sign in to comment.