Beispiel #1
0
        /// <summary>
        /// ゲーム開始
        /// </summary>
        private void StartGame()
        {
            if (!CanStartGame())
            {
                return;
            }

            GameHistory.Clear();

            var master = new GameMaster(MasterName);

            master.OutputActionEvent += OutputParsonAction;

            foreach (var name in PlayerList)
            {
                var player = new Player(name);

                player.OutputActionEvent += OutputParsonAction;

                master.AddPlayer(player);
            }

            Sim = new GameSimulator(master, () =>
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    IsGamePlaying = false;
                    Sim           = null;
                    RaiseCommandCanExecute();
                });
            });

            Sim.RunSimAsync();
            IsGamePlaying = true;
            RaiseCommandCanExecute();
        }
        public async Task <GameState> LoadHistoryGetState()
        {
            var       tokens    = accountHandler.FindTokenByMetadata(Player1Address, "GameId", Id.ToString());
            GameState lastState = null;

            GameHistory.Clear();
            GameHistoryTxIds.Clear();
            //var tok = tokens.Values.OrderBy(t => t.TimeStamp);

            foreach (var t in tokens)
            {
                try
                {
                    if (t.Value.Metadata != null)
                    {
                        if (t.Value.Metadata.TryGetValue("GameData", out var gameData))
                        {
                            var parsedData = JsonConvert.DeserializeObject <ChessGameDto>(gameData);

                            if (parsedData != null)
                            {
                                if (parsedData.GameId == Id.ToString() && parsedData.Type == GameDtoTypes.ChessGame)
                                {
                                    //GameHistoryTxIds.Add(t.Key);

                                    if (parsedData.Players.Count > 0)
                                    {
                                        Players = parsedData.Players;
                                    }

                                    parsedData.TxId      = t.Key;
                                    parsedData.TimeStamp = t.Value.TimeStamp;
                                    GameHistory.Add(parsedData);
                                }

                                lastState = JsonConvert.DeserializeObject <GameState>(parsedData.GameState);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error("Chess Game - wrong format of gameData in token metadata. Cannot load history", ex);
                    return(null);
                }
            }

            var ghist = GameHistory.OrderBy(t => t.TimeStamp);

            GameHistory = ghist.ToList <ChessGameDto>();
            foreach (var gh in GameHistory)
            {
                GameHistoryTxIds.Add(gh.TxId);
            }

            var last = GameHistoryTxIds.LastOrDefault();

            if (last != null)
            {
                ActualStateTxId = last;
            }

            return(await Task.FromResult(lastState));
        }