4.6 KiB

TaeKwonDo Competition Filer Function

using System;
using System.Collections.Generic;
using System.Linq;

public class PlayerDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public DateOnly DateOfBirth { get; set; }
    public int ClubId { get; set; }
    public double? Weight { get; set; }
    public double? Height { get; set; }
    public string? Picture { get; set; }
    public string? Phone { get; set; }
    public string? CPR { get; set; }
    public string? Email { get; set; }
}

public class GroupedPlayers
{
    public string Gender { get; set; }
    public string AgeCategory { get; set; }
    public string WeightClass { get; set; }
    public List<PlayerDto> Players { get; set; }
}

public static class TaekwondoPredrawHelper
{
    // Calculate age from DateOfBirth compared to current year
    private static int CalculateAge(DateOnly dob)
    {
        var today = DateOnly.FromDateTime(DateTime.Now);
        int age = today.Year - dob.Year;
        if (dob > today.AddYears(-age)) age--;
        return age;
    }

    // Official WT Age Categories (source: World Taekwondo rules)
    private static string GetAgeCategory(int age)
    {
        if (age >= 4 && age <= 5) return "4-5 Years";
        if (age >= 6 && age <= 7) return "6-7 Years";
        if (age >= 8 && age <= 9) return "8-9 Years";
        if (age >= 10 && age <= 11) return "10-11 Years (Child)";
        if (age >= 12 && age <= 14) return "12-14 Years (Cadet)";
        if (age >= 15 && age <= 17) return "15-17 Years (Junior)";
        if (age >= 18 && age <= 32) return "18-32 Years (Senior)";
        if (age >= 33 && age <= 40) return "33-40 Years (Ultra 1)";
        if (age >= 41 && age <= 50) return "41-50 Years (Ultra 2)";
        if (age >= 51 && age <= 60) return "51-60 Years (Ultra 3)";
        if (age >= 61) return "61+ Years (Ultra 4)";
        return "Unknown";
    }

    // WT Weight Classes example (adjust for event specifics)
    private static string GetWeightClass(double? weight)
    {
        if (!weight.HasValue) return "Unknown";
        if (weight < 46) return "Under 46kg";
        if (weight < 54) return "Under 54kg";
        if (weight < 58) return "Under 58kg";
        if (weight < 63) return "Under 63kg";
        if (weight < 68) return "Under 68kg";
        if (weight < 74) return "Under 74kg";
        if (weight < 80) return "Under 80kg";
        if (weight < 87) return "Under 87kg";
        return "Over 87kg";
    }

    public static List<GroupedPlayers> GroupPlayersForPredraw(List<PlayerDto> players)
    {
        var grouped = players.GroupBy(p =>
            new
            {
                Gender = p.Gender,
                AgeCategory = GetAgeCategory(CalculateAge(p.DateOfBirth)),
                WeightClass = GetWeightClass(p.Weight)
            })
            .Select(g => new GroupedPlayers
            {
                Gender = g.Key.Gender,
                AgeCategory = g.Key.AgeCategory,
                WeightClass = g.Key.WeightClass,
                Players = g.ToList()
            })
            .ToList();

        return grouped;
    }
}

How To Use The Filter

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        // 1. Participants registered for the competition
        var participants = new List<PlayerDto>
        {
            new PlayerDto { Id = 101, Name = "Ali", Gender = "Male", DateOfBirth = new DateOnly(1998, 5, 10), Weight = 65 },
            new PlayerDto { Id = 102, Name = "Sara", Gender = "Female", DateOfBirth = new DateOnly(2010, 8, 20), Weight = 54 },
            new PlayerDto { Id = 103, Name = "John", Gender = "Male", DateOfBirth = new DateOnly(2007, 3, 15), Weight = 57 },
            new PlayerDto { Id = 104, Name = "Maria", Gender = "Female", DateOfBirth = new DateOnly(1985, 12, 5), Weight = 73 },
            new PlayerDto { Id = 105, Name = "Khaled", Gender = "Male", DateOfBirth = new DateOnly(1978, 7, 1), Weight = 82 }
        };

        // 2. Group participants using the WT predraw helper
        var groupedParticipants = TaekwondoPredrawHelper.GroupPlayersForPredraw(participants);

        // 3. Display grouped results for predraw
        foreach (var group in groupedParticipants)
        {
            Console.WriteLine($"Group: Gender={group.Gender}, Age Category={group.AgeCategory}, Weight Class={group.WeightClass}");
            foreach (var player in group.Players)
            {
                Console.WriteLine($"  - {player.Name} (DOB: {player.DateOfBirth}, Weight: {player.Weight}kg)");
            }
        }
    }
}