コード例 #1
0
        public async Task <Dictionary <string, string> > Load(string s)
        {
            if (s == "")
            {
                return(null);
            }

            Dictionary <string, string> sections = StaticHelpers.GetSections(s);

            if (_game.Deserialize(sections, _view.Deck) == false)
            {
                return(null);
            }

            Dictionary <string, string> game = StaticHelpers.DeserializeDictionary(sections["State"]);

            if (sections == null)
            {
                return(null);
            }

            if (game["Version"] != SERIALIZATION_VERSION)
            {
                return(null);
            }

            _gameState               = (GameState)Enum.Parse(typeof(GameState), game["GameState"]);
            _dealer                  = (PlayerType)Enum.Parse(typeof(PlayerType), game["Dealer"]);
            _totalCardsDropped       = Convert.ToInt32(game["TotalCardsDropped"]);
            _nPlayerCountingPoint    = Convert.ToInt32(game["PlayerCountingPoints"]);
            _nPlayerPointsThisTurn   = Convert.ToInt32(game["PlayerPointsThisTurn"]);
            _nComputerCountingPoint  = Convert.ToInt32(game["ComputerCountingPoints"]);
            _nComputerPointsThisTurn = Convert.ToInt32(game["ComputerPointsThisTurn"]);
            _nCribPointsThisTurn     = Convert.ToInt32(game["CribPointsThisTurn"]);
            int playerScore   = Convert.ToInt32(game["PlayerScore"]);
            int computerScore = Convert.ToInt32(game["ComputerScore"]);


            _hfs      = _game.GetHfs();
            _view.Hfs = _hfs;

            Dictionary <string, string> grids = StaticHelpers.DeserializeDictionary(sections["Grids"]);

            List <CardView> PlayerCards   = StaticHelpers.DeserializeToList(grids["Player"], _view.Deck);
            List <CardView> ComputerCards = StaticHelpers.DeserializeToList(grids["Computer"], _view.Deck);
            List <CardView> cribCards     = StaticHelpers.DeserializeToList(grids["Crib"], _view.Deck);



            CardView        SharedCard  = StaticHelpers.CardFromString(grids["SharedCard"], _view.Deck);
            List <CardView> playedCards = StaticHelpers.DeserializeToList(grids["Played"], _view.Deck);

            List <Task <object> > taskList = new List <Task <object> >();

            _view.MoveCards(taskList, playedCards, _view.GridPlayedCards);
            _view.MoveCards(taskList, cribCards, _view.GridCrib);
            _view.MoveCards(taskList, PlayerCards, _view.GridPlayer);
            _view.MoveCards(taskList, ComputerCards, _view.GridComputer);

            if (_gameState == GameState.PlayerGiveToCrib)
            {
                _crib.AddRange(playedCards);
            }

            await Task.WhenAll(taskList);

            SharedCard.BoostZindex(ZIndexBoost.SmallBoost);

            _game.UpdateScoreDirect(PlayerType.Player, ScoreType.Saved, playerScore);     // give the points to the player
            _game.UpdateScoreDirect(PlayerType.Computer, ScoreType.Saved, computerScore); // give the points to the player

            await UiState.AddScore(PlayerType.Player, playerScore);

            await UiState.AddScore(PlayerType.Computer, computerScore);

            _view.CountControl.Count = _game.CurrentCount;

            await FixUpUiState();

            string scoreHistory = "";

            if (sections.TryGetValue("Score History", out scoreHistory))
            {
                await _view.Load(scoreHistory);
            }

            if (_gameState == GameState.PlayerScoreHand)
            {
                //
                //  if we saved in the state where the player is supposed to score the hard, we need to drive the state machine
                //  through the completion of the GameState.ScoreHand states
                if (_dealer == PlayerType.Player)
                {
                    await SetState(GameState.PlayerScoreHand);
                    await SetState(GameState.ShowCrib);
                    await SetState(GameState.PlayerScoreCrib);

                    _gameState = GameState.EndOfHand;
                }
                else
                {
                    _gameState = GameState.ScoreHands;
                }
            }
            else if (_gameState == GameState.PlayerScoreCrib)
            {
                await SetState(GameState.PlayerScoreCrib);

                _gameState = GameState.EndOfHand;
            }



            SetStateAsync(_gameState);


            return(sections);
        }