From dfd683c5c9a488157010c8c20f26e7b792134431 Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Sun, 16 May 2021 15:39:17 +0800 Subject: [PATCH] fix(adventure): fix repeated counting ending rewards --- packages/plugin-adventure/package.json | 2 +- packages/plugin-adventure/src/index.ts | 6 +++--- packages/plugin-adventure/src/phase.ts | 19 ++++++++----------- packages/plugin-assets/package.json | 2 +- packages/plugin-chat/package.json | 2 +- 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/packages/plugin-adventure/package.json b/packages/plugin-adventure/package.json index 7aa3ae6d4f..b9e84fbdf2 100644 --- a/packages/plugin-adventure/package.json +++ b/packages/plugin-adventure/package.json @@ -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": [ diff --git a/packages/plugin-adventure/src/index.ts b/packages/plugin-adventure/src/index.ts index 655b4148c3..91abdbd87e 100644 --- a/packages/plugin-adventure/src/index.ts +++ b/packages/plugin-adventure/src/index.ts @@ -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 }) } diff --git a/packages/plugin-adventure/src/phase.ts b/packages/plugin-adventure/src/phase.ts index 062219e080..61ded06db6 100644 --- a/packages/plugin-adventure/src/phase.ts +++ b/packages/plugin-adventure/src/phase.ts @@ -89,7 +89,7 @@ export namespace Phase { } export const endingMap: Record = {} - export const endingCount: Record = {} + export const endingCount: Record> = {} export const reversedEndingMap: Record = {} /** 键:prefix,值:[剧情线名,结局数] */ export const lines: Record = {} @@ -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)) { @@ -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]>(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[]>('select id, endings from user where json_length(endings)') + for (const { id, endings } of data) { + for (const name in endings) { + endingCount[name]?.add(id) + } } }) } diff --git a/packages/plugin-assets/package.json b/packages/plugin-assets/package.json index 383bb7f703..2a1e3ad697 100644 --- a/packages/plugin-assets/package.json +++ b/packages/plugin-assets/package.json @@ -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", diff --git a/packages/plugin-chat/package.json b/packages/plugin-chat/package.json index c8c9100d43..db377bfb14 100644 --- a/packages/plugin-chat/package.json +++ b/packages/plugin-chat/package.json @@ -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",