public PlayerViewModelFactory( IStore <GameStateModel> gameStateStore, ShipStatusViewModelFactory shipStatusFactory, IStore <ViewStateModel> viewStateStore) { _gameStateStore = gameStateStore; _shipStatusFactory = shipStatusFactory; _viewStateStore = viewStateStore; }
public PlayerViewModel( IStore <GameStateModel> gameStateStore, GamePlayer player, ShipStatusViewModelFactory shipStatusFactory, IStore <ViewStateModel> viewStateStore) { var playerInfo = ((player == GamePlayer.Player1) ? gameStateStore.Select(gameState => ( definition: gameState.Definition.Player1, state: gameState.Player1 )) : gameStateStore.Select(gameState => ( definition: gameState.Definition.Player2, state: gameState.Player2 ))) .ToReactiveProperty(); IsActive = viewStateStore .Select(viewState => (viewState.ActivePlayer == player)) .ToReactiveProperty(); Name = playerInfo .Select(playerInfo => playerInfo.definition.Name) .ToReactiveProperty(); PlayTime = playerInfo .Select(playerInfo => playerInfo.state.PlayTime) .ToReactiveProperty(); ShipStatuses = gameStateStore .Select(gameState => gameState.Definition.Ships.Length) .DistinctUntilChanged() .Select(shipCount => Enumerable.Range(0, shipCount) .Select(shipIndex => shipStatusFactory.Create( player: player, shipIndex: shipIndex)) .ToImmutableArray()) .ToReactiveProperty(); Wins = playerInfo .Select(player => player.state.Wins) .ToReactiveProperty(); }