@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.
- package/dist/Actions/ActionFailed.js +3 -7
- package/dist/Actions/ActionFailed.js.map +1 -1
- package/dist/Actions/AnimationComplete.js +3 -7
- package/dist/Actions/AnimationComplete.js.map +1 -1
- package/dist/Actions/AnimationPaused.js +3 -7
- package/dist/Actions/AnimationPaused.js.map +1 -1
- package/dist/Actions/AnimationStart.js +1 -4
- package/dist/Actions/AnimationStart.js.map +1 -1
- package/dist/Actions/ClearFailures.d.ts +1 -5
- package/dist/Actions/ClearFailures.js +3 -9
- package/dist/Actions/ClearFailures.js.map +1 -1
- package/dist/Actions/GameNotificationsReceived.d.ts +2 -4
- package/dist/Actions/GameNotificationsReceived.js +2 -9
- package/dist/Actions/GameNotificationsReceived.js.map +1 -1
- package/dist/Actions/GameOver.js +3 -7
- package/dist/Actions/GameOver.js.map +1 -1
- package/dist/Actions/GameStateLoaded.js +5 -18
- package/dist/Actions/GameStateLoaded.js.map +1 -1
- package/dist/Actions/MovePlayed.d.ts +4 -4
- package/dist/Actions/MovePlayed.js +3 -4
- package/dist/Actions/MovePlayed.js.map +1 -1
- package/dist/Actions/MoveUndone.js +3 -8
- package/dist/Actions/MoveUndone.js.map +1 -1
- package/dist/Actions/PlayerQuit.d.ts +3 -9
- package/dist/Actions/PlayerQuit.js +2 -19
- package/dist/Actions/PlayerQuit.js.map +1 -1
- package/dist/Actions/SetAnimationsSpeed.d.ts +1 -6
- package/dist/Actions/SetAnimationsSpeed.js +3 -9
- package/dist/Actions/SetAnimationsSpeed.js.map +1 -1
- package/dist/Actions/giveUp.d.ts +1 -0
- package/dist/Actions/giveUp.js +3 -0
- package/dist/Actions/giveUp.js.map +1 -0
- package/dist/Actions/index.d.ts +7 -11
- package/dist/Actions/index.js +7 -27
- package/dist/Actions/index.js.map +1 -1
- package/dist/Actions/loadGameState.d.ts +14 -0
- package/dist/Actions/loadGameState.js +3 -0
- package/dist/Actions/loadGameState.js.map +1 -0
- package/dist/Actions/playMove.d.ts +10 -0
- package/dist/Actions/playMove.js +3 -0
- package/dist/Actions/playMove.js.map +1 -0
- package/dist/Actions/receiveNotifications.d.ts +8 -0
- package/dist/Actions/receiveNotifications.js +3 -0
- package/dist/Actions/receiveNotifications.js.map +1 -0
- package/dist/Actions/removePlayer.d.ts +5 -0
- package/dist/Actions/removePlayer.js +3 -0
- package/dist/Actions/removePlayer.js.map +1 -0
- package/dist/Actions/reportFailure.d.ts +1 -0
- package/dist/Actions/reportFailure.js +3 -0
- package/dist/Actions/reportFailure.js.map +1 -0
- package/dist/Actions/setGameOver.d.ts +2 -0
- package/dist/Actions/setGameOver.js +3 -0
- package/dist/Actions/setGameOver.js.map +1 -0
- package/dist/Actions/startAnimation.d.ts +1 -0
- package/dist/Actions/startAnimation.js +3 -0
- package/dist/Actions/startAnimation.js.map +1 -0
- package/dist/Actions/undoMove.d.ts +5 -0
- package/dist/Actions/undoMove.js +4 -0
- package/dist/Actions/undoMove.js.map +1 -0
- package/dist/Chat/ChatChannel.js +1 -2
- package/dist/Chat/Message.js +1 -2
- package/dist/Chat/index.js +2 -18
- package/dist/Chat/index.js.map +1 -1
- package/dist/Deckbuilding/Deck.js +1 -2
- package/dist/Deckbuilding/index.js +4 -24
- package/dist/Deckbuilding/index.js.map +1 -1
- package/dist/Deckbuilding/useDeleteDeck.d.ts +4 -1
- package/dist/Deckbuilding/useDeleteDeck.js +8 -14
- package/dist/Deckbuilding/useDeleteDeck.js.map +1 -1
- package/dist/Deckbuilding/useMyDecks.d.ts +2 -1
- package/dist/Deckbuilding/useMyDecks.js +7 -13
- package/dist/Deckbuilding/useMyDecks.js.map +1 -1
- package/dist/Deckbuilding/useSaveDeck.d.ts +4 -1
- package/dist/Deckbuilding/useSaveDeck.js +8 -14
- package/dist/Deckbuilding/useSaveDeck.js.map +1 -1
- package/dist/EjectPlayer/EjectPlayer.d.ts +1 -6
- package/dist/EjectPlayer/EjectPlayer.js +2 -8
- package/dist/EjectPlayer/EjectPlayer.js.map +1 -1
- package/dist/EjectPlayer/index.js +1 -17
- package/dist/EjectPlayer/index.js.map +1 -1
- package/dist/Failure/Failure.js +1 -4
- package/dist/Failure/Failure.js.map +1 -1
- package/dist/Failure/index.js +1 -17
- package/dist/Failure/index.js.map +1 -1
- package/dist/GiveUp/GiveUp.js +2 -5
- package/dist/GiveUp/GiveUp.js.map +1 -1
- package/dist/GiveUp/index.js +1 -17
- package/dist/GiveUp/index.js.map +1 -1
- package/dist/Hooks/index.js +5 -21
- package/dist/Hooks/index.js.map +1 -1
- package/dist/Hooks/useChatMessages.d.ts +1 -6
- package/dist/Hooks/useChatMessages.js +35 -44
- package/dist/Hooks/useChatMessages.js.map +1 -1
- package/dist/Hooks/useMessageAuthor.js +12 -16
- package/dist/Hooks/useMessageAuthor.js.map +1 -1
- package/dist/Hooks/useOptions.js +3 -6
- package/dist/Hooks/useOptions.js.map +1 -1
- package/dist/Hooks/useRematch.d.ts +1 -1
- package/dist/Hooks/useRematch.js +16 -20
- package/dist/Hooks/useRematch.js.map +1 -1
- package/dist/Hooks/useSendMessage.d.ts +4 -1
- package/dist/Hooks/useSendMessage.js +5 -12
- package/dist/Hooks/useSendMessage.js.map +1 -1
- package/dist/Log/StartLiveLog.d.ts +1 -4
- package/dist/Log/StartLiveLog.js +3 -5
- package/dist/Log/StartLiveLog.js.map +1 -1
- package/dist/Log/StopLiveLog.d.ts +1 -4
- package/dist/Log/StopLiveLog.js +4 -7
- package/dist/Log/StopLiveLog.js.map +1 -1
- package/dist/Log/index.d.ts +2 -2
- package/dist/Log/index.js +4 -20
- package/dist/Log/index.js.map +1 -1
- package/dist/Log/useLogControls.js +11 -14
- package/dist/Log/useLogControls.js.map +1 -1
- package/dist/PlayerTime/PlayerTime.js +1 -2
- package/dist/PlayerTime/index.js +1 -17
- package/dist/PlayerTime/index.js.map +1 -1
- package/dist/Rematch/RematchQueries.js +23 -13
- package/dist/Rematch/RematchQueries.js.map +1 -1
- package/dist/Rematch/index.js +1 -17
- package/dist/Rematch/index.js.map +1 -1
- package/dist/Sound/MuteSounds.d.ts +1 -4
- package/dist/Sound/MuteSounds.js +5 -8
- package/dist/Sound/MuteSounds.js.map +1 -1
- package/dist/Sound/UnmuteSounds.d.ts +1 -4
- package/dist/Sound/UnmuteSounds.js +3 -5
- package/dist/Sound/UnmuteSounds.js.map +1 -1
- package/dist/Sound/index.d.ts +2 -2
- package/dist/Sound/index.js +3 -19
- package/dist/Sound/index.js.map +1 -1
- package/dist/Sound/useSoundControls.js +11 -14
- package/dist/Sound/useSoundControls.js.map +1 -1
- package/dist/Store/DisplayedAction.js +4 -10
- package/dist/Store/DisplayedAction.js.map +1 -1
- package/dist/Store/GamePageState.d.ts +0 -1
- package/dist/Store/GamePageState.js +1 -2
- package/dist/Store/PlayedMove.js +1 -2
- package/dist/Store/endAnimation.d.ts +1 -0
- package/dist/Store/endAnimation.js +3 -0
- package/dist/Store/endAnimation.js.map +1 -0
- package/dist/Store/gameMiddleware.js +30 -41
- package/dist/Store/gameMiddleware.js.map +1 -1
- package/dist/Store/gameReducer.d.ts +2 -11
- package/dist/Store/gameReducer.js +301 -377
- package/dist/Store/gameReducer.js.map +1 -1
- package/dist/Store/index.d.ts +1 -0
- package/dist/Store/index.js +4 -19
- package/dist/Store/index.js.map +1 -1
- package/dist/Store/localMovePreviewMiddleware.js +15 -13
- package/dist/Store/localMovePreviewMiddleware.js.map +1 -1
- package/dist/Store/store.d.ts +13 -0
- package/dist/Store/store.js +27 -0
- package/dist/Store/store.js.map +1 -0
- package/dist/Tutorial/PlayTutorialMoves.d.ts +1 -9
- package/dist/Tutorial/PlayTutorialMoves.js +2 -6
- package/dist/Tutorial/PlayTutorialMoves.js.map +1 -1
- package/dist/Tutorial/Tutorial.js +1 -2
- package/dist/Tutorial/TutorialDescription.d.ts +1 -1
- package/dist/Tutorial/TutorialDescription.js +1 -2
- package/dist/Tutorial/index.js +4 -20
- package/dist/Tutorial/index.js.map +1 -1
- package/dist/Tutorial/useTutorial.js +11 -20
- package/dist/Tutorial/useTutorial.js.map +1 -1
- package/dist/Types/GameAI.js +1 -2
- package/dist/Types/GameData.js +1 -2
- package/dist/Types/GameMessage.js +1 -2
- package/dist/Types/GameMode.js +2 -5
- package/dist/Types/GameMode.js.map +1 -1
- package/dist/Types/GameNotification.js +1 -2
- package/dist/Types/GameOptions.js +2 -5
- package/dist/Types/GameOptions.js.map +1 -1
- package/dist/Types/MovePlayedNotification.d.ts +1 -1
- package/dist/Types/MovePlayedNotification.js +3 -8
- package/dist/Types/MovePlayedNotification.js.map +1 -1
- package/dist/Types/MoveUndoneNotification.d.ts +1 -1
- package/dist/Types/MoveUndoneNotification.js +3 -8
- package/dist/Types/MoveUndoneNotification.js.map +1 -1
- package/dist/Types/Player.js +1 -2
- package/dist/Types/PlayerQuitReason.js +2 -5
- package/dist/Types/PlayerQuitReason.js.map +1 -1
- package/dist/Types/ProxyGameData.js +1 -2
- package/dist/Types/Tournament.js +1 -2
- package/dist/Types/index.js +12 -28
- package/dist/Types/index.js.map +1 -1
- package/dist/User/User.js +1 -2
- package/dist/User/index.js +2 -18
- package/dist/User/index.js.map +1 -1
- package/dist/User/useMe.js +9 -19
- package/dist/User/useMe.js.map +1 -1
- package/dist/animations/Animation.js +1 -2
- package/dist/animations/AnimationContext.d.ts +1 -1
- package/dist/animations/AnimationContext.js +1 -2
- package/dist/animations/AnimationState.js +1 -2
- package/dist/animations/AnimationStep.js +2 -5
- package/dist/animations/AnimationStep.js.map +1 -1
- package/dist/animations/Animations.js +9 -16
- package/dist/animations/Animations.js.map +1 -1
- package/dist/animations/AnimationsListener.d.ts +6 -0
- package/dist/animations/AnimationsListener.js +54 -0
- package/dist/animations/AnimationsListener.js.map +1 -0
- package/dist/animations/AnimationsListenerCreator.d.ts +1 -4
- package/dist/animations/AnimationsListenerCreator.js +24 -30
- package/dist/animations/AnimationsListenerCreator.js.map +1 -1
- package/dist/animations/animationMiddleware.d.ts +4 -0
- package/dist/animations/animationMiddleware.js +50 -0
- package/dist/animations/animationMiddleware.js.map +1 -0
- package/dist/animations/index.d.ts +0 -1
- package/dist/animations/index.js +5 -22
- package/dist/animations/index.js.map +1 -1
- package/dist/api/Client/GameClientAPI.d.ts +5 -6
- package/dist/api/Client/GameClientAPI.js +70 -119
- package/dist/api/Client/GameClientAPI.js.map +1 -1
- package/dist/api/Client/GameNotificationsListener.js +51 -104
- package/dist/api/Client/GameNotificationsListener.js.map +1 -1
- package/dist/api/Client/RemoteGameLoader.js +50 -45
- package/dist/api/Client/RemoteGameLoader.js.map +1 -1
- package/dist/api/Client/RemoteGameProvider.js +15 -51
- package/dist/api/Client/RemoteGameProvider.js.map +1 -1
- package/dist/api/Client/apollo-client.d.ts +1 -1
- package/dist/api/Client/apollo-client.js +14 -31
- package/dist/api/Client/apollo-client.js.map +1 -1
- package/dist/api/Client/index.js +4 -20
- package/dist/api/Client/index.js.map +1 -1
- package/dist/api/Client/pusher.js +5 -12
- package/dist/api/Client/pusher.js.map +1 -1
- package/dist/api/GameAPI.d.ts +2 -3
- package/dist/api/GameAPI.js +1 -2
- package/dist/api/Local/ConsoleTools.d.ts +1 -2
- package/dist/api/Local/ConsoleTools.js +15 -22
- package/dist/api/Local/ConsoleTools.js.map +1 -1
- package/dist/api/Local/GameLocalAPI.d.ts +4 -5
- package/dist/api/Local/GameLocalAPI.js +264 -363
- package/dist/api/Local/GameLocalAPI.js.map +1 -1
- package/dist/api/Local/LocalGameProvider.d.ts +1 -1
- package/dist/api/Local/LocalGameProvider.js +17 -48
- package/dist/api/Local/LocalGameProvider.js.map +1 -1
- package/dist/api/Local/index.js +1 -17
- package/dist/api/Local/index.js.map +1 -1
- package/dist/api/index.js +2 -18
- package/dist/api/index.js.map +1 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.js +16 -36
- package/dist/index.js.map +1 -1
- package/dist/util/ArrayUtil.js +4 -8
- package/dist/util/ArrayUtil.js.map +1 -1
- package/package.json +66 -62
- package/dist/Actions/ActionType.d.ts +0 -3
- package/dist/Actions/ActionType.js +0 -8
- package/dist/Actions/ActionType.js.map +0 -1
- package/dist/Scoring/ScoringDescription.d.ts +0 -8
- package/dist/Scoring/ScoringDescription.js +0 -3
- package/dist/Scoring/ScoringDescription.js.map +0 -1
- package/dist/Scoring/index.d.ts +0 -1
- package/dist/Scoring/index.js +0 -18
- package/dist/Scoring/index.js.map +0 -1
- package/dist/util/TimeUtil.d.ts +0 -2
- package/dist/util/TimeUtil.js +0 -27
- package/dist/util/TimeUtil.js.map +0 -1
|
@@ -1,347 +1,278 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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,
|
|
77
|
-
soundsMuted:
|
|
25
|
+
animationIncrement: 0, animationsSpeed: 1,
|
|
26
|
+
soundsMuted: soundsMuted(), clientTimeDelta: 0, liveLogStopped: false
|
|
78
27
|
};
|
|
79
|
-
return
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
return
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
return
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
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
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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
|
|
234
|
+
draft.playedMoves.push({ actionId: action.id, move, consequenceIndex });
|
|
303
235
|
}
|
|
304
|
-
animatePlayedAction(action, draft,
|
|
236
|
+
animatePlayedAction(action, draft, AnimationStep.AFTER_MOVE);
|
|
305
237
|
}
|
|
306
238
|
function undoNextMove(action, draft) {
|
|
307
|
-
|
|
239
|
+
const rules = new GameRules(JSON.parse(JSON.stringify(draft.setup)), draft.client);
|
|
308
240
|
action.played--;
|
|
309
|
-
|
|
310
|
-
.map(
|
|
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,
|
|
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
|
-
|
|
320
|
-
|
|
321
|
-
if (
|
|
322
|
-
|
|
323
|
-
id:
|
|
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 (
|
|
331
|
-
|
|
332
|
-
draft.playedMoves.push({ actionId: action.id, move: consequence, 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 (
|
|
336
|
-
|
|
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(
|
|
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
|
-
|
|
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 (
|
|
294
|
+
if (isMovePlayedNotification(notification)) {
|
|
364
295
|
applyMovePlayedNotification(draft, notification);
|
|
365
296
|
}
|
|
366
|
-
else if (
|
|
297
|
+
else if (isMoveUndoneNotification(notification)) {
|
|
367
298
|
applyMoveUndoneNotification(draft, notification);
|
|
368
299
|
}
|
|
369
300
|
}
|
|
370
301
|
function applyMovePlayedNotification(draft, notification) {
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
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 =
|
|
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 (
|
|
390
|
-
|
|
391
|
-
draft.playedMoves.push({ actionId: action.id, move: consequence, 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 (!
|
|
395
|
-
|
|
396
|
-
if (
|
|
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 (
|
|
409
|
-
if (!
|
|
410
|
-
|
|
411
|
-
if (
|
|
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(
|
|
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
|
-
|
|
349
|
+
action.consequences.push(...notification.consequences.slice(action.consequences.length));
|
|
421
350
|
}
|
|
422
351
|
}
|
|
423
352
|
}
|
|
424
353
|
else {
|
|
425
|
-
draft.actions.splice(
|
|
426
|
-
id:
|
|
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
|
-
(
|
|
436
|
-
|
|
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(
|
|
439
|
-
|
|
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(
|
|
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
|
-
|
|
462
|
-
action.activePlayers = draft.players.map(
|
|
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
|
-
|
|
465
|
-
|
|
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
|
|
395
|
+
action.animation = { id: draft.animationIncrement++, duration, step };
|
|
468
396
|
}
|
|
469
|
-
else if (step ===
|
|
397
|
+
else if (step === AnimationStep.BEFORE_MOVE) {
|
|
470
398
|
playNextMove(action, draft);
|
|
471
399
|
}
|
|
472
|
-
else if (action.played <= action.consequences.length
|
|
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
|
-
|
|
482
|
-
draft.legalMoves =
|
|
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
|
-
|
|
487
|
-
|
|
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
|
|
417
|
+
action.animation = { id: draft.animationIncrement++, duration, step };
|
|
491
418
|
}
|
|
492
|
-
else if (step ===
|
|
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
|
|
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(
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
actionsToReplay.forEach(
|
|
517
|
-
return
|
|
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
|
-
|
|
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(
|
|
529
|
-
if (
|
|
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(
|
|
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
|