@hieuzest/koishi-plugin-mahjongpub 0.2.7 → 0.2.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.
package/lib/index.js CHANGED
@@ -241,6 +241,7 @@ var ContestAdmin = class {
241
241
 
242
242
  // src/manager.ts
243
243
  var import_koishi2 = require("koishi");
244
+ var import_api = require("@hieuzest/koishi-plugin-majsoul-dhs/api");
244
245
  var import_lobby = require("@hieuzest/koishi-plugin-riichi-city/lobby");
245
246
 
246
247
  // src/mahjongpub.ts
@@ -346,22 +347,37 @@ var ContestExtra = class {
346
347
  }
347
348
  subscribers;
348
349
  stopCls;
350
+ type;
351
+ round;
352
+ lobby;
353
+ ver = 0;
349
354
  async broadcast(msg) {
350
355
  this.subscribers.forEach((channel) => this.ctx.sendMessage(channel, msg).catch((e) => this.ctx.logger.debug(e)));
351
356
  }
352
357
  };
358
+ function getRowPlayer(contest, team, rowi) {
359
+ const roleList = contest.t_type.split(/\s+/);
360
+ const roleSet = new Set(roleList).values().toArray();
361
+ const index = roleSet.indexOf(roleList[rowi]);
362
+ return team.players[index]?.split("##", 1)[0];
363
+ }
364
+ __name(getRowPlayer, "getRowPlayer");
353
365
  var ContestManager = class {
354
366
  constructor(ctx, config) {
355
367
  this.ctx = ctx;
356
368
  this.config = config;
357
369
  this.mahjongpub = MahjongPub.new(ctx);
358
370
  ctx.command("mahjongpub.manager", { authority: 3 }).action(import_koishi2.noop);
359
- ctx.command("mahjongpub.manager.init").channelFields(["mahjongpub/bind-contest"]).option("clear", "-c").action(async ({ session, options }) => {
371
+ ctx.command("mahjongpub.manager.init").channelFields(["mahjongpub/bind-contest"]).option("clear", "-c").option("type", "-t [type:string]").option("ver", "-v [ver:number]").option("round", "-r [round:number]").option("lobby", "-l [lobby:string]").action(async ({ session, options }) => {
360
372
  const cid = +(session.channel["mahjongpub/bind-contest"] || 0);
361
373
  if (!cid) return "Unauthorized.";
362
374
  this.extra[cid] ??= new ContestExtra(ctx);
363
375
  this.extra[cid].subscribers.add(session.cid);
364
376
  if (options.clear) this.extra[cid].stopCls.clear();
377
+ if (options.type) this.extra[cid].type = options.type;
378
+ if (!(0, import_koishi2.isNullable)(options.ver)) this.extra[cid].ver = options.ver;
379
+ if (!(0, import_koishi2.isNullable)(options.round)) this.extra[cid].round = options.round;
380
+ if (!(0, import_koishi2.isNullable)(options.lobby)) this.extra[cid].lobby = options.lobby;
365
381
  return "Finished.";
366
382
  });
367
383
  ctx.command("mahjongpub.manager.deinit").channelFields(["mahjongpub/bind-contest"]).option("clear", "-c").action(async ({ session, options }) => {
@@ -374,17 +390,17 @@ var ContestManager = class {
374
390
  ctx.command("mahjongpub.manager.start [cls:number]").channelFields(["mahjongpub/bind-contest"]).option("force", "-f").action(async ({ session, options }, cls) => {
375
391
  const cid = +(session.channel["mahjongpub/bind-contest"] || 0);
376
392
  if (!cid) return "Unauthorized.";
377
- this.extra[cid] ??= new ContestExtra(ctx);
378
- this.extra[cid].subscribers.add(session.cid);
393
+ const cextra = this.extra[cid] ??= new ContestExtra(ctx);
394
+ cextra.subscribers.add(session.cid);
379
395
  if (cls) {
380
- this.extra[cid].stopCls.delete(cls);
396
+ cextra.stopCls.delete(cls);
381
397
  await this.startMatch(cid, cls);
382
398
  } else {
383
- this.extra[cid].stopCls.clear();
399
+ cextra.stopCls.clear();
384
400
  const contest = await this.mahjongpub.getContest(cid);
385
401
  const rounds = await this.mahjongpub.getRounds(cid);
386
402
  for (const r of Object.values(rounds)) {
387
- if (r.round !== contest.c_round) continue;
403
+ if (r.round !== (cextra.round ?? contest.c_round)) continue;
388
404
  await this.startMatch(cid, r.t_class, 0, options.force);
389
405
  await (0, import_koishi2.sleep)(config.startInterval);
390
406
  }
@@ -393,15 +409,15 @@ var ContestManager = class {
393
409
  ctx.command("mahjongpub.manager.stop [cls:number]").channelFields(["mahjongpub/bind-contest"]).action(async ({ session }, cls) => {
394
410
  const cid = +(session.channel["mahjongpub/bind-contest"] || 0);
395
411
  if (!cid) return "Unauthorized/";
396
- this.extra[cid] ??= new ContestExtra(ctx);
412
+ const cextra = this.extra[cid] ??= new ContestExtra(ctx);
397
413
  if (cls) {
398
- this.extra[cid].stopCls.add(cls);
414
+ cextra.stopCls.add(cls);
399
415
  } else {
400
416
  const contest = await this.mahjongpub.getContest(cid);
401
417
  const rounds = await this.mahjongpub.getRounds(cid);
402
418
  for (const r of Object.values(rounds)) {
403
- if (r.round !== contest.c_round) continue;
404
- this.extra[cid].stopCls.add(r.t_class);
419
+ if (r.round !== (cextra.round ?? contest.c_round)) continue;
420
+ cextra.stopCls.add(r.t_class);
405
421
  }
406
422
  }
407
423
  return "Finished.";
@@ -419,12 +435,13 @@ var ContestManager = class {
419
435
  static {
420
436
  __name(this, "ContestManager");
421
437
  }
422
- static inject = ["server", "mahjong", "mahjong.database", "mahjongpub", "sendMessage", "zx-dhs"];
438
+ static inject = ["server", "mahjong", "mahjong.database", "mahjongpub", "sendMessage", "zx-dhs", "majsoul-dhs"];
423
439
  mahjongpub;
424
440
  extra = /* @__PURE__ */ Object.create(null);
425
441
  async _startMatch(cid, cls, timeout = 0, force = false) {
442
+ const cextra = this.extra[cid];
426
443
  const tag = `${cls}组`;
427
- if (!this.extra[cid] || this.extra[cid]?.stopCls.has(cls)) {
444
+ if (!cextra || cextra.stopCls.has(cls)) {
428
445
  this.ctx.logger.info(`try starting match ${cid}-${cls}, but stopped`);
429
446
  return;
430
447
  }
@@ -434,41 +451,53 @@ var ContestManager = class {
434
451
  const teams = await this.mahjongpub.getTeams(cid);
435
452
  const [lastRecord] = await this.ctx.mahjong.database.db("scoreboard").collection("matches").find({
436
453
  cid: `${cid}`,
437
- round: +contest.c_round,
438
- cls
454
+ round: cextra.round ?? +contest.c_round,
455
+ cls,
456
+ ver: cextra.ver
439
457
  }).sort({ rowi: -1 }).limit(1).toArray();
440
458
  if (lastRecord && !lastRecord._finished) {
441
459
  if (force) {
442
460
  await this.ctx.mahjong.database.db("scoreboard").collection("matches").deleteOne({ _id: lastRecord._id });
443
461
  this.ctx.setTimeout(() => this.startMatch(cid, cls, timeout), 0);
444
462
  } else {
445
- this.extra[cid]?.broadcast(`[${tag}] 发现进行中的比赛,已忽略`);
463
+ cextra.broadcast(`[${tag}] 发现进行中的比赛,已忽略`);
446
464
  return;
447
465
  }
448
466
  }
449
- if (lastRecord && lastRecord.results?.some((x) => x.num < 0) && !this.config.allowNegativeScore) {
467
+ if (cextra.type === "ti" && lastRecord && lastRecord.results?.some((x) => x.num < 0) && !this.config.allowNegativeScore) {
450
468
  return;
451
469
  }
452
470
  const rowi = lastRecord ? lastRecord.rowi + 1 : 0;
453
- const round = Object.values(rounds).find((x) => x.round === contest.c_round && x.t_class === cls);
454
- const players = round.tids.map((tid) => teams[tid].players[Math.floor(rowi / 2)]).map((x) => x?.split("##", 1)[0]);
471
+ const round = Object.values(rounds).find((x) => x.round === (cextra.round ?? contest.c_round) && x.t_class === cls);
472
+ const players = round.tids.map((tid) => getRowPlayer(contest, teams[tid], rowi));
455
473
  if (!players.every((x) => x)) {
456
- this.extra[cid]?.broadcast(`[${tag}] 失败: 名单未填写`);
474
+ cextra.broadcast(`[${tag}] 失败: 名单未填写`);
457
475
  return;
458
476
  }
459
477
  try {
460
- this.ctx.logger.info(`starting match ${cid}-${cls}, players ${players.map((x, i) => `${x} ${lastRecord ? lastRecord.results?.[i].num : 1e5}`).join(", ")}`);
461
- await this.ctx["zx-dhs"].startMatch(+round.code, players.map((x, i) => import_lobby.Player.fromPattern(x, lastRecord ? lastRecord.results?.[i].num : 1e5)));
462
- this.extra[cid]?.broadcast(`[${tag}] 成功:` + players.join(","));
478
+ if (cextra.type === "ti") {
479
+ this.ctx.logger.info(`starting ti match ${cid}-${cls}, players ${players.map((x, i) => `${x} ${lastRecord ? lastRecord.results?.[i].num : 1e5}`).join(", ")}`);
480
+ await this.ctx["zx-dhs"].startMatch(
481
+ +(cextra.lobby ?? round.code),
482
+ players.map((x, i) => import_lobby.Player.fromPattern(x, lastRecord ? lastRecord.results?.[i].num : 1e5))
483
+ );
484
+ } else if (cextra.type === "ssb") {
485
+ this.ctx.logger.info(`starting ssb match ${cid}-${cls}, players ${players.join(", ")}`);
486
+ await this.ctx["majsoul-dhs"].startMatch(+(cextra.lobby ?? round.code), players.map((x) => import_api.Player.fromPattern(x, 25e3)));
487
+ }
488
+ cextra.broadcast(`[${tag}] 成功:` + players.join(","));
463
489
  } catch (e) {
464
- if (e instanceof import_lobby.DHSError) {
465
- this.extra[cid]?.broadcast(`[${tag}] 失败:` + e.message + ` (timeout=${timeout / import_koishi2.Time.second}s)`);
490
+ if (e instanceof import_lobby.DHSError || e instanceof import_api.DHSError) {
491
+ cextra.broadcast(`[${tag}] 失败:` + e.message + ` (timeout=${timeout / import_koishi2.Time.second}s)`);
466
492
  if (timeout < this.config.startMaxTimeout) {
467
493
  this.ctx.setTimeout(() => this.startMatch(cid, cls, timeout + this.config.startTimeoutInterval), this.config.startTimeoutInterval);
494
+ } else {
495
+ this.ctx.logger.warn(`start match ${cid}-${cls} exceed max retires`);
496
+ cextra.broadcast(`[${tag}] 失败:超过最大重试时间 【${players.join(" ")}】`);
468
497
  }
469
498
  } else {
470
499
  this.ctx.logger.warn(`start match ${cid}-${cls} failed: ${e.message}`);
471
- this.extra[cid]?.broadcast(`[${tag}] 失败:` + e.message);
500
+ cextra.broadcast(`[${tag}] 失败:` + e.message);
472
501
  }
473
502
  }
474
503
  }
package/lib/manager.d.ts CHANGED
@@ -25,6 +25,10 @@ declare class ContestExtra {
25
25
  ctx: Context;
26
26
  subscribers: Set<string>;
27
27
  stopCls: Set<number>;
28
+ type: 'ti' | 'ssb';
29
+ round?: number;
30
+ lobby?: string;
31
+ ver: number;
28
32
  constructor(ctx: Context);
29
33
  broadcast(msg: string): Promise<void>;
30
34
  }
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.7",
4
+ "version": "0.2.9",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -16,6 +16,7 @@
16
16
  ],
17
17
  "peerDependencies": {
18
18
  "@hieuzest/koishi-plugin-riichi-city": "^0.5.2",
19
+ "@hieuzest/koishi-plugin-majsoul-dhs": "^2.5.0",
19
20
  "koishi": "^4.17.1"
20
21
  },
21
22
  "dependencies": {