Esempio n. 1
0
        public GameDialog()
        {
            DataContext = new ViewModel();
            InitializeComponent();

            if (_progressStyles == null)
            {
                _progressStyles = GoodShuffle.Get((string[])FindResource("ProgressRingStyles"));
            }

            ProgressRing.Style = FindResource(_progressStyles.Next) as Style;

            _cancellationSource = new CancellationTokenSource();
            CancellationToken   = _cancellationSource.Token;

            var rhmSettingsButton = new Button {
                Content   = "RHM Settings",
                Command   = RhmService.Instance.ShowSettingsCommand,
                MinHeight = 21,
                MinWidth  = 65,
                Margin    = new Thickness(4, 0, 0, 0)
            };

            rhmSettingsButton.SetBinding(VisibilityProperty, new Binding {
                Source    = RhmService.Instance,
                Path      = new PropertyPath(nameof(RhmService.Active)),
                Converter = new FirstFloor.ModernUI.Windows.Converters.BooleanToVisibilityConverter()
            });

            Buttons = new[] { rhmSettingsButton, CancelButton };
        }
Esempio n. 2
0
        public void GoodShuffleTest_TestRandomity()
        {
            // arrange
            var array = new[] { 1, 2, 3, 4, 5 };

            // act
            var v0 = CountAverage(() => GoodShuffle.Get(array).Next, x => x == 5);
            var v1 = CountAverage(() => GoodShuffle.Get(array).Next, x => x == 1);

            // assert
            Assert.AreEqual(0.2, v0, 0.15, "v0: isn’t random enough");
            Assert.AreEqual(0.2, v1, 0.15, "v1: isn’t random enough");
        }
Esempio n. 3
0
        public void GoodShuffleTest_TestUniformity()
        {
            var array = new[] { 1, 2, 3, 4, 5 };
            var g1    = GoodShuffle.Get(array);
            var g2    = GoodShuffle.Get(array);


            var n1Of10  = Enumerable.Range(0, 10).Select(x => g1.Next).Count(x => x == 1);
            var n2Of100 = Enumerable.Range(0, 100).Select(x => g2.Next).Count(x => x == 2);


            Assert.AreEqual(n1Of10, 2, 0, "n1Of10: isn’t uniform enough");
            Assert.AreEqual(n2Of100, 20, 0, "n2Of100: isn’t uniform enough");
        }
Esempio n. 4
0
        public void FindClosestTest()
        {
            var         values = Enum.GetValues(typeof(WeatherType)).OfType <WeatherType>().ToArray();
            WeatherType?result = null;

            foreach (var type in values)
            {
                for (var i = 1; i < values.Length; i++)
                {
                    result = type.FindClosestWeather(GoodShuffle.Get(values).Take(i));
                }
            }

            Console.WriteLine($"result: {result}");
        }
Esempio n. 5
0
        public GameDialog()
        {
            DataContext = new ViewModel();
            InitializeComponent();

            if (_progressStyles == null)
            {
                _progressStyles = GoodShuffle.Get(ExtraProgressRings.Styles.Keys);
            }

            _cancellationSource = new CancellationTokenSource();
            CancellationToken   = _cancellationSource.Token;

            ProgressRing.Style = ExtraProgressRings.Styles.GetValueOrDefault(_progressStyles.Next);
            Buttons            = new[] { CancelButton };
        }
