Exemple #1
0
        public bool IsPenalty(Match match, out UnbalancedTeams penalty)
        {
            int difference = 0;

            for (int position = 0; position < Team.MaxSize; position++)
            {
                if (match.PositionShouldBeFilled((Position)position))
                {
                    difference += new EffectiveGrade(match.Team1.Player(position), (Position)position).Score();
                    difference -= new EffectiveGrade(match.Team2.Player(position), (Position)position).Score();
                }
            }
            if (difference == 0)
            {
                penalty = null;
                return(false);
            }
            if (difference < 0)
            {
                difference = -difference;
            }
            EffectiveGrade[] team1Grades = new EffectiveGrade[Team.MaxSize];
            EffectiveGrade[] team2Grades = new EffectiveGrade[Team.MaxSize];
            for (int position = 0; position < Team.MaxSize; position++)
            {
                if (match.PositionShouldBeFilled((Position)position))
                {
                    team1Grades[position] = new EffectiveGrade(match.Team1.Player(position), (Position)position);
                    team2Grades[position] = new EffectiveGrade(match.Team2.Player(position), (Position)position);
                }
            }
            penalty = new UnbalancedTeams
            {
                match       = match,
                historical  = new HistoryOfPenalty(),
                score       = weights.UnbalancedTeams.Score * difference / match.Size,
                team1Grades = team1Grades,
                team2Grades = team2Grades,
            };
            return(true);
        }
