Exemple #1
0
        public SetupGamespaceViewModel(
            SetupGamespaceBoardPositionViewModelFactory boardPositionFactory,
            GameBoardColumnHeadingsViewModel columnHeadings,
            IStore <GameStateModel> gameStateStore,
            Random random,
            GameBoardRowHeadingsViewModel rowHeadings,
            SetupGamespaceShipSegmentViewModelFactory shipSegmentFactory,
            IStore <ViewStateModel> viewStateStore)
            : base(
                boardPositionFactory.Create,
                columnHeadings,
                gameStateStore,
                rowHeadings)
        {
            var activePlayer = viewStateStore
                               .Select(viewState => viewState.ActivePlayer)
                               .WhereNotNull()
                               .DistinctUntilChanged()
                               .ShareReplay(1);

            CompleteSetupCommand = ReactiveCommand.Create(
                execute: activePlayer
                .Select(activePlayer => new Action(() =>
            {
                gameStateStore.Dispatch(new CompleteSetupAction(activePlayer));
                viewStateStore.Dispatch(new ToggleActivePlayerAction());
            })),
                canExecute: activePlayer
                .Select(activePlayer => gameStateStore
                        .Select(BoardSelectors.IsValid[activePlayer]))
                .Switch()
                .DistinctUntilChanged());

            RandomizeShipsCommand = ReactiveCommand.Create(
                activePlayer
                .Select(activePlayer => new Action(() => gameStateStore.Dispatch(new RandomizeShipsAction(
                                                                                     activePlayer,
                                                                                     random)))));

            ShipSegments = gameStateStore
                           .Select(gameState => gameState.Definition.Ships)
                           .DistinctUntilChanged()
                           .Select(definitions => definitions
                                   .SelectMany((ship, shipIndex) => ship.Segments
                                               .Select(segment => shipSegmentFactory.Create(segment, shipIndex)))
                                   .ToImmutableArray())
                           .ToReactiveProperty();
        }
        public RunningGamespaceViewModel(
            RunningGamespaceBoardPositionViewModelFactory boardPositionFactory,
            GameBoardColumnHeadingsViewModel columnHeadings,
            IStore <GameStateModel> gameStateStore,
            GameBoardRowHeadingsViewModel rowHeadings)
            : base(
                boardPositionFactory.Create,
                columnHeadings,
                gameStateStore,
                rowHeadings)
        {
            EndTurnCommand = ReactiveCommand.Create(
                execute:    () => gameStateStore.Dispatch(new EndTurnAction()),
                canExecute: gameStateStore
                .Select(gameState => (gameState.CurrentPlayer == GamePlayer.Player1)
                        ? gameState.Player1.HasMissed
                        : gameState.Player2.HasMissed)
                .DistinctUntilChanged());

            TogglePauseCommand = ReactiveCommand.Create(
                () => gameStateStore.Dispatch(new TogglePauseAction()));
        }
        public CompletedGamespaceViewModel(
            CompletedGamespaceBoardPositionViewModelFactory boardPositionFactory,
            GameBoardColumnHeadingsViewModel columnHeadings,
            IStore <GameStateModel> gameStateStore,
            Random random,
            GameBoardRowHeadingsViewModel rowHeadings,
            IStore <ViewStateModel> viewStateStore)
            : base(
                boardPositionFactory.Create,
                columnHeadings,
                gameStateStore,
                rowHeadings)
        {
            BeginSetupCommand = ReactiveCommand.Create(() =>
            {
                gameStateStore.Dispatch(new BeginSetupAction());
                gameStateStore.Dispatch(new RandomizeShipsAction(GamePlayer.Player1, random));
                gameStateStore.Dispatch(new RandomizeShipsAction(GamePlayer.Player2, random));
                viewStateStore.Dispatch(new SetActivePlayerAction(GamePlayer.Player1));
            });

            ToggleActivePlayerCommand = ReactiveCommand.Create(() => viewStateStore.Dispatch(new ToggleActivePlayerAction()));
        }