Example #1
0
 private static void LoadNamesFilter()
 {
     try
     {
         if (File.Exists(NamesFilter))
         {
             foreach (var item01 in File.ReadLines(NamesFilter, Encoding.UTF8))
             {
                 if (string.IsNullOrWhiteSpace(item01) || item01 == string.Empty || item01[0] == '#')
                 {
                     continue;
                 }
                 if (item01[0] == '!' && item01.Length > 1)
                 {
                     s_words.AddRange(item01.Substring(1).ToLowerInvariant().Split(' ').Select(x => x.Trim()));
                 }
                 else if (item01[0] == '@' && item01.Length > 1)
                 {
                     s_regex.Add(new Regex(item01.Trim().Substring(1), RegexOptions.Compiled | RegexOptions.IgnoreCase));
                 }
                 else
                 {
                     s_exact.Add(item01.Trim().ToLowerInvariant());
                 }
             }
         }
         ServerLogger.LogInfo($"Loaded {s_exact.Count + s_words.Count + s_regex.Count} naming rule(s)");
     }
     catch (Exception exp)
     {
         ServerLogger.LogException(exp, $"Exception while loading names filter \"{NamesFilter}\"");
     }
 }
        protected virtual ReturnCode ConnectAndLogin()
        {
            _isPumpEnabled = false;
            var api = Api;

            if (api.IsConnected())
            {
                api.Disconnect();
            }

            Thread.Sleep(100);

            Debug.Assert(!_server.IsNullOrEmpty());
            _logger.LogInfo($"Try to connect to server {_server}");
            var code = api.Connect(_server);

            if (code == ReturnCode.RET_OK)
            {
                _logger.LogInfo($"Successfully connected to server {_server}");
            }
            else
            {
                LogError($"Unable to connect to server {_server}", code);
                return(code);
            }

            Debug.Assert(_login > 0 && !_password.IsNullOrEmpty());
            _logger.LogInfo($"Try to login to server {_server} with id {_login}");
            code = api.Login(_login, _password);

            if (code == ReturnCode.RET_OK)
            {
                _logger.LogInfo($"Successfully logged to server {_server} with id {_login}");
            }
            else
            {
                LogError($"Unable to login to server {_server} with id {_login}", code);
                return(code);
            }

            return(code);
        }
 private bool Load()
 {
     if (ServerDB.Connected && ServerDB.Ping())
     {
         ServerLogger.LogInfo($"Connected to database");
     }
     else
     {
         ServerLogger.LogError($"Couldn't connect to database: {Environment.NewLine}{ServerDB.ConnectionString}");
         return(false);
     }
     ServerLogger.LogInfo($"Data loaded: {DataMgr.Info}");
     return(true);
 }
        private async Task <bool> LoadAsync()
        {
            if (await ServerDB.PingAsync())
            {
                ServerLogger.LogInfo($"Connected to database");
            }
            else
            {
                ServerLogger.LogError($"Couldn't connect to database: {Environment.NewLine}{ServerDB.ConnectionString}");
                return(false);
            }
            await DataMgr.LoadAllAsync();

            ServerLogger.LogInfo($"Data loaded: {DataMgr.Info}");
            return(true);
        }
