Пример #1
0
        public void ShouldProcessGame()
        {
            var seasonConfig = new SeasonCompetitionConfig("Test", null, 1, null, null, 1, null, null, null, null, null, null);
            var season       = new Season(seasonConfig, seasonConfig.Name, 1, null, null, null, null, true, false, 1, null);
            var teams        = new List <Team>()
            {
                CreateTeam("Team 1", 1), CreateTeam("Team 2", 2), CreateTeam("Team 3", 3), CreateTeam("Team 4", 4)
            };
            var rules = new GameRules(null, true, 1, 3, 10, true);
            var games = new List <ScheduleGame>()
            {
                new ScheduleGame(null, 1, 1, 1, teams[0], null, teams[1], null, 1, 1, true, 1, 0, rules, false),
                new ScheduleGame(null, 1, 1, 1, teams[0], null, teams[2], null, 3, 1, true, 1, 0, rules, false),
                new ScheduleGame(null, 1, 1, 1, teams[0], null, teams[3], null, 1, 4, true, 1, 0, rules, false)
            };

            var team1 = new SeasonTeam(null, teams[0], "Team 1", null, null, 5, null, 1, null, null, null);
            var team2 = new SeasonTeam(null, teams[1], "Team 2", null, null, 5, null, 1, null, null, null);
            var team3 = new SeasonTeam(null, teams[2], "Team 3", null, null, 5, null, 1, null, null, null);
            var team4 = new SeasonTeam(null, teams[3], "Team 4", null, null, 5, null, 1, null, null, null);

            season.Teams = new List <CompetitionTeam>()
            {
                team1, team2, team3, team4
            };

            games.ForEach(g => { season.ProcessGame(g, 1); });

            StrictEqual(3, team1.Stats.Games);
            StrictEqual(3, team1.Stats.Points);
            StrictEqual(1, team1.Stats.Wins);
            StrictEqual(1, team1.Stats.Loses);
            StrictEqual(1, team1.Stats.Ties);
            StrictEqual(5, team1.Stats.GoalsFor);
            StrictEqual(6, team1.Stats.GoalsAgainst);

            StrictEqual(1, team2.Stats.Games);
            StrictEqual(1, team2.Stats.Points);
            StrictEqual(0, team2.Stats.Wins);
            StrictEqual(0, team2.Stats.Loses);
            StrictEqual(1, team2.Stats.Ties);
            StrictEqual(1, team2.Stats.GoalsFor);
            StrictEqual(1, team2.Stats.GoalsAgainst);

            StrictEqual(1, team3.Stats.Games);
            StrictEqual(0, team3.Stats.Points);
            StrictEqual(0, team3.Stats.Wins);
            StrictEqual(1, team3.Stats.Loses);
            StrictEqual(0, team3.Stats.Ties);
            StrictEqual(1, team3.Stats.GoalsFor);
            StrictEqual(3, team3.Stats.GoalsAgainst);

            StrictEqual(1, team4.Stats.Games);
            StrictEqual(2, team4.Stats.Points);
            StrictEqual(1, team4.Stats.Wins);
            StrictEqual(0, team4.Stats.Loses);
            StrictEqual(0, team4.Stats.Ties);
            StrictEqual(4, team4.Stats.GoalsFor);
            StrictEqual(1, team4.Stats.GoalsAgainst);
        }
Пример #2
0
        public PlayoffTeam CreateAndAddTeam(Season season, IList <SeasonTeamRule> rule)
        {
            var team = new SeasonTeam(season, rule[0].Parent, rule[0].Parent.Name, rule[0].Parent.Skill, 2, 1, null);

            rule.ToList().ForEach(tr =>
            {
                var div = season.Divisions.ToList().Where(sd => sd.Name.Equals(tr.Division.Name)).First();

                div.AddTeam(team);
            });

            return(team);
        }
