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