@hieuzest/koishi-plugin-riichi-city 0.5.7 → 0.5.8

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
@@ -38,12 +38,12 @@ var require_zh_CN = __commonJS({
38
38
  });
39
39
 
40
40
  // src/index.ts
41
- var index_exports = {};
42
- __export(index_exports, {
41
+ var src_exports = {};
42
+ __export(src_exports, {
43
43
  RiichiCity: () => RiichiCity,
44
- default: () => index_default
44
+ default: () => src_default
45
45
  });
46
- module.exports = __toCommonJS(index_exports);
46
+ module.exports = __toCommonJS(src_exports);
47
47
  var import_koishi3 = require("koishi");
48
48
  var import_api2 = require("./api");
49
49
 
@@ -499,7 +499,7 @@ var RiichiCity = class extends import_koishi3.Service {
499
499
  })
500
500
  ]);
501
501
  })(RiichiCity || (RiichiCity = {}));
502
- var index_default = RiichiCity;
502
+ var src_default = RiichiCity;
503
503
  // Annotate the CommonJS export names for ESM import in node:
504
504
  0 && (module.exports = {
505
505
  RiichiCity
package/lib/lobby.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Disposable, SessionError } from 'koishi';
2
2
  import { RiichiCityApi } from './api';
3
3
  import { LobbyDetails } from './types';
4
+ import { Lock } from './utils';
4
5
  export declare class DHSError extends SessionError {
5
6
  }
6
7
  export declare class PlayerNotFoundError extends DHSError {
@@ -59,6 +60,7 @@ export declare class RiichiCityLobby {
59
60
  notify?: Disposable;
60
61
  notifyIdle: number;
61
62
  notifyCache?: string[];
63
+ lock: Lock;
62
64
  constructor(api: RiichiCityApi, id: number, extra: {
63
65
  adminAccountId: number;
64
66
  });
package/lib/lobby.js CHANGED
@@ -32,6 +32,31 @@ __export(lobby_exports, {
32
32
  module.exports = __toCommonJS(lobby_exports);
33
33
  var import_koishi = require("koishi");
34
34
  var import_api = require("./api");
35
+
36
+ // src/utils.ts
37
+ function noop() {
38
+ }
39
+ __name(noop, "noop");
40
+ var Lock = class {
41
+ static {
42
+ __name(this, "Lock");
43
+ }
44
+ tip;
45
+ constructor() {
46
+ this.tip = Promise.resolve(void 0);
47
+ }
48
+ async acquire() {
49
+ const oldTip = this.tip;
50
+ let resolver = noop;
51
+ const promise = new Promise((resolve) => {
52
+ resolver = resolve;
53
+ });
54
+ this.tip = oldTip.then(() => promise);
55
+ return oldTip.then(() => resolver);
56
+ }
57
+ };
58
+
59
+ // src/lobby.ts
35
60
  var BOT_NAME = "電腦";
36
61
  var DHSError = class extends import_koishi.SessionError {
37
62
  static {
@@ -136,6 +161,7 @@ var RiichiCityLobby = class {
136
161
  notify;
137
162
  notifyIdle = 0;
138
163
  notifyCache;
164
+ lock = new Lock();
139
165
  async init() {
140
166
  this.details = await this.api.enterLobby(this.id);
141
167
  this.classifyID = this.details.classifyID;
@@ -172,32 +198,37 @@ var RiichiCityLobby = class {
172
198
  return res.map((x) => new Paipu(this, x.roomID, x.players.map((p) => new Player(p.nickname, p.points, p.userId)), { paipuID: x.paiPuId }));
173
199
  }
174
200
  async startMatch(players, tag = "", randomPosition = true) {
175
- const onlinePlayers = await this.listPlayers();
176
- const notFound = [];
177
- for (const player of players) {
178
- if (player.accountId === null || player.accountId === void 0) {
179
- if (player.nickname in onlinePlayers) {
180
- player.accountId = onlinePlayers[player.nickname];
181
- } else {
182
- notFound.push(player.pattern);
201
+ const release = await this.lock.acquire();
202
+ try {
203
+ const onlinePlayers = await this.listPlayers();
204
+ const notFound = [];
205
+ for (const player of players) {
206
+ if (player.accountId === null || player.accountId === void 0) {
207
+ if (player.nickname in onlinePlayers) {
208
+ player.accountId = onlinePlayers[player.nickname];
209
+ } else {
210
+ notFound.push(player.pattern);
211
+ }
212
+ } else if (player.accountId) {
213
+ if (!Object.values(onlinePlayers).includes(player.accountId)) notFound.push(player.pattern);
183
214
  }
184
- } else if (player.accountId) {
185
- if (!Object.values(onlinePlayers).includes(player.accountId)) notFound.push(player.pattern);
186
215
  }
187
- }
188
- if (notFound.length) throw new PlayerNotFoundError(...notFound);
189
- for (const player of players) {
190
- if (player.accountId === 0) {
191
- player.accountId = await this.api.addLobbyBot();
216
+ if (notFound.length) throw new PlayerNotFoundError(...notFound);
217
+ for (const player of players) {
218
+ if (player.accountId === 0) {
219
+ player.accountId = await this.api.addLobbyBot();
220
+ }
192
221
  }
222
+ if (randomPosition) players = import_koishi.Random.shuffle(players);
223
+ return await this.api.startMatch({
224
+ table_idx: 1,
225
+ usersID: players.map((p) => p.accountId),
226
+ initPoints: players.map((p) => p.point),
227
+ matchID: this.id
228
+ });
229
+ } finally {
230
+ release();
193
231
  }
194
- if (randomPosition) players = import_koishi.Random.shuffle(players);
195
- return await this.api.startMatch({
196
- table_idx: 1,
197
- usersID: players.map((p) => p.accountId),
198
- initPoints: players.map((p) => p.point),
199
- matchID: this.id
200
- });
201
232
  }
202
233
  };
203
234
  // Annotate the CommonJS export names for ESM import in node:
package/lib/utils.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ export declare class Resolver<T> {
2
+ readonly promise: Promise<T>;
3
+ private resolver;
4
+ private rejecter;
5
+ constructor();
6
+ get resolve(): (result?: T | PromiseLike<T>) => void;
7
+ get reject(): (reason: any | PromiseLike<any>) => void;
8
+ }
9
+ export declare class Lock {
10
+ private tip;
11
+ constructor();
12
+ acquire(): Promise<() => void>;
13
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hieuzest/koishi-plugin-riichi-city",
3
3
  "description": "",
4
- "version": "0.5.7",
4
+ "version": "0.5.8",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -38,5 +38,12 @@
38
38
  ]
39
39
  },
40
40
  "hidden": true
41
+ },
42
+ "devDependencies": {
43
+ "@hieuzest/koishi-buildtools": "^1.0.2",
44
+ "@hieuzest/koishi-plugin-mahjong": "^3.0.0",
45
+ "@koishijs/plugin-admin": "^2.0.0-beta.4",
46
+ "@koishijs/plugin-proxy-agent": "^0.3.3",
47
+ "koishi": "^4.18.9"
41
48
  }
42
49
  }