Пример #3
0
        public async Task <IActionResult> AddTeam(int seasonID, int globalTeamId)
        {
            var season = await _seasons.GetSeasonById(seasonID);

            var globalTeam = await Context.Teams.SingleOrDefaultAsync(t => t.Id == globalTeamId);

            if (season is null || globalTeam is null)
            {
                return(NotFound());
            }

            var engines = await Context.Engines
                          .Select(t => new { t.Id, t.Name })
                          .ToListAsync();

            ViewBag.engines = new SelectList(engines, nameof(Engine.Id), nameof(Engine.Name));
            var rubbers = await Context.Rubbers
                          .Select(r => new { r.RubberId, r.Name })
                          .ToListAsync();

            ViewBag.rubbers  = new SelectList(rubbers, nameof(Rubber.RubberId), nameof(Rubber.Name));
            ViewBag.seasonId = seasonID;

            var seasonTeam = new SeasonTeam
            {
                Team   = globalTeam,
                Season = season
            };

            // Adds last previous used values from team as default
            var allSeasonTeams = await Context.SeasonTeams.ToListAsync();

            var lastTeam = allSeasonTeams.LastOrDefault(lt => lt.TeamId == globalTeamId);

            if (lastTeam != null)
            {
                seasonTeam.Name         = lastTeam.Name;
                seasonTeam.Principal    = lastTeam.Principal;
                seasonTeam.Colour       = lastTeam.Colour;
                seasonTeam.Accent       = lastTeam.Accent;
                seasonTeam.Chassis      = lastTeam.Chassis;
                seasonTeam.Reliability  = lastTeam.Reliability;
                seasonTeam.EngineId     = lastTeam.EngineId;
                seasonTeam.RubberId     = lastTeam.RubberId;
                seasonTeam.Topspeed     = lastTeam.Topspeed;
                seasonTeam.Acceleration = lastTeam.Acceleration;
                seasonTeam.Handling     = lastTeam.Handling;
            }

            return(View("AddOrUpdateTeam", seasonTeam));
        }
Пример #4
0
        public async Task <IActionResult> AddTeam(int seasonID, int globalTeamID, [Bind] SeasonTeam seasonTeam)
        {
            // Get and validate URL parameter objects.
            var season = await Context.Seasons
                         .Include(s => s.Teams)
                         .ThenInclude(t => t.Team)
                         .SingleOrDefaultAsync(s => s.SeasonId == seasonID);

            var globalTeam = await Context.Teams.SingleOrDefaultAsync(t => t.Id == globalTeamID);

            if (season is null || globalTeam is null || seasonTeam is null)
            {
                return(NotFound());
            }

            if (season.Teams.Select(d => d.TeamId).Contains(seasonTeam.TeamId))
            {
                return(UnprocessableEntity());
            }

            if (ModelState.IsValid)
            {
                // Set the Season and global Driver again as these are not bound in the view.
                seasonTeam.SeasonId = seasonID;
                seasonTeam.TeamId   = globalTeamID;

                // Persist the new SeasonDriver and return to AddDrivers page.
                await Context.AddAsync(seasonTeam);

                await Context.SaveChangesAsync();

                return(RedirectToAction(nameof(AddTeams), new { seasonID }));
            }
            else
            {
                var engines = await Context.Engines
                              .Where(e => !e.Archived)
                              .Select(t => new { t.Id, t.Name })
                              .ToListAsync();

                ViewBag.engines = new SelectList(engines, nameof(Engine.Id), nameof(Engine.Name));
                var rubbers = await Context.Rubbers
                              .Select(r => new { r.RubberId, r.Name })
                              .ToListAsync();

                ViewBag.rubbers = new SelectList(rubbers, nameof(Rubber.RubberId), nameof(Rubber.Name));
                return(View("AddOrUpdateTeam", seasonTeam));
            }
        }
Пример #5
0
        private SeasonTeam CreateSeasonTeam(Competition competition, int id, string name, SeasonDivision division)
        {
            var team = new SeasonTeam(competition, new Team()
            {
                Id = id, Name = name
            }, 1, null, division);

            if (division.Teams == null)
            {
                division.Teams = new List <SeasonTeam>();
            }
            division.Teams.Add(team);

            return(team);
        }
