koishi-plugin-maple-points 0.0.1

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.
package/lib/index.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ import { Context, Schema } from 'koishi';
2
+ export declare const name = "maple-points";
3
+ export declare const using: readonly ["database"];
4
+ export interface Config {
5
+ }
6
+ export declare const Config: Schema<Config>;
7
+ interface User {
8
+ id: number;
9
+ name: string;
10
+ points: number;
11
+ }
12
+ interface Item {
13
+ name: string;
14
+ minPrice: number;
15
+ lastBidder?: number;
16
+ currentPrice: number;
17
+ }
18
+ declare module 'koishi' {
19
+ interface Tables {
20
+ 'maple-points-user': User;
21
+ 'maple-points-items': Item;
22
+ }
23
+ }
24
+ export declare function apply(ctx: Context): void;
25
+ export {};
package/lib/index.js ADDED
@@ -0,0 +1,276 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name2 in all)
8
+ __defProp(target, name2, { get: all[name2], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Config: () => Config,
24
+ apply: () => apply,
25
+ name: () => name,
26
+ using: () => using
27
+ });
28
+ module.exports = __toCommonJS(src_exports);
29
+ var import_koishi = require("koishi");
30
+ var name = "maple-points";
31
+ var using = ["database"];
32
+ var Config = import_koishi.Schema.object({});
33
+ function apply(ctx) {
34
+ ctx.model.extend("maple-points-user", {
35
+ id: "unsigned",
36
+ name: "string",
37
+ points: "integer"
38
+ }, {
39
+ primary: "id",
40
+ autoInc: false
41
+ });
42
+ ctx.model.extend("maple-points-items", {
43
+ name: "string",
44
+ minPrice: "integer",
45
+ lastBidder: "unsigned",
46
+ currentPrice: "integer"
47
+ }, {
48
+ primary: "name",
49
+ autoInc: false
50
+ });
51
+ async function initItems() {
52
+ const existingItems = await ctx.database.get("maple-points-items", {});
53
+ if (existingItems.length > 0) return;
54
+ const defaultItems = [
55
+ { name: "耳环", minPrice: 200, currentPrice: 0, lastBidder: null },
56
+ { name: "项链", minPrice: 200, currentPrice: 0, lastBidder: null },
57
+ { name: "手腕", minPrice: 200, currentPrice: 0, lastBidder: null },
58
+ { name: "戒指", minPrice: 200, currentPrice: 0, lastBidder: null },
59
+ { name: "头", minPrice: 400, currentPrice: 0, lastBidder: null },
60
+ { name: "手", minPrice: 400, currentPrice: 0, lastBidder: null },
61
+ { name: "脚", minPrice: 400, currentPrice: 0, lastBidder: null },
62
+ { name: "衣服", minPrice: 800, currentPrice: 0, lastBidder: null },
63
+ { name: "裤子", minPrice: 800, currentPrice: 0, lastBidder: null },
64
+ { name: "自选武器", minPrice: 1e3, currentPrice: 0, lastBidder: null },
65
+ { name: "随机武器", minPrice: 1e3, currentPrice: 0, lastBidder: null }
66
+ ];
67
+ for (const item of defaultItems) {
68
+ await ctx.database.create("maple-points-items", item);
69
+ }
70
+ }
71
+ __name(initItems, "initItems");
72
+ ctx.on("ready", () => {
73
+ initItems().catch(console.error);
74
+ });
75
+ const root = ctx.command("积分");
76
+ ctx.command("积分/重置积分 <points:number>").usage("重置积分 <积分数量> - 将群内所有人的积分设置为指定值").example("重置积分 1000").action(async ({ session }, points) => {
77
+ if (!points) return "格式错误!请使用:重置积分 <积分数量>";
78
+ const memberList = await session.bot.getGuildMemberList(session.guildId);
79
+ const members = Object.values(memberList);
80
+ const botId = session.bot.selfId;
81
+ const existingUsers = await ctx.database.get("maple-points-user", {});
82
+ const existingUserIds = new Set(existingUsers.map((user) => user.id));
83
+ let addedCount = 0;
84
+ let updatedCount = 0;
85
+ for (const member of members) {
86
+ if (member.userId === botId) continue;
87
+ const userId = parseInt(member.userId);
88
+ const userName = member.nickname || member.username;
89
+ if (!existingUserIds.has(userId)) {
90
+ await ctx.database.create("maple-points-user", {
91
+ id: userId,
92
+ name: userName,
93
+ points
94
+ });
95
+ addedCount++;
96
+ } else {
97
+ await ctx.database.set("maple-points-user", { id: userId }, {
98
+ name: userName,
99
+ // 也更新名字(可能改名了)
100
+ points
101
+ });
102
+ updatedCount++;
103
+ }
104
+ }
105
+ let result = `已将群内所有人的积分重置为 ${points}`;
106
+ if (addedCount > 0) {
107
+ result += `
108
+ 新增了 ${addedCount} 名用户到用户表`;
109
+ }
110
+ if (updatedCount > 0) {
111
+ result += `
112
+ 更新了 ${updatedCount} 名用户的积分`;
113
+ }
114
+ return result;
115
+ });
116
+ ctx.command("积分/查询积分").usage("查询群内所有人的积分排行").example("查询积分").action(async ({ session }) => {
117
+ const users = await ctx.database.get("maple-points-user", {});
118
+ const sortedUsers = users.sort((a, b) => b.points - a.points);
119
+ let output = "【用户积分表】\n";
120
+ sortedUsers.forEach((user, index) => {
121
+ output += `${index + 1}. ${user.name} 积分:${user.points}
122
+ `;
123
+ });
124
+ const currentUser = await ctx.database.get("maple-points-user", {
125
+ id: parseInt(session.userId)
126
+ });
127
+ const userPoints = currentUser[0]?.points || 0;
128
+ output += "──────────\n";
129
+ output += `你的积分:${userPoints}`;
130
+ return output;
131
+ });
132
+ ctx.command("积分/分配积分").usage("将装备总价值平均分配给所有用户").example("分配积分").action(async ({ session }) => {
133
+ const items = await ctx.database.get("maple-points-items", {});
134
+ const totalValue = items.reduce((sum, item) => sum + item.currentPrice, 0);
135
+ const users = await ctx.database.get("maple-points-user", {});
136
+ const perUser = Math.floor(totalValue / users.length);
137
+ for (const user of users) {
138
+ await ctx.database.set("maple-points-user", { id: user.id }, {
139
+ points: user.points + perUser
140
+ });
141
+ }
142
+ for (const item of items) {
143
+ await ctx.database.set("maple-points-items", { name: item.name }, {
144
+ lastBidder: null,
145
+ currentPrice: 0
146
+ });
147
+ }
148
+ return `已将 ${totalValue} 积分平均分配给 ${users.length} 名用户,每人获得 ${perUser} 积分`;
149
+ });
150
+ ctx.command("积分/查询装备").usage("查看所有装备需求情况").example("查询装备").action(async ({ session }) => {
151
+ const items = await ctx.database.get("maple-points-items", {});
152
+ const sortedItems = items.sort((a, b) => b.currentPrice - a.currentPrice);
153
+ let output = "【装备需求表】\n";
154
+ for (const item of sortedItems) {
155
+ const bidder = item.lastBidder ? await getUserName(item.lastBidder) : "无";
156
+ const priceInfo = item.currentPrice > 0 ? item.currentPrice : `底价:${item.minPrice}`;
157
+ output += `${item.name} ${bidder} ${priceInfo}
158
+ `;
159
+ }
160
+ output += "──────────\n";
161
+ output += "你当前需求的装备:\n";
162
+ const userItems = sortedItems.filter(
163
+ (item) => item.lastBidder === parseInt(session.userId)
164
+ );
165
+ if (userItems.length === 0) {
166
+ output += "无";
167
+ } else {
168
+ userItems.forEach((item) => {
169
+ output += `${item.name} ${item.currentPrice}
170
+ `;
171
+ });
172
+ }
173
+ return output;
174
+ async function getUserName(userId) {
175
+ const user = await ctx.database.get("maple-points-user", { id: userId });
176
+ return user[0]?.name || "未知用户";
177
+ }
178
+ __name(getUserName, "getUserName");
179
+ });
180
+ ctx.command("积分/需求 <itemName:string> [price:number]").usage("需求 <装备名> [价格] - 对指定装备出价").example("需求 戒指").example("需求 戒指 300").action(async ({ session }, itemName, price) => {
181
+ if (!itemName) return "格式错误!请使用:需求 <装备名> [价格]";
182
+ const items = await ctx.database.get("maple-points-items", { name: itemName });
183
+ if (items.length === 0) {
184
+ return `没有找到名为【${itemName}】的装备!请输入【查询装备】查看可需求的装备`;
185
+ }
186
+ const item = items[0];
187
+ const user = (await ctx.database.get("maple-points-user", {
188
+ id: parseInt(session.userId)
189
+ }))[0];
190
+ if (price === void 0) {
191
+ if (!item.lastBidder) {
192
+ price = item.minPrice;
193
+ } else {
194
+ const lastBidder = await ctx.database.get("maple-points-user", {
195
+ id: item.lastBidder
196
+ });
197
+ return `当前【${itemName}】的出价人为:${lastBidder[0]?.name},价格为:${item.currentPrice},您的积分为:${user.points}。是否加100积分进行需求?是/否`;
198
+ }
199
+ }
200
+ if (price > user.points) {
201
+ return `您没有那么多积分!当前【${itemName}】的价格为:${item.currentPrice || item.minPrice},您的积分为:${user.points}`;
202
+ }
203
+ if (session.content === "是" && item.lastBidder) {
204
+ price = item.currentPrice + 100;
205
+ if (price > user.points) {
206
+ return `您没有那么多积分!当前【${itemName}】的价格为:${item.currentPrice},您的积分为:${user.points}`;
207
+ }
208
+ } else if (session.content === "否" && item.lastBidder) {
209
+ return "需求失败!";
210
+ }
211
+ if (item.lastBidder) {
212
+ const lastBidder = (await ctx.database.get("maple-points-user", {
213
+ id: item.lastBidder
214
+ }))[0];
215
+ await ctx.database.set("maple-points-user", { id: item.lastBidder }, {
216
+ points: lastBidder.points + item.currentPrice
217
+ });
218
+ }
219
+ await ctx.database.set("maple-points-user", { id: parseInt(session.userId) }, {
220
+ points: user.points - price
221
+ });
222
+ await ctx.database.set("maple-points-items", { name: itemName }, {
223
+ lastBidder: parseInt(session.userId),
224
+ currentPrice: price
225
+ });
226
+ return `需求【${itemName}】成功!扣除${price}积分,当前积分:${user.points - price}`;
227
+ });
228
+ ctx.command("积分/分配装备 <itemName:string> <userName:string> <price:number>").usage("分配装备 <装备名> <用户名> <价格> - 手动分配装备").example("分配装备 武器 张三 1000").action(async ({ session }, itemName, userName, price) => {
229
+ if (!itemName || !userName || !price) {
230
+ return "格式错误!请使用:分配装备 <装备名> <用户名> <价格>";
231
+ }
232
+ const items = await ctx.database.get("maple-points-items", { name: itemName });
233
+ if (items.length === 0) return `装备【${itemName}】不存在!`;
234
+ if (items[0].lastBidder) return `分配失败!【${itemName}】已有人需求!`;
235
+ const users = await ctx.database.get("maple-points-user", { name: userName });
236
+ if (users.length === 0) {
237
+ return `没有找到名为【${userName}】的人!请输入【查询积分】查看用户名`;
238
+ }
239
+ const user = users[0];
240
+ if (price > user.points) {
241
+ return `【${userName}】积分不足!需要${price}积分,当前只有${user.points}积分`;
242
+ }
243
+ await ctx.database.set("maple-points-user", { id: user.id }, {
244
+ points: user.points - price
245
+ });
246
+ await ctx.database.set("maple-points-items", { name: itemName }, {
247
+ lastBidder: user.id,
248
+ currentPrice: price
249
+ });
250
+ return `分配【${itemName}】成功!扣除【${userName}】${price}积分,当前积分:${user.points - price}`;
251
+ });
252
+ ctx.command("积分/增加积分 <userName:string> <points:number>").usage("增加积分 <用户名> <积分> - 增加或扣除用户积分").example("增加积分 张三 100").example("增加积分 李四 -50").action(async ({ session }, userName, points) => {
253
+ if (!userName || points === void 0) {
254
+ return "格式错误!请使用:增加积分 <用户名> <积分>";
255
+ }
256
+ const users = await ctx.database.get("maple-points-user", { name: userName });
257
+ if (users.length === 0) {
258
+ return `没有找到名为【${userName}】的人!请输入【查询积分】查看用户名`;
259
+ }
260
+ const user = users[0];
261
+ const newPoints = user.points + points;
262
+ await ctx.database.set("maple-points-user", { id: user.id }, {
263
+ points: newPoints
264
+ });
265
+ const action = points >= 0 ? "增加" : "扣除";
266
+ return `增加积分成功!【${userName}】${action}${Math.abs(points)}积分,当前积分:${newPoints}`;
267
+ });
268
+ }
269
+ __name(apply, "apply");
270
+ // Annotate the CommonJS export names for ESM import in node:
271
+ 0 && (module.exports = {
272
+ Config,
273
+ apply,
274
+ name,
275
+ using
276
+ });
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "koishi-plugin-maple-points",
3
+ "description": "-",
4
+ "version": "0.0.1",
5
+ "main": "lib/index.js",
6
+ "typings": "lib/index.d.ts",
7
+ "files": [
8
+ "lib",
9
+ "dist"
10
+ ],
11
+ "license": "MIT",
12
+ "keywords": [
13
+ "maple"
14
+ ],
15
+ "peerDependencies": {
16
+ "koishi": "4.18.10"
17
+ }
18
+ }
package/readme.md ADDED
@@ -0,0 +1,5 @@
1
+ # koishi-plugin-maple-points
2
+
3
+ [![npm](https://img.shields.io/npm/v/koishi-plugin-maple-points?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-maple-points)
4
+
5
+ -