@konoui/mjimage 0.0.21 → 0.0.23

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.
@@ -1,117 +0,0 @@
1
- import { Wind } from "../core/constants";
2
- import { PlaceManager, NewDoraEvent, DistributeEvent, DrawEvent, DiscardEvent, ReachEvent as PlayerReachEvent, TsumoEvent as PlayerTsumoEvent, CallEvent, RonEvent, Player, EventHandler } from "./../controller";
3
- type IDs = 0 | 1 | 2 | 3;
4
- type Bakaze = "E" | "S" | "W" | "N";
5
- export type MJAIEvent = NoneEvent | StartGameEvent | StartKyokuEvent | TsumoEvent | DahaiEvent | PonEvent | ChiEvent | AnkanEvent | DaiminkanEvent | KakanEvent | DoraEvent | ReachEvent | ReachAcceptedEvent | HoraEvent | RyukyokuEvent | EndKyokuEvent | EndGameEvent;
6
- interface NoneEvent {
7
- type: "none";
8
- }
9
- interface StartGameEvent {
10
- type: "start_game";
11
- id: IDs;
12
- }
13
- interface StartKyokuEvent {
14
- type: "start_kyoku";
15
- bakaze: Bakaze;
16
- dora_marker: string;
17
- kyoku: number;
18
- honba: number;
19
- kyotaku: number;
20
- oya: IDs;
21
- scores: number[];
22
- tehais: string[][];
23
- }
24
- interface TsumoEvent {
25
- type: "tsumo";
26
- actor: IDs;
27
- pai: string;
28
- }
29
- interface DahaiEvent {
30
- type: "dahai";
31
- actor: IDs;
32
- pai: string;
33
- tsumogiri: boolean;
34
- }
35
- interface PonEvent {
36
- type: "pon";
37
- actor: IDs;
38
- target: IDs;
39
- pai: string;
40
- consumed: string[];
41
- }
42
- interface ChiEvent {
43
- type: "chi";
44
- actor: IDs;
45
- target: IDs;
46
- pai: string;
47
- consumed: string[];
48
- }
49
- interface AnkanEvent {
50
- type: "ankan";
51
- actor: IDs;
52
- target: IDs;
53
- pai: string;
54
- consumed: string[];
55
- }
56
- interface DaiminkanEvent {
57
- type: "daiminkan";
58
- actor: IDs;
59
- target: IDs;
60
- pai: string;
61
- consumed: string[];
62
- }
63
- interface KakanEvent {
64
- type: "kakan";
65
- actor: IDs;
66
- pai: string;
67
- consumed: string[];
68
- }
69
- interface DoraEvent {
70
- type: "dora";
71
- dora_marker: string;
72
- }
73
- interface ReachEvent {
74
- type: "reach";
75
- actor: IDs;
76
- }
77
- interface ReachAcceptedEvent {
78
- type: "reach_accepted";
79
- actor: IDs;
80
- }
81
- interface HoraEvent {
82
- type: "hora";
83
- actor: IDs;
84
- target: IDs;
85
- pai: string;
86
- uradora_markers: string[];
87
- hora_tehais: string[];
88
- yakus: [string, number][];
89
- fu: number;
90
- fan: number;
91
- hora_points: number;
92
- deltas: number[];
93
- scores: number[];
94
- }
95
- interface RyukyokuEvent {
96
- type: "ryukyoku";
97
- }
98
- interface EndKyokuEvent {
99
- type: "end_kyoku";
100
- }
101
- interface EndGameEvent {
102
- type: "end_game";
103
- }
104
- export declare class MJAIPlayer {
105
- genID: () => string;
106
- reachEventRecorder: {
107
- [actor: string]: boolean;
108
- };
109
- from: EventHandler;
110
- to: EventHandler;
111
- player: Player;
112
- constructor();
113
- react(e: string): void;
114
- handle(e: MJAIEvent): void;
115
- toPlayerEvent: (myWind: Wind, pm: PlaceManager, e: MJAIEvent) => CallEvent | DistributeEvent | RonEvent | PlayerTsumoEvent | DiscardEvent | DrawEvent | PlayerReachEvent | NewDoraEvent;
116
- }
117
- export {};
@@ -1,348 +0,0 @@
1
- "use strict";
2
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
- if (ar || !(i in from)) {
5
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
- ar[i] = from[i];
7
- }
8
- }
9
- return to.concat(ar || Array.prototype.slice.call(from));
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.MJAIPlayer = void 0;
13
- var constants_1 = require("../core/constants");
14
- var parser_1 = require("../core/parser");
15
- var controller_1 = require("./../controller");
16
- var M = ["5mr", "1m", "2m", "3m", "4m", "5m", "6m", "7m", "8m", "9m"];
17
- var S = ["5sr", "1s", "2s", "3s", "4s", "5s", "6s", "7s", "8s", "9s"];
18
- var P = ["5pr", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p"];
19
- var Z = ["ERROR-WIND", "E", "S", "W", "N", "P", "F", "C"];
20
- var UNSCREEN = "?";
21
- var MJAIPlayer = (function () {
22
- function MJAIPlayer() {
23
- var _this = this;
24
- this.genID = (0, controller_1.incrementalIDGenerator)();
25
- this.reachEventRecorder = {};
26
- this.toPlayerEvent = function (myWind, pm, e) {
27
- var _a, _b;
28
- var kyoku = toKyoku(pm.round);
29
- switch (e.type) {
30
- case "start_kyoku": {
31
- var pe = {
32
- id: _this.genID(),
33
- type: "DISTRIBUTE",
34
- hands: toHands(e.tehais, e.kyoku),
35
- wind: myWind,
36
- doraMarker: toTile(e.dora_marker),
37
- sticks: { reach: e.kyotaku, dead: e.honba },
38
- scores: toScores(e.scores),
39
- round: toRound(e.bakaze, e.kyotaku),
40
- places: toPlaces(e.kyoku),
41
- players: ["0", "1", "2", "3"],
42
- };
43
- return pe;
44
- }
45
- case "tsumo": {
46
- var pe = {
47
- id: _this.genID(),
48
- type: "DRAW",
49
- iam: toPlayerWind(e.actor, kyoku),
50
- wind: myWind,
51
- tile: toTile(e.pai),
52
- };
53
- return pe;
54
- }
55
- case "dahai": {
56
- if (_this.reachEventRecorder[e.actor]) {
57
- _this.reachEventRecorder[e.actor] = false;
58
- var pe_1 = {
59
- id: _this.genID(),
60
- type: "REACH",
61
- wind: myWind,
62
- iam: toPlayerWind(e.actor, kyoku),
63
- tile: toTile(e.pai),
64
- };
65
- return pe_1;
66
- }
67
- var pe = {
68
- id: _this.genID(),
69
- type: "DISCARD",
70
- iam: toPlayerWind(e.actor, kyoku),
71
- wind: myWind,
72
- tile: toTile(e.pai),
73
- };
74
- return pe;
75
- }
76
- case "dora": {
77
- var pe = {
78
- id: _this.genID(),
79
- type: "NEW_DORA",
80
- doraMarker: toTile(e.dora_marker),
81
- wind: myWind,
82
- };
83
- return pe;
84
- }
85
- case "ankan": {
86
- var tiles = __spreadArray([toTile(e.pai)], toTiles(e.consumed), true);
87
- var pe = {
88
- id: _this.genID(),
89
- type: "AN_KAN",
90
- wind: myWind,
91
- iam: toPlayerWind(e.actor, kyoku),
92
- block: new parser_1.BlockAnKan(tiles),
93
- };
94
- return pe;
95
- }
96
- case "chi": {
97
- var block = new parser_1.BlockChi([
98
- toTile(e.pai).add(constants_1.OPERATOR.HORIZONTAL),
99
- toTile(e.consumed[0]),
100
- toTile(e.consumed[1]),
101
- ]);
102
- var pe = {
103
- id: _this.genID(),
104
- type: "CHI",
105
- wind: myWind,
106
- iam: toPlayerWind(e.actor, kyoku),
107
- block: block,
108
- };
109
- return pe;
110
- }
111
- case "pon": {
112
- var idx = Math.abs(e.actor - e.target);
113
- if (idx == 3)
114
- idx = 0;
115
- if (idx == 2)
116
- idx = 1;
117
- if (idx == 1)
118
- idx = 2;
119
- var block = new parser_1.BlockPon([
120
- toTile(e.pai).add(constants_1.OPERATOR.HORIZONTAL),
121
- toTile(e.consumed[0]),
122
- toTile(e.consumed[1]),
123
- ]);
124
- _a = [block.tiles[idx], block.tiles[0]], block.tiles[0] = _a[0], block.tiles[idx] = _a[1];
125
- var pe = {
126
- id: _this.genID(),
127
- type: "CHI",
128
- wind: myWind,
129
- iam: toPlayerWind(e.actor, kyoku),
130
- block: block,
131
- };
132
- return pe;
133
- }
134
- case "daiminkan": {
135
- var idx = Math.abs(e.actor - e.target);
136
- if (idx == 3)
137
- idx = 0;
138
- if (idx == 2)
139
- idx = 1;
140
- if (idx == 1)
141
- idx = 2;
142
- var block = new parser_1.BlockDaiKan([
143
- toTile(e.pai).add(constants_1.OPERATOR.HORIZONTAL),
144
- toTile(e.consumed[0]),
145
- toTile(e.consumed[1]),
146
- toTile(e.consumed[2]),
147
- ]);
148
- _b = [block.tiles[idx], block.tiles[0]], block.tiles[0] = _b[0], block.tiles[idx] = _b[1];
149
- var pe = {
150
- id: _this.genID(),
151
- type: "CHI",
152
- wind: myWind,
153
- iam: toPlayerWind(e.actor, kyoku),
154
- block: block,
155
- };
156
- return pe;
157
- }
158
- case "kakan": {
159
- var block = new parser_1.BlockAnKan([
160
- toTile(e.pai).add(constants_1.OPERATOR.HORIZONTAL),
161
- toTile(e.consumed[0]).add(constants_1.OPERATOR.HORIZONTAL),
162
- toTile(e.consumed[1]),
163
- toTile(e.consumed[2]),
164
- ]);
165
- var pe = {
166
- id: _this.genID(),
167
- type: "CHI",
168
- wind: myWind,
169
- iam: toPlayerWind(e.actor, kyoku),
170
- block: block,
171
- };
172
- return pe;
173
- }
174
- case "hora": {
175
- if (e.actor != e.target) {
176
- var pe = {
177
- id: _this.genID(),
178
- wind: myWind,
179
- type: "RON",
180
- iam: toPlayerWind(e.actor, kyoku),
181
- targetInfo: {
182
- wind: toPlayerWind(e.target, kyoku),
183
- tile: toTile(e.pai),
184
- },
185
- pushBackReachStick: false,
186
- ret: toWinResult(e.hora_tehais, e.fan, e.fu, e.yakus, e.hora_points, e.deltas, toKyoku(pm.round)),
187
- };
188
- return pe;
189
- }
190
- else {
191
- var pe = {
192
- id: _this.genID(),
193
- wind: myWind,
194
- type: "TSUMO",
195
- iam: toPlayerWind(e.actor, kyoku),
196
- lastTile: toTile(e.pai),
197
- ret: toWinResult(e.hora_tehais, e.fan, e.fu, e.yakus, e.hora_points, e.deltas, toKyoku(pm.round)),
198
- };
199
- return pe;
200
- }
201
- }
202
- default:
203
- throw new Error("unexpected MJAI event ".concat(e));
204
- }
205
- };
206
- var _a = (0, controller_1.createEventPipe)(), ce = _a[0], pe = _a[1];
207
- this.from = ce;
208
- this.to = pe;
209
- this.player = new controller_1.Player("-1", this.to);
210
- this.from.on(function (e) {
211
- });
212
- }
213
- MJAIPlayer.prototype.react = function (e) {
214
- var _this = this;
215
- var events = JSON.parse(e);
216
- events.forEach(function (e) {
217
- _this.handle(e);
218
- });
219
- };
220
- MJAIPlayer.prototype.handle = function (e) {
221
- switch (e.type) {
222
- case "start_game": {
223
- this.player.id = e.id.toString();
224
- break;
225
- }
226
- case "reach": {
227
- this.reachEventRecorder[e.actor] = true;
228
- break;
229
- }
230
- case "end_game":
231
- break;
232
- default:
233
- var pe = this.toPlayerEvent(this.player.myWind, this.player.placeManager, e);
234
- this.from.emit(pe);
235
- }
236
- };
237
- return MJAIPlayer;
238
- }());
239
- exports.MJAIPlayer = MJAIPlayer;
240
- var toWinResult = function (tehais, fan, fu, yaku, hora_points, deltas, kyoku) {
241
- var d = (0, controller_1.createWindMap)(0);
242
- d[toPlayerWind(0, kyoku)] = deltas[0];
243
- d[toPlayerWind(1, kyoku)] = deltas[1];
244
- d[toPlayerWind(2, kyoku)] = deltas[2];
245
- d[toPlayerWind(3, kyoku)] = deltas[3];
246
- var points = yaku.map(function (v) {
247
- return { double: v[1], name: v[0] };
248
- });
249
- var ret = {
250
- deltas: d,
251
- fu: fu,
252
- sum: fan,
253
- points: points,
254
- point: hora_points,
255
- hand: [],
256
- params: {
257
- doraMarkers: [new parser_1.Tile(constants_1.TYPE.BACK, 0)],
258
- round: "1w1",
259
- myWind: "1w",
260
- },
261
- };
262
- return ret;
263
- };
264
- var toPlaces = function (kyoku) {
265
- return {
266
- "0": toPlayerWind(0, kyoku),
267
- "1": toPlayerWind(1, kyoku),
268
- "2": toPlayerWind(2, kyoku),
269
- "3": toPlayerWind(3, kyoku),
270
- };
271
- };
272
- var toScores = function (scores) {
273
- return {
274
- "0": scores[0],
275
- "1": scores[1],
276
- "2": scores[2],
277
- "3": scores[3],
278
- };
279
- };
280
- var toHands = function (tehais, kyoku) {
281
- var hands = (0, controller_1.createWindMap)("");
282
- for (var actor = 0; actor < tehais.length; actor++) {
283
- var w = toPlayerWind(actor, kyoku);
284
- var tehai = tehais[actor];
285
- hands[w] = toTiles(tehai).toString();
286
- }
287
- return hands;
288
- };
289
- var toPlayerWind = function (actor, kyoku) {
290
- var idx = Math.abs(actor + 1 - kyoku) + 1;
291
- return toWind(Z[idx]);
292
- };
293
- var toRound = function (b, kyoku) {
294
- return "".concat(toWind(b)).concat(kyoku);
295
- };
296
- var toKyoku = function (round) {
297
- return Number(round[2]);
298
- };
299
- var toWind = function (b) {
300
- var idx = Z.findIndex(function (v) { return v == b; });
301
- return "".concat(idx, "w");
302
- };
303
- var toBakaze = function (b) {
304
- var idx = b[0];
305
- return Z[Number(idx)];
306
- };
307
- var toTile = function (hai) {
308
- var idx = Z.findIndex(function (v) { return v == hai; });
309
- if (idx >= 0)
310
- return new parser_1.Tile(constants_1.TYPE.Z, idx);
311
- idx = M.findIndex(function (v) { return v == hai; });
312
- if (idx >= 0)
313
- return new parser_1.Tile(constants_1.TYPE.M, idx);
314
- idx = S.findIndex(function (v) { return v == hai; });
315
- if (idx >= 0)
316
- return new parser_1.Tile(constants_1.TYPE.S, idx);
317
- idx = P.findIndex(function (v) { return v == hai; });
318
- if (idx >= 0)
319
- return new parser_1.Tile(constants_1.TYPE.P, idx);
320
- return new parser_1.Tile(constants_1.TYPE.BACK, 0);
321
- };
322
- var toTiles = function (hais) {
323
- var tiles = [];
324
- for (var _i = 0, hais_1 = hais; _i < hais_1.length; _i++) {
325
- var hai = hais_1[_i];
326
- tiles.push(toTile(hai));
327
- }
328
- return tiles;
329
- };
330
- var toHais = function (tiles) {
331
- var hais = [];
332
- for (var _i = 0, tiles_1 = tiles; _i < tiles_1.length; _i++) {
333
- var tile = tiles_1[_i];
334
- var n = tile.n;
335
- var type = tile.t;
336
- if (type == constants_1.TYPE.Z)
337
- hais.push(Z[n]);
338
- if (type == constants_1.TYPE.M)
339
- hais.push(M[n]);
340
- if (type == constants_1.TYPE.S)
341
- hais.push(S[n]);
342
- if (type == constants_1.TYPE.P)
343
- hais.push(P[n]);
344
- if (type == constants_1.TYPE.BACK)
345
- hais.push(UNSCREEN);
346
- }
347
- return hais;
348
- };