Пример #6
0
        public async Task <IActionResult> UpdateTeam(int seasonID, int teamID, [Bind] SeasonTeam updatedTeam)
        {
            var season = await Context.Seasons
                         .Include(s => s.Teams)
                         .ThenInclude(t => t.Team)
                         .SingleOrDefaultAsync(s => s.SeasonId == seasonID);

            var team = season.Teams.SingleOrDefault(d => d.SeasonTeamId == teamID);

            if (season is null || team is null || updatedTeam is null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                team.Name         = updatedTeam.Name;
                team.Principal    = updatedTeam.Principal;
                team.Colour       = updatedTeam.Colour;
                team.Accent       = updatedTeam.Accent;
                team.Chassis      = updatedTeam.Chassis;
                team.Topspeed     = updatedTeam.Topspeed;
                team.Acceleration = updatedTeam.Acceleration;
                team.Handling     = updatedTeam.Handling;
                team.Reliability  = updatedTeam.Reliability;
                team.EngineId     = updatedTeam.EngineId;
                team.RubberId     = updatedTeam.RubberId;
                Context.Update(team);
                await Context.SaveChangesAsync();

                return(RedirectToAction(nameof(Detail), new { seasonID }));
            }
            else
            {
                var engines = await Context.Engines
                              .Where(e => !e.Archived)
                              .Select(t => new { t.Id, t.Name })
                              .ToListAsync();

                ViewBag.engines = new SelectList(engines, nameof(Engine.Id), nameof(Engine.Name));
                var rubbers = await Context.Rubbers
                              .Select(r => new { r.RubberId, r.Name })
                              .ToListAsync();

                ViewBag.rubbers = new SelectList(rubbers, nameof(Rubber.RubberId), nameof(Rubber.Name));
                return(View("AddOrUpdateDriver", team));
            }
        }
Пример #7
0
        // Create a dictionary from a given SeasonTeam object
        public static Dictionary <string, int> CreateTeamSpecDictionary(SeasonTeam team)
        {
            if (team is null)
            {
                return(null);
            }

            Dictionary <string, int> teamSpecs = new Dictionary <string, int>
            {
                { "Topspeed", team.Topspeed },
                { "Acceleration", team.Acceleration },
                { "Handling", team.Handling }
            };

            return(teamSpecs);
        }
