Exemplo n.º 1
0
        private GameOperationResponse HandleRetrieveCharacters(int sessionId, RetrieveCharacters operation)
        {
            try
            {
                var characters = this.application.CharacterDatabase.Query <PlayerData>("PlayerData/ByUsername")
                                 //.Customize(x => x.WaitForNonStaleResultsAsOfNow())
                                 .Where(chr => chr.Username.Equals(operation.Username, StringComparison.CurrentCultureIgnoreCase))
                                 .Select(chr => new { chr.Name, chr.Race, chr.Origin, chr.Level })
                                 .ToArray();

                if (characters.Length > 0)
                {
                    var characterCollection = new CharacterStructure[characters.Length];
                    for (var i = 0; i < characters.Length; i++)
                    {
                        var chr = characters[i];
                        characterCollection[i] = new CharacterStructure(chr.Race, chr.Origin, chr.Level, chr.Name);
                    }

                    return(new RetrieveCharactersResponse(operation.OperationCode)
                    {
                        ReturnCode = (short)ResultCode.Ok,
                        Characters = characterCollection
                    });
                }

                return(operation.GetErrorResponse((short)ResultCode.Ok));
            }
            catch (Exception e)
            {
                _logger.Error(e);
                return(operation.GetErrorResponse((short)ResultCode.Fail));
            }
        }
Exemplo n.º 2
0
        private GameOperationResponse HandleCreateNewUser(int sessionId, CreateUser operation)
        {
            try
            {
                var username         = operation.Username.ToUpper();
                var existingUserData = this.application.UserDatabase.Query <UserData>("UserData/ByUsername")
                                       //.Customize(x => x.WaitForNonStaleResultsAsOfNow())
                                       .Select(user => new { user.Username })
                                       .FirstOrDefault(user => user.Username.Equals(operation.Username, StringComparison.CurrentCultureIgnoreCase));

                if (existingUserData != null)
                {
                    return(operation.GetErrorResponse((short)ResultCode.UsernameAlreadyExists));
                }

                ResultCode resultCode;
                if (LoginHelper.IsValidUsername(username, out resultCode) == false)
                {
                    return(operation.GetErrorResponse((short)resultCode));
                }

                if (LoginHelper.IsValidPassword(operation.Password, out resultCode) == false)
                {
                    return(operation.GetErrorResponse((short)resultCode));
                }

                var passwordHash = SaltedHash.Create(operation.Password);
                var newUserInfo  = new UserData
                {
                    Id        = UserData.GenerateId(username),
                    Username  = username,
                    Salt      = passwordHash.Salt,
                    Password  = passwordHash.Hash,
                    IsBanned  = false,
                    CreatedOn = DateTime.Now,
                    LastLogin = null,
                };

                this.application.UserDatabase.Store(newUserInfo);
                return(operation.GetErrorResponse((short)ResultCode.Ok));
            }
            catch (Exception e)
            {
                _logger.Error(e);
                return(operation.GetErrorResponse((short)ResultCode.Fail));
            }
        }
Exemplo n.º 3
0
 private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
 {
     Logger.Error(e.ExceptionObject);
 }
        void HandlePingResponse(OperationResponse response)
        {
            var currTime = (int)(DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond);

            var pingResponse = new Operations.PingResponse(this.Protocol, response);

            if (!pingResponse.IsValid)
            {
                if (_logger.IsErrorEnabled)
                {
                    _logger.Error("Received unknown ping response");
                }

                return;
            }

            // creating new latency list
            if (latencyList == null)
            {
                latencyList = new List <int>();
            }

            // adding the latency for the last packet received
            var latency = (currTime - pingResponse.ServerTimeStamp) >> 1;

            latencyList.Add(latency);

            // initial (temporary) time sync
            var mto = pingResponse.MasterTimeStamp - currTime + latency;

            Interlocked.Exchange(ref masterTimeOffset, mto);

            if (pingCount > InternalPingRequestCount)
            {
                // sorting low to high latency
                latencyList.Sort();

                var median = this.latencyList[(latencyList.Count >> 1) + 1];
                var mean   = this.latencyList.Average();

                // calculating the diff mean squares
                var meanSquaresList = new List <float>();
                this.latencyList.ForEach(n => meanSquaresList.Add((float)Math.Pow(n - mean, 2)));

                // calculating the standard deviation
                var deviation = (float)Math.Sqrt(meanSquaresList.Average() / (meanSquaresList.Count - 1));

                // omiting higher latencies due to ack delay
                var newLatencyList = new List <int>();
                this.latencyList.ForEach(n =>
                {
                    if (n <= deviation + median)
                    {
                        newLatencyList.Add(n);
                    }
                });

                // calculating the average latency (estimate)
                var meanLatency = newLatencyList.Average();
                var nMto        = (int)Math.Round(pingResponse.MasterTimeStamp - currTime + meanLatency);

                Interlocked.Exchange(ref masterTimeOffset, nMto);
                this.latencyList = null;

                Logger.DebugFormat("Time sync completed. Master Time Offset = {0}. MTS={1} STS={2}. OSTS={3}", mto, pingResponse.MasterTimeStamp, currTime, pingResponse.ServerTimeStamp);
            }
        }