Пример #1
0
        public void AddRecentServer(ServerInformation information) {
            if (LoadedOnly.Any(x => x.Id == information.GetUniqueId())) return;

            try {
                CreateAndAddEntry(information);
            } catch (Exception e) {
                Logging.Warning("Recent add error: " + e);
            }

            AddToRecentList(information.Ip, information.PortC);
        }
Пример #2
0
        private static ServerInformation PrepareLan(ServerInformation result, string ip) {
            if (result.Ip == string.Empty) {
                result.Ip = ip;
            }

            result.L = true;

            for (var i = 0; i < result.Durations.Length; i++) {
                if ((Game.SessionType?)result.SessionTypes.ElementAtOrDefault(i) != Game.SessionType.Race &&
                        result.Durations[i] < 60) {
                    result.Durations[i] *= 60;
                }
            }

            return result;
        }
Пример #3
0
 /// <summary>
 /// Update current entry using new information.
 /// </summary>
 /// <param name="information"></param>
 /// <returns>True if update is possible and was done, false if 
 /// changes require to recreate whole ServerEntry</returns>
 private bool UpdateValuesFrom(ServerInformation information) {
     if (Ip != information.Ip ||
             Port != information.Port ||
             PortC != information.PortC ||
             PortT != information.PortT) return false;
     SetSomeProperties(information);
     return true;
 }
Пример #4
0
        private void SetSomeProperties(ServerInformation information) {
            PreviousUpdateTime = DateTime.Now;
            Name = Regex.Replace(information.Name.Trim(), @"\s+", " ");

            {
                var country = information.Country.FirstOrDefault() ?? "";
                Country = Country != null && country == @"na" ? Country : country;
            }

            {
                var countryId = information.Country.ElementAtOrDefault(1) ?? "";
                CountryId = CountryId != null && countryId == @"na" ? CountryId : countryId;
            }

            CurrentDriversCount = information.Clients;
            Capacity = information.Capacity;

            PasswordRequired = information.Password;
            if (PasswordRequired) {
                Password = ValuesStorage.GetEncryptedString(PasswordStorageKey);
            }
            
            CarIds = information.CarIds;
            CarsOrTheirIds = CarIds.Select(x => new CarOrOnlyCarIdEntry(x, GetCarWrapper(x))).ToList();
            TrackId = information.TrackId;
            Track = GetTrack(TrackId);

            var errorMessage = "";
            var error = SetMissingCarErrorIfNeeded(ref errorMessage);
            error = SetMissingTrackErrorIfNeeded(ref errorMessage) || error;
            if (error) {
                Status = ServerStatus.Error;
                ErrorMessage = errorMessage;
            }

            var seconds = (int)Game.ConditionProperties.GetSeconds(information.Time);
            Time = $"{seconds / 60 / 60:D2}:{seconds / 60 % 60:D2}";
            SessionEnd = DateTime.Now + TimeSpan.FromSeconds(information.TimeLeft - Math.Round(information.Timestamp / 1000d));

            Sessions = information.SessionTypes.Select((x, i) => new Session {
                IsActive = x == information.Session,
                Duration = information.Durations[i],
                Type = (Game.SessionType)x
            }).ToList();

            BookingMode = !information.PickUp;
        }
Пример #5
0
        public ServerEntry(IOnlineManager manager, [NotNull] ServerInformation information, bool? forceIsLan = null)
                : base(manager, information.GetUniqueId(), true) {
            if (information == null) throw new ArgumentNullException(nameof(information));

            OriginalInformation = information;

            IsLan = forceIsLan ?? information.IsLan;

            Ip = information.Ip;
            Port = information.Port;
            PortC = information.PortC;
            PortT = information.PortT;

            Ping = null;
            SetSomeProperties(information);
        }
Пример #6
0
        public static ServerInformation[] Deserialize(Stream stream) {
            var reader = new JsonTextReader(new StreamReader(stream));

            var response = new List<ServerInformation>(1200);
            var currentProperty = string.Empty;

            reader.MatchNext(JsonToken.StartArray);
            while (reader.IsMatchNext(JsonToken.StartObject)) {
                var entry = new ServerInformation();

                while (reader.Until(JsonToken.EndObject)) {
                    switch (reader.TokenType) {
                        case JsonToken.PropertyName:
                            currentProperty = reader.Value.ToString();
                            break;

                        case JsonToken.String:
                            switch (currentProperty) {
                                case "ip":
                                    entry.Ip = reader.Value.ToString();
                                    break;
                                case "name":
                                    entry.Name = reader.Value.ToString();
                                    break;
                                case "track":
                                    entry.TrackId = reader.Value.ToString();
                                    break;
                            }
                            break;

                        case JsonToken.Integer:
                            switch (currentProperty) {
                                case "port":
                                    entry.Port = int.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
                                    break;
                                case "cport":
                                    entry.PortC = int.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
                                    break;
                                case "tport":
                                    entry.PortT = int.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
                                    break;
                                case "clients":
                                    entry.Clients = int.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
                                    break;
                                case "maxclients":
                                    entry.Capacity = int.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
                                    break;
                                case "timeofday":
                                    entry.Time = int.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
                                    break;
                                case "session":
                                    entry.Session = int.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
                                    break;
                                case "timeleft":
                                    entry.TimeLeft = long.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
                                    break;
                                case "timestamp":
                                    entry.Timestamp = long.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
                                    break;
                                case "lastupdate":
                                    entry.LastUpdate = long.Parse(reader.Value.ToString(), CultureInfo.InvariantCulture);
                                    break;
                            }
                            break;

                        case JsonToken.Boolean:
                            switch (currentProperty) {
                                case "pass":
                                    entry.Password = bool.Parse(reader.Value.ToString());
                                    break;
                                case "pickup":
                                    entry.PickUp = bool.Parse(reader.Value.ToString());
                                    break;
                                case "l":
                                    entry.L = bool.Parse(reader.Value.ToString());
                                    break;
                            }
                            break;

                        case JsonToken.StartArray:
                            switch (currentProperty) {
                                case "cars":
                                    entry.CarIds = reader.ReadStringArray(1);
                                    break;
                                case "sessiontypes":
                                    entry.SessionTypes = reader.ReadIntArray(1);
                                    break;
                                case "durations":
                                    entry.Durations = reader.ReadLongArray(1);
                                    break;
                                case "country":
                                    entry.Country = reader.ReadStringArray(2);
                                    break;
                                default:
                                    while (reader.Until(JsonToken.EndArray)) { }
                                    break;
                            }
                            break;

                        default:
                            throw new Exception("Unexpected token: " + reader.TokenType);
                    }
                }

                response.Add(entry);
            }

            return response.ToArray();
        }