Пример #8
0
        public BaseResultDto <int> SavePlayerStatisticsList(List <PlayerSeasonStatisticsDto> players)
        {
            var result = new BaseResultDto <int>();

            using (var context = _contextFactory.Create())
            {
                var allSeasons = context.Seasons.ToList();

                foreach (var playerDto in players)
                {
                    var dbPlayer = context.PlayerSeasonStatistics.FirstOrDefault(p => p.Player.Name == playerDto.Player.Name &&
                                                                                 p.SeasonTeam.Season.Id == playerDto.SeasonTeam.Season.Id);

                    if (dbPlayer != null)
                    {
                        var tempPlayer = context.Players.FirstOrDefault(p => p.Name == playerDto.Player.Name);

                        if (tempPlayer.LastCost != playerDto.Player.LastCost && tempPlayer.LastCost > 0)
                        {
                            var costChange = new PlayerCostChange
                            {
                                ChangeCaptureDate = DateTime.Today,
                                PlayerId          = tempPlayer.Id,
                                Delta             = playerDto.Player.LastCost - tempPlayer.LastCost
                            };

                            context.PlayerCostChanges.Add(costChange);
                            context.SaveChanges();
                        }

                        dbPlayer.MinutesPlayed   = playerDto.MinutesPlayed;
                        dbPlayer.OwnGoals        = playerDto.OwnGoals;
                        dbPlayer.PenaltiesMissed = playerDto.PenaltiesMissed;
                        dbPlayer.XA          = playerDto.XA;
                        dbPlayer.XA90        = playerDto.XA90;
                        dbPlayer.XG          = playerDto.XG;
                        dbPlayer.XG90        = playerDto.XG90;
                        dbPlayer.YellowCards = playerDto.YellowCards;
                        dbPlayer.RedCards    = playerDto.RedCards;
                        dbPlayer.Apps        = playerDto.Apps;
                        dbPlayer.CleanSheets = playerDto.CleanSheets;
                        tempPlayer.LastCost  = playerDto.Player.LastCost;

                        context.Players.AddOrUpdate(tempPlayer);
                        context.PlayerSeasonStatistics.AddOrUpdate(dbPlayer);
                        context.SaveChanges();
                    }
                    else
                    {
                        var seasonTeam = context.SeasonTeams.FirstOrDefault(st => st.Season.Id == playerDto.SeasonTeam.Season.Id &&
                                                                            st.Team.Name.Equals(playerDto.SeasonTeam.Team.Name));

                        if (seasonTeam == null)
                        {
                            var season = allSeasons.FirstOrDefault(s => s.Id == playerDto.SeasonTeam.Season.Id);

                            if (season == null)
                            {
                                result.Status = false;
                                return(result);
                            }

                            var dbTeam = context.Teams.FirstOrDefault(t => t.Name.Equals(playerDto.SeasonTeam.Team.Name));

                            if (dbTeam == null)
                            {
                                dbTeam = new Team
                                {
                                    Name = playerDto.SeasonTeam.Team.Name
                                };

                                context.Teams.Add(dbTeam);
                                context.SaveChanges();
                            }

                            seasonTeam = new SeasonTeam
                            {
                                TeamId   = dbTeam.Id,
                                SeasonId = season.Id
                            };

                            context.SeasonTeams.Add(seasonTeam);
                            context.SaveChanges();
                        }

                        var p = context.Players.FirstOrDefault(pa => pa.Name == playerDto.Player.Name);

                        if (p == null)
                        {
                            var position = context.Positions.FirstOrDefault(po => po.AppId == (int)playerDto.Player.Position);

                            p = new Player
                            {
                                ExternalId = playerDto.Player.ExternalId,
                                Name       = playerDto.Player.Name,
                                SecondName = playerDto.Player.SecondName,
                                PositionId = position.id,
                                LastCost   = playerDto.Player.LastCost
                            };

                            context.Players.Add(p);
                            context.SaveChanges();
                        }



                        dbPlayer = new PlayerSeasonStatistics
                        {
                            PlayerId        = p.Id,
                            SeasonTeamId    = seasonTeam.Id,
                            MinutesPlayed   = playerDto.MinutesPlayed,
                            OwnGoals        = playerDto.OwnGoals,
                            PenaltiesMissed = playerDto.PenaltiesMissed,
                            RedCards        = playerDto.RedCards,
                            XA          = playerDto.XA,
                            XA90        = playerDto.XA90,
                            XG          = playerDto.XG,
                            XG90        = playerDto.XG90,
                            YellowCards = playerDto.YellowCards,
                            Apps        = playerDto.Apps,
                            CleanSheets = playerDto.CleanSheets
                        };

                        context.PlayerSeasonStatistics.Add(dbPlayer);
                        context.SaveChanges();
                    }
                }
            }

            return(result);
        }
