Ejemplo n.º 1
0
        public static TcpConnectionHandler GetInstance(ITcpLogger logger, CommandHandler commandHandler, PlayerHandler playerHandler)
        {
            if (TcpConnectionHandler._instance == null)
            {
                TcpConnectionHandler._instance = new TcpConnectionHandler();
            }

            TcpConnectionHandler._instance._logger         = logger;
            TcpConnectionHandler._instance._commandHandler = commandHandler;
            TcpConnectionHandler._instance._playerHandler  = playerHandler;
            return(TcpConnectionHandler._instance);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Starts the game and its management/handlers etc.
        /// </summary>
        public async void StartAsync(MySqlConnectionSettings mySqlConnSettings)
        {
            if (_loadingInProgress)
            {
                _logger.UpdateLog("Game loading in progress!");
                return;
            }

            _loadingInProgress = true;

            try
            {
                if (this.State == GameState.Active)
                {
                    _logger.UpdateLog("The game is already active!");
                    return;
                }

                _mySqlConnSettings = mySqlConnSettings;
                int counter;

                try
                {
                    //GAME DATA
                    using (MySqlDbManager gwdDbManager = new MySqlDbManager(_mySqlConnSettings))
                    {
                        #region Connection starting
                        //CONNECTION STARTING
                        _logger.UpdateLog("Starting connection for data loading...");
                        bool connectionSuccess = false;
                        using (BoxedData connStartData = await gwdDbManager.StartConnectionTaskStart())
                        {
                            if (!String.IsNullOrEmpty(connStartData.Msg))
                            {
                                _logger.UpdateLog(connStartData.Msg);
                            }
                            connectionSuccess = (bool)connStartData.Data;
                        }

                        if (!connectionSuccess)
                        {
                            throw new Exception("connection not estabilished!");
                        }

                        #endregion

                        #region Account data
                        //ACCOUNT DATA
                        _accountData = new AccountData(_logger);
                        _accountData.LoadAccountsAsync(gwdDbManager);

                        while (!_accountData.IsLoaded)
                        {
                            await Task.Factory.StartNew(() => Thread.Sleep(1000));

                            if (_accountData.LoadingError)
                            {
                                throw new Exception("account data loading failure!");
                            }
                        }
                        //_accountData.ShowAccountsInLog(); //NOTE: only for testing (shows secret account details)!

                        #endregion

                        #region Game world data
                        //GAME DATA LOADING
                        _gameWorldData = new GameWorldData(_logger);
                        _gameWorldData.LoadWorldDataAsync(gwdDbManager);
                        counter = 0;
                        while (!_gameWorldData.IsLoaded)
                        {
                            await Task.Factory.StartNew(() => Thread.Sleep(1000));

                            counter++;

                            if (_gameWorldData.LoadingError)
                            {
                                throw new Exception("game world data loading failure!");
                            }
                        }

                        _logger.UpdateLog($"Game data loading ended in {counter} seconds.");

                        #endregion
                    }

                    //PLAYER HANDLER
                    _playerHandler = new PlayerHandler(_logger);

                    //GAME STATE HANDLER
                    _gameStateHandler = new GameStateHandler(_logger, _gameWorldData, _gameWorldData, _playerHandler);

                    //CHAT HANDLER
                    _chatHandler = new ChatHandler(_logger, _gameWorldData, _playerHandler);

                    //COMMAND HANDLER
                    _commandHandler = new CommandHandler(_logger);
                    _commandHandler.RegisterCommandHandlingStrategy(new CmdPingStrategy());
                    _commandHandler.RegisterCommandHandlingStrategy(new MoveCharRequestStrategy(_logger, _gameStateHandler, _gameWorldData, _playerHandler));
                    _commandHandler.RegisterCommandHandlingStrategy(new CmdChatStrategy(_logger, _chatHandler));
                    _commandHandler.RegisterCommandHandlingStrategy(new SwitchPlaceRequestStrategy(_logger, _gameStateHandler, _gameWorldData, _gameWorldData, _playerHandler));
                    _commandHandler.RegisterCommandHandlingStrategy(new CmdGetLocationCharsStrategy(_logger, _gameWorldData));
                    _commandHandler.RegisterCommandHandlingStrategy(new CmdGetWorldDetailsStrategy(_logger, _gameWorldData));
                    _commandHandler.RegisterCommandHandlingStrategy(new CmdLoginStrategy(_logger, _accountData, _playerHandler));
                    _commandHandler.RegisterCommandHandlingStrategy(new CmdLogoutStrategy(_playerHandler));
                    _commandHandler.RegisterCommandHandlingStrategy(new CmdGetAccountCharsStrategy(_logger, _accountData, _gameWorldData));
                    _commandHandler.RegisterCommandHandlingStrategy(new CmdChooseCharacterStrategy(_logger, _accountData, _gameWorldData, _playerHandler));

                    //CONNECTION HANDLER
                    _tcpConnectionHandler = TcpConnectionHandler.GetInstance(_tcpLogger, _commandHandler, _playerHandler);
                    _tcpConnectionHandler.StartConnection(_uiHandler.GetTcpConnectionSettings());

                    //REGULAR OPERATION HANDLER
                    _regularOperationHandler = new RegularOperationHandler(_logger);

                    //ETC
                    this.State = GameState.Active;
                }
                catch (Exception exception)
                {
                    this.State = GameState.Error;
                    _logger.UpdateLog($"An error occured during game starting: {exception.Message}");
                }
            }
            finally
            {
                _logger.UpdateLog($"*** ENDED WITH GAME STATE [{this.State.ToString()}]");
                _loadingInProgress = false;
            }
        }