Exemple #2
0
        public static void Output(string filename, IList <Player> players, IList <Day> history, Weights weights)
        {
            if (!Directory.Exists(Path.GetDirectoryName(filename)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(filename));
            }
            using StreamWriter streamWriter = new StreamWriter(filename);

            streamWriter.Write(JsonConvert.SerializeObject(new Dict()
            {
                { "players", players.Select(WritePlayer) },
                { "history", history.Select(WriteDay) },
                { "weights", WriteWeights(weights) },
            }));

            Dict WritePlayer(Player player) => new Dict
            {
                { "Name", player.Name },
                { "ID", player.ID },
                { "PositionPrimary", player.PositionPrimary },
                { "PositionSecondary", player.PositionSecondary },
                { "GradePrimary", player.GradePrimary },
                { "GradeSecondary", player.GradeSecondary },
                { "PreferredTeamSizes", player.PreferredTeamSizes },
                { "TagNumber", player.TagNumber },
            };

            Dict WriteDay(Day day) => new Dict
            {
                { "date", day.date },
                { "matches", day.matches.Select(WriteMatch) },
            };

            Dict WriteMatch(Match match) => new Dict
            {
                { "Rink", match.rink },
                { "isFixed", match.isFixed },
                { "dontModify", match.dontModify },
                { "Team1", TeamAsString(match.Team1) },
                { "Team2", TeamAsString(match.Team2) },
                { "penalties", match.penalties.Select(WritePenalty) }
            };

            IEnumerable <T> relevantToTeam <T>(Team team, IList <T> list)
            {
                for (int position = 0; position < Team.MaxSize; position++)
                {
                    if (team.PositionShouldBeFilled((Position)position))
                    {
                        yield return(list[position]);
                    }
                }
            }

            string TeamAsString(Team team) => string.Join(",", relevantToTeam(team, team.players).Select(ConvertPlayer));

            string ConvertPlayer(Player player) => player?.ID.ToString() ?? "_";

            Dict WritePenalty(Penalty penalty) => penalty switch
            {
                PairAlreadyPlayedInTeam p => WritePenaltyPAIRALREADYPLAYEDINTEAM(p),
                PairAlreadyPlayedAgainstEachOther p => WritePenaltyPAIRALREADYPLAYEDAGAINSTEACHOTHER(p),
                IncorrectPosition p => WritePenaltyINCORRECTPOSITION(p),
                WrongTeamSize p => WritePenaltyWRONGTEAMSIZE(p),
                UnbalancedPlayers p => WritePenaltyUNBALANCEDPLAYERS(p),
                UnbalancedTeams p => WritePenaltyUNBALANCEDTEAMS(p),
                _ => throw new ArgumentException("Unknown penalty")
            };

            Dict WritePenaltyPAIRALREADYPLAYEDINTEAM(PairAlreadyPlayedInTeam penalty) => new Dict
            {
                { "type", "PAIRALREADYPLAYEDINTEAM" },
                { "historical.mostRecentGameIndex", penalty.historical.mostRecentGameIndex },
                { "historical.numberOfOccurences", penalty.historical.numberOfOccurences },
                { "historical.score", penalty.historical.score },
                { "player1", ConvertPlayer(penalty.player1) },
                { "player2", ConvertPlayer(penalty.player2) },
                { "score", penalty.score },
            };

            Dict WritePenaltyPAIRALREADYPLAYEDAGAINSTEACHOTHER(PairAlreadyPlayedAgainstEachOther penalty) => new Dict
            {
                { "type", "PAIRALREADYPLAYEDAGAINSTEACHOTHER" },
                { "historical.mostRecentGameIndex", penalty.historical.mostRecentGameIndex },
                { "historical.numberOfOccurences", penalty.historical.numberOfOccurences },
                { "historical.score", penalty.historical.score },
                { "player1", ConvertPlayer(penalty.player1) },
                { "player2", ConvertPlayer(penalty.player2) },
                { "score", penalty.score },
            };

            Dict WritePenaltyINCORRECTPOSITION(IncorrectPosition penalty) => new Dict
            {
                { "type", "INCORRECTPOSITION" },
                { "historical.mostRecentGameIndex", penalty.historical.mostRecentGameIndex },
                { "historical.numberOfOccurences", penalty.historical.numberOfOccurences },
                { "historical.score", penalty.historical.score },
                { "player", ConvertPlayer(penalty.player) },
                { "givenPosition", penalty.givenPosition },
                { "wantedPosition", penalty.wantedPosition },
                { "usedSecondary", penalty.usedSecondary },
                { "grade", penalty.grade },
            };

            Dict WritePenaltyWRONGTEAMSIZE(WrongTeamSize penalty) => new Dict
            {
                { "type", "WRONGTEAMSIZE" },
                { "historical.mostRecentGameIndex", penalty.historical.mostRecentGameIndex },
                { "historical.numberOfOccurences", penalty.historical.numberOfOccurences },
                { "historical.score", penalty.historical.score },
                { "player", ConvertPlayer(penalty.player) },
                { "score", penalty.score },
                { "givenSize", penalty.givenSize },
                { "wantedSize", penalty.wantedSize },
            };

            Dict WritePenaltyUNBALANCEDPLAYERS(UnbalancedPlayers penalty) => new Dict
            {
                { "type", "UNBALANCEDPLAYERS" },
                { "historical.mostRecentGameIndex", penalty.historical.mostRecentGameIndex },
                { "historical.numberOfOccurences", penalty.historical.numberOfOccurences },
                { "historical.score", penalty.historical.score },
                { "player1", ConvertPlayer(penalty.player1) },
                { "player2", ConvertPlayer(penalty.player2) },
                { "grade1", WriteEffectiveGrade(penalty.grade1) },
                { "grade2", WriteEffectiveGrade(penalty.grade2) },
                { "score", penalty.score },
            };

            Dict WritePenaltyUNBALANCEDTEAMS(UnbalancedTeams penalty) => new Dict
            {
                { "type", "UNBALANCEDTEAMS" },
                { "score", penalty.score },
                { "grade1", relevantToTeam(penalty.match.Team1, penalty.team1Grades).Select(WriteEffectiveGrade) },
                { "grade2", relevantToTeam(penalty.match.Team2, penalty.team2Grades).Select(WriteEffectiveGrade) },
            };

            Dict WriteEffectiveGrade(EffectiveGrade effectiveGrade) => new Dict
            {
                { "grade", effectiveGrade.grade },
                { "positionIsPrimary", effectiveGrade.positionIsPrimary },
                { "positionIsSecondary", effectiveGrade.positionIsSecondary },
            };

            Dict WriteWeights(Weights weights) => new Dict
            {
                { "IncorrectPosition", WriteWeight(weights.IncorrectPosition) },
                { "IncorrectTeamSize", WriteWeight(weights.IncorrectTeamSize) },
                { "PairPlayedTogetherAgainstEachOther", WriteWeight(weights.PairPlayedTogetherAgainstEachOther) },
                { "PairPlayedTogetherInTeam", WriteWeight(weights.PairPlayedTogetherInTeam) },
                { "SecondaryPosition", WriteWeight(weights.SecondaryPosition) },
                { "UnbalancedPlayers", WriteWeight(weights.UnbalancedPlayers) },
                { "UnbalancedTeams", WriteWeight(weights.UnbalancedTeams) },
                { "GoodSkipsGetSkip", WriteWeight(weights.GoodSkipsGetSkip) },
                { "GoodLeadsMoveUp", WriteWeight(weights.GoodLeadsMoveUp) },
            };

            Dict WriteWeight(Weight weight) => new Dict
            {
                { "Score", weight.Score },
                { "Multiplier", weight.Multiplier },
            };
        }