feat: add shared rarity config and helpers
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
31
shared/lib/rarity.test.ts
Normal file
31
shared/lib/rarity.test.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { describe, it, expect } from "bun:test";
|
||||||
|
import { getRarityConfig, RARITY_CONFIG, defaultName } from "./rarity";
|
||||||
|
|
||||||
|
describe("getRarityConfig", () => {
|
||||||
|
it("returns correct config for known rarities", () => {
|
||||||
|
expect(getRarityConfig("SSR").color).toBe(0xF1C40F);
|
||||||
|
expect(getRarityConfig("SSR").emoji).toBe("🌟");
|
||||||
|
expect(getRarityConfig("SSR").label).toBe("SSR");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns correct config for loot types", () => {
|
||||||
|
expect(getRarityConfig("CURRENCY").color).toBe(0x2ECC71);
|
||||||
|
expect(getRarityConfig("XP").color).toBe(0x1ABC9C);
|
||||||
|
expect(getRarityConfig("NOTHING").color).toBe(0x636363);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("falls back to Common for unknown rarity", () => {
|
||||||
|
const result = getRarityConfig("LEGENDARY");
|
||||||
|
expect(result).toEqual(RARITY_CONFIG["C"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("defaultName", () => {
|
||||||
|
it("extracts filename from path", () => {
|
||||||
|
expect(defaultName("/assets/items/sword.png")).toBe("sword.png");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns image.png for empty path", () => {
|
||||||
|
expect(defaultName("")).toBe("image.png");
|
||||||
|
});
|
||||||
|
});
|
||||||
17
shared/lib/rarity.ts
Normal file
17
shared/lib/rarity.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
export const RARITY_CONFIG: Record<string, { color: number; emoji: string; label: string }> = {
|
||||||
|
C: { color: 0x95A5A6, emoji: "📦", label: "Common" },
|
||||||
|
R: { color: 0x3498DB, emoji: "📦", label: "Rare" },
|
||||||
|
SR: { color: 0x9B59B6, emoji: "✨", label: "Super Rare" },
|
||||||
|
SSR: { color: 0xF1C40F, emoji: "🌟", label: "SSR" },
|
||||||
|
CURRENCY: { color: 0x2ECC71, emoji: "💰", label: "Currency" },
|
||||||
|
XP: { color: 0x1ABC9C, emoji: "🔮", label: "Experience" },
|
||||||
|
NOTHING: { color: 0x636363, emoji: "💨", label: "Empty" },
|
||||||
|
};
|
||||||
|
|
||||||
|
export function getRarityConfig(rarity: string): { color: number; emoji: string; label: string } {
|
||||||
|
return RARITY_CONFIG[rarity] ?? RARITY_CONFIG["C"];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function defaultName(path: string): string {
|
||||||
|
return path.split("/").pop() || "image.png";
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user