Example #5
0
 public static async Task DeleateAllOutdatedBansAsync()
 {
     try
     {
         using (var connection = await GetConnectionAsync())
         {
             using (var command = connection.CreateCommand())
             {
                 command.CommandText = $"DELETE FROM {tb_13} WHERE ban_end <= @now";
                 command.Parameters.AddWithValue("now", DateTime.Now);
                 ServerLogger.LogInfo($"Removed {await command.ExecuteNonQueryAsyncEx()} outdated ban/bans!");
             }
         }
     }
     catch (Exception exp)
     {
         ServerLogger.LogException(exp);
     }
 }
        private void UpdateStatus()
        {
            MasterPlayer target = null; Tuple <byte, string, short, DateTime> state;

            if (_status.ID != 0)
            {
                switch (_status.Status)
                {
                case OnlineStatus.Online:
                case OnlineStatus.Offline:
                case OnlineStatus.Blocker:
                case OnlineStatus.Blockee:
                case OnlineStatus.Incoming:
                    ServerLogger.LogInfo($"Player[{_player.Id}] received update friend status for user[{_status.ID}] status: {_status.Status}");
                    break;

                case OnlineStatus.Remove:
                    if (_server.TryGetByUserId(_status.ID, out target))
                    {
                        _save.Friends.Remove(_status.ID);
                        target._save.Friends.Remove(_user.ID);
                        _player.UpdateFriend(_status.Fill(target, OnlineStatus.Remove));
                        target._player.UpdateFriend(_status.Fill(this, OnlineStatus.Remove));
                    }
                    else
                    {
                        ServerLogger.LogInfo($"Player[{_player.Id}] received update friend status for offline user[{_status.ID}] status: {_status.Status}");
                    }
                    break;

                case OnlineStatus.Outgoing:
                    if (_server.TryGetByUserId(_status.ID, out target))
                    {
                        if (_save.Friends.TryGetValue(target._user.ID, out state))
                        {
                            _save.Friends[target._user.ID] =
                                new Tuple <byte, string, short, DateTime>((byte)OnlineStatus.Online, target._char.Pony.Name, (short)target._char.Pony.CutieMark0, DateTime.Now);
                            target._save.Friends[_user.ID] =
                                new Tuple <byte, string, short, DateTime>((byte)OnlineStatus.Online, _char.Pony.Name, (short)_char.Pony.CutieMark0, DateTime.Now);
                            _player.UpdateFriend(_status.Fill(target, OnlineStatus.Online));
                            target._player.UpdateFriend(_status.Fill(this, OnlineStatus.Online));
                        }
                        else
                        {
                            _save.Friends[target._user.ID] =
                                new Tuple <byte, string, short, DateTime>((byte)OnlineStatus.Outgoing, target._char.Pony.Name, (short)target._char.Pony.CutieMark0, DateTime.Now);
                            target._save.Friends[_user.ID] =
                                new Tuple <byte, string, short, DateTime>((byte)OnlineStatus.Incoming, _char.Pony.Name, (short)_char.Pony.CutieMark0, DateTime.Now);
                            target._player.UpdateFriend(_status.Fill(this, OnlineStatus.Incoming));
                            _player.UpdateFriend(_status.Fill(target, OnlineStatus.Outgoing));
                        }
                    }
                    else
                    {
                        ServerLogger.LogInfo($"Player[{_player.Id}] received update friend status for offline user[{_status.ID}] status: {_status.Status}");
                    }
                    break;
                }
                ServerDB.UpdateUserSave(_user.ID, _save);
                if (target != null)
                {
                    ServerDB.UpdateUserSave(target._user.ID, target._save);
                }
            }
            else if (_status.PlayerID != 0)
            {
                switch (_status.Status)
                {
                case OnlineStatus.Online:
                case OnlineStatus.Remove:
                case OnlineStatus.Offline:
                case OnlineStatus.Blocker:
                case OnlineStatus.Blockee:
                case OnlineStatus.Incoming:
                    ServerLogger.LogInfo($"Player[{_player.Id}] received update friend status for player[{_status.PlayerID}] status: {_status.Status}");
                    break;

                case OnlineStatus.Outgoing:
                    if (_server.TryGetById(_status.PlayerID, out target))
                    {
                        if (_save.Friends.TryGetValue(target._user.ID, out state))
                        {
                            _save.Friends[target._user.ID] =
                                new Tuple <byte, string, short, DateTime>((byte)OnlineStatus.Online, target._char.Pony.Name, (short)target._char.Pony.CutieMark0, DateTime.Now);
                            target._save.Friends[_user.ID] =
                                new Tuple <byte, string, short, DateTime>((byte)OnlineStatus.Online, _char.Pony.Name, (short)_char.Pony.CutieMark0, DateTime.Now);
                            _player.UpdateFriend(_status.Fill(target, OnlineStatus.Online));
                            target._player.UpdateFriend(_status.Fill(this, OnlineStatus.Online));
                        }
                        else
                        {
                            _save.Friends[target._user.ID] =
                                new Tuple <byte, string, short, DateTime>((byte)OnlineStatus.Outgoing, target._char.Pony.Name, (short)target._char.Pony.CutieMark0, DateTime.Now);
                            target._save.Friends[_user.ID] =
                                new Tuple <byte, string, short, DateTime>((byte)OnlineStatus.Incoming, _char.Pony.Name, (short)_char.Pony.CutieMark0, DateTime.Now);
                            target._player.UpdateFriend(_status.Fill(this, OnlineStatus.Incoming));
                            _player.UpdateFriend(_status.Fill(target, OnlineStatus.Outgoing));
                        }
                    }
                    break;
                }
            }
        }
        //TODO: handleSSPFollowerRequest
        public async Task <AddSSPFollowerResponse> AddSSPFollower(
            AddSSPFollowerRequest ptr, CancellationToken cancellationToken)
        {
            AddSSPFollowerResponse ptrResp = null;

            m_ptrLogger.LogOk("START");

            if (ptr._dataTransMode == DT_TRANS_ADD)
            {
                await _followerService.InsertSSPFollower(ptr._sspfollower, cancellationToken);

                bool ret = true;
                ptrResp                = new AddSSPFollowerResponse(); //MT_AddSSPFollowerResponse_ID
                ptrResp._retCode       = ret == true ? RC_OK : RC_ERROR;
                ptrResp._dataTransMode = ptr._dataTransMode;
                CppHelper.memcpy(ref ptrResp._sspfollower, ptr._sspfollower);
                //SentDataUsingSocketID(ptrResp, MT_AddSSPFollowerResponse_ID, ptr._header._socketID);
                if (ret)
                {
                    //await AddFollowerAsync(ptr._sspfollower._sspMT4Login, ptr._sspfollower._followerMT4Login,
                    //    ptr._sspfollower._followerMT4ServerIndex, cancellationToken);
                    //await AddFollowerVolumeAsync(ptr._sspfollower._sspSignalIndex,
                    //    ptr._sspfollower._followerMT4ServerIndex, ptr._sspfollower._followerMT4Login,
                    //    ptr._sspfollower._followervolume, cancellationToken);

                    m_ptrLogger.LogInfo(
                        "SSP follower added. Follower master login: %d SSP master login : %d SignalIndex: %d",
                        ptr._sspfollower._followerMasterLogin, ptr._sspfollower._sspMasterLogin, ptr._sspfollower._sspSignalIndex);
                }
                else
                {
                    m_ptrLogger.LogError(
                        "Unable to add SSP follower. Follower master login: %d SSP master login : %d SignalIndex: %d",
                        ptr._sspfollower._followerMasterLogin, ptr._sspfollower._sspMasterLogin, ptr._sspfollower._sspSignalIndex);
                }
            }
            else if (ptr._dataTransMode == DT_TRANS_MODIFY)
            {
                await _followerService.ModifySSPFollower(ptr._sspfollower, cancellationToken);

                bool ret = true;
                ptrResp                = new AddSSPFollowerResponse(); // MT_AddSSPFollowerResponse_ID
                ptrResp._retCode       = ret == true ? RC_OK : RC_ERROR;
                ptrResp._dataTransMode = ptr._dataTransMode;
                CppHelper.memcpy(ref ptrResp._sspfollower, ptr._sspfollower);
                //SentDataUsingSocketID(ptrResp, MT_AddSSPFollowerResponse_ID, ptr._header._socketID);
                if (ret)
                {
                    //await UpdateFollowerVolumeAsync(ptr._sspfollower._sspSignalIndex, ptr._sspfollower._followerMT4ServerIndex,
                    //    ptr._sspfollower._followerMT4Login, ptr._sspfollower._followervolume, cancellationToken);

                    m_ptrLogger.LogInfo(
                        "SSP follower modified. Follower master login: %d SSP master login : %d SignalIndex: %d",
                        ptr._sspfollower._followerMasterLogin, ptr._sspfollower._sspMasterLogin, ptr._sspfollower._sspSignalIndex);
                }
                else
                {
                    m_ptrLogger.LogError(
                        "Unable to modify SSP follower. Follower master login: %d SSP master login : %d SignalIndex: %d",
                        ptr._sspfollower._followerMasterLogin, ptr._sspfollower._sspMasterLogin, ptr._sspfollower._sspSignalIndex);
                }
            }
            else if (ptr._dataTransMode == DT_TRANS_DELETE)
            {
                await _followerService.DeleteSSPFollower(ptr._sspfollower, cancellationToken);

                bool ret = true;
                ptrResp                = new AddSSPFollowerResponse(); // MT_AddSSPFollowerResponse_ID
                ptrResp._retCode       = ret == true ? RC_OK : RC_ERROR;
                ptrResp._dataTransMode = ptr._dataTransMode;
                CppHelper.memcpy(ref ptrResp._sspfollower, ptr._sspfollower);
                //SentDataUsingSocketID(ptrResp, MT_AddSSPFollowerResponse_ID, ptr._header._socketID);
                if (ret)
                {
                    //await RemoveFollowerAsync(ptr._sspfollower._sspMT4Login,
                    //    ptr._sspfollower._followerMT4Login,
                    //    ptr._sspfollower._followerMT4ServerIndex, cancellationToken);
                    //await RemoveFollowerVolumeAsync(ptr._sspfollower._sspSignalIndex,
                    //    ptr._sspfollower._followerMT4ServerIndex, ptr._sspfollower._followerMT4Login,
                    //    ptr._sspfollower._followervolume, cancellationToken);

                    m_ptrLogger.LogInfo(
                        "SSP follower deleted. Follower master login: %d SSP master login : %d SignalIndex: %d",
                        ptr._sspfollower._followerMasterLogin, ptr._sspfollower._sspMasterLogin, ptr._sspfollower._sspSignalIndex);
                }
                else
                {
                    m_ptrLogger.LogError(
                        "Unable to delete SSP follower. Follower master login: %d SSP master login : %d SignalIndex: %d",
                        ptr._sspfollower._followerMasterLogin, ptr._sspfollower._sspMasterLogin, ptr._sspfollower._sspSignalIndex);
                }
            }

            m_ptrLogger.LogOk("END");
            return(ptrResp);
        }
Example #8
0
        public async Task StartAsync(CancellationToken cancellationToken, bool startThreads = true)
        {
            Debug.Assert(!_isStarting);

            if (_isStarting)
            {
                throw new InvalidOperationException();
            }

            _isStarting = true;
            _factory    = ManagerHost.CreateFactory(HandleException);
            CheckFactory();
            _factory.WinsockStartup();
            var m_masterUserSetting = await _masterSettingsService.GetCachedMasterSettingsAsync(cancellationToken);

            var mt4Accounts = await _mt4AccountService.GetAllMT4Accounts(cancellationToken);

            lock (_mt4LoginsByServerIndex)
                foreach (var mt4Account in mt4Accounts)
                {
                    AddMt4LoginNoLock(mt4Account._mt4ServerIndex, mt4Account._mt4Login);
                }

            m_ptrLogger.LogInfo("Initializng DEMO MT4 Manager");
            Debug.Assert(_demo == null);
            _demo = new WitFXServerConnector(
                eMT4ServerType.SRV_TYPE_DEMO, this, _factory,
                m_masterUserSetting._demoServer, m_masterUserSetting._demoManagerLogin,
                m_masterUserSetting._demoManagerPassword, _baseLogger, true, true,
                startThreads: startThreads);

            CppHelper.memcpy(ref _demo.m_masterUserSetting, m_masterUserSetting);

            //_demo.setMT4SocialUsers(m_mapMT4MasterLogin);
            //_demo.setMT4ResponseFunPtr(onMT4Response);
            //_demo.setMarketDataFunPtr(onMarketData);
            //_demo.setSymbolInfoFunPtr(onSymbolInfo);
            //_demo.setOnTradeFunPtr(onTradeResponse);
            //_demo.setOnMarginFunPtr(onMarginLevelResponse);

            m_ptrLogger.LogInfo("Initializng LIVE MT4 Manager");
            Debug.Assert(_live == null);
            _live = new WitFXServerConnector(
                eMT4ServerType.SRV_TYPE_LIVE, this, _factory,
                m_masterUserSetting._liveServer, m_masterUserSetting._liveManagerLogin,
                m_masterUserSetting._liveManagerPassword, _baseLogger, true, true,
                startThreads: startThreads);

            CppHelper.memcpy(ref _live.m_masterUserSetting, m_masterUserSetting);

            //_live.setMT4SocialUsers(m_mapMT4MasterLogin);
            //_live.setMT4ResponseFunPtr(onMT4Response);
            //_live.setOnTradeFunPtr(onTradeResponse);
            //_live.setOnMarginFunPtr(onMarginLevelResponse);

            if (startThreads)
            {
                if (_demo.IsAPIValid())
                {
                    m_ptrLogger.LogInfo("Starting DEMO MT4 Manager connection...");
                    _demo.startNormalManager();
                }
                else
                {
                    m_ptrLogger.LogError("MT4 MANAGER API IS NOT VALID");
                }
                if (_live.IsAPIValid())
                {
                    m_ptrLogger.LogInfo("Starting LIVE MT4 Manager connection...");
                    _live.startNormalManager();
                }
                else
                {
                    m_ptrLogger.LogError("MT4 MANAGER API IS NOT VALID");
                }

                var demoUpdateWorker = _updateWorkerFactory(eMT4ServerType.SRV_TYPE_DEMO);
                await demoUpdateWorker.ConfigureAsync(cancellationToken);

                _workerManager.StartWorker(demoUpdateWorker);

                var liveUpdateWorker = _updateWorkerFactory(eMT4ServerType.SRV_TYPE_LIVE);
                await liveUpdateWorker.ConfigureAsync(cancellationToken);

                _workerManager.StartWorker(liveUpdateWorker);
            }
        }