@gamepark/react-client 7.0.0-beta.0 → 7.0.0-beta.2

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.
Files changed (258) hide show
  1. package/dist/Actions/ActionFailed.js +3 -7
  2. package/dist/Actions/ActionFailed.js.map +1 -1
  3. package/dist/Actions/AnimationComplete.js +3 -7
  4. package/dist/Actions/AnimationComplete.js.map +1 -1
  5. package/dist/Actions/AnimationPaused.js +3 -7
  6. package/dist/Actions/AnimationPaused.js.map +1 -1
  7. package/dist/Actions/AnimationStart.js +1 -4
  8. package/dist/Actions/AnimationStart.js.map +1 -1
  9. package/dist/Actions/ClearFailures.d.ts +1 -5
  10. package/dist/Actions/ClearFailures.js +3 -9
  11. package/dist/Actions/ClearFailures.js.map +1 -1
  12. package/dist/Actions/GameNotificationsReceived.d.ts +2 -4
  13. package/dist/Actions/GameNotificationsReceived.js +2 -9
  14. package/dist/Actions/GameNotificationsReceived.js.map +1 -1
  15. package/dist/Actions/GameOver.js +3 -7
  16. package/dist/Actions/GameOver.js.map +1 -1
  17. package/dist/Actions/GameStateLoaded.js +5 -18
  18. package/dist/Actions/GameStateLoaded.js.map +1 -1
  19. package/dist/Actions/MovePlayed.d.ts +4 -4
  20. package/dist/Actions/MovePlayed.js +3 -4
  21. package/dist/Actions/MovePlayed.js.map +1 -1
  22. package/dist/Actions/MoveUndone.js +3 -8
  23. package/dist/Actions/MoveUndone.js.map +1 -1
  24. package/dist/Actions/PlayerQuit.d.ts +3 -9
  25. package/dist/Actions/PlayerQuit.js +2 -19
  26. package/dist/Actions/PlayerQuit.js.map +1 -1
  27. package/dist/Actions/SetAnimationsSpeed.d.ts +1 -6
  28. package/dist/Actions/SetAnimationsSpeed.js +3 -9
  29. package/dist/Actions/SetAnimationsSpeed.js.map +1 -1
  30. package/dist/Actions/giveUp.d.ts +1 -0
  31. package/dist/Actions/giveUp.js +3 -0
  32. package/dist/Actions/giveUp.js.map +1 -0
  33. package/dist/Actions/index.d.ts +7 -11
  34. package/dist/Actions/index.js +7 -27
  35. package/dist/Actions/index.js.map +1 -1
  36. package/dist/Actions/loadGameState.d.ts +14 -0
  37. package/dist/Actions/loadGameState.js +3 -0
  38. package/dist/Actions/loadGameState.js.map +1 -0
  39. package/dist/Actions/playMove.d.ts +10 -0
  40. package/dist/Actions/playMove.js +3 -0
  41. package/dist/Actions/playMove.js.map +1 -0
  42. package/dist/Actions/receiveNotifications.d.ts +8 -0
  43. package/dist/Actions/receiveNotifications.js +3 -0
  44. package/dist/Actions/receiveNotifications.js.map +1 -0
  45. package/dist/Actions/removePlayer.d.ts +5 -0
  46. package/dist/Actions/removePlayer.js +3 -0
  47. package/dist/Actions/removePlayer.js.map +1 -0
  48. package/dist/Actions/reportFailure.d.ts +1 -0
  49. package/dist/Actions/reportFailure.js +3 -0
  50. package/dist/Actions/reportFailure.js.map +1 -0
  51. package/dist/Actions/setGameOver.d.ts +2 -0
  52. package/dist/Actions/setGameOver.js +3 -0
  53. package/dist/Actions/setGameOver.js.map +1 -0
  54. package/dist/Actions/startAnimation.d.ts +1 -0
  55. package/dist/Actions/startAnimation.js +3 -0
  56. package/dist/Actions/startAnimation.js.map +1 -0
  57. package/dist/Actions/undoMove.d.ts +5 -0
  58. package/dist/Actions/undoMove.js +4 -0
  59. package/dist/Actions/undoMove.js.map +1 -0
  60. package/dist/Chat/ChatChannel.js +1 -2
  61. package/dist/Chat/Message.js +1 -2
  62. package/dist/Chat/index.js +2 -18
  63. package/dist/Chat/index.js.map +1 -1
  64. package/dist/Deckbuilding/Deck.js +1 -2
  65. package/dist/Deckbuilding/index.js +4 -24
  66. package/dist/Deckbuilding/index.js.map +1 -1
  67. package/dist/Deckbuilding/useDeleteDeck.d.ts +4 -1
  68. package/dist/Deckbuilding/useDeleteDeck.js +8 -14
  69. package/dist/Deckbuilding/useDeleteDeck.js.map +1 -1
  70. package/dist/Deckbuilding/useMyDecks.d.ts +2 -1
  71. package/dist/Deckbuilding/useMyDecks.js +7 -13
  72. package/dist/Deckbuilding/useMyDecks.js.map +1 -1
  73. package/dist/Deckbuilding/useSaveDeck.d.ts +4 -1
  74. package/dist/Deckbuilding/useSaveDeck.js +8 -14
  75. package/dist/Deckbuilding/useSaveDeck.js.map +1 -1
  76. package/dist/EjectPlayer/EjectPlayer.d.ts +1 -6
  77. package/dist/EjectPlayer/EjectPlayer.js +2 -8
  78. package/dist/EjectPlayer/EjectPlayer.js.map +1 -1
  79. package/dist/EjectPlayer/index.js +1 -17
  80. package/dist/EjectPlayer/index.js.map +1 -1
  81. package/dist/Failure/Failure.js +1 -4
  82. package/dist/Failure/Failure.js.map +1 -1
  83. package/dist/Failure/index.js +1 -17
  84. package/dist/Failure/index.js.map +1 -1
  85. package/dist/GiveUp/GiveUp.js +2 -5
  86. package/dist/GiveUp/GiveUp.js.map +1 -1
  87. package/dist/GiveUp/index.js +1 -17
  88. package/dist/GiveUp/index.js.map +1 -1
  89. package/dist/Hooks/index.js +5 -21
  90. package/dist/Hooks/index.js.map +1 -1
  91. package/dist/Hooks/useChatMessages.d.ts +1 -6
  92. package/dist/Hooks/useChatMessages.js +35 -44
  93. package/dist/Hooks/useChatMessages.js.map +1 -1
  94. package/dist/Hooks/useMessageAuthor.js +12 -16
  95. package/dist/Hooks/useMessageAuthor.js.map +1 -1
  96. package/dist/Hooks/useOptions.js +3 -6
  97. package/dist/Hooks/useOptions.js.map +1 -1
  98. package/dist/Hooks/useRematch.d.ts +1 -1
  99. package/dist/Hooks/useRematch.js +16 -20
  100. package/dist/Hooks/useRematch.js.map +1 -1
  101. package/dist/Hooks/useSendMessage.d.ts +4 -1
  102. package/dist/Hooks/useSendMessage.js +5 -12
  103. package/dist/Hooks/useSendMessage.js.map +1 -1
  104. package/dist/Log/StartLiveLog.d.ts +1 -4
  105. package/dist/Log/StartLiveLog.js +3 -5
  106. package/dist/Log/StartLiveLog.js.map +1 -1
  107. package/dist/Log/StopLiveLog.d.ts +1 -4
  108. package/dist/Log/StopLiveLog.js +4 -7
  109. package/dist/Log/StopLiveLog.js.map +1 -1
  110. package/dist/Log/index.d.ts +2 -2
  111. package/dist/Log/index.js +4 -20
  112. package/dist/Log/index.js.map +1 -1
  113. package/dist/Log/useLogControls.js +11 -14
  114. package/dist/Log/useLogControls.js.map +1 -1
  115. package/dist/PlayerTime/PlayerTime.js +1 -2
  116. package/dist/PlayerTime/index.js +1 -17
  117. package/dist/PlayerTime/index.js.map +1 -1
  118. package/dist/Rematch/RematchQueries.js +23 -13
  119. package/dist/Rematch/RematchQueries.js.map +1 -1
  120. package/dist/Rematch/index.js +1 -17
  121. package/dist/Rematch/index.js.map +1 -1
  122. package/dist/Sound/MuteSounds.d.ts +1 -4
  123. package/dist/Sound/MuteSounds.js +5 -8
  124. package/dist/Sound/MuteSounds.js.map +1 -1
  125. package/dist/Sound/UnmuteSounds.d.ts +1 -4
  126. package/dist/Sound/UnmuteSounds.js +3 -5
  127. package/dist/Sound/UnmuteSounds.js.map +1 -1
  128. package/dist/Sound/index.d.ts +2 -2
  129. package/dist/Sound/index.js +3 -19
  130. package/dist/Sound/index.js.map +1 -1
  131. package/dist/Sound/useSoundControls.js +11 -14
  132. package/dist/Sound/useSoundControls.js.map +1 -1
  133. package/dist/Store/DisplayedAction.js +4 -10
  134. package/dist/Store/DisplayedAction.js.map +1 -1
  135. package/dist/Store/GamePageState.d.ts +0 -1
  136. package/dist/Store/GamePageState.js +1 -2
  137. package/dist/Store/PlayedMove.js +1 -2
  138. package/dist/Store/endAnimation.d.ts +1 -0
  139. package/dist/Store/endAnimation.js +3 -0
  140. package/dist/Store/endAnimation.js.map +1 -0
  141. package/dist/Store/gameMiddleware.js +30 -41
  142. package/dist/Store/gameMiddleware.js.map +1 -1
  143. package/dist/Store/gameReducer.d.ts +2 -11
  144. package/dist/Store/gameReducer.js +301 -377
  145. package/dist/Store/gameReducer.js.map +1 -1
  146. package/dist/Store/index.d.ts +1 -0
  147. package/dist/Store/index.js +4 -19
  148. package/dist/Store/index.js.map +1 -1
  149. package/dist/Store/localMovePreviewMiddleware.js +15 -13
  150. package/dist/Store/localMovePreviewMiddleware.js.map +1 -1
  151. package/dist/Store/store.d.ts +13 -0
  152. package/dist/Store/store.js +27 -0
  153. package/dist/Store/store.js.map +1 -0
  154. package/dist/Tutorial/PlayTutorialMoves.d.ts +1 -9
  155. package/dist/Tutorial/PlayTutorialMoves.js +2 -6
  156. package/dist/Tutorial/PlayTutorialMoves.js.map +1 -1
  157. package/dist/Tutorial/Tutorial.js +1 -2
  158. package/dist/Tutorial/TutorialDescription.d.ts +1 -1
  159. package/dist/Tutorial/TutorialDescription.js +1 -2
  160. package/dist/Tutorial/index.js +4 -20
  161. package/dist/Tutorial/index.js.map +1 -1
  162. package/dist/Tutorial/useTutorial.js +11 -20
  163. package/dist/Tutorial/useTutorial.js.map +1 -1
  164. package/dist/Types/GameAI.js +1 -2
  165. package/dist/Types/GameData.js +1 -2
  166. package/dist/Types/GameMessage.js +1 -2
  167. package/dist/Types/GameMode.js +2 -5
  168. package/dist/Types/GameMode.js.map +1 -1
  169. package/dist/Types/GameNotification.js +1 -2
  170. package/dist/Types/GameOptions.js +2 -5
  171. package/dist/Types/GameOptions.js.map +1 -1
  172. package/dist/Types/MovePlayedNotification.d.ts +1 -1
  173. package/dist/Types/MovePlayedNotification.js +3 -8
  174. package/dist/Types/MovePlayedNotification.js.map +1 -1
  175. package/dist/Types/MoveUndoneNotification.d.ts +1 -1
  176. package/dist/Types/MoveUndoneNotification.js +3 -8
  177. package/dist/Types/MoveUndoneNotification.js.map +1 -1
  178. package/dist/Types/Player.js +1 -2
  179. package/dist/Types/PlayerQuitReason.js +2 -5
  180. package/dist/Types/PlayerQuitReason.js.map +1 -1
  181. package/dist/Types/ProxyGameData.js +1 -2
  182. package/dist/Types/Tournament.js +1 -2
  183. package/dist/Types/index.js +12 -28
  184. package/dist/Types/index.js.map +1 -1
  185. package/dist/User/User.js +1 -2
  186. package/dist/User/index.js +2 -18
  187. package/dist/User/index.js.map +1 -1
  188. package/dist/User/useMe.js +9 -19
  189. package/dist/User/useMe.js.map +1 -1
  190. package/dist/animations/Animation.js +1 -2
  191. package/dist/animations/AnimationContext.d.ts +1 -1
  192. package/dist/animations/AnimationContext.js +1 -2
  193. package/dist/animations/AnimationState.js +1 -2
  194. package/dist/animations/AnimationStep.js +2 -5
  195. package/dist/animations/AnimationStep.js.map +1 -1
  196. package/dist/animations/Animations.js +9 -16
  197. package/dist/animations/Animations.js.map +1 -1
  198. package/dist/animations/AnimationsListener.d.ts +6 -0
  199. package/dist/animations/AnimationsListener.js +54 -0
  200. package/dist/animations/AnimationsListener.js.map +1 -0
  201. package/dist/animations/AnimationsListenerCreator.d.ts +1 -4
  202. package/dist/animations/AnimationsListenerCreator.js +24 -30
  203. package/dist/animations/AnimationsListenerCreator.js.map +1 -1
  204. package/dist/animations/animationMiddleware.d.ts +4 -0
  205. package/dist/animations/animationMiddleware.js +50 -0
  206. package/dist/animations/animationMiddleware.js.map +1 -0
  207. package/dist/animations/index.d.ts +0 -1
  208. package/dist/animations/index.js +5 -22
  209. package/dist/animations/index.js.map +1 -1
  210. package/dist/api/Client/GameClientAPI.d.ts +5 -6
  211. package/dist/api/Client/GameClientAPI.js +70 -119
  212. package/dist/api/Client/GameClientAPI.js.map +1 -1
  213. package/dist/api/Client/GameNotificationsListener.js +51 -104
  214. package/dist/api/Client/GameNotificationsListener.js.map +1 -1
  215. package/dist/api/Client/RemoteGameLoader.js +50 -45
  216. package/dist/api/Client/RemoteGameLoader.js.map +1 -1
  217. package/dist/api/Client/RemoteGameProvider.js +15 -51
  218. package/dist/api/Client/RemoteGameProvider.js.map +1 -1
  219. package/dist/api/Client/apollo-client.d.ts +1 -1
  220. package/dist/api/Client/apollo-client.js +14 -31
  221. package/dist/api/Client/apollo-client.js.map +1 -1
  222. package/dist/api/Client/index.js +4 -20
  223. package/dist/api/Client/index.js.map +1 -1
  224. package/dist/api/Client/pusher.js +5 -12
  225. package/dist/api/Client/pusher.js.map +1 -1
  226. package/dist/api/GameAPI.d.ts +2 -3
  227. package/dist/api/GameAPI.js +1 -2
  228. package/dist/api/Local/ConsoleTools.d.ts +1 -2
  229. package/dist/api/Local/ConsoleTools.js +15 -22
  230. package/dist/api/Local/ConsoleTools.js.map +1 -1
  231. package/dist/api/Local/GameLocalAPI.d.ts +4 -5
  232. package/dist/api/Local/GameLocalAPI.js +264 -363
  233. package/dist/api/Local/GameLocalAPI.js.map +1 -1
  234. package/dist/api/Local/LocalGameProvider.d.ts +1 -1
  235. package/dist/api/Local/LocalGameProvider.js +17 -48
  236. package/dist/api/Local/LocalGameProvider.js.map +1 -1
  237. package/dist/api/Local/index.js +1 -17
  238. package/dist/api/Local/index.js.map +1 -1
  239. package/dist/api/index.js +2 -18
  240. package/dist/api/index.js.map +1 -1
  241. package/dist/index.d.ts +0 -2
  242. package/dist/index.js +16 -36
  243. package/dist/index.js.map +1 -1
  244. package/dist/util/ArrayUtil.js +4 -8
  245. package/dist/util/ArrayUtil.js.map +1 -1
  246. package/package.json +66 -62
  247. package/dist/Actions/ActionType.d.ts +0 -3
  248. package/dist/Actions/ActionType.js +0 -8
  249. package/dist/Actions/ActionType.js.map +0 -1
  250. package/dist/Scoring/ScoringDescription.d.ts +0 -8
  251. package/dist/Scoring/ScoringDescription.js +0 -3
  252. package/dist/Scoring/ScoringDescription.js.map +0 -1
  253. package/dist/Scoring/index.d.ts +0 -1
  254. package/dist/Scoring/index.js +0 -18
  255. package/dist/Scoring/index.js.map +0 -1
  256. package/dist/util/TimeUtil.d.ts +0 -2
  257. package/dist/util/TimeUtil.js +0 -27
  258. package/dist/util/TimeUtil.js.map +0 -1
