@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,347 +1,278 @@
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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
- if (k2 === undefined) k2 = k;
15
- var desc = Object.getOwnPropertyDescriptor(m, k);
16
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
- desc = { enumerable: true, get: function() { return m[k]; } };
18
- }
19
- Object.defineProperty(o, k2, desc);
20
- }) : (function(o, m, k, k2) {
21
- if (k2 === undefined) k2 = k;
22
- o[k2] = m[k];
23
- }));
24
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
- Object.defineProperty(o, "default", { enumerable: true, value: v });
26
- }) : function(o, v) {
27
- o["default"] = v;
28
- });
29
- var __importStar = (this && this.__importStar) || (function () {
30
- var ownKeys = function(o) {
31
- ownKeys = Object.getOwnPropertyNames || function (o) {
32
- var ar = [];
33
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
34
- return ar;
35
- };
36
- return ownKeys(o);
37
- };
38
- return function (mod) {
39
- if (mod && mod.__esModule) return mod;
40
- var result = {};
41
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
42
- __setModuleDefault(result, mod);
43
- return result;
44
- };
45
- })();
46
- var __importDefault = (this && this.__importDefault) || function (mod) {
47
- return (mod && mod.__esModule) ? mod : { "default": mod };
48
- };
49
- Object.defineProperty(exports, "__esModule", { value: true });
50
- exports.gameReducer = gameReducer;
51
- exports.getGameAfterAnimations = getGameAfterAnimations;
52
- exports.replayActions = replayActions;
53
- exports.replayAction = replayAction;
54
- exports.isGameAction = isGameAction;
55
- var rules_api_1 = require("@gamepark/rules-api");
56
- var assert_1 = __importDefault(require("assert"));
57
- var es_toolkit_1 = require("es-toolkit");
58
- var compat_1 = require("es-toolkit/compat");
59
- var immer_1 = __importStar(require("immer"));
60
- var Actions_1 = require("../Actions");
61
- var PlayerQuit_1 = require("../Actions/PlayerQuit");
62
- var animations_1 = require("../animations");
63
- var Failure_1 = require("../Failure");
64
- var GiveUp_1 = require("../GiveUp");
65
- var Log_1 = require("../Log");
66
- var Sound_1 = require("../Sound");
67
- var Types_1 = require("../Types");
68
- var ArrayUtil_1 = require("../util/ArrayUtil");
69
- var DisplayedAction_1 = require("./DisplayedAction");
70
- function gameReducer(context) {
71
- var _a;
72
- var GameRules = context.Rules;
73
- var animations = (_a = context.animations) !== null && _a !== void 0 ? _a : new animations_1.Animations();
74
- var initialState = {
1
+ import { hasUndo, hasUnpredictableMoves, loopWithFuse } from '@gamepark/rules-api';
2
+ import { createReducer } from '@reduxjs/toolkit';
3
+ import assert from 'assert';
4
+ import { isEqual, merge } from 'es-toolkit';
5
+ import { uniqueId } from 'es-toolkit/compat';
6
+ import { castDraft } from 'immer';
7
+ import { clearFailures, giveUp, playMove, receiveNotifications, setAnimationsSpeed, setGameOver, undoMove } from '../Actions';
8
+ import { loadGameState } from '../Actions/loadGameState';
9
+ import { removePlayer } from '../Actions/removePlayer';
10
+ import { reportFailure } from '../Actions/reportFailure';
11
+ import { startAnimation } from '../Actions/startAnimation';
12
+ import { Animations, AnimationStep } from '../animations';
13
+ import { Failure } from '../Failure';
14
+ import { startLiveLog, stopLiveLog } from '../Log';
15
+ import { muteSounds, soundsMuted, unmuteSounds } from '../Sound';
16
+ import { isMovePlayedNotification, isMoveUndoneNotification } from '../Types';
17
+ import { findLastIndex, findLastItem } from '../util/ArrayUtil';
18
+ import { getAnimatedMove } from './DisplayedAction';
19
+ import { endAnimation } from './endAnimation';
20
+ export function gameReducer(context) {
21
+ const GameRules = context?.Rules;
22
+ const animations = context?.animations ?? new Animations();
23
+ const initialState = {
75
24
  players: [], pendingNotifications: [], nextNotificationIndex: 0, legalMoves: [], failures: [],
76
- animationIncrement: 0, animationsSpeed: 1, animationPaused: false,
77
- soundsMuted: (0, Sound_1.soundsMuted)(), clientTimeDelta: 0, liveLogStopped: false
25
+ animationIncrement: 0, animationsSpeed: 1,
26
+ soundsMuted: soundsMuted(), clientTimeDelta: 0, liveLogStopped: false
78
27
  };
79
- return function (state, event) {
80
- if (state === void 0) { state = initialState; }
81
- switch (event.type) {
82
- case Actions_1.GAME_STATE_LOADED:
83
- return (0, immer_1.default)(state, function (draft) {
84
- if (event.serverTime) {
85
- draft.clientTimeDelta = approximateTimeDelta(event.serverTime, draft.clientTimeDelta);
86
- }
87
- draft.players = event.players;
88
- draft.options = event.options;
89
- draft.playerId = event.playerId;
90
- draft.state = event.state;
91
- if (event.playerId !== undefined) {
92
- draft.legalMoves = (0, immer_1.castDraft)(new GameRules(event.state).getLegalMoves(event.playerId));
93
- }
94
- draft.setup = event.setup;
95
- draft.gameOver = !!event.endDate;
96
- draft.gameMode = event.mode;
97
- draft.tournament = event.tournament;
98
- draft.nextNotificationIndex = event.notifications.length;
99
- if (!event.endDate) {
100
- draft.client = { player: event.playerId };
101
- }
102
- setActionsFromPastNotifications(draft, event.notifications);
103
- });
104
- case Actions_1.GAME_NOTIFICATIONS_RECEIVED:
105
- return (0, immer_1.default)(state, function (draft) {
106
- if (event.serverTime) {
107
- draft.clientTimeDelta = approximateTimeDelta(event.serverTime, draft.clientTimeDelta);
108
- }
109
- draft.players.forEach(function (player) {
110
- var _a;
111
- var newPlayerTime = (_a = event.players.find(function (p) { return p.id === player.id; })) === null || _a === void 0 ? void 0 : _a.time;
112
- player.time = newPlayerTime || player.time;
113
- });
114
- if (event.notificationsIndex == 0 && !draft.actions) {
115
- setActionsFromPastNotifications(draft, event.notifications);
116
- }
117
- for (var i = Math.max(draft.nextNotificationIndex - event.notificationsIndex, 0); i < event.notifications.length; i++) {
118
- draft.pendingNotifications.push(__assign(__assign({}, event.notifications[i]), { index: i + event.notificationsIndex }));
119
- }
120
- draft.pendingNotifications.sort(function (n1, n2) { return n1.index - n2.index; });
121
- if (draft.state) {
122
- applyPendingNotifications(draft);
123
- }
124
- });
125
- case Actions_1.MOVE_PLAYED:
126
- return (0, immer_1.default)(state, function (draft) {
127
- var _a;
128
- if (!draft.state)
129
- throw new Error('Cannot play move before game state is loaded');
130
- if (!draft.actions)
131
- throw new Error('Cannot play move before game history is loaded');
132
- var transient = event.transient;
133
- var local = (_a = event.local) !== null && _a !== void 0 ? _a : transient;
134
- if (state.playerId === undefined && !local)
135
- throw new Error('Spectators cannot play');
136
- var action = {
137
- id: 'local-' + (0, compat_1.uniqueId)(),
138
- playerId: state.playerId,
139
- move: event.move,
140
- consequences: [],
141
- played: 0,
142
- pending: !local,
143
- local: local,
144
- transient: transient
145
- };
146
- var rules = getGameAfterAnimations(draft, GameRules);
147
- var isUnpredictable = function (move) { return (0, rules_api_1.hasUnpredictableMoves)(rules) && rules.isUnpredictableMove(move, action.playerId); };
148
- if (!local && (event.delayed || isUnpredictable(event.move))) {
149
- action.delayed = true;
150
- }
151
- else {
152
- action.consequences = [];
153
- var pendingMoves_1 = rules.play(JSON.parse(JSON.stringify(event.move)), { local: local, transient: transient });
154
- (0, rules_api_1.loopWithFuse)(function () {
155
- var move = pendingMoves_1.shift();
156
- if (!move || isUnpredictable(move))
157
- return false;
158
- action.consequences.push(move);
159
- pendingMoves_1.unshift.apply(pendingMoves_1, rules.play(JSON.parse(JSON.stringify(move)), { local: local, transient: transient }));
160
- return true;
161
- }, { errorFn: function () { return new Error("Infinite loop detected while applying move consequences: ".concat(JSON.stringify(action), ")")); } });
162
- if (event.skipAnimation) {
163
- playNextMove(action, draft);
164
- }
165
- else {
166
- animatePlayedAction(action, draft);
167
- }
168
- }
169
- if (!transient || action.animation) {
170
- draft.actions.push(action);
171
- }
172
- });
173
- case Actions_1.MOVE_UNDONE:
174
- return (0, immer_1.default)(state, function (draft) {
175
- if (!state.actions || !draft.actions || !state.setup || !state.state) {
176
- return console.error('Cannot undo before actions history is loaded');
177
- }
178
- var action = (0, ArrayUtil_1.findLastItem)(draft.actions, function (action) { return action.id === event.actionId; });
179
- if (!action)
180
- return console.error("Could not find action to undo with this id: ".concat(event.actionId));
181
- if (!action.local && state.playerId === undefined) {
182
- return console.error('Spectators cannot undo moves');
183
- }
184
- action.cancelled = true;
185
- if (!action.local) {
186
- action.cancelPending = true;
187
- }
188
- if (!action.local && (event.delayed || !new GameRules(state.state, state.client).isTurnToPlay(state.playerId))) {
189
- action.delayed = true;
190
- }
191
- else {
192
- animateUndoneAction(action, draft);
193
- }
194
- });
195
- case Actions_1.ACTION_FAILED:
196
- return (0, immer_1.default)(state, function (draft) {
197
- if (draft.actions) {
198
- switch (event.reason) {
199
- case Failure_1.Failure.UNDO_FORBIDDEN:
200
- for (var _i = 0, _a = draft.actions; _i < _a.length; _i++) {
201
- var action = _a[_i];
202
- if (action.cancelPending) {
203
- action.cancelled = false;
204
- action.cancelPending = false;
205
- }
206
- }
207
- break;
208
- default:
209
- draft.actions = draft.actions.filter(function (action) { return !action.pending; });
210
- var rules = new GameRules(JSON.parse(JSON.stringify(draft.setup)), state.client);
211
- replayActions(rules, draft.actions, state.state);
212
- draft.state = rules.game;
213
- }
214
- }
215
- draft.failures.push(event.reason);
216
- });
217
- case Actions_1.CLEAR_FAILURES:
218
- return (0, immer_1.default)(state, function (draft) {
219
- draft.failures = [];
220
- });
221
- case Actions_1.ANIMATION_START:
222
- return (0, immer_1.default)(state, function (draft) {
223
- if (!draft.actions)
224
- return;
225
- var action = draft.actions.find(function (action) { return action.id === event.actionId; });
226
- if (action) {
227
- animateAction(action, draft);
228
- }
229
- });
230
- case Actions_1.ANIMATION_COMPLETE:
231
- return (0, immer_1.default)(state, function (draft) {
232
- var _a;
233
- if (draft.actions) {
234
- var action = draft.actions.find(function (action) { var _a; return ((_a = action.animation) === null || _a === void 0 ? void 0 : _a.id) === event.animationId; });
235
- if (!action) {
236
- return console.error('ANIMATION_COMPLETE event received but not animation found with provided id');
237
- }
238
- var step = (_a = action.animation) === null || _a === void 0 ? void 0 : _a.step;
239
- if (action.transient) {
240
- draft.actions.splice(draft.actions.indexOf(action), 1);
241
- }
242
- else {
243
- delete action.animation;
244
- }
245
- if (step === animations_1.AnimationStep.BEFORE_MOVE) {
246
- playNextMove(action, draft);
247
- }
248
- else if (step === animations_1.AnimationStep.BEFORE_UNDO) {
249
- undoNextMove(action, draft);
250
- }
251
- else {
252
- refreshLegalMoves(draft);
28
+ return createReducer(initialState, (builder) => {
29
+ builder.addCase(loadGameState, (draft, { payload }) => {
30
+ if (payload.serverTime) {
31
+ draft.clientTimeDelta = approximateTimeDelta(payload.serverTime, draft.clientTimeDelta);
32
+ }
33
+ draft.players = payload.players;
34
+ draft.options = payload.options;
35
+ draft.playerId = payload.playerId;
36
+ draft.state = payload.state;
37
+ if (payload.playerId !== undefined) {
38
+ draft.legalMoves = castDraft(new GameRules(payload.state).getLegalMoves(payload.playerId));
39
+ }
40
+ draft.setup = payload.setup;
41
+ draft.gameOver = !!payload.endDate;
42
+ draft.gameMode = payload.mode;
43
+ draft.tournament = payload.tournament;
44
+ draft.nextNotificationIndex = payload.notifications.length;
45
+ if (!payload.endDate) {
46
+ draft.client = { player: payload.playerId };
47
+ }
48
+ setActionsFromPastNotifications(draft, payload.notifications);
49
+ });
50
+ builder.addCase(receiveNotifications, (draft, { payload }) => {
51
+ if (payload.serverTime) {
52
+ draft.clientTimeDelta = approximateTimeDelta(payload.serverTime, draft.clientTimeDelta);
53
+ }
54
+ for (const player of draft.players) {
55
+ const newPlayerTime = payload.players.find(p => p.id === player.id)?.time;
56
+ player.time = newPlayerTime ?? player.time;
57
+ }
58
+ if (payload.notificationsIndex == 0 && !draft.actions) {
59
+ setActionsFromPastNotifications(draft, payload.notifications);
60
+ }
61
+ for (let i = Math.max(draft.nextNotificationIndex - payload.notificationsIndex, 0); i < payload.notifications.length; i++) {
62
+ draft.pendingNotifications.push({ ...payload.notifications[i], index: i + payload.notificationsIndex });
63
+ }
64
+ draft.pendingNotifications.sort((n1, n2) => n1.index - n2.index);
65
+ if (draft.state) {
66
+ applyPendingNotifications(draft);
67
+ }
68
+ });
69
+ builder.addCase(playMove, (draft, { payload: { move, options } }) => {
70
+ if (!draft.state)
71
+ throw new Error('Cannot play move before game state is loaded');
72
+ if (!draft.actions)
73
+ throw new Error('Cannot play move before game history is loaded');
74
+ const transient = options?.transient;
75
+ const local = options?.local ?? transient;
76
+ if (draft.playerId === undefined && !local)
77
+ throw new Error('Spectators cannot play');
78
+ const action = {
79
+ id: 'local-' + uniqueId(),
80
+ playerId: draft.playerId,
81
+ move,
82
+ consequences: [],
83
+ played: 0,
84
+ pending: !local,
85
+ local,
86
+ transient
87
+ };
88
+ const rules = getGameAfterAnimations(draft, GameRules);
89
+ const isUnpredictable = (move) => hasUnpredictableMoves(rules) && rules.isUnpredictableMove(move, action.playerId);
90
+ if (!local && (options?.delayed || isUnpredictable(move))) {
91
+ action.delayed = true;
92
+ }
93
+ else {
94
+ action.consequences = [];
95
+ const pendingMoves = rules.play(JSON.parse(JSON.stringify(move)), { local, transient });
96
+ loopWithFuse(() => {
97
+ const move = pendingMoves.shift();
98
+ if (!move || isUnpredictable(move))
99
+ return false;
100
+ action.consequences.push(move);
101
+ pendingMoves.unshift(...rules.play(JSON.parse(JSON.stringify(move)), { local, transient }));
102
+ return true;
103
+ }, { errorFn: () => new Error(`Infinite loop detected while applying move consequences: ${JSON.stringify(action)})`) });
104
+ if (options?.skipAnimation) {
105
+ playNextMove(action, draft);
106
+ }
107
+ else {
108
+ animatePlayedAction(action, draft);
109
+ }
110
+ }
111
+ if (!transient || action.animation) {
112
+ draft.actions.push(action);
113
+ }
114
+ });
115
+ builder.addCase(undoMove, (draft, { payload: { actionId, delayed } }) => {
116
+ if (!draft.actions || !draft.setup || !draft.state) {
117
+ return console.error('Cannot undo before actions history is loaded');
118
+ }
119
+ const action = findLastItem(draft.actions, action => action.id === actionId);
120
+ if (!action)
121
+ return console.error(`Could not find action to undo with this id: ${actionId}`);
122
+ if (!action.local && draft.playerId === undefined) {
123
+ return console.error('Spectators cannot undo moves');
124
+ }
125
+ action.cancelled = true;
126
+ if (!action.local) {
127
+ action.cancelPending = true;
128
+ }
129
+ if (!action.local && (delayed || !new GameRules(draft.state, draft.client).isTurnToPlay(draft.playerId))) {
130
+ action.delayed = true;
131
+ }
132
+ else {
133
+ animateUndoneAction(action, draft);
134
+ }
135
+ });
136
+ builder.addCase(reportFailure, (draft, { payload: reason }) => {
137
+ if (draft.actions) {
138
+ switch (reason) {
139
+ case Failure.UNDO_FORBIDDEN:
140
+ for (const action of draft.actions) {
141
+ if (action.cancelPending) {
142
+ action.cancelled = false;
143
+ action.cancelPending = false;
144
+ }
253
145
  }
254
- }
255
- });
256
- case Actions_1.ANIMATION_PAUSED:
257
- return (0, immer_1.default)(state, function (draft) {
258
- draft.animationPaused = event.pause;
259
- });
260
- case Actions_1.SET_ANIMATIONS_SPEED:
261
- return __assign(__assign({}, state), { animationsSpeed: event.speed });
262
- case GiveUp_1.GIVE_UP:
263
- return __assign(__assign({}, state), { playerId: undefined });
264
- case Sound_1.MUTE_SOUNDS:
265
- return __assign(__assign({}, state), { soundsMuted: true });
266
- case Sound_1.UNMUTE_SOUNDS:
267
- return __assign(__assign({}, state), { soundsMuted: false });
268
- case Log_1.START_LIVE_LOG:
269
- return __assign(__assign({}, state), { liveLogStopped: false });
270
- case Log_1.STOP_LIVE_LOG:
271
- return __assign(__assign({}, state), { liveLogStopped: true });
272
- case PlayerQuit_1.PLAYER_QUIT:
273
- return (0, immer_1.default)(state, function (draft) {
274
- var player = draft.players.find(function (p) { return p.id === event.playerId; });
275
- if (player) {
276
- player.quit = true;
277
- player.quitReason = event.reason;
278
- }
279
- if (draft.playerId === event.playerId) {
280
- delete draft.playerId;
281
- }
282
- });
283
- case Actions_1.GAME_OVER:
284
- return __assign(__assign({}, state), { gameOver: true, players: state.players.map(function (player) {
285
- var _a;
286
- var p = event.players.find(function (p) { return p.id === player.id; });
287
- if (!p)
288
- return player;
289
- else
290
- return (__assign(__assign({}, player), { time: (_a = p.time) !== null && _a !== void 0 ? _a : player.time, gamePointsDelta: p.gamePointsDelta, tournamentPoints: p.tournamentPoints }));
291
- }) });
292
- }
293
- return state;
294
- };
146
+ break;
147
+ default:
148
+ draft.actions = draft.actions.filter(action => !action.pending);
149
+ const rules = new GameRules(JSON.parse(JSON.stringify(draft.setup)), draft.client);
150
+ replayActions(rules, draft.actions, draft.state);
151
+ draft.state = rules.game;
152
+ }
153
+ }
154
+ draft.failures.push(reason);
155
+ });
156
+ builder.addCase(clearFailures, (draft) => {
157
+ draft.failures = [];
158
+ });
159
+ builder.addCase(startAnimation, (draft, { payload: actionId }) => {
160
+ if (!draft.actions)
161
+ return;
162
+ const action = draft.actions.find(action => action.id === actionId);
163
+ if (action) {
164
+ animateAction(action, draft);
165
+ }
166
+ });
167
+ builder.addCase(endAnimation, (draft, { payload: animationId }) => {
168
+ if (draft.actions) {
169
+ const action = draft.actions.find(action => action.animation?.id === animationId);
170
+ if (!action) {
171
+ return console.error('endAnimation action received but not animation found with provided id');
172
+ }
173
+ const step = action.animation?.step;
174
+ if (action.transient) {
175
+ draft.actions.splice(draft.actions.indexOf(action), 1);
176
+ }
177
+ else {
178
+ delete action.animation;
179
+ }
180
+ if (step === AnimationStep.BEFORE_MOVE) {
181
+ playNextMove(action, draft);
182
+ }
183
+ else if (step === AnimationStep.BEFORE_UNDO) {
184
+ undoNextMove(action, draft);
185
+ }
186
+ else {
187
+ refreshLegalMoves(draft);
188
+ }
189
+ }
190
+ });
191
+ builder.addCase(setAnimationsSpeed, (draft, { payload: speed }) => {
192
+ draft.animationsSpeed = speed;
193
+ });
194
+ builder.addCase(giveUp, (draft) => {
195
+ draft.playerId = undefined;
196
+ });
197
+ builder.addCase(muteSounds, (draft) => {
198
+ draft.soundsMuted = true;
199
+ });
200
+ builder.addCase(unmuteSounds, (draft) => {
201
+ draft.soundsMuted = false;
202
+ });
203
+ builder.addCase(startLiveLog, (draft) => {
204
+ draft.liveLogStopped = false;
205
+ });
206
+ builder.addCase(stopLiveLog, (draft) => {
207
+ draft.liveLogStopped = true;
208
+ });
209
+ builder.addCase(removePlayer, (draft, { payload: { playerId, reason } }) => {
210
+ const player = draft.players.find(p => p.id === playerId);
211
+ if (player) {
212
+ player.quit = true;
213
+ player.quitReason = reason;
214
+ }
215
+ if (draft.playerId === playerId) {
216
+ delete draft.playerId;
217
+ }
218
+ });
219
+ builder.addCase(setGameOver, (draft, { payload: players }) => {
220
+ draft.gameOver = true;
221
+ for (const player of draft.players) {
222
+ const newPlayerData = players.find(p => p.id === player.id) ?? {};
223
+ merge(player, newPlayerData);
224
+ }
225
+ });
226
+ });
295
227
  function playNextMove(action, draft) {
296
- var consequenceIndex = action.played === 0 ? undefined : action.played - 1;
297
- var move = consequenceIndex === undefined ? action.move : action.consequences[consequenceIndex];
298
- var rules = new GameRules(draft.state, draft.client);
228
+ const consequenceIndex = action.played === 0 ? undefined : action.played - 1;
229
+ const move = consequenceIndex === undefined ? action.move : action.consequences[consequenceIndex];
230
+ const rules = new GameRules(draft.state, draft.client);
299
231
  rules.play(JSON.parse(JSON.stringify(move)), { local: action.local, transient: action.transient });
300
232
  action.played++;
301
233
  if (!action.local && !action.transient && !action.pending) {
302
- draft.playedMoves.push({ actionId: action.id, move: move, consequenceIndex: consequenceIndex });
234
+ draft.playedMoves.push({ actionId: action.id, move, consequenceIndex });
303
235
  }
304
- animatePlayedAction(action, draft, animations_1.AnimationStep.AFTER_MOVE);
236
+ animatePlayedAction(action, draft, AnimationStep.AFTER_MOVE);
305
237
  }
306
238
  function undoNextMove(action, draft) {
307
- var rules = new GameRules(JSON.parse(JSON.stringify(draft.setup)), draft.client);
239
+ const rules = new GameRules(JSON.parse(JSON.stringify(draft.setup)), draft.client);
308
240
  action.played--;
309
- var actionsPlayed = draft.actions.filter(function (action) { return action.played > 0; })
310
- .map(function (action) { return (__assign(__assign({}, action), { consequences: action.consequences.slice(0, action.played - 1) })); });
241
+ const actionsPlayed = draft.actions.filter(action => action.played > 0)
242
+ .map(action => ({
243
+ ...action,
244
+ consequences: action.consequences.slice(0, action.played - 1)
245
+ }));
311
246
  replayActions(rules, actionsPlayed, draft.state);
312
247
  draft.state = rules.game;
313
- animateUndoneAction(action, draft, animations_1.AnimationStep.AFTER_UNDO);
248
+ animateUndoneAction(action, draft, AnimationStep.AFTER_UNDO);
314
249
  }
315
250
  function setActionsFromPastNotifications(draft, notifications) {
316
- var _a;
317
251
  draft.actions = [];
318
252
  draft.playedMoves = [];
319
- var _loop_1 = function (i) {
320
- var notification = notifications[i];
321
- if ((0, Types_1.isMovePlayedNotification)(notification)) {
322
- var action = {
323
- id: (_a = notification.actionId) !== null && _a !== void 0 ? _a : undefined,
253
+ for (let i = 0; i < draft.nextNotificationIndex; i++) {
254
+ const notification = notifications[i];
255
+ if (isMovePlayedNotification(notification)) {
256
+ const action = {
257
+ id: notification.actionId ?? undefined,
324
258
  playerId: notification.playerId, move: notification.moveView,
325
259
  consequences: notification.consequences,
326
260
  played: 1 + notification.consequences.length
327
261
  };
328
262
  draft.actions.push(action);
329
263
  draft.playedMoves.push({ actionId: action.id, move: action.move });
330
- for (var consequenceIndex = 0; consequenceIndex < action.consequences.length; consequenceIndex++) {
331
- var consequence = action.consequences[consequenceIndex];
332
- draft.playedMoves.push({ actionId: action.id, move: consequence, consequenceIndex: consequenceIndex });
264
+ for (let consequenceIndex = 0; consequenceIndex < action.consequences.length; consequenceIndex++) {
265
+ const consequence = action.consequences[consequenceIndex];
266
+ draft.playedMoves.push({ actionId: action.id, move: consequence, consequenceIndex });
333
267
  }
334
268
  }
335
- else if ((0, Types_1.isMoveUndoneNotification)(notification)) {
336
- var index = notification.actionId ? (0, ArrayUtil_1.findLastIndex)(draft.actions, function (action) { return action.id === notification.actionId; }) : notification.moveUndone;
269
+ else if (isMoveUndoneNotification(notification)) {
270
+ const index = notification.actionId ? findLastIndex(draft.actions, action => action.id === notification.actionId) : notification.moveUndone;
337
271
  if (index !== -1) {
338
272
  draft.actions.splice(index, 1);
339
273
  }
340
- draft.playedMoves = draft.playedMoves.filter(function (playedMove) { return playedMove.actionId !== notification.actionId; });
274
+ draft.playedMoves = draft.playedMoves.filter((playedMove) => playedMove.actionId !== notification.actionId);
341
275
  }
342
- };
343
- for (var i = 0; i < draft.nextNotificationIndex; i++) {
344
- _loop_1(i);
345
276
  }
346
277
  }
347
278
  function applyPendingNotifications(draft) {
@@ -349,7 +280,7 @@ function gameReducer(context) {
349
280
  return;
350
281
  }
351
282
  while (draft.pendingNotifications.length && draft.pendingNotifications[0].index <= draft.nextNotificationIndex) {
352
- var notification = draft.pendingNotifications.shift();
283
+ const notification = draft.pendingNotifications.shift();
353
284
  if (notification.index < draft.nextNotificationIndex) {
354
285
  console.info('A notification was received twice: ' + JSON.stringify(notification));
355
286
  }
@@ -360,23 +291,21 @@ function gameReducer(context) {
360
291
  }
361
292
  }
362
293
  function applyNotification(draft, notification) {
363
- if ((0, Types_1.isMovePlayedNotification)(notification)) {
294
+ if (isMovePlayedNotification(notification)) {
364
295
  applyMovePlayedNotification(draft, notification);
365
296
  }
366
- else if ((0, Types_1.isMoveUndoneNotification)(notification)) {
297
+ else if (isMoveUndoneNotification(notification)) {
367
298
  applyMoveUndoneNotification(draft, notification);
368
299
  }
369
300
  }
370
301
  function applyMovePlayedNotification(draft, notification) {
371
- var _a;
372
- var _b, _c;
373
- (0, assert_1.default)(draft.actions !== undefined, 'Cannot apply a notification before action history is loaded');
374
- var action;
375
- if (notification.playerId == draft.playerId) {
376
- action = draft.actions.find(function (action) { return action.pending && action.playerId == notification.playerId; });
302
+ assert(draft.actions !== undefined, 'Cannot apply a notification before action history is loaded');
303
+ let action;
304
+ if (notification.playerId === draft.playerId) {
305
+ action = draft.actions.find(action => action.pending && action.playerId == notification.playerId);
377
306
  }
378
307
  if (action) {
379
- action.id = (_b = notification.actionId) !== null && _b !== void 0 ? _b : undefined;
308
+ action.id = notification.actionId ?? undefined;
380
309
  action.pending = false;
381
310
  if (action.delayed) {
382
311
  action.move = notification.moveView;
@@ -386,14 +315,14 @@ function gameReducer(context) {
386
315
  else {
387
316
  if (action.played !== 0) {
388
317
  draft.playedMoves.push({ actionId: action.id, move: action.move });
389
- for (var consequenceIndex = 0; consequenceIndex < action.played - 1; consequenceIndex++) {
390
- var consequence = action.consequences[consequenceIndex];
391
- draft.playedMoves.push({ actionId: action.id, move: consequence, consequenceIndex: consequenceIndex });
318
+ for (let consequenceIndex = 0; consequenceIndex < action.played - 1; consequenceIndex++) {
319
+ const consequence = action.consequences[consequenceIndex];
320
+ draft.playedMoves.push({ actionId: action.id, move: consequence, consequenceIndex });
392
321
  }
393
322
  }
394
- if (!(0, es_toolkit_1.isEqual)(JSON.parse(JSON.stringify(action.move)), JSON.parse(JSON.stringify(notification.moveView)))) {
395
- var rules = new GameRules(draft.state);
396
- if ((0, rules_api_1.hasUnpredictableMoves)(rules) && rules.canIgnoreServerDifference
323
+ if (!isEqual(JSON.parse(JSON.stringify(action.move)), JSON.parse(JSON.stringify(notification.moveView)))) {
324
+ const rules = new GameRules(draft.state);
325
+ if (hasUnpredictableMoves(rules) && rules.canIgnoreServerDifference
397
326
  && rules.canIgnoreServerDifference(action.move, notification.moveView)) {
398
327
  action.move = notification.moveView;
399
328
  }
@@ -405,25 +334,25 @@ function gameReducer(context) {
405
334
  console.error('The server returned less consequences that what was played locally:', action.consequences, notification.consequences);
406
335
  }
407
336
  else {
408
- for (var i = 0; i < action.consequences.length; i++) {
409
- if (!(0, es_toolkit_1.isEqual)(JSON.parse(JSON.stringify(action.consequences[i])), JSON.parse(JSON.stringify(notification.consequences[i])))) {
410
- var rules = new GameRules(draft.state);
411
- if ((0, rules_api_1.hasUnpredictableMoves)(rules) && rules.canIgnoreServerDifference
337
+ for (let i = 0; i < action.consequences.length; i++) {
338
+ if (!isEqual(JSON.parse(JSON.stringify(action.consequences[i])), JSON.parse(JSON.stringify(notification.consequences[i])))) {
339
+ const rules = new GameRules(draft.state);
340
+ if (hasUnpredictableMoves(rules) && rules.canIgnoreServerDifference
412
341
  && rules.canIgnoreServerDifference(action.consequences[i], notification.consequences[i])) {
413
342
  action.consequences[i] = notification.consequences[i];
414
343
  }
415
344
  else {
416
- console.error("Consequence #".concat(i, " was played on the client, but the server returned a different consequence:"), JSON.parse(JSON.stringify(action.consequences[i])), notification.consequences[i]);
345
+ console.error(`Consequence #${i} was played on the client, but the server returned a different consequence:`, JSON.parse(JSON.stringify(action.consequences[i])), notification.consequences[i]);
417
346
  }
418
347
  }
419
348
  }
420
- (_a = action.consequences).push.apply(_a, notification.consequences.slice(action.consequences.length));
349
+ action.consequences.push(...notification.consequences.slice(action.consequences.length));
421
350
  }
422
351
  }
423
352
  }
424
353
  else {
425
- draft.actions.splice((0, ArrayUtil_1.findLastIndex)(draft.actions, function (action) { return !action.pending; }) + 1, 0, {
426
- id: (_c = notification.actionId) !== null && _c !== void 0 ? _c : undefined,
354
+ draft.actions.splice(findLastIndex(draft.actions, action => !action.pending) + 1, 0, {
355
+ id: notification.actionId ?? undefined,
427
356
  playerId: notification.playerId,
428
357
  move: notification.moveView,
429
358
  consequences: notification.consequences,
@@ -432,11 +361,11 @@ function gameReducer(context) {
432
361
  }
433
362
  }
434
363
  function applyMoveUndoneNotification(draft, notification) {
435
- (0, assert_1.default)(draft.actions !== undefined, 'Cannot apply a notification before action history is loaded');
436
- var index = (0, ArrayUtil_1.findLastIndex)(draft.actions, function (action) { return action.id === notification.actionId; });
364
+ assert(draft.actions !== undefined, 'Cannot apply a notification before action history is loaded');
365
+ const index = findLastIndex(draft.actions, action => action.id === notification.actionId);
437
366
  if (index === -1)
438
- return console.error("Received move undone notification, but action ".concat(notification.actionId, " is unknown"));
439
- var cancelledAction = draft.actions[index];
367
+ return console.error(`Received move undone notification, but action ${notification.actionId} is unknown`);
368
+ const cancelledAction = draft.actions[index];
440
369
  if (!cancelledAction.played && !cancelledAction.animation) {
441
370
  draft.actions.splice(index, 1);
442
371
  }
@@ -445,7 +374,7 @@ function gameReducer(context) {
445
374
  delete cancelledAction.cancelPending;
446
375
  delete cancelledAction.delayed;
447
376
  }
448
- draft.playedMoves = draft.playedMoves.filter(function (move) { return move.actionId !== notification.actionId; });
377
+ draft.playedMoves = draft.playedMoves.filter((move) => move.actionId !== notification.actionId);
449
378
  }
450
379
  function animateAction(action, draft) {
451
380
  if (!action.cancelled) {
@@ -455,21 +384,20 @@ function gameReducer(context) {
455
384
  animateUndoneAction(action, draft);
456
385
  }
457
386
  }
458
- function animatePlayedAction(action, draft, step) {
459
- if (step === void 0) { step = animations_1.AnimationStep.BEFORE_MOVE; }
387
+ function animatePlayedAction(action, draft, step = AnimationStep.BEFORE_MOVE) {
460
388
  if (action.activePlayers === undefined) {
461
- var rules_1 = new GameRules(draft.state, draft.client);
462
- action.activePlayers = draft.players.map(function (p) { return p.id; }).filter(function (p) { return rules_1.isTurnToPlay(p); });
389
+ const rules = new GameRules(draft.state, draft.client);
390
+ action.activePlayers = draft.players.map(p => p.id).filter(p => rules.isTurnToPlay(p));
463
391
  }
464
- var move = (0, DisplayedAction_1.getAnimatedMove)(action, step);
465
- var duration = animations.getDuration(move, getAnimationContext(action, draft, step)) / draft.animationsSpeed;
392
+ const move = getAnimatedMove(action, step);
393
+ const duration = animations.getDuration(move, getAnimationContext(action, draft, step)) / draft.animationsSpeed;
466
394
  if (duration > 0) {
467
- action.animation = { id: draft.animationIncrement++, duration: duration, step: step };
395
+ action.animation = { id: draft.animationIncrement++, duration, step };
468
396
  }
469
- else if (step === animations_1.AnimationStep.BEFORE_MOVE) {
397
+ else if (step === AnimationStep.BEFORE_MOVE) {
470
398
  playNextMove(action, draft);
471
399
  }
472
- else if (action.played <= action.consequences.length && !draft.animationPaused) {
400
+ else if (action.played <= action.consequences.length) {
473
401
  animatePlayedAction(action, draft);
474
402
  }
475
403
  else {
@@ -478,18 +406,17 @@ function gameReducer(context) {
478
406
  }
479
407
  function refreshLegalMoves(draft) {
480
408
  if (draft.playerId !== undefined) {
481
- var rules = new GameRules(draft.state, draft.client);
482
- draft.legalMoves = (0, immer_1.castDraft)(rules.getLegalMoves(draft.playerId));
409
+ const rules = new GameRules(draft.state, draft.client);
410
+ draft.legalMoves = castDraft(rules.getLegalMoves(draft.playerId));
483
411
  }
484
412
  }
485
- function animateUndoneAction(action, draft, step) {
486
- if (step === void 0) { step = animations_1.AnimationStep.BEFORE_UNDO; }
487
- var move = (0, DisplayedAction_1.getAnimatedMove)(action, step);
488
- var duration = animations.getDuration(move, getAnimationContext(action, draft, step)) / draft.animationsSpeed;
413
+ function animateUndoneAction(action, draft, step = AnimationStep.BEFORE_UNDO) {
414
+ const move = getAnimatedMove(action, step);
415
+ const duration = animations.getDuration(move, getAnimationContext(action, draft, step)) / draft.animationsSpeed;
489
416
  if (duration > 0) {
490
- action.animation = { id: draft.animationIncrement++, duration: duration, step: step };
417
+ action.animation = { id: draft.animationIncrement++, duration, step };
491
418
  }
492
- else if (step === animations_1.AnimationStep.BEFORE_UNDO) {
419
+ else if (step === AnimationStep.BEFORE_UNDO) {
493
420
  undoNextMove(action, draft);
494
421
  }
495
422
  else if (action.played > 0) {
@@ -503,37 +430,34 @@ function gameReducer(context) {
503
430
  }
504
431
  }
505
432
  function getAnimationContext(action, draft, step) {
506
- return __assign(__assign({}, context), { action: action, game: draft.state, playerId: draft.playerId, step: step });
433
+ return { ...context, action: action, game: draft.state, playerId: draft.playerId, step };
507
434
  }
508
435
  }
509
- function getGameAfterAnimations(state, Rules) {
436
+ export function getGameAfterAnimations(state, Rules) {
510
437
  if (!state.actions || !state.setup) {
511
438
  throw new Error('Cannot play move before game history is loaded');
512
439
  }
513
- if (state.actions.some(function (action) { return action.animation || action.cancelled; })) {
514
- var rules_2 = new Rules(JSON.parse(JSON.stringify(state.setup)), state.client);
515
- var actionsToReplay = state.actions.filter(function (action) { return !action.delayed && !action.cancelled; });
516
- actionsToReplay.forEach(function (action) { return replayAction(rules_2, action); });
517
- return rules_2;
440
+ if (state.actions.some(action => action.animation || action.cancelled)) {
441
+ const rules = new Rules(JSON.parse(JSON.stringify(state.setup)), state.client);
442
+ const actionsToReplay = state.actions.filter(action => !action.delayed && !action.cancelled);
443
+ actionsToReplay.forEach(action => replayAction(rules, action));
444
+ return rules;
518
445
  }
519
446
  else {
520
447
  return new Rules(JSON.parse(JSON.stringify(state.state)), { player: state.playerId });
521
448
  }
522
449
  }
523
- var estimatedLatency = 100;
450
+ const estimatedLatency = 100;
524
451
  function approximateTimeDelta(serverTime, clientTimeDelta) {
525
452
  return Math.min(serverTime + estimatedLatency - new Date().getTime(), clientTimeDelta || Infinity);
526
453
  }
527
- function replayActions(rules, actions, previousState) {
528
- actions.forEach(function (action) { return replayAction(rules, action); });
529
- if ((0, rules_api_1.hasUndo)(rules) && rules.restoreTransientState)
454
+ export function replayActions(rules, actions, previousState) {
455
+ actions.forEach(action => replayAction(rules, action));
456
+ if (hasUndo(rules) && rules.restoreTransientState)
530
457
  rules.restoreTransientState(previousState);
531
458
  }
532
- function replayAction(rules, action) {
459
+ export function replayAction(rules, action) {
533
460
  rules.play(JSON.parse(JSON.stringify(action.move)), { local: action.local, transient: action.transient });
534
- action.consequences.forEach(function (move) { return rules.play(JSON.parse(JSON.stringify(move)), { local: action.local, transient: action.transient }); });
535
- }
536
- function isGameAction(action) {
537
- return typeof action === 'object' && typeof action.type === 'number';
461
+ action.consequences.forEach(move => rules.play(JSON.parse(JSON.stringify(move)), { local: action.local, transient: action.transient }));
538
462
  }
539
463
  //# sourceMappingURL=gameReducer.js.map