Пример #9
0
        public BaseResultDto <int> SaveTeamStatisticsList(List <SeasonTeamDto> teams)
        {
            var result = new BaseResultDto <int>();

            using (var context = _contextFactory.Create())
            {
                foreach (var team in teams)
                {
                    var stDb = context.SeasonTeams.FirstOrDefault(st => st.Team.Name == team.Team.Name && st.Season.Id == team.Season.Id && st.Season.League.Id == team.Season.League.Id);

                    if (stDb == null)
                    {
                        var sDb = context.Seasons.FirstOrDefault(s => s.Id == team.Season.Id);
                        var tDb = context.Teams.FirstOrDefault(t => t.Name.Equals(team.Team.Name));

                        if (tDb == null)
                        {
                            tDb = new Team
                            {
                                Name = team.Team.Name
                            };

                            context.Teams.Add(tDb);
                            context.SaveChanges();
                        }

                        stDb = new SeasonTeam
                        {
                            TeamId      = tDb.Id,
                            SeasonId    = sDb.Id,
                            Points      = team.Points,
                            Drawn       = team.Drawn,
                            GamesPlayed = team.GamesPlayed,
                            Lost        = team.Lost,
                            Position    = team.Position,
                            Won         = team.Won,
                            XGFor       = team.XGFor,
                            XGAgainst   = team.XGAgainst,
                            XPoints     = team.XPoints,
                            CleanSheets = team.CleanSheets
                        };

                        context.SeasonTeams.Add(stDb);
                        context.SaveChanges();
                    }
                    else
                    {
                        stDb.Points      = team.Points;
                        stDb.Drawn       = team.Drawn;
                        stDb.GamesPlayed = team.GamesPlayed;
                        stDb.Lost        = team.Lost;
                        stDb.Position    = team.Position;
                        stDb.Won         = team.Won;
                        stDb.XGFor       = team.XGFor;
                        stDb.XGAgainst   = team.XGAgainst;
                        stDb.XPoints     = team.XPoints;
                        stDb.CleanSheets = team.CleanSheets;

                        context.SeasonTeams.AddOrUpdate(stDb);
                        context.SaveChanges();
                    }
                }
            }

            result.Status = true;

            return(result);
        }