@@ -1,144 +1,90 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
24
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
50
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
51
- if (ar || !(i in from)) {
52
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
53
- ar[i] = from[i];
54
- }
55
- }
56
- return to.concat(ar || Array.prototype.slice.call(from));
57
- };
58
- var __importDefault = (this && this.__importDefault) || function (mod) {
59
- return (mod && mod.__esModule) ? mod : { "default": mod };
60
- };
61
- Object.defineProperty(exports, "__esModule", { value: true });
62
- exports.GameLocalAPI = void 0;
63
- var rules_api_1 = require("@gamepark/rules-api");
64
- var assert_1 = __importDefault(require("assert"));
65
- var Actions_1 = require("../../Actions");
66
- var Failure_1 = require("../../Failure");
67
- var Types_1 = require("../../Types");
68
- var ArrayUtil_1 = require("../../util/ArrayUtil");
69
- var GameLocalAPI = (function () {
70
- function GameLocalAPI(_a) {
71
- var Rules = _a.Rules, ServerRules = _a.ServerRules, GameSetup = _a.GameSetup, ai = _a.ai, game = _a.game, storage = _a.storage, optionsSpec = _a.optionsSpec, tutorial = _a.tutorial;
72
- this.lastActionId = 0;
73
- this.notificationsCount = 0;
74
- this.autoPlayersCounter = 0;
75
- this.aiProcessing = false;
76
- this.tutorialMovesToPlay = 0;
77
- this.storage = storage !== null && storage !== void 0 ? storage : game;
78
- this.Rules = ServerRules !== null && ServerRules !== void 0 ? ServerRules : Rules;
1
+ import { applyAutomaticMoves, generatePlayersOption, getGameView, getMoveView, hasHiddenInformation, hasSecretInformation, hasUndo, isSecretAction, isWithPlayerIdOptions, isWithPlayerOptions, playAction, playActionWithViews, RandomBot, replayActions } from '@gamepark/rules-api';
2
+ import assert from 'assert';
3
+ import { receiveNotifications, setAnimationsSpeed, setGameOver } from '../../Actions';
4
+ import { loadGameState } from '../../Actions/loadGameState';
5
+ import { reportFailure } from '../../Actions/reportFailure';
6
+ import { Failure } from '../../Failure';
7
+ import { GameMode, GameSpeed, movePlayedNotification, moveUndoneNotification } from '../../Types';
8
+ import { findLastIndex } from '../../util/ArrayUtil';
9
+ export class GameLocalAPI {
10
+ storage;
11
+ gameSetup;
12
+ Rules;
13
+ ClientRules;
14
+ optionsSpec;
15
+ ai;
16
+ tutorial;
17
+ dispatch;
18
+ lastActionId = 0;
19
+ notificationsCount = 0;
20
+ autoPlayersCounter = 0;
21
+ aiProcessing = false;
22
+ tutorialMovesToPlay = 0;
23
+ constructor({ Rules, ServerRules, GameSetup, ai, game, storage, optionsSpec, tutorial }) {
24
+ this.storage = storage ?? game;
25
+ this.Rules = ServerRules ?? Rules;
79
26
  this.gameSetup = new GameSetup();
80
27
  this.ClientRules = Rules;
81
28
  this.optionsSpec = optionsSpec;
82
29
  this.ai = ai;
83
30
  this.tutorial = tutorial;
84
31
  }
85
- GameLocalAPI.prototype.init = function (dispatch) {
86
- var _this = this;
87
- var _a;
32
+ init(dispatch) {
88
33
  this.dispatch = dispatch;
89
- var animationsSpeed = parseFloat(localStorage.getItem('animationsSpeed') || '1');
90
- this.dispatch((0, Actions_1.setAnimationsSpeed)(animationsSpeed));
91
- var data = this.initLocalStore();
92
- var visibleActions = data.actions.filter(function (action) { return !(0, rules_api_1.isSecretAction)(action) || !action.secrets.some(function (secret) { return secret.recipient === data.playerId; }); });
93
- var notifications = this.getNotifications(data);
34
+ const animationsSpeed = parseFloat(localStorage.getItem('animationsSpeed') || '1');
35
+ this.dispatch(setAnimationsSpeed(animationsSpeed));
36
+ const data = this.initLocalStore();
37
+ const visibleActions = data.actions.filter(action => !isSecretAction(action) || !action.secrets.some(secret => secret.recipient === data.playerId));
38
+ const notifications = this.getNotifications(data);
94
39
  this.notificationsCount = notifications.length;
95
- var dataToTransfer = {
96
- myPlayerId: data.playerId, setup: data.setup, state: data.state,
97
- notifications: notifications,
98
- players: data.players, options: (_a = data.options) !== null && _a !== void 0 ? _a : { speed: Types_1.GameSpeed.Disabled, maxSpareTime: 0, maxExceedTime: 0 },
99
- mode: data.tutorial ? Types_1.GameMode.TUTORIAL : Types_1.GameMode.FRIENDLY, notificationsChannel: '', endDate: null, tournament: null
40
+ const dataToTransfer = {
41
+ myPlayerId: data.playerId, setup: data.setup, state: data.state, notifications,
42
+ players: data.players, options: data.options ?? { speed: GameSpeed.Disabled, maxSpareTime: 0, maxExceedTime: 0 },
43
+ mode: data.tutorial ? GameMode.TUTORIAL : GameMode.FRIENDLY, notificationsChannel: '', endDate: null, tournament: null
100
44
  };
101
- var initialGame = new this.Rules(JSON.parse(JSON.stringify(data.setup)));
102
- var rules = new this.Rules(data.state);
103
- var isOver = rules.isOver(data.players.map(function (p) { return p.id; }));
45
+ const initialGame = new this.Rules(JSON.parse(JSON.stringify(data.setup)));
46
+ const rules = new this.Rules(data.state);
47
+ const isOver = rules.isOver(data.players.map(p => p.id));
104
48
  if (isOver) {
105
49
  dataToTransfer.endDate = new Date();
106
50
  }
107
- if ((0, rules_api_1.hasHiddenInformation)(initialGame) && !isOver) {
108
- dataToTransfer.setup = verifyNonMutation(function () { return (0, rules_api_1.getGameView)(initialGame, data.playerId); }, function () { return initialGame.game; }, 'getView must not change the game state');
51
+ if (hasHiddenInformation(initialGame) && !isOver) {
52
+ dataToTransfer.setup = verifyNonMutation(() => getGameView(initialGame, data.playerId), () => initialGame.game, 'getView must not change the game state');
109
53
  if (visibleActions.length < data.actions.length) {
110
54
  dataToTransfer.setup = JSON.parse(JSON.stringify(dataToTransfer.setup));
111
- (0, rules_api_1.replayActions)(initialGame, visibleActions);
112
- dataToTransfer.state = verifyNonMutation(function () { return (0, rules_api_1.getGameView)(initialGame, data.playerId); }, function () { return initialGame.game; }, 'getView must not change the game state');
55
+ replayActions(initialGame, visibleActions);
56
+ dataToTransfer.state = verifyNonMutation(() => getGameView(initialGame, data.playerId), () => initialGame.game, 'getView must not change the game state');
113
57
  }
114
58
  else {
115
- if ((0, rules_api_1.hasHiddenInformation)(rules)) {
116
- dataToTransfer.state = verifyNonMutation(function () { return (0, rules_api_1.getGameView)(rules, data.playerId); }, function () { return rules.game; }, 'getView must not change the game state');
59
+ if (hasHiddenInformation(rules)) {
60
+ dataToTransfer.state = verifyNonMutation(() => getGameView(rules, data.playerId), () => rules.game, 'getView must not change the game state');
117
61
  }
118
62
  }
119
63
  }
120
- mimicDataTransfer(dataToTransfer).then(function (gameData) { return _this.dispatch((0, Actions_1.gameStateLoaded)(gameData)); });
121
- };
122
- GameLocalAPI.prototype.initLocalStore = function () {
123
- var _a;
124
- var localStore = localStorage.getItem(this.storage);
64
+ mimicDataTransfer(dataToTransfer).then(gameData => this.dispatch(loadGameState({
65
+ ...(gameData), playerId: gameData.myPlayerId ?? undefined, serverTime: undefined, endDate: gameData.endDate ?? undefined,
66
+ tournament: gameData.tournament ?? undefined
67
+ })));
68
+ }
69
+ initLocalStore() {
70
+ const localStore = localStorage.getItem(this.storage);
125
71
  if (localStore) {
126
- var data = JSON.parse(localStore);
72
+ const data = JSON.parse(localStore);
127
73
  if (process.env.NODE_ENV !== 'development' && this.tutorial && !data.tutorial) {
128
74
  return this.newTutorial();
129
75
  }
130
- if (((_a = this.tutorial) === null || _a === void 0 ? void 0 : _a.version) && data.tutorial && data.tutorial !== this.tutorial.version) {
76
+ if (this.tutorial?.version && data.tutorial && data.tutorial !== this.tutorial.version) {
131
77
  return this.newTutorial();
132
78
  }
133
79
  if (data.actions.length > 0 && !data.actions[0].id) {
134
- data.actions.forEach(function (action, index) {
80
+ data.actions.forEach((action, index) => {
135
81
  if (!action.id)
136
82
  action.id = index.toString();
137
83
  });
138
84
  localStorage.setItem(this.storage, JSON.stringify(data));
139
85
  }
140
86
  if (data.actions.length > 0) {
141
- var lastActionId = parseInt(data.actions[data.actions.length - 1].id);
87
+ const lastActionId = parseInt(data.actions[data.actions.length - 1].id);
142
88
  if (!isNaN(lastActionId)) {
143
89
  this.lastActionId = lastActionId;
144
90
  }
@@ -148,38 +94,30 @@ var GameLocalAPI = (function () {
148
94
  else {
149
95
  return this.tutorial ? this.newTutorial() : this.newGame();
150
96
  }
151
- };
152
- GameLocalAPI.prototype.getNotifications = function (data) {
153
- var rules = new this.Rules(JSON.parse(JSON.stringify(data.setup)));
154
- var actions = data.actions.filter(function (action) { return !(0, rules_api_1.isSecretAction)(action) || !action.secrets.some(function (secret) { return secret.recipient === data.playerId; }); });
155
- if ((0, rules_api_1.hasHiddenInformation)(rules) && !new this.Rules(data.state).isOver(data.players.map(function (p) { return p.id; }))) {
156
- var clientGame = new this.ClientRules(JSON.parse(JSON.stringify((0, rules_api_1.getGameView)(rules, data.playerId))), { player: data.playerId });
157
- var notifications = [];
158
- var _loop_1 = function (action) {
159
- var moveView = verifyNonMutation(function () { return (0, rules_api_1.getMoveView)(rules, JSON.parse(JSON.stringify(action.move)), data.playerId); }, function () { return rules.game; }, 'getMoveView must not change the state of the game');
97
+ }
98
+ getNotifications(data) {
99
+ const rules = new this.Rules(JSON.parse(JSON.stringify(data.setup)));
100
+ const actions = data.actions.filter(action => !isSecretAction(action) || !action.secrets.some(secret => secret.recipient === data.playerId));
101
+ if (hasHiddenInformation(rules) && !new this.Rules(data.state).isOver(data.players.map(p => p.id))) {
102
+ const clientGame = new this.ClientRules(JSON.parse(JSON.stringify(getGameView(rules, data.playerId))), { player: data.playerId });
103
+ const notifications = [];
104
+ for (const action of actions) {
105
+ const moveView = verifyNonMutation(() => getMoveView(rules, JSON.parse(JSON.stringify(action.move)), data.playerId), () => rules.game, 'getMoveView must not change the state of the game');
160
106
  clientGame.play(JSON.parse(JSON.stringify(moveView)));
161
107
  rules.play(JSON.parse(JSON.stringify(action.move)));
162
- var consequences = [];
163
- var _loop_2 = function (move) {
164
- var moveView_1 = verifyNonMutation(function () { return (0, rules_api_1.getMoveView)(rules, JSON.parse(JSON.stringify(move)), data.playerId); }, function () { return rules.game; }, 'getMoveView must not change the state of the game');
165
- consequences.push(moveView_1);
166
- clientGame.play(JSON.parse(JSON.stringify(moveView_1)));
108
+ const consequences = [];
109
+ for (const move of action.consequences) {
110
+ const moveView = verifyNonMutation(() => getMoveView(rules, JSON.parse(JSON.stringify(move)), data.playerId), () => rules.game, 'getMoveView must not change the state of the game');
111
+ consequences.push(moveView);
112
+ clientGame.play(JSON.parse(JSON.stringify(moveView)));
167
113
  rules.play(JSON.parse(JSON.stringify(move)));
168
- };
169
- for (var _a = 0, _b = action.consequences; _a < _b.length; _a++) {
170
- var move = _b[_a];
171
- _loop_2(move);
172
114
  }
173
- notifications.push((0, Types_1.movePlayedNotification)(__assign(__assign({}, action), { move: moveView, consequences: consequences })));
174
- };
175
- for (var _i = 0, actions_1 = actions; _i < actions_1.length; _i++) {
176
- var action = actions_1[_i];
177
- _loop_1(action);
115
+ notifications.push(movePlayedNotification({ ...action, move: moveView, consequences }));
178
116
  }
179
- var serverGameView = JSON.parse(JSON.stringify((0, rules_api_1.getGameView)(rules, data.playerId)), function (_, value) { return sortObjectKeys(value); });
180
- var clientGameView = JSON.parse(JSON.stringify(clientGame.game), function (_, value) { return sortObjectKeys(value); });
117
+ const serverGameView = JSON.parse(JSON.stringify(getGameView(rules, data.playerId)), (_, value) => sortObjectKeys(value));
118
+ const clientGameView = JSON.parse(JSON.stringify(clientGame.game), (_, value) => sortObjectKeys(value));
181
119
  try {
182
- assert_1.default.deepStrictEqual(serverGameView, clientGameView);
120
+ assert.deepStrictEqual(serverGameView, clientGameView);
183
121
  }
184
122
  catch (e) {
185
123
  console.error('Something is wrong with rules.getView or rules.getMoveView implementation: the view is not the same when moveViews are applied');
@@ -190,351 +128,314 @@ var GameLocalAPI = (function () {
190
128
  return notifications;
191
129
  }
192
130
  else {
193
- return actions.map(function (action) { return (0, Types_1.movePlayedNotification)(action); });
131
+ return actions.map(action => movePlayedNotification(action));
194
132
  }
195
- };
196
- GameLocalAPI.prototype.newTutorial = function () {
197
- var _a;
133
+ }
134
+ newTutorial() {
198
135
  if (!this.tutorial) {
199
136
  throw new Error('Pass a Tutorial in the options of createGameStore first');
200
137
  }
201
- var _b = this.tutorial.setupTutorial(), state = _b[0], players = _b[1];
202
- return this.initGameStore(state, players, (_a = this.tutorial.version) !== null && _a !== void 0 ? _a : 1);
203
- };
204
- GameLocalAPI.prototype.getGameOptions = function (arg) {
205
- if (arg === void 0) { arg = 2; }
206
- var options = typeof arg === 'object' ? arg : { players: typeof arg === 'number' ? arg : 2 };
138
+ const [state, players] = this.tutorial.setupTutorial();
139
+ return this.initGameStore(state, players, this.tutorial.version ?? 1);
140
+ }
141
+ getGameOptions(arg = 2) {
142
+ const options = typeof arg === 'object' ? arg : { players: typeof arg === 'number' ? arg : 2 };
207
143
  if (typeof options.players !== 'number' && !Array.isArray(options.players))
208
144
  options.players = 2;
209
- if (this.optionsSpec && (0, rules_api_1.isWithPlayerOptions)(this.optionsSpec)) {
145
+ if (this.optionsSpec && isWithPlayerOptions(this.optionsSpec)) {
210
146
  if (typeof options.players === 'number') {
211
- options.players = __spreadArray([], Array(options.players), true).map(function () { return ({}); });
147
+ options.players = [...Array(options.players)].map(() => ({}));
212
148
  }
213
149
  else {
214
- for (var i = 0; i < options.players.length; i++) {
150
+ for (let i = 0; i < options.players.length; i++) {
215
151
  if (typeof options.players[i] !== 'object')
216
152
  options.players[i] = {};
217
153
  }
218
154
  }
219
- var _loop_3 = function (option) {
220
- var playerOptionValue = this_1.optionsSpec.players[option];
221
- var optionValues = (0, rules_api_1.generatePlayersOption)(options.players.map(function (p) { var _a; return (_a = p[option]) !== null && _a !== void 0 ? _a : null; }), playerOptionValue);
222
- for (var i = 0; i < options.players.length; i++) {
155
+ for (const option in this.optionsSpec.players) {
156
+ const playerOptionValue = this.optionsSpec.players[option];
157
+ const optionValues = generatePlayersOption(options.players.map((p) => p[option] ?? null), playerOptionValue);
158
+ for (let i = 0; i < options.players.length; i++) {
223
159
  options.players[i][option] = optionValues[i];
224
160
  }
225
- };
226
- var this_1 = this;
227
- for (var option in this.optionsSpec.players) {
228
- _loop_3(option);
229
161
  }
230
162
  }
231
163
  return options;
232
- };
233
- GameLocalAPI.prototype.newGame = function (arg) {
234
- if (arg === void 0) { arg = 2; }
235
- var options = this.getGameOptions(arg);
236
- var playerIds = this.optionsSpec && (0, rules_api_1.isWithPlayerIdOptions)(this.optionsSpec) ?
237
- options.players.map(function (player) { return player.id; })
238
- : (Array.isArray(options.players) ? options.players : __spreadArray([], Array(options.players), true)).map(function (_, index) { return index + 1; });
239
- var players = playerIds.map(function (id) { return ({ id: id }); });
164
+ }
165
+ newGame(arg = 2) {
166
+ const options = this.getGameOptions(arg);
167
+ const playerIds = this.optionsSpec && isWithPlayerIdOptions(this.optionsSpec) ?
168
+ options.players.map((player) => player.id)
169
+ : (Array.isArray(options.players) ? options.players : [...Array(options.players)]).map((_, index) => index + 1);
170
+ const players = playerIds.map(id => ({ id }));
240
171
  return this.initGameStore(this.gameSetup.setup(options), players);
241
- };
242
- GameLocalAPI.prototype.initGameStore = function (game, players, tutorial) {
243
- var rules = new this.Rules(game);
244
- var playerId = players[0].id;
245
- (0, rules_api_1.applyAutomaticMoves)(rules);
246
- var setup = JSON.parse(JSON.stringify(rules.game));
247
- var localStore = { players: players, setup: setup, state: rules.game, actions: [], playerId: playerId, monkeyOpponents: !!tutorial, tutorial: tutorial };
172
+ }
173
+ initGameStore(game, players, tutorial) {
174
+ const rules = new this.Rules(game);
175
+ const playerId = players[0].id;
176
+ applyAutomaticMoves(rules);
177
+ const setup = JSON.parse(JSON.stringify(rules.game));
178
+ const localStore = { players, setup, state: rules.game, actions: [], playerId, monkeyOpponents: !!tutorial, tutorial };
248
179
  localStorage.setItem(this.storage, JSON.stringify(localStore));
249
180
  return localStore;
250
- };
251
- GameLocalAPI.prototype.getLocalStore = function () {
181
+ }
182
+ getLocalStore() {
252
183
  return JSON.parse(localStorage.getItem(this.storage));
253
- };
254
- GameLocalAPI.prototype.play = function (move) {
255
- var _this = this;
256
- return mimicDataTransfer(move).then(function (move) {
257
- var _a;
258
- var localStore = _this.getLocalStore();
259
- var rules = new _this.Rules(localStore.state);
260
- if (!verifyNonMutation(function () { return rules.isLegalMove(localStore.playerId, move); }, function () { return rules.game; }, 'isLegalMove and getLegalMoves must not change the game state')) {
184
+ }
185
+ play(move) {
186
+ return mimicDataTransfer(move).then(move => {
187
+ const localStore = this.getLocalStore();
188
+ const rules = new this.Rules(localStore.state);
189
+ if (!verifyNonMutation(() => rules.isLegalMove(localStore.playerId, move), () => rules.game, 'isLegalMove and getLegalMoves must not change the game state')) {
261
190
  console.error('This move is not authorized right now: ' + JSON.stringify(move));
262
- if (_this.tutorial) {
263
- (_a = _this.dispatch) === null || _a === void 0 ? void 0 : _a.call(_this, (0, Actions_1.actionFailed)(Failure_1.Failure.TUTORIAL_MOVE_EXPECTED));
191
+ if (this.tutorial) {
192
+ this.dispatch?.(reportFailure(Failure.TUTORIAL_MOVE_EXPECTED));
264
193
  }
265
194
  if (rules.getLegalMoves) {
266
195
  console.info('Legal moves are:', rules.getLegalMoves(localStore.playerId));
267
196
  }
268
197
  return;
269
198
  }
270
- _this.autoPlayersCounter = 0;
271
- _this.playActions(localStore.playerId, [move]);
272
- }).catch(function (error) {
199
+ this.autoPlayersCounter = 0;
200
+ this.playActions(localStore.playerId, [move]);
201
+ }).catch(error => {
273
202
  console.error('The move could not be applied due to this error:', error);
274
203
  throw error;
275
204
  });
276
- };
277
- GameLocalAPI.prototype.playActions = function (playerId, moves) {
278
- var _a;
279
- var _this = this;
280
- var _b, _c;
281
- var localStore = this.getLocalStore();
282
- var game = new this.Rules(localStore.state);
283
- var actions = [];
284
- var notifications = [];
285
- if ((0, rules_api_1.hasHiddenInformation)(game)) {
286
- for (var _i = 0, moves_1 = moves; _i < moves_1.length; _i++) {
287
- var move = moves_1[_i];
288
- var recipients = (0, rules_api_1.hasSecretInformation)(game) && game.getPlayerMoveView ? localStore.players.map(function (player) { return player.id; }) : [];
289
- var id = (++this.lastActionId).toString();
290
- var _d = (0, rules_api_1.playActionWithViews)(game, move, playerId, recipients, id), action = _d.action, views = _d.views;
291
- if ((0, rules_api_1.hasSecretInformation)(game) && game.keepMoveSecret) {
292
- var _loop_4 = function (secretAction) {
293
- var secretToPlayer = secretAction.secrets.find(function (secret) { return secret.recipient === localStore.playerId; });
205
+ }
206
+ playActions(playerId, moves) {
207
+ const localStore = this.getLocalStore();
208
+ const game = new this.Rules(localStore.state);
209
+ const actions = [];
210
+ const notifications = [];
211
+ if (hasHiddenInformation(game)) {
212
+ for (const move of moves) {
213
+ const recipients = hasSecretInformation(game) && game.getPlayerMoveView ? localStore.players.map(player => player.id) : [];
214
+ const id = (++this.lastActionId).toString();
215
+ const { action, views } = playActionWithViews(game, move, playerId, recipients, id);
216
+ if (hasSecretInformation(game) && game.keepMoveSecret) {
217
+ for (const secretAction of localStore.actions.filter(isSecretAction)) {
218
+ const secretToPlayer = secretAction.secrets.find(secret => secret.recipient === localStore.playerId);
294
219
  if (secretToPlayer && !game.keepMoveSecret(secretAction.move, localStore.playerId)) {
295
- notifications.push((0, Types_1.movePlayedNotification)(secretToPlayer.action));
220
+ notifications.push(movePlayedNotification(secretToPlayer.action));
296
221
  }
297
- secretAction.secrets = secretAction.secrets.filter(function (secret) { return secret.recipient === undefined || game.keepMoveSecret(secretAction.move, secret.recipient); });
222
+ secretAction.secrets = secretAction.secrets.filter(secret => secret.recipient === undefined || game.keepMoveSecret(secretAction.move, secret.recipient));
298
223
  if (secretAction.secrets.length === 1) {
299
224
  delete secretAction.secrets;
300
225
  }
301
- };
302
- for (var _e = 0, _f = localStore.actions.filter(rules_api_1.isSecretAction); _e < _f.length; _e++) {
303
- var secretAction = _f[_e];
304
- _loop_4(secretAction);
305
226
  }
306
- var secrets = [];
307
- var _loop_5 = function (player) {
308
- var view = (_b = views.find(function (view) { return view.recipient === player.id; })) !== null && _b !== void 0 ? _b : views.find(function (view) { return !view.recipient; });
227
+ const secrets = [];
228
+ for (const player of localStore.players) {
229
+ const view = views.find(view => view.recipient === player.id) ?? views.find(view => !view.recipient);
309
230
  if (player.id !== playerId && game.keepMoveSecret(move, player.id)) {
310
231
  secrets.push({ recipient: player.id, action: view.action });
311
232
  }
312
233
  else if (player.id === localStore.playerId) {
313
- notifications.push((0, Types_1.movePlayedNotification)(view.action));
234
+ notifications.push(movePlayedNotification(view.action));
314
235
  }
315
- };
316
- for (var _g = 0, _h = localStore.players; _g < _h.length; _g++) {
317
- var player = _h[_g];
318
- _loop_5(player);
319
236
  }
320
- var spectatorView = views.find(function (view) { return view.recipient === undefined; });
237
+ const spectatorView = views.find(view => view.recipient === undefined);
321
238
  if (secrets.length) {
322
239
  action.secrets = secrets;
323
240
  secrets.push(spectatorView);
324
241
  }
325
242
  else if (localStore.playerId === undefined) {
326
- notifications.push((0, Types_1.movePlayedNotification)(spectatorView.action));
243
+ notifications.push(movePlayedNotification(spectatorView.action));
327
244
  }
328
245
  }
329
246
  else {
330
- var view = (_c = views.find(function (view) { return view.recipient === localStore.playerId; })) !== null && _c !== void 0 ? _c : views.find(function (view) { return !view.recipient; });
331
- notifications.push((0, Types_1.movePlayedNotification)(view.action));
247
+ const view = views.find(view => view.recipient === localStore.playerId) ?? views.find(view => !view.recipient);
248
+ notifications.push(movePlayedNotification(view.action));
332
249
  }
333
250
  actions.push(action);
334
251
  }
335
252
  }
336
253
  else {
337
- for (var _j = 0, moves_2 = moves; _j < moves_2.length; _j++) {
338
- var move = moves_2[_j];
339
- var id = (++this.lastActionId).toString();
340
- var action = (0, rules_api_1.playAction)(game, move, playerId);
254
+ for (const move of moves) {
255
+ const id = (++this.lastActionId).toString();
256
+ const action = playAction(game, move, playerId);
341
257
  action.id = id;
342
258
  actions.push(action);
343
- notifications.push((0, Types_1.movePlayedNotification)(action));
259
+ notifications.push(movePlayedNotification(action));
344
260
  }
345
261
  }
346
- var index = this.incrementNotificationsCount(notifications.length);
347
- mimicDataTransfer({ notifications: notifications, index: index }, { sizeWarn: 9000 }).then(function (data) {
348
- return _this.dispatch((0, Actions_1.gameNotificationsReceived)(data.notifications, data.index, localStore.players));
349
- });
350
- (_a = localStore.actions).push.apply(_a, actions);
262
+ const index = this.incrementNotificationsCount(notifications.length);
263
+ mimicDataTransfer({ notifications, index }, { sizeWarn: 9000 }).then(data => this.dispatch(receiveNotifications({
264
+ notifications: data.notifications,
265
+ notificationsIndex: data.index,
266
+ players: localStore.players,
267
+ serverTime: undefined
268
+ })));
269
+ localStore.actions.push(...actions);
351
270
  localStorage.setItem(this.storage, JSON.stringify(localStore));
352
- setTimeout(function () { return _this.runAutoPlayers(); }, 100 + Math.random() * 100);
353
- };
354
- GameLocalAPI.prototype.incrementNotificationsCount = function (quantity) {
355
- if (quantity === void 0) { quantity = 1; }
356
- var previousValue = this.notificationsCount;
271
+ setTimeout(() => this.runAutoPlayers(), 100 + Math.random() * 100);
272
+ }
273
+ incrementNotificationsCount(quantity = 1) {
274
+ const previousValue = this.notificationsCount;
357
275
  this.notificationsCount += quantity;
358
276
  return previousValue;
359
- };
360
- GameLocalAPI.prototype.undo = function (arg, ignoreRules) {
361
- var _this = this;
362
- if (arg === void 0) { arg = 1; }
277
+ }
278
+ undo(arg = 1, ignoreRules) {
363
279
  if (typeof arg === 'string') {
364
- mimicDataTransfer(arg).then(function (actionId) {
365
- var localStore = _this.getLocalStore();
366
- var index = (0, ArrayUtil_1.findLastIndex)(localStore.actions, function (action) { return action.id === actionId; });
280
+ mimicDataTransfer(arg).then(actionId => {
281
+ const localStore = this.getLocalStore();
282
+ const index = findLastIndex(localStore.actions, action => action.id === actionId);
367
283
  if (index === -1)
368
284
  return;
369
- var action = localStore.actions[index];
285
+ const action = localStore.actions[index];
370
286
  if (!ignoreRules) {
371
- var game = new _this.Rules(localStore.state);
372
- if (!(0, rules_api_1.hasUndo)(game))
287
+ const game = new this.Rules(localStore.state);
288
+ if (!hasUndo(game))
373
289
  return console.error('This game does not have UNDO feature');
374
290
  if (!game.canUndo(action, localStore.actions.slice(index + 1)))
375
291
  return console.error('Trying to UNDO a move while it is forbidden');
376
292
  }
377
293
  localStore.actions.splice(index, 1);
378
- var rules = new _this.Rules(JSON.parse(JSON.stringify(localStore.setup)));
379
- (0, rules_api_1.replayActions)(rules, localStore.actions);
294
+ const rules = new this.Rules(JSON.parse(JSON.stringify(localStore.setup)));
295
+ replayActions(rules, localStore.actions);
380
296
  localStore.state = rules.game;
381
- localStorage.setItem(_this.storage, JSON.stringify(localStore));
382
- if (!(0, rules_api_1.isSecretAction)(action) || !action.secrets.some(function (secret) { return secret.recipient === localStore.playerId; })) {
383
- mimicDataTransfer({ notification: (0, Types_1.moveUndoneNotification)(action, index), index: _this.incrementNotificationsCount() }).then(function (data) {
384
- return _this.dispatch((0, Actions_1.gameNotificationsReceived)([data.notification], data.index, localStore.players));
385
- });
297
+ localStorage.setItem(this.storage, JSON.stringify(localStore));
298
+ if (!isSecretAction(action) || !action.secrets.some(secret => secret.recipient === localStore.playerId)) {
299
+ mimicDataTransfer({ notification: moveUndoneNotification(action, index), index: this.incrementNotificationsCount() }).then(data => this.dispatch(receiveNotifications({
300
+ notifications: [data.notification],
301
+ notificationsIndex: data.index,
302
+ players: localStore.players,
303
+ serverTime: undefined
304
+ })));
386
305
  }
387
306
  });
388
307
  }
389
308
  else if (arg > 0) {
390
- var actions = this.getLocalStore().actions;
309
+ const actions = this.getLocalStore().actions;
391
310
  if (!actions.length)
392
311
  return console.warn('Cannot undo, no actions were played yet');
393
- for (var i = actions.length - 1; i > actions.length - 1 - arg; i--) {
312
+ for (let i = actions.length - 1; i > actions.length - 1 - arg; i--) {
394
313
  this.undo(actions[i].id, true);
395
314
  }
396
315
  }
397
- };
398
- GameLocalAPI.prototype.startBots = function () {
399
- var localStore = this.getLocalStore();
316
+ }
317
+ startBots() {
318
+ const localStore = this.getLocalStore();
400
319
  localStore.monkeyOpponents = true;
401
320
  localStorage.setItem(this.storage, JSON.stringify(localStore));
402
321
  this.autoPlayersCounter = 0;
403
322
  this.runAutoPlayers();
404
- };
405
- GameLocalAPI.prototype.stopBots = function () {
406
- var localStore = this.getLocalStore();
323
+ }
324
+ stopBots() {
325
+ const localStore = this.getLocalStore();
407
326
  localStore.monkeyOpponents = false;
408
327
  localStorage.setItem(this.storage, JSON.stringify(localStore));
409
- };
410
- GameLocalAPI.prototype.runAutoPlayers = function () {
411
- return __awaiter(this, void 0, void 0, function () {
412
- var localStore, rules, nextExpectedMove, expectedMoves, expectedMove_1, player, player, moves;
413
- var _a;
414
- return __generator(this, function (_b) {
415
- switch (_b.label) {
416
- case 0:
417
- if (this.autoPlayersCounter > 100) {
418
- console.warn('The automatic players played 100 times. There might be an infinite loop, stopping now.');
419
- return [2];
420
- }
421
- localStore = this.getLocalStore();
422
- rules = new this.Rules(localStore.state);
423
- if (rules.isOver(localStore.players.map(function (player) { return player.id; }))) {
424
- console.info('Game is over');
425
- (_a = this.dispatch) === null || _a === void 0 ? void 0 : _a.call(this, (0, Actions_1.gameOver)([]));
426
- return [2];
427
- }
428
- if (localStore.tutorial && this.tutorial) {
429
- if (!this.tutorialMovesToPlay)
430
- return [2];
431
- nextExpectedMove = this.tutorial.getNextMove(rules, localStore.actions.length);
432
- if (nextExpectedMove) {
433
- expectedMoves = Array.isArray(nextExpectedMove) ? nextExpectedMove : [nextExpectedMove];
434
- if (expectedMoves.some(function (expectedMove) { return rules.isLegalMove(localStore.playerId, expectedMove); })) {
435
- if (this.tutorialMovesToPlay !== Infinity) {
436
- this.tutorialMovesToPlay = 0;
437
- }
438
- return [2];
439
- }
440
- expectedMove_1 = expectedMoves[0];
441
- player = localStore.players.find(function (player) { return rules.isLegalMove(player.id, expectedMove_1); });
442
- if (player) {
443
- this.tutorialMovesToPlay--;
444
- return [2, this.playActions(player.id, [expectedMove_1])];
445
- }
446
- else {
447
- console.error('Tutorial expects a move that no player can play: ', expectedMove_1);
448
- }
449
- }
450
- }
451
- if (!(localStore.monkeyOpponents && !this.aiProcessing)) return [3, 3];
452
- player = localStore.players.find(function (player) { return player.id !== localStore.playerId && rules.isTurnToPlay(player.id); });
453
- if (!player) return [3, 2];
454
- this.aiProcessing = true;
455
- return [4, this.getAI(player.id)(rules.game, player.id)];
456
- case 1:
457
- moves = _b.sent();
458
- this.aiProcessing = false;
459
- if (moves.length) {
460
- this.playActions(player.id, moves);
461
- this.autoPlayersCounter++;
462
- }
463
- else {
464
- console.error("Player ".concat(player.id, " is active but did not return any move to play!"));
465
- }
466
- return [3, 3];
467
- case 2:
468
- if (localStore.playerId === undefined || !rules.isTurnToPlay(localStore.playerId)) {
469
- console.error('Game is not over but there is no active player!');
470
- }
471
- _b.label = 3;
472
- case 3: return [2];
328
+ }
329
+ async runAutoPlayers() {
330
+ if (this.autoPlayersCounter > 100) {
331
+ console.warn('The automatic players played 100 times. There might be an infinite loop, stopping now.');
332
+ return;
333
+ }
334
+ const localStore = this.getLocalStore();
335
+ const rules = new this.Rules(localStore.state);
336
+ if (rules.isOver(localStore.players.map(player => player.id))) {
337
+ console.info('Game is over');
338
+ this.dispatch?.(setGameOver([]));
339
+ return;
340
+ }
341
+ if (localStore.tutorial && this.tutorial) {
342
+ if (!this.tutorialMovesToPlay)
343
+ return;
344
+ const nextExpectedMove = this.tutorial.getNextMove(rules, localStore.actions.length);
345
+ if (nextExpectedMove) {
346
+ const expectedMoves = Array.isArray(nextExpectedMove) ? nextExpectedMove : [nextExpectedMove];
347
+ if (expectedMoves.some(expectedMove => rules.isLegalMove(localStore.playerId, expectedMove))) {
348
+ if (this.tutorialMovesToPlay !== Infinity) {
349
+ this.tutorialMovesToPlay = 0;
350
+ }
351
+ return;
473
352
  }
474
- });
475
- });
476
- };
477
- GameLocalAPI.prototype.getAI = function (playerId) {
353
+ const expectedMove = expectedMoves[0];
354
+ const player = localStore.players.find(player => rules.isLegalMove(player.id, expectedMove));
355
+ if (player) {
356
+ this.tutorialMovesToPlay--;
357
+ return this.playActions(player.id, [expectedMove]);
358
+ }
359
+ else {
360
+ console.error('Tutorial expects a move that no player can play: ', expectedMove);
361
+ }
362
+ }
363
+ }
364
+ if (localStore.monkeyOpponents && !this.aiProcessing) {
365
+ const player = localStore.players.find(player => player.id !== localStore.playerId && rules.isTurnToPlay(player.id));
366
+ if (player) {
367
+ this.aiProcessing = true;
368
+ const moves = await this.getAI(player.id)(rules.game, player.id);
369
+ this.aiProcessing = false;
370
+ if (moves.length) {
371
+ this.playActions(player.id, moves);
372
+ this.autoPlayersCounter++;
373
+ }
374
+ else {
375
+ console.error(`Player ${player.id} is active but did not return any move to play!`);
376
+ }
377
+ }
378
+ else if (localStore.playerId === undefined || !rules.isTurnToPlay(localStore.playerId)) {
379
+ console.error('Game is not over but there is no active player!');
380
+ }
381
+ }
382
+ }
383
+ getAI(playerId) {
478
384
  if (this.ai)
479
385
  return this.ai;
480
- var bot = new rules_api_1.RandomBot(this.Rules, playerId);
481
- return function (game) { return Promise.resolve(bot.run(game)); };
482
- };
483
- GameLocalAPI.prototype.setPlayerName = function (name, playerId) {
484
- var localStore = this.getLocalStore();
485
- var player = localStore.players.find(function (player) { return player.id === (playerId || localStore.playerId); });
386
+ const bot = new RandomBot(this.Rules, playerId);
387
+ return (game) => Promise.resolve(bot.run(game));
388
+ }
389
+ setPlayerName(name, playerId) {
390
+ const localStore = this.getLocalStore();
391
+ const player = localStore.players.find(player => player.id === (playerId || localStore.playerId));
486
392
  if (!player) {
487
393
  throw new Error('Player not found');
488
394
  }
489
395
  player.name = name;
490
396
  localStorage.setItem(this.storage, JSON.stringify(localStore));
491
- };
492
- GameLocalAPI.prototype.setAnimationsSpeed = function (speed) {
493
- if (speed === void 0) { speed = 1; }
494
- this.dispatch((0, Actions_1.setAnimationsSpeed)(speed));
397
+ }
398
+ setAnimationsSpeed(speed = 1) {
399
+ this.dispatch(setAnimationsSpeed(speed));
495
400
  localStorage.setItem('animationsSpeed', speed.toString());
496
- };
497
- GameLocalAPI.prototype.eject = function () {
498
- };
499
- GameLocalAPI.prototype.giveUp = function () {
500
- };
501
- GameLocalAPI.prototype.playTutorialMoves = function (quantity) {
502
- var _this = this;
401
+ }
402
+ eject() {
403
+ }
404
+ giveUp() {
405
+ }
406
+ playTutorialMoves(quantity) {
503
407
  if (quantity) {
504
408
  this.tutorialMovesToPlay += quantity;
505
409
  this.autoPlayersCounter = 0;
506
- setTimeout(function () { return _this.runAutoPlayers(); }, 100 + Math.random() * 100);
410
+ setTimeout(() => this.runAutoPlayers(), 100 + Math.random() * 100);
507
411
  }
508
412
  else {
509
413
  this.tutorialMovesToPlay = 0;
510
414
  }
511
- };
512
- return GameLocalAPI;
513
- }());
514
- exports.GameLocalAPI = GameLocalAPI;
515
- function mimicDataTransfer(data, options) {
516
- if (options === void 0) { options = {}; }
517
- var _a = options.sizeWarn, sizeWarn = _a === void 0 ? 0 : _a, _b = options.duration, duration = _b === void 0 ? 100 : _b;
518
- var stringData = JSON.stringify(data !== null && data !== void 0 ? data : null);
415
+ }
416
+ }
417
+ function mimicDataTransfer(data, options = {}) {
418
+ const { sizeWarn = 0, duration = 100 } = options;
419
+ const stringData = JSON.stringify(data ?? null);
519
420
  data = JSON.parse(stringData);
520
421
  if (sizeWarn && stringData) {
521
422
  if (stringData.length > sizeWarn) {
522
- console.warn("Notification payload of ".concat(stringData.length, " bytes exceeds ").concat(sizeWarn, " bytes, which can cause extra http requests."), stringData);
423
+ console.warn(`Notification payload of ${stringData.length} bytes exceeds ${sizeWarn} bytes, which can cause extra http requests.`, stringData);
523
424
  }
524
425
  }
525
- return new Promise(function (resolve) { return setTimeout(function () { return resolve(data); }, duration); });
426
+ return new Promise(resolve => setTimeout(() => resolve(data), duration));
526
427
  }
527
428
  function verifyNonMutation(operation, getState, message) {
528
- var initialState = JSON.stringify(getState());
529
- var result = operation();
530
- var finalState = JSON.stringify(getState());
531
- (0, assert_1.default)(initialState === finalState, message);
429
+ const initialState = JSON.stringify(getState());
430
+ const result = operation();
431
+ const finalState = JSON.stringify(getState());
432
+ assert(initialState === finalState, message);
532
433
  return result;
533
434
  }
534
435
  function sortObjectKeys(value) {
535
436
  if (typeof value !== 'object' || value === null)
536
437
  return value;
537
- return Object.keys(value).sort().reduce(function (obj, key) {
438
+ return Object.keys(value).sort().reduce((obj, key) => {
538
439
  obj[key] = value[key];
539
440
  return obj;
540
441
  }, {});