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

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