Esempio n. 6
0
        private static Game.BaseModeProperties GetModeProperties(Options options)
        {
            switch (options.Mode)
            {
            case Mode.Practice:
                return(new Game.PracticeProperties {
                    Penalties = false
                });

            case Mode.HotLap:
                return(new Game.HotlapProperties {
                    GhostCar = true,
                    RecordGhostCar = true,
                    Penalties = true
                });

            case Mode.Race:
                var skins = GoodShuffle.Get(CarsManager.Instance.GetById(options.CarId).SkinsManager.WrappersList
                                            .Where(x => x.Value.Enabled).Select(x => x.Id));

                return(new Game.RaceProperties {
                    Penalties = true,
                    JumpStartPenalty = Game.JumpStartPenaltyType.DriveThrough,
                    AiLevel = 100,
                    RaceLaps = options.RaceLaps,
                    StartingPosition = options.Opponents,
                    BotCars = Enumerable.Range(1, options.Opponents).Select(x => new Game.AiCar {
                        AiLevel = MathUtils.Random(80, 100),
                        CarId = options.CarId,
                        DriverName = "Bot #" + x,
                        SkinId = skins.Next
                    })
                });

            case Mode.Drift:
                return(new Game.DriftProperties {
                    Penalties = true,
                    StartType = Game.StartType.RegularStart
                });

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Esempio n. 7
0
        public void GoodShuffleTest_TestUniformityWithIgnored()
        {
            var array = new[] { 1, 2, 3, 4, 5 };
            var g1    = GoodShuffle.Get(array);

            g1.IgnoreOnce(3);
            var g2 = GoodShuffle.Get(array);

            g2.IgnoreOnce(4);


            var listWithSkipped3 = Enumerable.Range(0, 9).Select(x => g1.Next).ToList();
            var n3Of9            = listWithSkipped3.Count(x => x == 3);
            var n4Of109          = Enumerable.Range(0, 109).Select(x => g2.Next).Count(x => x == 4);


            Assert.AreEqual(1, n3Of9, "isn’t uniform enough");
            Assert.AreEqual(21, n4Of109, "isn’t uniform enough");
        }
Esempio n. 8
0
        public GameDialog()
        {
            DataContext = new ViewModel();
            InitializeComponent();

            if (_progressStyles == null)
            {
                _progressStyles = GoodShuffle.Get(ExtraProgressRings.Styles.Keys);
            }

            _cancellationSource = new CancellationTokenSource();
            CancellationToken   = _cancellationSource.Token;

            if (!AppAppearanceManager.Instance.SoftwareRenderingMode)
            {
                ProgressRing.Style = ExtraProgressRings.Styles.GetValueOrDefault(_progressStyles.Next);
            }

            Buttons = new[] { OptionHideCancelButton?null : CancelButton };
        }
Esempio n. 9
0
        public async Task <IList <Game.AiCar> > GenerateGameEntries(CancellationToken cancellation = default(CancellationToken))
        {
            if (IsBusy)
            {
                await RebuildGridAsync();

                if (cancellation.IsCancellationRequested)
                {
                    return(null);
                }
            }

            var opponentsNumber = OpponentsNumberLimited;

            if (FilteredView.Count == 0 || opponentsNumber == 0)
            {
                return(new Game.AiCar[0]);
            }

            var skins = new Dictionary <string, GoodShuffle <CarSkinObject> >();

            foreach (var car in FilteredView.OfType <RaceGridEntry>().Where(x => x.CarSkin == null).Select(x => x.Car).Distinct())
            {
                await car.SkinsManager.EnsureLoadedAsync();

                if (cancellation.IsCancellationRequested)
                {
                    return(null);
                }

                skins[car.Id] = GoodShuffle.Get(car.EnabledOnlySkins);
            }

            NameNationality[] nameNationalities;
            if (opponentsNumber == 7 && OptionNfsPorscheNames)
            {
                nameNationalities = new[] {
                    new NameNationality {
                        Name = "Dylan", Nationality = "Wales"
                    },
                    new NameNationality {
                        Name = "Parise", Nationality = "Italy"
                    },
                    new NameNationality {
                        Name = "Steele", Nationality = "United States"
                    },
                    new NameNationality {
                        Name = "Wingnut", Nationality = "England"
                    },
                    new NameNationality {
                        Name = "Leadfoot", Nationality = "Australia"
                    },
                    new NameNationality {
                        Name = "Amazon", Nationality = "United States"
                    },
                    new NameNationality {
                        Name = "Backlash", Nationality = "United States"
                    }
                };
            }
            else if (DataProvider.Instance.NationalitiesAndNames.Any())
            {
                nameNationalities = GoodShuffle.Get(DataProvider.Instance.NationalitiesAndNamesList).Take(opponentsNumber).ToArray();
            }
            else
            {
                nameNationalities = null;
            }

            List <int> aiLevels;

            if (AiLevelFixed)
            {
                aiLevels = null;
            }
            else
            {
                var aiLevelsInner = from i in Enumerable.Range(0, opponentsNumber)
                                    select AiLevelMin + ((opponentsNumber < 2 ? 1d : 1d - i / (opponentsNumber - 1d)) * (AiLevel - AiLevelMin)).RoundToInt();
                if (AiLevelArrangeReverse)
                {
                    aiLevelsInner = aiLevelsInner.Reverse();
                }

                if (Equals(AiLevelArrangeRandom, 1d))
                {
                    aiLevelsInner = GoodShuffle.Get(aiLevelsInner);
                }
                else if (AiLevelArrangeRandom > 0d)
                {
                    aiLevelsInner = LimitedShuffle.Get(aiLevelsInner, AiLevelArrangeRandom);
                }

                aiLevels = aiLevelsInner.Take(opponentsNumber).ToList();
                Logging.Debug("AI levels: " + aiLevels.Select(x => $@"{x}%").JoinToString(@", "));
            }

            IEnumerable <RaceGridEntry> final;

            if (Mode.CandidatesMode)
            {
                var list = FilteredView.OfType <RaceGridEntry>().SelectMany(x => new[] { x }.Repeat(x.CandidatePriority)).ToList();

                if (ShuffleCandidates)
                {
                    var shuffled = GoodShuffle.Get(list);

                    if (_playerCar != null)
                    {
                        var same = list.FirstOrDefault(x => x.Car == _playerCar);
                        if (same != null)
                        {
                            shuffled.IgnoreOnce(same);
                        }
                    }

                    final = shuffled.Take(opponentsNumber);
                }
                else
                {
                    var skip = _playerCar;
                    final = LinqExtension.RangeFrom().Select(x => list.RandomElement()).Where(x => {
                        if (x.Car == skip)
                        {
                            skip = null;
                            return(false);
                        }

                        return(true);
                    }).Take(opponentsNumber);
                }
            }
            else
            {
                final = NonfilteredList.Where(x => !x.SpecialEntry);
            }

            if (_playerCar != null)
            {
                skins.GetValueOrDefault(_playerCar.Id)?.IgnoreOnce(_playerCar.SelectedSkin);
            }

            var takenNames = new List <string>(opponentsNumber);

            return(final.Take(opponentsNumber).Select((entry, i) => {
                var level = entry.AiLevel ?? aiLevels?[i] ?? 100;

                var skin = entry.CarSkin;
                if (skin == null)
                {
                    skin = skins.GetValueOrDefault(entry.Car.Id)?.Next;
                }

                var name = entry.Name;
                if (string.IsNullOrWhiteSpace(name) && SettingsHolder.Drive.QuickDriveUseSkinNames)
                {
                    var skinDriverNames = skin?.DriverName?.Split(',').Select(x => x.Trim()).Where(x => x.Length > 0).ToList();
                    if (skinDriverNames?.Count > 0)
                    {
                        name = GoodShuffle.Get(skinDriverNames).Take(skinDriverNames.Count).FirstOrDefault(x => !takenNames.Contains(x)) ?? name;
                        takenNames.Add(name);
                    }
                }

                if (string.IsNullOrWhiteSpace(name))
                {
                    name = nameNationalities?[i].Name ?? @"AI #" + i;
                    takenNames.Add(name);
                }

                var nationality = entry.Nationality ?? nameNationalities?[i].Nationality ?? @"Italy";
                var skinId = skin?.Id;

                return new Game.AiCar {
                    AiLevel = level,
                    CarId = entry.Car.Id,
                    DriverName = AiLevelInDriverName ? $@"{name} ({level}%)" : name,
                    Nationality = nationality,
                    Setup = "",
                    SkinId = skinId
                };
            }).ToList());
        }