koishi-plugin-ciyi 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +23 -12
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -42,8 +42,8 @@ var Config = import_koishi.Schema.object({
42
42
  atReply: import_koishi.Schema.boolean().default(true).description("响应时 @"),
43
43
  quoteReply: import_koishi.Schema.boolean().default(false).description("响应时引用"),
44
44
  isEnableMiddleware: import_koishi.Schema.boolean().default(true).description("是否启用中间件,若启用,猜测词语时可以不加前缀"),
45
- maxHistory: import_koishi.Schema.number().default(20).description("最大历史记录数,值为 0 时不限制"),
46
- maxRank: import_koishi.Schema.number().default(10).description("最大排行榜人数,值为 0 时不限制")
45
+ maxHistory: import_koishi.Schema.number().default(20).min(0).description("最大历史记录数"),
46
+ maxRank: import_koishi.Schema.number().default(10).min(0).description("最大排行榜人数")
47
47
  });
48
48
  function apply(ctx, cfg) {
49
49
  ctx.model.extend("ciyi", {
@@ -95,18 +95,28 @@ function apply(ctx, cfg) {
95
95
  });
96
96
  function formatCiyiRanks(ranks) {
97
97
  ranks.sort((a, b) => b.score - a.score);
98
- const topMaxRanks = ranks.slice(0, cfg.maxRank || void 0);
99
- return topMaxRanks.map((rank, index) => {
98
+ const maxRank = cfg.maxRank;
99
+ const needsSlicing = maxRank !== void 0 && maxRank < ranks.length;
100
+ const slicedRanks = needsSlicing ? ranks.slice(0, maxRank) : ranks;
101
+ const formattedString = slicedRanks.map((rank, index) => {
100
102
  return `${index + 1}. ${rank.username} ${rank.score}`;
101
103
  }).join("\n");
104
+ return needsSlicing ? `${formattedString}
105
+ ...` : formattedString;
102
106
  }
103
107
  __name(formatCiyiRanks, "formatCiyiRanks");
104
108
  function formatHistories(histories) {
105
- return histories.sort((a, b) => a.rank - b.rank).slice(0, cfg.maxHistory || void 0).map((history, index) => {
109
+ const sortedHistories = histories.sort((a, b) => a.rank - b.rank);
110
+ const maxHistory = cfg.maxHistory;
111
+ const needsSlicing = maxHistory !== void 0 && maxHistory < sortedHistories.length;
112
+ const slicedHistories = needsSlicing ? sortedHistories.slice(0, maxHistory) : sortedHistories;
113
+ const formattedString = slicedHistories.map((history, index) => {
106
114
  const leftHintChar = history.leftHint.length > 1 ? history.leftHint[1] : "?";
107
115
  const rightHintChar = history.rightHint.length > 0 ? history.rightHint[0] : "?";
108
116
  return `${index + 1}. ?${leftHintChar})${history.guess}(${rightHintChar}? ${history.rank}`;
109
117
  }).join("\n");
118
+ return needsSlicing ? `${formattedString}
119
+ ...` : formattedString;
110
120
  }
111
121
  __name(formatHistories, "formatHistories");
112
122
  function getHistory(targetString, stringArray) {
@@ -150,12 +160,6 @@ ${formattedRanks}`;
150
160
  if (gameInfo[0].guessedHistoryInOneGame.includes(guess)) {
151
161
  return await sendMsg(session, "你已经猜过这个词了!");
152
162
  }
153
- const rankList = gameInfo[0].rankList;
154
- const history = [...gameInfo[0].history, getHistory(guess, rankList)];
155
- await ctx.database.set("ciyi", { channelId: session.channelId }, {
156
- guessedHistoryInOneGame: [...gameInfo[0].guessedHistoryInOneGame, guess],
157
- history
158
- });
159
163
  if (guess === gameInfo[0].answer) {
160
164
  await ctx.database.set("ciyi", { channelId: session.channelId }, {
161
165
  isOver: true,
@@ -163,7 +167,8 @@ ${formattedRanks}`;
163
167
  guessedHistoryInOneGame: []
164
168
  });
165
169
  const msg = `恭喜你猜对了!
166
- 正确答案:${gameInfo[0].answer}`;
170
+ 正确答案:${gameInfo[0].answer}
171
+ 猜测次数:${gameInfo[0].history.length + 1} 次`;
167
172
  const playerInfo = await ctx.database.get("ciyi_rank", { userId: session.userId });
168
173
  if (playerInfo.length === 0) {
169
174
  await ctx.database.create("ciyi_rank", {
@@ -179,6 +184,12 @@ ${formattedRanks}`;
179
184
  }
180
185
  return await sendMsg(session, msg);
181
186
  }
187
+ const rankList = gameInfo[0].rankList;
188
+ const history = [...gameInfo[0].history, getHistory(guess, rankList)];
189
+ await ctx.database.set("ciyi", { channelId: session.channelId }, {
190
+ guessedHistoryInOneGame: [...gameInfo[0].guessedHistoryInOneGame, guess],
191
+ history
192
+ });
182
193
  const historyString = formatHistories(history);
183
194
  return await sendMsg(session, historyString);
184
195
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-ciyi",
3
3
  "description": "Koishi 的词意(猜词游戏)插件。根据词语的含义相似程度,猜测正确的词语。",
4
- "version": "0.0.7",
4
+ "version": "0.0.9",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [