public async Task <ConnectResult> SendAccountAuthentication(ConnectData connectData)
        {
            var response = new SocketMessage();

            if (_socketManager.IsReconnecting)
            {
                response = await SendStandardMessage(
                    RouteProvider.AUTHENTICATION_RECONNECT,
                    RouteProvider.AuthenticationReconnect(connectData, _socketManager.ActiveCharacterId));
            }
            else
            {
                response = await SendStandardMessage(RouteProvider.LOGIN, RouteProvider.Login(connectData));
            }

            var loginData = SocketUtilities.ParseDataFromResponse <LoginDataDTO>(response.Response);

            ConnectResult connectResult = _socketManager.GetConnectResult();

            connectResult.IsConnected  = true;
            connectResult.AccessToken  = loginData?.AccessToken;
            connectResult.TW2AccountId = loginData?.PlayerId;

            _socketManager.ConnectData.AccessToken = loginData?.AccessToken;

            DataEvents.InvokeLoginDataAvailable(loginData);
            DataEvents.InvokeConnectionResult(connectResult);

            return(connectResult);
        }
        public void ParseResponseAsync(string response)
        {
            if (string.IsNullOrEmpty(response))
            {
                Log.Info("The response was null/empty");
                return;
            }

            // open connection or ping response
            if (response == "40" || response == "3")
            {
                return;
            }


            response = SocketUtilities.CleanResponse(response);

            var errorMessage = new ErrorMessageDTO();
            var sytemError   = new SystemErrorDTO();


            switch (string.Empty)
            {
            case RouteProvider.CHARACTER_SELECTION_FAILED:
                errorMessage = SocketUtilities.ParseDataFromResponse <ErrorMessageDTO>(response);
                Log.Error($"Character selection failed: {errorMessage.Message}");

                StopConnection();
                break;


            case RouteProvider.SYSTEM_ERROR:
                sytemError = SocketUtilities.ParseDataFromResponse <SystemErrorDTO>(response);

                ParseSystemError(response);
                Log.Error($"Character selection failed: {errorMessage.Message}");

                StopConnection();
                break;

            case RouteProvider.MESSAGE_ERROR:
                errorMessage = SocketUtilities.ParseDataFromResponse <ErrorMessageDTO>(response);
                Log.Error($"Socket Error: {errorMessage.ErrorCode} - {errorMessage.Message}");
                StopConnection();
                break;

            case RouteProvider.EXCEPTION_ERROR:
                sytemError = SocketUtilities.ParseDataFromResponse <SystemErrorDTO>(response);
                Log.Error($"Server exception error: {errorMessage.Message}");

                AddToConnectionLog("THE SERVER IS MOST LIKELY DOWN!");
                StopConnection();
                break;

            default:

                break;
            }
        }
        public async Task <DateTime> GetSystemTimeAsync()
        {
            var response = await SendStandardMessage(RouteProvider.SYSTEM_GETTIME);

            var systemTime = SocketUtilities.ParseDataFromResponse <SystemTimeDTO>(response.Response);

            return(DateTime.UnixEpoch.AddSeconds(systemTime.Time + systemTime.Offset));
        }
        private async Task <bool> SendGetGroups()
        {
            var response = await SendStandardMessage(RouteProvider.GET_GROUPS);

            var groups = SocketUtilities.ParseDataFromResponse <GroupsDTO>(response.Response)?.Groups?.ToList();

            DataEvents.InvokeGroupsDataAvailable(groups.ToList <IGroup>());
            return(groups.Count > 0);
        }
        /// <summary>
        /// This will request the incoming commands and the events on the player
        /// </summary>
        /// <returns></returns>
        private async Task <bool> SendGetGlobalInformation()
        {
            var response = await SendStandardMessage(RouteProvider.GLOBALINFORMATION_GETINFO);

            var globalInfo = SocketUtilities.ParseDataFromResponse <GlobalInformationDTO>(response.Response);

            //TODO Store commands in DB
            return(true);
        }
        private void ParseSystemError(string response)
        {
            var systemError = SocketUtilities.ParseDataFromResponse <SystemErrorDTO>(response);

            if (systemError.Cause == RouteProvider.LOGIN)
            {
                //ConnectResult connectResult = _socketManager.GetConnectResult();
                //connectResult.IsConnected = false;
                //connectResult.Message = systemError.Message;

                //_dataManager.SetConnectionResult(connectResult);
            }
        }
        public async Task <List <IVillage> > SendGetVillagesByArea(int x, int y, int width = 25, int height = 25)
        {
            object dataObject = new
            {
                height,
                width,
                x,
                y,
            };

            var response = await SendStandardMessage(RouteProvider.MAP_GETVILLAGESBYAREA, dataObject);

            var villageList = SocketUtilities.ParseDataFromResponse <VillagesDTO>(response.Response).Villages;

            return(villageList.ToList <IVillage>());
        }
        private async Task <bool> SendCharacterSelect(ICharacter selectedCharacter)
        {
            if (selectedCharacter == null)
            {
                return(false);
            }

            object dataObject = RouteProvider.SelectCharacter(selectedCharacter);

            var response = await SendStandardMessage(RouteProvider.SELECT_CHARACTER, dataObject);

            var charSelected = SocketUtilities.ParseDataFromResponse <CharacterSelectedDTO>(response.Response);

            _socketManager.ActiveCharacterId = charSelected.OwnerId;
            _socketManager.ActiveWorldId     = charSelected.WorldId;

            return(true);
        }
Esempio n. 9
0
        /// <summary>
        /// Used to determine the type of response received and to parse the initial connection configuration.
        /// </summary>
        /// <param name="response">The response of the websocket</param>
        /// <returns></returns>
        private void ParseSocketResponse(string response)
        {
            if (string.IsNullOrEmpty(response) || !response.StartsWith("0{\"sid\":"))
            {
                return;
            }

            response = SocketUtilities.CleanResponse(response);

            SocketResponse socketResponse = new SocketResponse();

            JsonSerializerSettings serializerSettings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            };

            try
            {
                socketResponse = JsonConvert.DeserializeObject <SocketResponse>(response, serializerSettings);
            }
            catch (Exception e)
            {
                Log.Error(e, $"Could not deserialize the following string in {nameof(ParseSocketResponse)}: {Environment.NewLine} {response}");
            }

            if (!string.IsNullOrEmpty(socketResponse.SessionID))
            {
                _sessionId = socketResponse.SessionID;
            }

            // The pingTimeout and pingInterval are always send in the same message.
            if (socketResponse.PingTimeout > 1000 && socketResponse.PingInterval > 1000)
            {
                _pingTimeout  = socketResponse.PingTimeout;
                _pingInterval = socketResponse.PingInterval;
            }
        }
Esempio n. 10
0
        public void AddToConnectionLog(string message)
        {
            lock (lockObj)
            {
                // Filter out the header
                string header = SocketUtilities.ParseHeaderStringFromResponse(message);
                if (!string.IsNullOrEmpty(header))
                {
                    message = message.Replace(header, string.Empty);
                }

                // Filter out the msg tag
                message = message.Replace("\"msg\",", string.Empty);

                // Limit the message length
                if (message.Length > 2000)
                {
                    message = message.Substring(0, 2000);
                }

                Log.Info(message);
                ConnectionLogUpdated.Invoke(this, message + Environment.NewLine);
            }
        }
        public async Task <List <IVillage> > GetVillagesByAutocomplete(string nameToSearch)
        {
            object data = new VillageAutocompleteDTO
            {
                Types = new List <string>
                {
                    "village"
                },
                String = nameToSearch,
                Amount = 5
            };

            var response = await SendStandardMessage(RouteProvider.AUTOCOMPLETION_AUTOCOMPLETE, data);

            var autocompleteDto = SocketUtilities.ParseDataFromResponse <AutocompleteDTO>(response.Response);

            //Set the worldId for each village
            foreach (VillageDTO villageDto in autocompleteDto.Result.Village)
            {
                villageDto.WorldId = _socketManager.GetCurrentWorldId();
            }

            return(autocompleteDto.Result.Village.ToList <IVillage>());
        }
        private async Task <bool> SendGetCharacterInfo()
        {
            var response = await SendStandardMessage(RouteProvider.CHARACTER_GETINFO);

            var characterData = SocketUtilities.ParseDataFromResponse <CharacterDataDTO>(response.Response);

            if (characterData != null)
            {
                foreach (IVillage village in characterData.Villages)
                {
                    village.CharacterId = _socketManager.ActiveCharacterId;
                    village.WorldId     = _socketManager.ActiveWorldId;
                }

                DataEvents.InvokeCharacterDataAvailable(characterData);
            }

            // Take the first village as the active village, its unclear how TW2 determines te active village.
            // This is done somewhere in javascript but not communicated through websockets.
            // TODO Find out how the active village is determined
            activeVillage = characterData.Villages[0];

            return(characterData != null);
        }