@hieuzest/koishi-plugin-mahjongpub 0.1.9 → 0.1.10

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/api.d.ts CHANGED
@@ -23,7 +23,7 @@ export declare class TeamAdmin implements Team {
23
23
  constructor(ctx: Context, pw: string, options: {
24
24
  endpoint: string;
25
25
  });
26
- read(): Promise<void>;
26
+ read(force?: boolean): Promise<void>;
27
27
  write(): Promise<any>;
28
28
  }
29
29
  export declare class ContestAdmin {
@@ -38,5 +38,5 @@ export declare class ContestAdmin {
38
38
  constructor(ctx: Context, cid: string, pw: string, options: {
39
39
  endpoint: string;
40
40
  });
41
- read(): Promise<void>;
41
+ read(force?: boolean): Promise<void>;
42
42
  }
package/lib/api.js CHANGED
@@ -19,7 +19,9 @@ class TeamAdmin {
19
19
  endpoint: options.endpoint,
20
20
  });
21
21
  }
22
- async read() {
22
+ async read(force = false) {
23
+ if (this.tid && !force)
24
+ return;
23
25
  const data = await this.http.get('/api/data.php', {
24
26
  responseType: 'json',
25
27
  params: {
@@ -68,7 +70,9 @@ class ContestAdmin {
68
70
  endpoint: options.endpoint,
69
71
  });
70
72
  }
71
- async read() {
73
+ async read(force = false) {
74
+ if (this.teams && !force)
75
+ return;
72
76
  const payload = {
73
77
  posttype: 'login',
74
78
  cid: this.cid,
package/lib/index.d.ts CHANGED
@@ -11,12 +11,20 @@ declare module 'koishi' {
11
11
  'mahjongpub/bind-contestpw': string;
12
12
  }
13
13
  }
14
+ declare module '@koishijs/cache' {
15
+ interface Tables {
16
+ contests: ContestAdmin;
17
+ teams: TeamAdmin;
18
+ }
19
+ }
14
20
  export declare class MahjongPub {
15
21
  private ctx;
16
22
  private config;
23
+ teams: Dict<TeamAdmin>;
24
+ contests: Dict<ContestAdmin>;
17
25
  constructor(ctx: Context, config: MahjongPub.Config);
18
- getTeam(pw: string): TeamAdmin;
19
- getContest(cid: string, pw: string): ContestAdmin;
26
+ getTeam(pw: string): Promise<TeamAdmin>;
27
+ getContest(cid: string, pw: string): Promise<ContestAdmin>;
20
28
  }
21
29
  export declare namespace MahjongPub {
22
30
  const inject: {
package/lib/index.js CHANGED
@@ -12,6 +12,8 @@ function parsePlatform(target) {
12
12
  class MahjongPub {
13
13
  ctx;
14
14
  config;
15
+ teams = {};
16
+ contests = {};
15
17
  constructor(ctx, config) {
16
18
  this.ctx = ctx;
17
19
  this.config = config;
@@ -34,7 +36,7 @@ class MahjongPub {
34
36
  .action(async ({ session, options }, pw) => {
35
37
  if (pw?.length !== 20)
36
38
  return;
37
- const team = new api_1.TeamAdmin(ctx, pw, config);
39
+ const team = await this.getTeam(pw);
38
40
  try {
39
41
  await team.read();
40
42
  }
@@ -93,13 +95,13 @@ class MahjongPub {
93
95
  const [cid, cpw] = [session.channel['mahjongpub/bind-contest'], session.channel['mahjongpub/bind-contestpw']];
94
96
  if (!cid || !cpw)
95
97
  return session.text('.notbind');
96
- const contest = new api_1.ContestAdmin(ctx, cid, cpw, config);
98
+ const contest = await this.getContest(cid, cpw);
97
99
  await contest.read();
98
100
  pw = (contest.teams[options.team] ?? Object.values(contest.teams).find(t => t.name === options.team))?.pw;
99
101
  }
100
102
  if (!pw)
101
103
  return config.informNotbind ? session.text('.notbind') : '';
102
- const team = new api_1.TeamAdmin(ctx, pw, config);
104
+ const team = await this.getTeam(pw);
103
105
  try {
104
106
  await team.read();
105
107
  return session.text('.output', team);
@@ -119,7 +121,7 @@ class MahjongPub {
119
121
  : session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'] ?? session.user['mahjongpub/bind-team'];
120
122
  if (!pw)
121
123
  return config.informNotbind ? session.text('.notbind') : '';
122
- const team = new api_1.TeamAdmin(ctx, pw, config);
124
+ const team = await this.getTeam(pw);
123
125
  try {
124
126
  await team.read();
125
127
  if (!desc)
@@ -142,7 +144,7 @@ class MahjongPub {
142
144
  : session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'] ?? session.user['mahjongpub/bind-team'];
143
145
  if (!pw)
144
146
  return config.informNotbind ? session.text('.notbind') : '';
145
- const team = new api_1.TeamAdmin(ctx, pw, config);
147
+ const team = await this.getTeam(pw);
146
148
  try {
147
149
  await team.read();
148
150
  if (!img)
@@ -159,19 +161,29 @@ class MahjongPub {
159
161
  ctx.command('mahjongpub.team.add <...users:string>')
160
162
  .alias('!添加', '!添加')
161
163
  .option('channel', '-c <channel:channel>')
162
- .userFields(['mahjongpub/bind-team'])
163
- .channelFields(['mahjongpub/bind-team'])
164
+ .option('team', '-t <team:string>')
165
+ .userFields(['mahjongpub/bind-team', 'mahjongpub/bind-teams'])
166
+ .channelFields(['mahjongpub/bind-team', 'mahjongpub/bind-contest', 'mahjongpub/bind-contestpw'])
164
167
  .action(async ({ session, options }, ...users) => {
165
- const pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
168
+ let pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
166
169
  : session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'] ?? session.user['mahjongpub/bind-team'];
170
+ if (options.team) {
171
+ const [cid, cpw] = [session.channel['mahjongpub/bind-contest'], session.channel['mahjongpub/bind-contestpw']];
172
+ if (!cid || !cpw)
173
+ return session.text('.notbind');
174
+ const contest = await this.getContest(cid, cpw);
175
+ await contest.read();
176
+ pw = (contest.teams[options.team] ?? Object.values(contest.teams).find(t => t.name === options.team))?.pw;
177
+ }
167
178
  if (!pw)
168
179
  return config.informNotbind ? session.text('.notbind') : '';
169
- const team = new api_1.TeamAdmin(ctx, pw, config);
180
+ const team = await this.getTeam(pw);
170
181
  try {
171
182
  await team.read();
172
183
  team.players.push(...users);
173
- await team.write();
174
- return await team.write() ?? session.text('.success', team);
184
+ const msg = await team.write();
185
+ await team.read(true);
186
+ return msg + '\n' + session.text('.success', team);
175
187
  }
176
188
  catch (e) {
177
189
  ctx.logger.warn(e);
@@ -181,19 +193,30 @@ class MahjongPub {
181
193
  ctx.command('mahjongpub.team.remove <...indices:natural>')
182
194
  .alias('!删除', '!删除')
183
195
  .option('channel', '-c <channel:channel>')
184
- .userFields(['mahjongpub/bind-team'])
185
- .channelFields(['mahjongpub/bind-team'])
196
+ .option('team', '-t <team:string>')
197
+ .userFields(['mahjongpub/bind-team', 'mahjongpub/bind-teams'])
198
+ .channelFields(['mahjongpub/bind-team', 'mahjongpub/bind-contest', 'mahjongpub/bind-contestpw'])
186
199
  .action(async ({ session, options }, ...indices) => {
187
- const pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
200
+ let pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
188
201
  : session.isDirect ? session.user['mahjongpub/bind-team'] : session.channel['mahjongpub/bind-team'] ?? session.user['mahjongpub/bind-team'];
202
+ if (options.team) {
203
+ const [cid, cpw] = [session.channel['mahjongpub/bind-contest'], session.channel['mahjongpub/bind-contestpw']];
204
+ if (!cid || !cpw)
205
+ return session.text('.notbind');
206
+ const contest = await this.getContest(cid, cpw);
207
+ await contest.read();
208
+ pw = (contest.teams[options.team] ?? Object.values(contest.teams).find(t => t.name === options.team))?.pw;
209
+ }
189
210
  if (!pw)
190
211
  return config.informNotbind ? session.text('.notbind') : '';
191
- const team = new api_1.TeamAdmin(ctx, pw, config);
212
+ const team = await this.getTeam(pw);
192
213
  try {
193
214
  await team.read();
194
215
  team.players = team.players.filter((_, i) => !indices.includes(i + 1));
195
216
  await team.write();
196
- return await team.write() ?? session.text('.success', team);
217
+ const msg = await team.write();
218
+ await team.read(true);
219
+ return msg + '\n' + session.text('.success', team);
197
220
  }
198
221
  catch (e) {
199
222
  ctx.logger.warn(e);
@@ -204,7 +227,7 @@ class MahjongPub {
204
227
  .alias('!交换', '!交换')
205
228
  .option('channel', '-c <channel:channel>')
206
229
  .option('team', '-t <team:string>')
207
- .userFields(['mahjongpub/bind-team'])
230
+ .userFields(['mahjongpub/bind-team', 'mahjongpub/bind-teams'])
208
231
  .channelFields(['mahjongpub/bind-team', 'mahjongpub/bind-contest', 'mahjongpub/bind-contestpw'])
209
232
  .action(async ({ session, options }, ...indices) => {
210
233
  let pw = options.channel ? (await session.getChannel(parsePlatform(options.channel)[1], ['mahjongpub/bind-team']))['mahjongpub/bind-team']
@@ -213,7 +236,7 @@ class MahjongPub {
213
236
  const [cid, cpw] = [session.channel['mahjongpub/bind-contest'], session.channel['mahjongpub/bind-contestpw']];
214
237
  if (!cid || !cpw)
215
238
  return session.text('.notbind');
216
- const contest = new api_1.ContestAdmin(ctx, cid, cpw, config);
239
+ const contest = await this.getContest(cid, cpw);
217
240
  await contest.read();
218
241
  pw = (contest.teams[options.team] ?? Object.values(contest.teams).find(t => t.name === options.team))?.pw;
219
242
  }
@@ -221,7 +244,7 @@ class MahjongPub {
221
244
  return config.informNotbind ? session.text('.notbind') : '';
222
245
  if (!indices.length || indices.length % 2 !== 0)
223
246
  return session.text('.invalid');
224
- const team = new api_1.TeamAdmin(ctx, pw, config);
247
+ const team = await this.getTeam(pw);
225
248
  try {
226
249
  await team.read();
227
250
  for (let i = 0; i < indices.length; i += 2) {
@@ -230,7 +253,7 @@ class MahjongPub {
230
253
  team.players[indices[i + 1] - 1] = t;
231
254
  }
232
255
  const msg = await team.write();
233
- await team.read();
256
+ await team.read(true);
234
257
  return msg + '\n' + session.text('.success', team);
235
258
  }
236
259
  catch (e) {
@@ -238,12 +261,12 @@ class MahjongPub {
238
261
  return session.text('.failed');
239
262
  }
240
263
  });
241
- ctx.command('mahjongpub.contest.bind <cid:string> <pw:string>')
264
+ ctx.command('mahjongpub.contest.bind <cid:string> <cpw:string>')
242
265
  // .alias('!绑定', '!绑定')
243
266
  .option('channel', '-c <channel:channel>')
244
267
  .channelFields(['mahjongpub/bind-contest', 'mahjongpub/bind-contestpw'])
245
- .action(async ({ session, options }, cid, pw) => {
246
- const contest = new api_1.ContestAdmin(ctx, cid, pw, config);
268
+ .action(async ({ session, options }, cid, cpw) => {
269
+ const contest = await this.getContest(cid, cpw);
247
270
  try {
248
271
  await contest.read();
249
272
  }
@@ -251,19 +274,39 @@ class MahjongPub {
251
274
  ctx.logger.warn(e);
252
275
  return session.text('.failed');
253
276
  }
254
- if (options.channel)
255
- ctx.database.setChannel(...parsePlatform(options.channel), { 'mahjongpub/bind-contest': cid, 'mahjongpub/bind-contestpw': pw });
277
+ if (options.channel) {
278
+ ctx.database.setChannel(...parsePlatform(options.channel), {
279
+ 'mahjongpub/bind-contest': cid,
280
+ 'mahjongpub/bind-contestpw': cpw,
281
+ });
282
+ }
256
283
  else {
257
284
  session.channel['mahjongpub/bind-contest'] = cid;
258
- session.channel['mahjongpub/bind-contestpw'] = pw;
285
+ session.channel['mahjongpub/bind-contestpw'] = cpw;
259
286
  }
260
287
  return session.text('.success', contest);
261
288
  });
262
289
  }
263
- getTeam(pw) {
290
+ async getTeam(pw) {
291
+ // if (this.ctx.get('cache')) {
292
+ // let res = await this.ctx.cache.get('teams', pw)
293
+ // if (res) return res
294
+ // res = new TeamAdmin(this.ctx, pw, this.config)
295
+ // await this.ctx.cache.set('teams', pw, res, 1000 * 60)
296
+ // return res
297
+ // }
264
298
  return new api_1.TeamAdmin(this.ctx, pw, this.config);
265
299
  }
266
- getContest(cid, pw) {
300
+ async getContest(cid, pw) {
301
+ if (this.ctx.get('cache')) {
302
+ let res = await this.ctx.cache.get('contests', cid);
303
+ if (res)
304
+ return res;
305
+ res = new api_1.ContestAdmin(this.ctx, cid, pw, this.config);
306
+ await res.read();
307
+ await this.ctx.cache.set('contests', cid, res, 1000 * 600);
308
+ return res;
309
+ }
267
310
  return new api_1.ContestAdmin(this.ctx, cid, pw, this.config);
268
311
  }
269
312
  }
@@ -271,7 +314,7 @@ exports.MahjongPub = MahjongPub;
271
314
  (function (MahjongPub) {
272
315
  MahjongPub.inject = {
273
316
  required: ['database'],
274
- optional: ['assets'],
317
+ optional: ['assets', 'cache'],
275
318
  };
276
319
  MahjongPub.Config = koishi_1.Schema.object({
277
320
  informNotbind: koishi_1.Schema.boolean().default(false),
@@ -1 +1 @@
1
- {"commands":{"mahjongpub.team.bind":{"description":"绑定队伍","messages":{"failed":"绑定失败","success":"成功: [{cid}:{tid}] {name}"}},"mahjongpub.team.unbind":{"description":"解除绑定","messages":{"notbind":"未绑定","success":"成功"}},"mahjongpub.team.password":{"description":"查看密码","messages":{"notbind":"未绑定","output":"密码: {0}"}},"mahjongpub.team.stats":{"description":"查看队伍信息","messages":{"notbind":"未绑定","failed":"失败","output":"- [{cid}:{tid}] {name}\n{players.map((p, i) => '' + (i+1) + ': ' + p).join('\\n')}\n"}},"mahjongpub.team.desc":{"description":"队伍简介","messages":{"notbind":"未绑定","failed":"失败","output":"- [{cid}:{tid}] {name}\n{description}\n"}},"mahjongpub.team.logo":{"description":"队伍头像","messages":{"notbind":"未绑定","failed":"失败"}},"mahjongpub.team.add":{"description":"添加队员","messages":{"notbind":"未绑定","failed":"失败","success":"成功"}},"mahjongpub.team.remove":{"description":"删除队员","messages":{"notbind":"未绑定","failed":"失败","success":"成功"}},"mahjongpub.team.swap":{"description":"交换队员","messages":{"notbind":"未绑定","failed":"失败","invalid":"参数错误","success":"- [{cid}:{tid}] {name}\n{players.map((p, i) => '' + (i+1) + ': ' + p).join('\\n')}\n"}},"mahjongpub.contest.bind":{"description":"绑定比赛","messages":{"failed":"绑定失败","success":"成功: [{cid}] {name}"}}}}
1
+ {"commands":{"mahjongpub.team.bind":{"description":"绑定队伍","messages":{"failed":"绑定失败","success":"成功: [{cid}:{tid}] {name}"}},"mahjongpub.team.unbind":{"description":"解除绑定","messages":{"notbind":"未绑定","success":"成功"}},"mahjongpub.team.password":{"description":"查看密码","messages":{"notbind":"未绑定","output":"密码: {0}"}},"mahjongpub.team.stats":{"description":"查看队伍信息","messages":{"notbind":"未绑定","failed":"失败","output":"- [{cid}:{tid}] {name}\n{players.map((p, i) => '' + (i+1) + ': ' + p).join('\\n')}\n"}},"mahjongpub.team.desc":{"description":"队伍简介","messages":{"notbind":"未绑定","failed":"失败","output":"- [{cid}:{tid}] {name}\n{description}\n"}},"mahjongpub.team.logo":{"description":"队伍头像","messages":{"notbind":"未绑定","failed":"失败"}},"mahjongpub.team.add":{"description":"添加队员","messages":{"notbind":"未绑定","failed":"失败","success":"- [{cid}:{tid}] {name}\n{players.map((p, i) => '' + (i+1) + ': ' + p).join('\\n')}\n"}},"mahjongpub.team.remove":{"description":"删除队员","messages":{"notbind":"未绑定","failed":"失败","success":"- [{cid}:{tid}] {name}\n{players.map((p, i) => '' + (i+1) + ': ' + p).join('\\n')}\n"}},"mahjongpub.team.swap":{"description":"交换队员","messages":{"notbind":"未绑定","failed":"失败","invalid":"参数错误","success":"- [{cid}:{tid}] {name}\n{players.map((p, i) => '' + (i+1) + ': ' + p).join('\\n')}\n"}},"mahjongpub.contest.bind":{"description":"绑定比赛","messages":{"failed":"绑定失败","success":"成功: [{cid}] {name}"}}}}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hieuzest/koishi-plugin-mahjongpub",
3
3
  "description": "Mahjong.pub API",
4
- "version": "0.1.9",
4
+ "version": "0.1.10",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [