< Summary

Information
Class: RaidLoop.Core.ItemCatalog
Assembly: RaidLoop.Core
File(s): /home/runner/work/RaidLoop/RaidLoop/src/RaidLoop.Core/ItemCatalog.cs
Line coverage
90%
Covered lines: 248
Uncovered lines: 26
Coverable lines: 274
Total lines: 425
Line coverage: 90.5%
Branch coverage
59%
Covered branches: 32
Total branches: 54
Branch coverage: 59.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Get(...)100%11100%
GetByKey(...)50%2266.66%
GetByItemDefId(...)50%2266.66%
GetByLegacyName(...)50%2266.66%
TryGet(...)100%11100%
TryGetByLegacyName(...)100%44100%
TryResolveAuthoredItem(...)100%2020100%
TryGetKeyByLegacyName(...)0%110100%
TryGetItemDefIdByLegacyName(...)0%2040%
TryGetItemDefIdByKey(...)0%2040%
TryGetByKey(...)50%3250%
TryGetByItemDefId(...)100%22100%
Create(...)100%11100%
CreateByKey(...)100%11100%
CreateLegacy(...)100%11100%
RegisterAuthored(...)100%22100%
RegisterCanonical(...)100%11100%
RegisterAlias(...)100%11100%

File(s)

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

#LineLine coverage
 1namespace RaidLoop.Core;
 2
 3public static class ItemCatalog
 4{
 15    private static readonly Dictionary<int, Item> ItemsById = [];
 16    private static readonly Dictionary<string, Item> ItemsByKey = new(StringComparer.OrdinalIgnoreCase);
 7    // Legacy label aliases stay as a compatibility bridge while the app moves to key-first lookups.
 18    private static readonly Dictionary<string, Item> LegacyNames = new(StringComparer.OrdinalIgnoreCase);
 9
 10    static ItemCatalog()
 11    {
 112        RegisterCanonical(new Item("Rusty Knife", ItemType.Weapon, Value: 1, Slots: 1, Rarity: Rarity.Common, DisplayRar
 113        {
 114            ItemDefId = 1,
 115            Key = "rusty_knife"
 116        });
 17
 118        RegisterAuthored(
 119            itemDefId: 2,
 120            key: "makarov",
 121            displayName: "Makarov",
 122            itemType: ItemType.Weapon,
 123            weight: 2,
 124            value: 60,
 125            rarity: Rarity.Common,
 126            displayRarity: DisplayRarity.Common,
 127            aliases: ["Light Pistol"]);
 28
 129        RegisterAuthored(
 130            itemDefId: 3,
 131            key: "ppsh",
 132            displayName: "PPSH",
 133            itemType: ItemType.Weapon,
 134            weight: 12,
 135            value: 160,
 136            rarity: Rarity.Uncommon,
 137            displayRarity: DisplayRarity.Uncommon,
 138            aliases: ["Drum SMG"]);
 39
 140        RegisterAuthored(
 141            itemDefId: 4,
 142            key: "ak74",
 143            displayName: "AK74",
 144            itemType: ItemType.Weapon,
 145            weight: 7,
 146            value: 320,
 147            rarity: Rarity.Rare,
 148            displayRarity: DisplayRarity.Rare,
 149            aliases: ["Field Carbine"]);
 50
 151        RegisterAuthored(
 152            itemDefId: 5,
 153            key: "ak47",
 154            displayName: "AK47",
 155            itemType: ItemType.Weapon,
 156            weight: 10,
 157            value: 375,
 158            rarity: Rarity.Rare,
 159            displayRarity: DisplayRarity.Rare,
 160            aliases: ["Battle Rifle"]);
 61
 162        RegisterAuthored(
 163            itemDefId: 6,
 164            key: "svds",
 165            displayName: "SVDS",
 166            itemType: ItemType.Weapon,
 167            weight: 10,
 168            value: 550,
 169            rarity: Rarity.Epic,
 170            displayRarity: DisplayRarity.Epic,
 171            aliases: ["Marksman Rifle"]);
 72
 173        RegisterAuthored(
 174            itemDefId: 7,
 175            key: "pkp",
 176            displayName: "PKP",
 177            itemType: ItemType.Weapon,
 178            weight: 18,
 179            value: 800,
 180            rarity: Rarity.Legendary,
 181            displayRarity: DisplayRarity.Legendary,
 182            aliases: ["Support Machine Gun"]);
 83
 184        RegisterAuthored(
 185            itemDefId: 8,
 186            key: "6b2_body_armor",
 187            displayName: "6B2 body armor",
 188            itemType: ItemType.Armor,
 189            weight: 9,
 190            value: 95,
 191            rarity: Rarity.Common,
 192            displayRarity: DisplayRarity.Common,
 193            aliases: ["Soft Armor Vest"]);
 94
 195        RegisterAuthored(
 196            itemDefId: 9,
 197            key: "bnti_kirasa_n",
 198            displayName: "BNTI Kirasa-N",
 199            itemType: ItemType.Armor,
 1100            weight: 7,
 1101            value: 160,
 1102            rarity: Rarity.Uncommon,
 1103            displayRarity: DisplayRarity.Uncommon,
 1104            aliases: ["Reinforced Vest"]);
 105
 1106        RegisterAuthored(
 1107            itemDefId: 10,
 1108            key: "6b13_assault_armor",
 1109            displayName: "6B13 assault armor",
 1110            itemType: ItemType.Armor,
 1111            weight: 7,
 1112            value: 225,
 1113            rarity: Rarity.Rare,
 1114            displayRarity: DisplayRarity.Rare,
 1115            aliases: ["Light Plate Carrier"]);
 116
 1117        RegisterAuthored(
 1118            itemDefId: 11,
 1119            key: "fort_defender_2",
 1120            displayName: "FORT Defender-2",
 1121            itemType: ItemType.Armor,
 1122            weight: 22,
 1123            value: 375,
 1124            rarity: Rarity.Epic,
 1125            displayRarity: DisplayRarity.Epic,
 1126            aliases: ["Medium Plate Carrier"]);
 127
 1128        RegisterAuthored(
 1129            itemDefId: 12,
 1130            key: "6b43_zabralo_sh_body_armor",
 1131            displayName: "6B43 Zabralo-Sh body armor",
 1132            itemType: ItemType.Armor,
 1133            weight: 28,
 1134            value: 450,
 1135            rarity: Rarity.Legendary,
 1136            displayRarity: DisplayRarity.Legendary,
 1137            aliases: ["Heavy Plate Carrier"]);
 138
 1139        RegisterAuthored(
 1140            itemDefId: 13,
 1141            key: "nfm_thor",
 1142            displayName: "NFM THOR",
 1143            itemType: ItemType.Armor,
 1144            weight: 19,
 1145            value: 650,
 1146            rarity: Rarity.Legendary,
 1147            displayRarity: DisplayRarity.Legendary,
 1148            aliases: ["Assault Plate Carrier"]);
 149
 1150        RegisterCanonical(new Item("Small Backpack", ItemType.Backpack, Value: 25, Slots: 1, Rarity: Rarity.Common, Disp
 1151        {
 1152            ItemDefId = 14,
 1153            Key = "small_backpack"
 1154        });
 1155        RegisterAuthored(
 1156            itemDefId: 15,
 1157            key: "large_backpack",
 1158            displayName: "Large Backpack",
 1159            itemType: ItemType.Backpack,
 1160            weight: 1,
 1161            value: 50,
 1162            rarity: Rarity.Uncommon,
 1163            displayRarity: DisplayRarity.Uncommon);
 1164        RegisterCanonical(new Item("Tactical Backpack", ItemType.Backpack, Value: 75, Slots: 2, Rarity: Rarity.Rare, Dis
 1165        {
 1166            ItemDefId = 16,
 1167            Key = "tactical_backpack"
 1168        });
 1169        RegisterAuthored(
 1170            itemDefId: 17,
 1171            key: "tasmanian_tiger_trooper_35",
 1172            displayName: "Tasmanian Tiger Trooper 35",
 1173            itemType: ItemType.Backpack,
 1174            weight: 2,
 1175            value: 400,
 1176            rarity: Rarity.Epic,
 1177            displayRarity: DisplayRarity.Epic,
 1178            aliases: ["Hiking Backpack"]);
 1179        RegisterAuthored(
 1180            itemDefId: 18,
 1181            key: "6sh118",
 1182            displayName: "6Sh118",
 1183            itemType: ItemType.Backpack,
 1184            weight: 8,
 1185            value: 600,
 1186            rarity: Rarity.Legendary,
 1187            displayRarity: DisplayRarity.Legendary,
 1188            aliases: ["Raid Backpack"]);
 189
 1190        RegisterCanonical(new Item("Medkit", ItemType.Consumable, Value: 30, Slots: 1, Rarity: Rarity.Common, DisplayRar
 1191        {
 1192            ItemDefId = 19,
 1193            Key = "medkit"
 1194        });
 1195        RegisterCanonical(new Item("Bandage", ItemType.Sellable, Value: 15, Slots: 1, Rarity: Rarity.Common, DisplayRari
 1196        {
 1197            ItemDefId = 20,
 1198            Key = "bandage"
 1199        });
 1200        RegisterCanonical(new Item("Ammo Box", ItemType.Sellable, Value: 20, Slots: 1, Rarity: Rarity.Common, DisplayRar
 1201        {
 1202            ItemDefId = 21,
 1203            Key = "ammo_box"
 1204        });
 1205        RegisterCanonical(new Item("Scrap Metal", ItemType.Material, Value: 18, Slots: 1, Rarity: Rarity.Common, Display
 1206        {
 1207            ItemDefId = 22,
 1208            Key = "scrap_metal"
 1209        });
 1210        RegisterCanonical(new Item("Rare Scope", ItemType.Material, Value: 80, Slots: 1, Rarity: Rarity.Rare, DisplayRar
 1211        {
 1212            ItemDefId = 23,
 1213            Key = "rare_scope"
 1214        });
 1215        RegisterCanonical(new Item("Legendary Trigger Group", ItemType.Material, Value: 150, Slots: 1, Rarity: Rarity.Le
 1216        {
 1217            ItemDefId = 24,
 1218            Key = "legendary_trigger_group"
 1219        });
 1220    }
 221
 222    public static Item Get(string name)
 223    {
 130224        return GetByLegacyName(name);
 225    }
 226
 227    public static Item GetByKey(string key)
 228    {
 127229        if (!TryGetByKey(key, out var item))
 230        {
 0231            throw new KeyNotFoundException($"No authored item definition exists for key '{key}'.");
 232        }
 233
 127234        return item!;
 235    }
 236
 237    public static Item GetByItemDefId(int itemDefId)
 238    {
 9239        if (!TryGetByItemDefId(itemDefId, out var item))
 240        {
 0241            throw new KeyNotFoundException($"No authored item definition exists for itemDefId '{itemDefId}'.");
 242        }
 243
 9244        return item!;
 245    }
 246
 247    public static Item GetByLegacyName(string name)
 248    {
 290249        if (!TryGetByLegacyName(name, out var item))
 250        {
 0251            throw new KeyNotFoundException($"No authored item definition exists for '{name}'.");
 252        }
 253
 290254        return item!;
 255    }
 256
 257    // Legacy compatibility lookup. New code should prefer TryGetByKey/CreateByKey.
 258    public static bool TryGet(string name, out Item? item)
 259    {
 213260        return TryGetByLegacyName(name, out item);
 261    }
 262
 263    public static bool TryGetByLegacyName(string name, out Item? item)
 264    {
 515265        if (string.IsNullOrWhiteSpace(name))
 266        {
 3267            item = null;
 3268            return false;
 269        }
 270
 512271        if (TryGetByKey(name, out item))
 272        {
 347273            return true;
 274        }
 275
 165276        return LegacyNames.TryGetValue(name, out item);
 277    }
 278
 279    public static bool TryResolveAuthoredItem(int itemDefId, string? itemKey, string? legacyName, out Item? item)
 280    {
 27281        if (TryGetByItemDefId(itemDefId, out item) && item is not null)
 282        {
 12283            return true;
 284        }
 285
 15286        if (!string.IsNullOrWhiteSpace(itemKey) && TryGetByKey(itemKey, out item) && item is not null)
 287        {
 2288            return true;
 289        }
 290
 13291        if (itemDefId <= 0
 13292            && string.IsNullOrWhiteSpace(itemKey)
 13293            && !string.IsNullOrWhiteSpace(legacyName)
 13294            && TryGetByLegacyName(legacyName, out item)
 13295            && item is not null)
 296        {
 10297            return true;
 298        }
 299
 3300        item = null;
 3301        return false;
 302    }
 303
 304    public static bool TryGetKeyByLegacyName(string name, out string key)
 305    {
 0306        if (string.IsNullOrWhiteSpace(name))
 307        {
 0308            key = string.Empty;
 0309            return false;
 310        }
 311
 0312        if (TryGetByKey(name, out var item) && item is not null)
 313        {
 0314            key = item.Key;
 0315            return true;
 316        }
 317
 0318        if (LegacyNames.TryGetValue(name, out item) && item is not null)
 319        {
 0320            key = item.Key;
 0321            return true;
 322        }
 323
 0324        key = string.Empty;
 0325        return false;
 326    }
 327
 328    public static bool TryGetItemDefIdByLegacyName(string name, out int itemDefId)
 329    {
 0330        if (TryGetByLegacyName(name, out var item) && item is not null)
 331        {
 0332            itemDefId = item.ItemDefId;
 0333            return true;
 334        }
 335
 0336        itemDefId = 0;
 0337        return false;
 338    }
 339
 340    public static bool TryGetItemDefIdByKey(string key, out int itemDefId)
 341    {
 0342        if (TryGetByKey(key, out var item) && item is not null)
 343        {
 0344            itemDefId = item.ItemDefId;
 0345            return true;
 346        }
 347
 0348        itemDefId = 0;
 0349        return false;
 350    }
 351
 352    public static bool TryGetByKey(string key, out Item? item)
 353    {
 641354        if (string.IsNullOrWhiteSpace(key))
 355        {
 0356            item = null;
 0357            return false;
 358        }
 359
 641360        return ItemsByKey.TryGetValue(key, out item);
 361    }
 362
 363    public static bool TryGetByItemDefId(int itemDefId, out Item? item)
 364    {
 150365        if (itemDefId <= 0)
 366        {
 12367            item = null;
 12368            return false;
 369        }
 370
 138371        return ItemsById.TryGetValue(itemDefId, out item);
 372    }
 373
 374    public static Item Create(string name)
 375    {
 160376        return CreateLegacy(name);
 377    }
 378
 379    public static Item CreateByKey(string key)
 380    {
 103381        return GetByKey(key) with { };
 382    }
 383
 384    public static Item CreateLegacy(string name)
 385    {
 160386        return GetByLegacyName(name) with { };
 387    }
 388
 389    private static void RegisterAuthored(
 390        int itemDefId,
 391        string key,
 392        string displayName,
 393        ItemType itemType,
 394        int weight,
 395        int value,
 396        Rarity rarity,
 397        DisplayRarity displayRarity,
 398        params string[] aliases)
 399    {
 15400        var item = new Item(displayName, itemType, Weight: weight, Value: value, Slots: 1, Rarity: rarity, DisplayRarity
 15401        {
 15402            ItemDefId = itemDefId,
 15403            Key = key
 15404        };
 405
 15406        RegisterCanonical(item);
 407
 58408        foreach (var alias in aliases)
 409        {
 14410            RegisterAlias(alias, item);
 411        }
 15412    }
 413
 414    private static void RegisterCanonical(Item item)
 415    {
 24416        ItemsById[item.ItemDefId] = item;
 24417        ItemsByKey[item.Key] = item;
 24418        LegacyNames[item.Name] = item;
 24419    }
 420
 421    private static void RegisterAlias(string alias, Item item)
 422    {
 14423        LegacyNames[alias] = item with { Name = alias };
 14424    }
 425}