< Summary

Information
Class: RaidLoop.Core.LootTierProfile
Assembly: RaidLoop.Core
File(s): /home/runner/work/RaidLoop/RaidLoop/src/RaidLoop.Core/LootTierProfile.cs
Line coverage
86%
Covered lines: 25
Uncovered lines: 4
Coverable lines: 29
Total lines: 58
Line coverage: 86.2%
Branch coverage
66%
Covered branches: 8
Total branches: 12
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%4485.71%
Roll(...)75%8886.66%

File(s)

/home/runner/work/RaidLoop/RaidLoop/src/RaidLoop.Core/LootTierProfile.cs

#LineLine coverage
 1namespace RaidLoop.Core;
 2
 3public sealed class LootTierProfile
 4{
 5    private readonly (Rarity Tier, int Weight)[] _weights;
 6
 87    public LootTierProfile(int commonWeight, int uncommonWeight, int rareWeight, int epicWeight, int legendaryWeight)
 8    {
 89        _weights =
 810        [
 811            (Rarity.Common, commonWeight),
 812            (Rarity.Uncommon, uncommonWeight),
 813            (Rarity.Rare, rareWeight),
 814            (Rarity.Epic, epicWeight),
 815            (Rarity.Legendary, legendaryWeight)
 816        ];
 17
 4818        if (_weights.Any(entry => entry.Weight < 0))
 19        {
 020            throw new ArgumentOutOfRangeException(nameof(commonWeight), "Tier weights cannot be negative.");
 21        }
 22
 1823        if (_weights.All(entry => entry.Weight == 0))
 24        {
 025            throw new ArgumentOutOfRangeException(nameof(commonWeight), "At least one tier weight must be positive.");
 26        }
 827    }
 28
 29    public Rarity Roll(IRng rng, IReadOnlyCollection<Rarity> availableTiers)
 30    {
 1001531        ArgumentNullException.ThrowIfNull(rng);
 1001532        ArgumentNullException.ThrowIfNull(availableTiers);
 33
 1001534        var weightedAvailable = _weights
 5007535            .Where(entry => entry.Weight > 0 && availableTiers.Contains(entry.Tier))
 1001536            .ToArray();
 37
 1001538        if (weightedAvailable.Length == 0)
 39        {
 040            throw new InvalidOperationException("No weighted tiers are available to roll.");
 41        }
 42
 6007343        var totalWeight = weightedAvailable.Sum(entry => entry.Weight);
 1001544        var roll = rng.Next(0, totalWeight);
 1001545        var cumulative = 0;
 46
 4306147        foreach (var entry in weightedAvailable)
 48        {
 1652349            cumulative += entry.Weight;
 1652350            if (roll < cumulative)
 51            {
 1001552                return entry.Tier;
 53            }
 54        }
 55
 056        return weightedAvailable[^1].Tier;
 57    }
 58}