Пример #10
0
        /// <summary>
        /// Gets the points result of a single <see cref="StintResult"/>, with any enabled <see cref="StintResult"/> modifiers applied.
        /// </summary>
        /// <param name="stint">The <see cref="Stint"/> supplying the modifiers to use.</param>
        /// <param name="driverResult">The partial <see cref="DriverResult"/> from which to derive certain modifiers.</param>
        public void UpdateStintResult(StintResult stintResult,
                                      Stint stint,
                                      DriverResult driverResult,
                                      SeasonTeam team,
                                      Weather weather,
                                      Specification trackSpec,
                                      int driverCount,
                                      int qualyBonus,
                                      int pitMin,
                                      int pitMax,
                                      AppConfig appConfig)
        {
            if (stintResult is null || driverResult is null || stint is null || team is null || appConfig is null)
            {
                throw new NullReferenceException();
            }

            // Applies the increased or decreased odds for the specific track.
            double engineWeatherMultiplier = 1;
            int    weatherRNG = 0;
            int    weatherDNF = 0;

            var driver = driverResult.SeasonDriver;

            stintResult.StintStatus = StintStatus.Running;
            switch (weather)
            {
            case Weather.Sunny:
                engineWeatherMultiplier = appConfig.SunnyEngineMultiplier;
                break;

            case Weather.Overcast:
                engineWeatherMultiplier = appConfig.OvercastEngineMultiplier;
                break;

            case Weather.Rain:
                weatherRNG += appConfig.RainAdditionalRNG;
                weatherDNF += appConfig.RainDriverReliabilityModifier;
                engineWeatherMultiplier = appConfig.WetEngineMultiplier;
                break;

            case Weather.Storm:
                weatherRNG += appConfig.StormAdditionalRNG;
                weatherDNF += appConfig.StormDriverReliabilityModifier;
                engineWeatherMultiplier = appConfig.WetEngineMultiplier;
                break;
            }

            if (stint.ApplyReliability)
            {
                // Check for the reliability of the chassis.
                if (GetReliabilityResult(team.Reliability + driverResult.ChassisRelMod) == -1)
                {
                    stintResult.StintStatus = StintStatus.ChassisDNF;
                }
                // Check for the reliability of the driver.
                else if (GetReliabilityResult(driver.Reliability + weatherDNF + driverResult.DriverRelMod) == -1)
                {
                    stintResult.StintStatus = StintStatus.DriverDNF;
                    if (driver.SeasonDriverId == 1894)
                    {
                        stintResult.StintStatus = StintStatus.DriverDNF;
                    }
                }
            }

            if (stintResult.StintStatus == StintStatus.Running || stintResult.StintStatus == StintStatus.Mistake)
            {
                // Add one because Random.Next() has an exclusive upper bound.
                int result = _rng.Next(stint.RNGMinimum + driverResult.MinRNG, stint.RNGMaximum + weatherRNG + driverResult.MaxRNG + 1);
                // Iterate through GetReliabilityResult twice to check if a driver made a mistake or not, requires two consequential true's to return a mistake
                bool mistake = false;
                for (int i = 0; i < appConfig.MistakeAmountRolls; i++)
                {
                    mistake = GetReliabilityResult(driver.Reliability + weatherDNF + driverResult.DriverRelMod) == -1;
                    if (!mistake)
                    {
                        break;
                    }
                }
                // If the bool mistake is true then we have to subtract from the result
                if (mistake)
                {
                    result += _rng.Next(appConfig.MistakeLowerValue, appConfig.MistakeUpperValue);
                    stintResult.StintStatus = StintStatus.Mistake;
                }
                // In here we loop through the strategy of the driver to see if it is time for a pitstop
                foreach (var tyreStrat in driverResult.Strategy.Tyres.OrderBy(t => t.StintNumberApplied))
                {
                    // The value for the tyre in iteration matches the current stint number, so it is time for a pitstop
                    if (tyreStrat.StintNumberApplied == stint.Number && stint.Number != 1)
                    {
                        driverResult.TyreLife = tyreStrat.Tyre.Pace;
                        driverResult.CurrTyre = tyreStrat.Tyre;
                        stintResult.Pitstop   = true;
                    }
                }
                // Current status tells us there is a pitstop so calculate pitstop RNG over the result
                if (stintResult.Pitstop)
                {
                    result += _rng.Next(pitMin, pitMax + 1);
                }
                // Deals with the tyre wear for this driver and changes tyres if it is needed
                result += driverResult.TyreLife;
                // Current status tells us the driver is still running so we apply some of the wear to the tyre
                driverResult.TyreLife += _rng.Next(driverResult.CurrTyre.MaxWear + driverResult.MaxTyreWear, driverResult.CurrTyre.MinWear + driverResult.MinTyreWear);

                // Applies the qualifying bonus based on the amount of drivers for the current stint
                if (stint.ApplyQualifyingBonus)
                {
                    result += Helpers.GetQualifyingBonus(driverResult.Grid, driverCount, qualyBonus);
                }
                // Applies the quality of the driver to the current stint if it is relevant
                if (stint.ApplyDriverLevel)
                {
                    result += driver.Skill + driverResult.DriverRacePace;
                }
                // Applies the power of the chassis to the result when it applies
                if (stint.ApplyChassisLevel)
                {
                    int bonus       = Helpers.GetChassisBonus(Helpers.CreateTeamSpecDictionary(team), trackSpec.ToString());
                    int statusBonus = 0;
                    if (driver.DriverStatus == DriverStatus.First)
                    {
                        statusBonus = appConfig.ChassisModifierDriverStatus;
                    }
                    else if (driver.DriverStatus == DriverStatus.Second)
                    {
                        statusBonus = (appConfig.ChassisModifierDriverStatus * -1);
                    }

                    result += team.Chassis + driverResult.ChassisRacePace + bonus + statusBonus;
                }
                // Applies the power of the engine plus external factors when it is relevant for the current stint
                if (stint.ApplyEngineLevel)
                {
                    result += (int)Math.Round((team.Engine.Power + driverResult.EngineRacePace) * engineWeatherMultiplier);
                }
                // Finally adds the result of the stint to the stintresult
                stintResult.Result = result;
            }
        }
	private void detach_SeasonTeams(SeasonTeam entity)
	{
		this.SendPropertyChanging();
		entity.Team = null;
	}
	private void attach_SeasonTeams(SeasonTeam entity)
	{
		this.SendPropertyChanging();
		entity.Team = this;
	}
 partial void DeleteSeasonTeam(SeasonTeam instance);
 partial void UpdateSeasonTeam(SeasonTeam instance);
 partial void InsertSeasonTeam(SeasonTeam instance);