@hieuzest/koishi-plugin-mahjongpub 0.2.4 → 0.2.5

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.js CHANGED
@@ -371,7 +371,7 @@ var ContestManager = class {
371
371
  this.extra[cid].subscribers.delete(session.cid);
372
372
  return "Finished.";
373
373
  });
374
- ctx.command("mahjongpub.manager.start [cls:number]").channelFields(["mahjongpub/bind-contest"]).action(async ({ session }, cls) => {
374
+ ctx.command("mahjongpub.manager.start [cls:number]").channelFields(["mahjongpub/bind-contest"]).option("force", "-f").action(async ({ session, options }, cls) => {
375
375
  const cid = +(session.channel["mahjongpub/bind-contest"] || 0);
376
376
  if (!cid) return "Unauthorized.";
377
377
  this.extra[cid] ??= new ContestExtra(ctx);
@@ -385,7 +385,7 @@ var ContestManager = class {
385
385
  const rounds = await this.mahjongpub.getRounds(cid);
386
386
  for (const r of Object.values(rounds)) {
387
387
  if (r.round !== contest.c_round) continue;
388
- await this.startMatch(cid, r.t_class);
388
+ await this.startMatch(cid, r.t_class, 0, options.force);
389
389
  await (0, import_koishi2.sleep)(config.startInterval);
390
390
  }
391
391
  }
@@ -422,8 +422,8 @@ var ContestManager = class {
422
422
  static inject = ["server", "mahjong", "mahjong.database", "mahjongpub", "sendMessage", "zx-dhs"];
423
423
  mahjongpub;
424
424
  extra = /* @__PURE__ */ Object.create(null);
425
- async _startMatch(cid, cls, timeout = 0, tag) {
426
- tag ||= `${cls}组`;
425
+ async _startMatch(cid, cls, timeout = 0, force = false) {
426
+ const tag = `${cls}组`;
427
427
  if (!this.extra[cid] || this.extra[cid]?.stopCls.has(cls)) {
428
428
  this.ctx.logger.info(`try starting match ${cid}-${cls}, but stopped`);
429
429
  return;
@@ -438,7 +438,15 @@ var ContestManager = class {
438
438
  cls
439
439
  }).sort({ rowi: -1 }).limit(1).toArray();
440
440
  if (lastRecord && !lastRecord._finished) {
441
- this.extra[cid]?.broadcast(`[${tag}] 发现进行中的比赛,已忽略`);
441
+ if (force) {
442
+ await this.ctx.mahjong.database.db("scoreboard").collection("matches").deleteOne({ _id: lastRecord._id });
443
+ this.ctx.setTimeout(() => this.startMatch(cid, cls, timeout), 0);
444
+ } else {
445
+ this.extra[cid]?.broadcast(`[${tag}] 发现进行中的比赛,已忽略`);
446
+ return;
447
+ }
448
+ }
449
+ if (lastRecord && lastRecord.results?.some((x) => x.num < 0) && !this.config.allowNegativeScore) {
442
450
  return;
443
451
  }
444
452
  const rowi = lastRecord ? lastRecord.rowi + 1 : 0;
@@ -464,12 +472,16 @@ var ContestManager = class {
464
472
  }
465
473
  }
466
474
  }
467
- async startMatch(cid, cls, timeout = 0, tag) {
475
+ async startMatch(cid, cls, timeout = 0, force = false) {
476
+ const tag = `${cls}组`;
468
477
  try {
469
- return await this._startMatch(cid, cls, timeout, tag);
478
+ return await this._startMatch(cid, cls, timeout, force);
470
479
  } catch (e) {
471
480
  this.ctx.logger.warn(`start match ${cid}-${cls} internal failed: `, e);
472
481
  this.extra[cid]?.broadcast(`[${tag}] 失败:内部错误`);
482
+ if (timeout < this.config.startMaxTimeout) {
483
+ this.ctx.setTimeout(() => this.startMatch(cid, cls, timeout + this.config.startTimeoutInterval, force), this.config.startTimeoutInterval);
484
+ }
473
485
  }
474
486
  }
475
487
  async onMatchFinished(record) {
@@ -488,7 +500,8 @@ var ContestManager = class {
488
500
  startInterval: import_koishi2.Schema.number().default(import_koishi2.Time.second).description("比赛开始间隔"),
489
501
  startTimeoutInterval: import_koishi2.Schema.number().default(import_koishi2.Time.second * 30).description("比赛开始重试间隔"),
490
502
  startMaxTimeout: import_koishi2.Schema.number().default(import_koishi2.Time.second * 600).description("比赛开始最大重试间隔"),
491
- finishInterval: import_koishi2.Schema.number().default(import_koishi2.Time.second * 10).description("比赛结束间隔")
503
+ finishInterval: import_koishi2.Schema.number().default(import_koishi2.Time.second * 10).description("比赛结束间隔"),
504
+ allowNegativeScore: import_koishi2.Schema.boolean().default(false).description("允许负分")
492
505
  });
493
506
  })(ContestManager || (ContestManager = {}));
494
507
 
package/lib/manager.d.ts CHANGED
@@ -35,8 +35,8 @@ export declare class ContestManager {
35
35
  mahjongpub: MahjongPub;
36
36
  extra: Dict<ContestExtra>;
37
37
  constructor(ctx: Context, config: ContestManager.Config);
38
- _startMatch(cid: number, cls: number, timeout?: number, tag?: string): Promise<void>;
39
- startMatch(cid: number, cls: number, timeout?: number, tag?: string): Promise<void>;
38
+ _startMatch(cid: number, cls: number, timeout?: number, force?: boolean): Promise<void>;
39
+ startMatch(cid: number, cls: number, timeout?: number, force?: boolean): Promise<void>;
40
40
  onMatchFinished(record: MatchRecord): Promise<void>;
41
41
  }
42
42
  export declare namespace ContestManager {
@@ -46,6 +46,7 @@ export declare namespace ContestManager {
46
46
  startTimeoutInterval: number;
47
47
  startMaxTimeout: number;
48
48
  finishInterval: number;
49
+ allowNegativeScore: boolean;
49
50
  }
50
51
  const Config: Schema<Config>;
51
52
  }
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.2.4",
4
+ "version": "0.2.5",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [