3 Commits

Author SHA1 Message Date
hikari 8ccc3f4d0d refactor: hardcode Elysian role ID instead of env var 2026-03-24 18:15:27 -07:00
hikari 2c34fe2c81 fix: update join community modal with correct Discord invite URL 2026-03-24 18:11:54 -07:00
hikari 5025948530 feat: add community role grant and join prompt for players
- Grant Elysian Discord role to players on OAuth login (new and returning)
- Add inGuild flag to Player schema, seeded from role grant response
- Connect Discord Gateway WebSocket to keep inGuild in sync on join/leave
- Return inGuild from load endpoint; expose in game context
- Show join community modal once per session when inGuild is false
2026-03-24 18:10:22 -07:00
17 changed files with 242 additions and 356 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@elysium/api", "name": "@elysium/api",
"version": "0.3.2", "version": "0.3.1",
"private": true, "private": true,
"type": "module", "type": "module",
"main": "./prod/src/index.js", "main": "./prod/src/index.js",
+4
View File
@@ -1,4 +1,6 @@
DISCORD_CLIENT_ID="op://Environment Variables - Naomi/Elysium/discord client id"
DISCORD_CLIENT_SECRET="op://Environment Variables - Naomi/Elysium/discord client secret" DISCORD_CLIENT_SECRET="op://Environment Variables - Naomi/Elysium/discord client secret"
DISCORD_REDIRECT_URI="op://Environment Variables - Naomi/Elysium/discord redirect uri"
JWT_SECRET="op://Environment Variables - Naomi/Elysium/jwt secret" JWT_SECRET="op://Environment Variables - Naomi/Elysium/jwt secret"
DATABASE_URL="op://Environment Variables - Naomi/Elysium/mongo url" DATABASE_URL="op://Environment Variables - Naomi/Elysium/mongo url"
ANTI_CHEAT_SECRET="op://Environment Variables - Naomi/Elysium/anti cheat secret" ANTI_CHEAT_SECRET="op://Environment Variables - Naomi/Elysium/anti cheat secret"
@@ -6,4 +8,6 @@ PORT="op://Environment Variables - Naomi/Elysium/port"
CORS_ORIGIN="op://Environment Variables - Naomi/Elysium/origin" CORS_ORIGIN="op://Environment Variables - Naomi/Elysium/origin"
DISCORD_MILESTONE_WEBHOOK="op://Environment Variables - Naomi/Elysium/discord milestone webhook" DISCORD_MILESTONE_WEBHOOK="op://Environment Variables - Naomi/Elysium/discord milestone webhook"
DISCORD_BOT_TOKEN="op://Environment Variables - Naomi/Elysium/discord bot token" DISCORD_BOT_TOKEN="op://Environment Variables - Naomi/Elysium/discord bot token"
DISCORD_GUILD_ID="op://Environment Variables - Naomi/Elysium/discord guild id"
DISCORD_APOTHEOSIS_ROLE_ID="op://Environment Variables - Naomi/Elysium/discord apotheosis role id"
LOG_TOKEN="op://Environment Variables - Naomi/Alert Server/api_auth" LOG_TOKEN="op://Environment Variables - Naomi/Alert Server/api_auth"
+7 -92
View File
@@ -642,14 +642,6 @@ const patchAdventurerStats = (state: GameState): number => {
if (defaultAdventurer === undefined) { if (defaultAdventurer === undefined) {
continue; continue;
} }
const hasChanged
= savedAdventurer.baseCost !== defaultAdventurer.baseCost
|| savedAdventurer.class !== defaultAdventurer.class
|| savedAdventurer.combatPower !== defaultAdventurer.combatPower
|| savedAdventurer.essencePerSecond !== defaultAdventurer.essencePerSecond
|| savedAdventurer.goldPerSecond !== defaultAdventurer.goldPerSecond
|| savedAdventurer.level !== defaultAdventurer.level
|| savedAdventurer.name !== defaultAdventurer.name;
savedAdventurer.baseCost = defaultAdventurer.baseCost; savedAdventurer.baseCost = defaultAdventurer.baseCost;
savedAdventurer.class = defaultAdventurer.class; savedAdventurer.class = defaultAdventurer.class;
savedAdventurer.combatPower = defaultAdventurer.combatPower; savedAdventurer.combatPower = defaultAdventurer.combatPower;
@@ -657,9 +649,7 @@ const patchAdventurerStats = (state: GameState): number => {
savedAdventurer.goldPerSecond = defaultAdventurer.goldPerSecond; savedAdventurer.goldPerSecond = defaultAdventurer.goldPerSecond;
savedAdventurer.level = defaultAdventurer.level; savedAdventurer.level = defaultAdventurer.level;
savedAdventurer.name = defaultAdventurer.name; savedAdventurer.name = defaultAdventurer.name;
if (hasChanged) { patched = patched + 1;
patched = patched + 1;
}
} }
return patched; return patched;
}; };
@@ -680,15 +670,6 @@ const patchQuestStats = (state: GameState): number => {
if (defaultQuest === undefined) { if (defaultQuest === undefined) {
continue; continue;
} }
const savedPrereqs = JSON.stringify(savedQuest.prerequisiteIds);
const defaultPrereqs = JSON.stringify(defaultQuest.prerequisiteIds);
const hasChanged
= savedQuest.name !== defaultQuest.name
|| savedQuest.description !== defaultQuest.description
|| savedQuest.durationSeconds !== defaultQuest.durationSeconds
|| savedPrereqs !== defaultPrereqs
|| savedQuest.zoneId !== defaultQuest.zoneId
|| savedQuest.combatPowerRequired !== defaultQuest.combatPowerRequired;
savedQuest.name = defaultQuest.name; savedQuest.name = defaultQuest.name;
savedQuest.description = defaultQuest.description; savedQuest.description = defaultQuest.description;
savedQuest.durationSeconds = defaultQuest.durationSeconds; savedQuest.durationSeconds = defaultQuest.durationSeconds;
@@ -697,9 +678,7 @@ const patchQuestStats = (state: GameState): number => {
if (defaultQuest.combatPowerRequired !== undefined) { if (defaultQuest.combatPowerRequired !== undefined) {
savedQuest.combatPowerRequired = defaultQuest.combatPowerRequired; savedQuest.combatPowerRequired = defaultQuest.combatPowerRequired;
} }
if (hasChanged) { patched = patched + 1;
patched = patched + 1;
}
} }
return patched; return patched;
}; };
@@ -710,7 +689,6 @@ const patchQuestStats = (state: GameState): number => {
* @param state - The player's current game state (mutated in place). * @param state - The player's current game state (mutated in place).
* @returns The number of boss entries whose stats were updated. * @returns The number of boss entries whose stats were updated.
*/ */
/* eslint-disable-next-line complexity, max-statements -- Comparing many boss stat fields for change detection */
const patchBossStats = (state: GameState): number => { const patchBossStats = (state: GameState): number => {
const defaultBossMap = new Map(defaultBosses.map((boss) => { const defaultBossMap = new Map(defaultBosses.map((boss) => {
return [ boss.id, boss ] as const; return [ boss.id, boss ] as const;
@@ -721,20 +699,6 @@ const patchBossStats = (state: GameState): number => {
if (defaultBoss === undefined) { if (defaultBoss === undefined) {
continue; continue;
} }
const savedRewards = JSON.stringify(savedBoss.equipmentRewards);
const defaultRewards = JSON.stringify(defaultBoss.equipmentRewards);
const hasChanged
= savedBoss.name !== defaultBoss.name
|| savedBoss.description !== defaultBoss.description
|| savedBoss.maxHp !== defaultBoss.maxHp
|| savedBoss.damagePerSecond !== defaultBoss.damagePerSecond
|| savedBoss.goldReward !== defaultBoss.goldReward
|| savedBoss.essenceReward !== defaultBoss.essenceReward
|| savedBoss.crystalReward !== defaultBoss.crystalReward
|| savedRewards !== defaultRewards
|| savedBoss.prestigeRequirement !== defaultBoss.prestigeRequirement
|| savedBoss.zoneId !== defaultBoss.zoneId
|| savedBoss.bountyRunestones !== defaultBoss.bountyRunestones;
savedBoss.name = defaultBoss.name; savedBoss.name = defaultBoss.name;
savedBoss.description = defaultBoss.description; savedBoss.description = defaultBoss.description;
savedBoss.maxHp = defaultBoss.maxHp; savedBoss.maxHp = defaultBoss.maxHp;
@@ -746,9 +710,7 @@ const patchBossStats = (state: GameState): number => {
savedBoss.prestigeRequirement = defaultBoss.prestigeRequirement; savedBoss.prestigeRequirement = defaultBoss.prestigeRequirement;
savedBoss.zoneId = defaultBoss.zoneId; savedBoss.zoneId = defaultBoss.zoneId;
savedBoss.bountyRunestones = defaultBoss.bountyRunestones; savedBoss.bountyRunestones = defaultBoss.bountyRunestones;
if (hasChanged) { patched = patched + 1;
patched = patched + 1;
}
} }
return patched; return patched;
}; };
@@ -769,20 +731,12 @@ const patchZoneStats = (state: GameState): number => {
if (defaultZone === undefined) { if (defaultZone === undefined) {
continue; continue;
} }
const hasChanged
= savedZone.name !== defaultZone.name
|| savedZone.description !== defaultZone.description
|| savedZone.emoji !== defaultZone.emoji
|| savedZone.unlockBossId !== defaultZone.unlockBossId
|| savedZone.unlockQuestId !== defaultZone.unlockQuestId;
savedZone.name = defaultZone.name; savedZone.name = defaultZone.name;
savedZone.description = defaultZone.description; savedZone.description = defaultZone.description;
savedZone.emoji = defaultZone.emoji; savedZone.emoji = defaultZone.emoji;
savedZone.unlockBossId = defaultZone.unlockBossId; savedZone.unlockBossId = defaultZone.unlockBossId;
savedZone.unlockQuestId = defaultZone.unlockQuestId; savedZone.unlockQuestId = defaultZone.unlockQuestId;
if (hasChanged) { patched = patched + 1;
patched = patched + 1;
}
} }
return patched; return patched;
}; };
@@ -793,7 +747,6 @@ const patchZoneStats = (state: GameState): number => {
* @param state - The player's current game state (mutated in place). * @param state - The player's current game state (mutated in place).
* @returns The number of upgrade entries whose stats were updated. * @returns The number of upgrade entries whose stats were updated.
*/ */
/* eslint-disable-next-line complexity -- Comparing many upgrade stat fields for change detection */
const patchUpgradeStats = (state: GameState): number => { const patchUpgradeStats = (state: GameState): number => {
const defaultUpgradeMap = new Map(defaultUpgrades.map((upgrade) => { const defaultUpgradeMap = new Map(defaultUpgrades.map((upgrade) => {
return [ upgrade.id, upgrade ] as const; return [ upgrade.id, upgrade ] as const;
@@ -804,15 +757,6 @@ const patchUpgradeStats = (state: GameState): number => {
if (defaultUpgrade === undefined) { if (defaultUpgrade === undefined) {
continue; continue;
} }
const hasChanged
= savedUpgrade.name !== defaultUpgrade.name
|| savedUpgrade.description !== defaultUpgrade.description
|| savedUpgrade.target !== defaultUpgrade.target
|| savedUpgrade.adventurerId !== defaultUpgrade.adventurerId
|| savedUpgrade.multiplier !== defaultUpgrade.multiplier
|| savedUpgrade.costGold !== defaultUpgrade.costGold
|| savedUpgrade.costEssence !== defaultUpgrade.costEssence
|| savedUpgrade.costCrystals !== defaultUpgrade.costCrystals;
savedUpgrade.name = defaultUpgrade.name; savedUpgrade.name = defaultUpgrade.name;
savedUpgrade.description = defaultUpgrade.description; savedUpgrade.description = defaultUpgrade.description;
savedUpgrade.target = defaultUpgrade.target; savedUpgrade.target = defaultUpgrade.target;
@@ -823,9 +767,7 @@ const patchUpgradeStats = (state: GameState): number => {
savedUpgrade.costGold = defaultUpgrade.costGold; savedUpgrade.costGold = defaultUpgrade.costGold;
savedUpgrade.costEssence = defaultUpgrade.costEssence; savedUpgrade.costEssence = defaultUpgrade.costEssence;
savedUpgrade.costCrystals = defaultUpgrade.costCrystals; savedUpgrade.costCrystals = defaultUpgrade.costCrystals;
if (hasChanged) { patched = patched + 1;
patched = patched + 1;
}
} }
return patched; return patched;
}; };
@@ -836,7 +778,6 @@ const patchUpgradeStats = (state: GameState): number => {
* @param state - The player's current game state (mutated in place). * @param state - The player's current game state (mutated in place).
* @returns The number of equipment entries whose stats were updated. * @returns The number of equipment entries whose stats were updated.
*/ */
/* eslint-disable-next-line complexity, max-statements -- Comparing many equipment stat fields for change detection */
const patchEquipmentStats = (state: GameState): number => { const patchEquipmentStats = (state: GameState): number => {
const defaultEquipmentMap = new Map(defaultEquipment.map((item) => { const defaultEquipmentMap = new Map(defaultEquipment.map((item) => {
return [ item.id, item ] as const; return [ item.id, item ] as const;
@@ -847,18 +788,6 @@ const patchEquipmentStats = (state: GameState): number => {
if (defaultItem === undefined) { if (defaultItem === undefined) {
continue; continue;
} }
const savedBonus = JSON.stringify(savedItem.bonus);
const defaultBonus = JSON.stringify(defaultItem.bonus);
const savedCost = JSON.stringify(savedItem.cost);
const defaultCost = JSON.stringify(defaultItem.cost);
const hasChanged
= savedItem.name !== defaultItem.name
|| savedItem.description !== defaultItem.description
|| savedItem.type !== defaultItem.type
|| savedItem.rarity !== defaultItem.rarity
|| savedBonus !== defaultBonus
|| savedCost !== defaultCost
|| savedItem.setId !== defaultItem.setId;
savedItem.name = defaultItem.name; savedItem.name = defaultItem.name;
savedItem.description = defaultItem.description; savedItem.description = defaultItem.description;
savedItem.type = defaultItem.type; savedItem.type = defaultItem.type;
@@ -870,9 +799,7 @@ const patchEquipmentStats = (state: GameState): number => {
if (defaultItem.setId !== undefined) { if (defaultItem.setId !== undefined) {
savedItem.setId = defaultItem.setId; savedItem.setId = defaultItem.setId;
} }
if (hasChanged) { patched = patched + 1;
patched = patched + 1;
}
} }
return patched; return patched;
}; };
@@ -893,16 +820,6 @@ const patchAchievementStats = (state: GameState): number => {
if (defaultAchievement === undefined) { if (defaultAchievement === undefined) {
continue; continue;
} }
const savedCondition = JSON.stringify(savedAchievement.condition);
const defaultCondition = JSON.stringify(defaultAchievement.condition);
const savedReward = JSON.stringify(savedAchievement.reward);
const defaultReward = JSON.stringify(defaultAchievement.reward);
const hasChanged
= savedAchievement.name !== defaultAchievement.name
|| savedAchievement.description !== defaultAchievement.description
|| savedAchievement.icon !== defaultAchievement.icon
|| savedCondition !== defaultCondition
|| savedReward !== defaultReward;
savedAchievement.name = defaultAchievement.name; savedAchievement.name = defaultAchievement.name;
savedAchievement.description = defaultAchievement.description; savedAchievement.description = defaultAchievement.description;
savedAchievement.icon = defaultAchievement.icon; savedAchievement.icon = defaultAchievement.icon;
@@ -910,9 +827,7 @@ const patchAchievementStats = (state: GameState): number => {
if (defaultAchievement.reward !== undefined) { if (defaultAchievement.reward !== undefined) {
savedAchievement.reward = { ...defaultAchievement.reward }; savedAchievement.reward = { ...defaultAchievement.reward };
} }
if (hasChanged) { patched = patched + 1;
patched = patched + 1;
}
} }
return patched; return patched;
}; };
+21 -8
View File
@@ -7,9 +7,6 @@
/* eslint-disable @typescript-eslint/naming-convention -- Discord API requires snake_case fields and HTTP headers require Pascal-Case */ /* eslint-disable @typescript-eslint/naming-convention -- Discord API requires snake_case fields and HTTP headers require Pascal-Case */
import { logger } from "./logger.js"; import { logger } from "./logger.js";
const discordClientId = "1479551654264049908";
const discordRedirectUri = "https://elysium.nhcarrigan.com/api/auth/callback";
interface DiscordTokenResponse { interface DiscordTokenResponse {
access_token: string; access_token: string;
token_type: string; token_type: string;
@@ -34,18 +31,24 @@ interface DiscordUser {
const exchangeCode = async( const exchangeCode = async(
code: string, code: string,
): Promise<DiscordTokenResponse> => { ): Promise<DiscordTokenResponse> => {
const clientId = process.env.DISCORD_CLIENT_ID;
const clientSecret = process.env.DISCORD_CLIENT_SECRET; const clientSecret = process.env.DISCORD_CLIENT_SECRET;
const redirectUri = process.env.DISCORD_REDIRECT_URI;
if (clientSecret === undefined || clientSecret === "") { if (
clientId === undefined || clientId === ""
|| clientSecret === undefined || clientSecret === ""
|| redirectUri === undefined || redirectUri === ""
) {
throw new Error("Discord OAuth environment variables are required"); throw new Error("Discord OAuth environment variables are required");
} }
const parameters = new URLSearchParams({ const parameters = new URLSearchParams({
client_id: discordClientId, client_id: clientId,
client_secret: clientSecret, client_secret: clientSecret,
code: code, code: code,
grant_type: "authorization_code", grant_type: "authorization_code",
redirect_uri: discordRedirectUri, redirect_uri: redirectUri,
}); });
try { try {
@@ -143,9 +146,19 @@ const fetchDiscordUserById = async(
* @throws {Error} If OAuth environment variables are missing. * @throws {Error} If OAuth environment variables are missing.
*/ */
const buildOAuthUrl = (): string => { const buildOAuthUrl = (): string => {
const clientId = process.env.DISCORD_CLIENT_ID;
const redirectUri = process.env.DISCORD_REDIRECT_URI;
if (
clientId === undefined || clientId === ""
|| redirectUri === undefined || redirectUri === ""
) {
throw new Error("Discord OAuth environment variables are required");
}
const parameters = new URLSearchParams({ const parameters = new URLSearchParams({
client_id: discordClientId, client_id: clientId,
redirect_uri: discordRedirectUri, redirect_uri: redirectUri,
response_type: "code", response_type: "code",
scope: "identify", scope: "identify",
}); });
+4 -4
View File
@@ -8,8 +8,6 @@
import { prisma } from "../db/client.js"; import { prisma } from "../db/client.js";
import { logger } from "./logger.js"; import { logger } from "./logger.js";
const discordGuildId = "1354624415861833870";
/** /**
* Discord Gateway opcodes used by this client. * Discord Gateway opcodes used by this client.
*/ */
@@ -38,7 +36,8 @@ const handleGuildMemberAdd = async(
discordId: string, discordId: string,
guildId: string, guildId: string,
): Promise<void> => { ): Promise<void> => {
if (guildId !== discordGuildId) { const configuredGuildId = process.env.DISCORD_GUILD_ID;
if (guildId !== configuredGuildId) {
return; return;
} }
try { try {
@@ -67,7 +66,8 @@ const handleGuildMemberRemove = async(
discordId: string, discordId: string,
guildId: string, guildId: string,
): Promise<void> => { ): Promise<void> => {
if (guildId !== discordGuildId) { const configuredGuildId = process.env.DISCORD_GUILD_ID;
if (guildId !== configuredGuildId) {
return; return;
} }
try { try {
+14 -6
View File
@@ -18,9 +18,7 @@ const suppressNotifications = 4096;
/** /**
* The Discord role ID for the Elysian role granted to all Elysium players. * The Discord role ID for the Elysian role granted to all Elysium players.
*/ */
const discordGuildId = "1354624415861833870";
const elysianRoleId = "1486144823684628490"; const elysianRoleId = "1486144823684628490";
const apotheosisRoleId = "1479966598210129991";
/** /**
* Grants the Elysian Discord role to the given player and returns whether they are in the guild. * Grants the Elysian Discord role to the given player and returns whether they are in the guild.
@@ -30,14 +28,18 @@ const apotheosisRoleId = "1479966598210129991";
*/ */
const grantElysianRole = async(discordId: string): Promise<boolean> => { const grantElysianRole = async(discordId: string): Promise<boolean> => {
const botToken = process.env.DISCORD_BOT_TOKEN; const botToken = process.env.DISCORD_BOT_TOKEN;
const guildId = process.env.DISCORD_GUILD_ID;
if (botToken === undefined || botToken === "") { if (
botToken === undefined || botToken === ""
|| guildId === undefined || guildId === ""
) {
return false; return false;
} }
try { try {
const response = await fetch( const response = await fetch(
`${discordApi}/guilds/${discordGuildId}/members/${discordId}/roles/${elysianRoleId}`, `${discordApi}/guilds/${guildId}/members/${discordId}/roles/${elysianRoleId}`,
{ {
headers: { headers: {
"Authorization": `Bot ${botToken}`, "Authorization": `Bot ${botToken}`,
@@ -66,14 +68,20 @@ const grantElysianRole = async(discordId: string): Promise<boolean> => {
*/ */
const grantApotheosisRole = async(discordId: string): Promise<void> => { const grantApotheosisRole = async(discordId: string): Promise<void> => {
const botToken = process.env.DISCORD_BOT_TOKEN; const botToken = process.env.DISCORD_BOT_TOKEN;
const guildId = process.env.DISCORD_GUILD_ID;
const roleId = process.env.DISCORD_APOTHEOSIS_ROLE_ID;
if (botToken === undefined || botToken === "") { if (
botToken === undefined || botToken === ""
|| guildId === undefined || guildId === ""
|| roleId === undefined || roleId === ""
) {
return; return;
} }
try { try {
await fetch( await fetch(
`${discordApi}/guilds/${discordGuildId}/members/${discordId}/roles/${apotheosisRoleId}`, `${discordApi}/guilds/${guildId}/members/${discordId}/roles/${roleId}`,
{ {
headers: { headers: {
"Authorization": `Bot ${botToken}`, "Authorization": `Bot ${botToken}`,
+25 -3
View File
@@ -18,31 +18,51 @@ describe("discord service", () => {
}); });
describe("buildOAuthUrl", () => { describe("buildOAuthUrl", () => {
it("throws when DISCORD_CLIENT_ID is missing", async () => {
delete process.env["DISCORD_CLIENT_ID"];
process.env["DISCORD_REDIRECT_URI"] = "http://localhost/callback";
const { buildOAuthUrl } = await import("../../src/services/discord.js");
expect(() => buildOAuthUrl()).toThrow("Discord OAuth environment variables are required");
});
it("throws when DISCORD_REDIRECT_URI is missing", async () => {
process.env["DISCORD_CLIENT_ID"] = "client123";
delete process.env["DISCORD_REDIRECT_URI"];
const { buildOAuthUrl } = await import("../../src/services/discord.js");
expect(() => buildOAuthUrl()).toThrow("Discord OAuth environment variables are required");
});
it("returns a URL with correct query params", async () => { it("returns a URL with correct query params", async () => {
process.env["DISCORD_CLIENT_ID"] = "client123";
process.env["DISCORD_REDIRECT_URI"] = "http://localhost/callback";
const { buildOAuthUrl } = await import("../../src/services/discord.js"); const { buildOAuthUrl } = await import("../../src/services/discord.js");
const url = buildOAuthUrl(); const url = buildOAuthUrl();
expect(url).toContain("client_id=1479551654264049908"); expect(url).toContain("client_id=client123");
expect(url).toContain("response_type=code"); expect(url).toContain("response_type=code");
expect(url).toContain("scope=identify"); expect(url).toContain("scope=identify");
}); });
}); });
describe("exchangeCode", () => { describe("exchangeCode", () => {
it("throws when DISCORD_CLIENT_SECRET is missing", async () => { it("throws when env vars are missing", async () => {
delete process.env["DISCORD_CLIENT_SECRET"]; delete process.env["DISCORD_CLIENT_ID"];
const { exchangeCode } = await import("../../src/services/discord.js"); const { exchangeCode } = await import("../../src/services/discord.js");
await expect(exchangeCode("mycode")).rejects.toThrow("Discord OAuth environment variables are required"); await expect(exchangeCode("mycode")).rejects.toThrow("Discord OAuth environment variables are required");
}); });
it("throws when response is not ok", async () => { it("throws when response is not ok", async () => {
process.env["DISCORD_CLIENT_ID"] = "cid";
process.env["DISCORD_CLIENT_SECRET"] = "secret"; process.env["DISCORD_CLIENT_SECRET"] = "secret";
process.env["DISCORD_REDIRECT_URI"] = "http://localhost/cb";
mockFetch.mockResolvedValueOnce({ ok: false, statusText: "Unauthorized" }); mockFetch.mockResolvedValueOnce({ ok: false, statusText: "Unauthorized" });
const { exchangeCode } = await import("../../src/services/discord.js"); const { exchangeCode } = await import("../../src/services/discord.js");
await expect(exchangeCode("bad_code")).rejects.toThrow("Discord token exchange failed"); await expect(exchangeCode("bad_code")).rejects.toThrow("Discord token exchange failed");
}); });
it("returns parsed body on success", async () => { it("returns parsed body on success", async () => {
process.env["DISCORD_CLIENT_ID"] = "cid";
process.env["DISCORD_CLIENT_SECRET"] = "secret"; process.env["DISCORD_CLIENT_SECRET"] = "secret";
process.env["DISCORD_REDIRECT_URI"] = "http://localhost/cb";
const tokenData = { access_token: "tok", token_type: "Bearer", expires_in: 3600, refresh_token: "ref", scope: "identify" }; const tokenData = { access_token: "tok", token_type: "Bearer", expires_in: 3600, refresh_token: "ref", scope: "identify" };
mockFetch.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(tokenData) }); mockFetch.mockResolvedValueOnce({ ok: true, json: () => Promise.resolve(tokenData) });
const { exchangeCode } = await import("../../src/services/discord.js"); const { exchangeCode } = await import("../../src/services/discord.js");
@@ -76,7 +96,9 @@ describe("discord service", () => {
describe("exchangeCode non-Error throw", () => { describe("exchangeCode non-Error throw", () => {
it("re-throws when fetch rejects with a non-Error value", async () => { it("re-throws when fetch rejects with a non-Error value", async () => {
process.env["DISCORD_CLIENT_ID"] = "cid";
process.env["DISCORD_CLIENT_SECRET"] = "secret"; process.env["DISCORD_CLIENT_SECRET"] = "secret";
process.env["DISCORD_REDIRECT_URI"] = "http://localhost/cb";
mockFetch.mockRejectedValueOnce("raw string error"); mockFetch.mockRejectedValueOnce("raw string error");
const { exchangeCode } = await import("../../src/services/discord.js"); const { exchangeCode } = await import("../../src/services/discord.js");
await expect(exchangeCode("some_code")).rejects.toBe("raw string error"); await expect(exchangeCode("some_code")).rejects.toBe("raw string error");
+34 -11
View File
@@ -16,48 +16,60 @@ vi.mock("../../src/services/logger.js", () => ({
import { prisma } from "../../src/db/client.js"; import { prisma } from "../../src/db/client.js";
const discordGuildId = "1354624415861833870";
describe("gateway service", () => { describe("gateway service", () => {
const ORIGINAL_ENV = process.env;
beforeEach(() => { beforeEach(() => {
process.env = { ...ORIGINAL_ENV };
vi.resetAllMocks(); vi.resetAllMocks();
}); });
afterEach(() => { afterEach(() => {
vi.clearAllMocks(); process.env = ORIGINAL_ENV;
}); });
describe("handleGuildMemberAdd", () => { describe("handleGuildMemberAdd", () => {
it("sets inGuild to true for the matching guild", async () => { it("sets inGuild to true for the matching guild", async () => {
process.env["DISCORD_GUILD_ID"] = "guild123";
vi.mocked(prisma.player.updateMany).mockResolvedValueOnce({ count: 1 }); vi.mocked(prisma.player.updateMany).mockResolvedValueOnce({ count: 1 });
const { handleGuildMemberAdd } = await import("../../src/services/gateway.js"); const { handleGuildMemberAdd } = await import("../../src/services/gateway.js");
await handleGuildMemberAdd("user123", discordGuildId); await handleGuildMemberAdd("user123", "guild123");
expect(prisma.player.updateMany).toHaveBeenCalledWith({ expect(prisma.player.updateMany).toHaveBeenCalledWith({
data: { inGuild: true }, data: { inGuild: true },
where: { discordId: "user123" }, where: { discordId: "user123" },
}); });
}); });
it("no-ops when guild id does not match the configured guild", async () => { it("no-ops when guild id does not match", async () => {
process.env["DISCORD_GUILD_ID"] = "guild123";
const { handleGuildMemberAdd } = await import("../../src/services/gateway.js"); const { handleGuildMemberAdd } = await import("../../src/services/gateway.js");
await handleGuildMemberAdd("user123", "other_guild"); await handleGuildMemberAdd("user123", "other_guild");
expect(prisma.player.updateMany).not.toHaveBeenCalled(); expect(prisma.player.updateMany).not.toHaveBeenCalled();
}); });
it("no-ops when DISCORD_GUILD_ID env var is missing and guild does not match undefined", async () => {
delete process.env["DISCORD_GUILD_ID"];
const { handleGuildMemberAdd } = await import("../../src/services/gateway.js");
await handleGuildMemberAdd("user123", "guild123");
expect(prisma.player.updateMany).not.toHaveBeenCalled();
});
it("logs error when prisma throws an Error", async () => { it("logs error when prisma throws an Error", async () => {
process.env["DISCORD_GUILD_ID"] = "guild123";
const dbError = new Error("DB failure"); const dbError = new Error("DB failure");
vi.mocked(prisma.player.updateMany).mockRejectedValueOnce(dbError); vi.mocked(prisma.player.updateMany).mockRejectedValueOnce(dbError);
const { handleGuildMemberAdd } = await import("../../src/services/gateway.js"); const { handleGuildMemberAdd } = await import("../../src/services/gateway.js");
const { logger } = await import("../../src/services/logger.js"); const { logger } = await import("../../src/services/logger.js");
await handleGuildMemberAdd("user123", discordGuildId); await handleGuildMemberAdd("user123", "guild123");
expect(logger.error).toHaveBeenCalledWith("gateway_member_add", dbError); expect(logger.error).toHaveBeenCalledWith("gateway_member_add", dbError);
}); });
it("logs error when prisma throws a non-Error", async () => { it("logs error when prisma throws a non-Error", async () => {
process.env["DISCORD_GUILD_ID"] = "guild123";
vi.mocked(prisma.player.updateMany).mockRejectedValueOnce("raw error"); vi.mocked(prisma.player.updateMany).mockRejectedValueOnce("raw error");
const { handleGuildMemberAdd } = await import("../../src/services/gateway.js"); const { handleGuildMemberAdd } = await import("../../src/services/gateway.js");
const { logger } = await import("../../src/services/logger.js"); const { logger } = await import("../../src/services/logger.js");
await handleGuildMemberAdd("user123", discordGuildId); await handleGuildMemberAdd("user123", "guild123");
expect(logger.error).toHaveBeenCalledWith( expect(logger.error).toHaveBeenCalledWith(
"gateway_member_add", "gateway_member_add",
new Error("raw error"), new Error("raw error"),
@@ -67,35 +79,46 @@ describe("gateway service", () => {
describe("handleGuildMemberRemove", () => { describe("handleGuildMemberRemove", () => {
it("sets inGuild to false for the matching guild", async () => { it("sets inGuild to false for the matching guild", async () => {
process.env["DISCORD_GUILD_ID"] = "guild123";
vi.mocked(prisma.player.updateMany).mockResolvedValueOnce({ count: 1 }); vi.mocked(prisma.player.updateMany).mockResolvedValueOnce({ count: 1 });
const { handleGuildMemberRemove } = await import("../../src/services/gateway.js"); const { handleGuildMemberRemove } = await import("../../src/services/gateway.js");
await handleGuildMemberRemove("user123", discordGuildId); await handleGuildMemberRemove("user123", "guild123");
expect(prisma.player.updateMany).toHaveBeenCalledWith({ expect(prisma.player.updateMany).toHaveBeenCalledWith({
data: { inGuild: false }, data: { inGuild: false },
where: { discordId: "user123" }, where: { discordId: "user123" },
}); });
}); });
it("no-ops when guild id does not match the configured guild", async () => { it("no-ops when guild id does not match", async () => {
process.env["DISCORD_GUILD_ID"] = "guild123";
const { handleGuildMemberRemove } = await import("../../src/services/gateway.js"); const { handleGuildMemberRemove } = await import("../../src/services/gateway.js");
await handleGuildMemberRemove("user123", "other_guild"); await handleGuildMemberRemove("user123", "other_guild");
expect(prisma.player.updateMany).not.toHaveBeenCalled(); expect(prisma.player.updateMany).not.toHaveBeenCalled();
}); });
it("no-ops when DISCORD_GUILD_ID env var is missing", async () => {
delete process.env["DISCORD_GUILD_ID"];
const { handleGuildMemberRemove } = await import("../../src/services/gateway.js");
await handleGuildMemberRemove("user123", "guild123");
expect(prisma.player.updateMany).not.toHaveBeenCalled();
});
it("logs error when prisma throws an Error", async () => { it("logs error when prisma throws an Error", async () => {
process.env["DISCORD_GUILD_ID"] = "guild123";
const dbError = new Error("DB failure"); const dbError = new Error("DB failure");
vi.mocked(prisma.player.updateMany).mockRejectedValueOnce(dbError); vi.mocked(prisma.player.updateMany).mockRejectedValueOnce(dbError);
const { handleGuildMemberRemove } = await import("../../src/services/gateway.js"); const { handleGuildMemberRemove } = await import("../../src/services/gateway.js");
const { logger } = await import("../../src/services/logger.js"); const { logger } = await import("../../src/services/logger.js");
await handleGuildMemberRemove("user123", discordGuildId); await handleGuildMemberRemove("user123", "guild123");
expect(logger.error).toHaveBeenCalledWith("gateway_member_remove", dbError); expect(logger.error).toHaveBeenCalledWith("gateway_member_remove", dbError);
}); });
it("logs error when prisma throws a non-Error", async () => { it("logs error when prisma throws a non-Error", async () => {
process.env["DISCORD_GUILD_ID"] = "guild123";
vi.mocked(prisma.player.updateMany).mockRejectedValueOnce("raw error"); vi.mocked(prisma.player.updateMany).mockRejectedValueOnce("raw error");
const { handleGuildMemberRemove } = await import("../../src/services/gateway.js"); const { handleGuildMemberRemove } = await import("../../src/services/gateway.js");
const { logger } = await import("../../src/services/logger.js"); const { logger } = await import("../../src/services/logger.js");
await handleGuildMemberRemove("user123", discordGuildId); await handleGuildMemberRemove("user123", "guild123");
expect(logger.error).toHaveBeenCalledWith( expect(logger.error).toHaveBeenCalledWith(
"gateway_member_remove", "gateway_member_remove",
new Error("raw error"), new Error("raw error"),
+47 -4
View File
@@ -20,20 +20,42 @@ describe("webhook service", () => {
describe("grantApotheosisRole", () => { describe("grantApotheosisRole", () => {
it("does nothing when bot token is missing", async () => { it("does nothing when bot token is missing", async () => {
delete process.env["DISCORD_BOT_TOKEN"]; delete process.env["DISCORD_BOT_TOKEN"];
process.env["DISCORD_GUILD_ID"] = "guild123";
process.env["DISCORD_APOTHEOSIS_ROLE_ID"] = "role123";
const { grantApotheosisRole } = await import("../../src/services/webhook.js"); const { grantApotheosisRole } = await import("../../src/services/webhook.js");
await grantApotheosisRole("user123"); await grantApotheosisRole("user123");
expect(mockFetch).not.toHaveBeenCalled(); expect(mockFetch).not.toHaveBeenCalled();
}); });
it("calls Discord API with correct URL and auth when bot token is set", async () => { it("does nothing when guild id is missing", async () => {
process.env["DISCORD_BOT_TOKEN"] = "token";
delete process.env["DISCORD_GUILD_ID"];
process.env["DISCORD_APOTHEOSIS_ROLE_ID"] = "role123";
const { grantApotheosisRole } = await import("../../src/services/webhook.js");
await grantApotheosisRole("user123");
expect(mockFetch).not.toHaveBeenCalled();
});
it("does nothing when role id is missing", async () => {
process.env["DISCORD_BOT_TOKEN"] = "token";
process.env["DISCORD_GUILD_ID"] = "guild123";
delete process.env["DISCORD_APOTHEOSIS_ROLE_ID"];
const { grantApotheosisRole } = await import("../../src/services/webhook.js");
await grantApotheosisRole("user123");
expect(mockFetch).not.toHaveBeenCalled();
});
it("calls Discord API with correct URL and auth when env vars are set", async () => {
process.env["DISCORD_BOT_TOKEN"] = "bot_token"; process.env["DISCORD_BOT_TOKEN"] = "bot_token";
process.env["DISCORD_GUILD_ID"] = "guild123";
process.env["DISCORD_APOTHEOSIS_ROLE_ID"] = "role456";
mockFetch.mockResolvedValueOnce({ ok: true }); mockFetch.mockResolvedValueOnce({ ok: true });
const { grantApotheosisRole } = await import("../../src/services/webhook.js"); const { grantApotheosisRole } = await import("../../src/services/webhook.js");
await grantApotheosisRole("user789"); await grantApotheosisRole("user789");
expect(mockFetch).toHaveBeenCalledWith( expect(mockFetch).toHaveBeenCalledWith(
"https://discord.com/api/v10/guilds/1354624415861833870/members/user789/roles/1479966598210129991", "https://discord.com/api/v10/guilds/guild123/members/user789/roles/role456",
expect.objectContaining({ expect.objectContaining({
method: "PUT", method: "PUT",
headers: expect.objectContaining({ Authorization: "Bot bot_token" }), headers: expect.objectContaining({ Authorization: "Bot bot_token" }),
}), }),
); );
@@ -41,6 +63,8 @@ describe("webhook service", () => {
it("swallows fetch errors gracefully", async () => { it("swallows fetch errors gracefully", async () => {
process.env["DISCORD_BOT_TOKEN"] = "tok"; process.env["DISCORD_BOT_TOKEN"] = "tok";
process.env["DISCORD_GUILD_ID"] = "g";
process.env["DISCORD_APOTHEOSIS_ROLE_ID"] = "r";
mockFetch.mockRejectedValueOnce(new Error("Network error")); mockFetch.mockRejectedValueOnce(new Error("Network error"));
const { grantApotheosisRole } = await import("../../src/services/webhook.js"); const { grantApotheosisRole } = await import("../../src/services/webhook.js");
await expect(grantApotheosisRole("user")).resolves.toBeUndefined(); await expect(grantApotheosisRole("user")).resolves.toBeUndefined();
@@ -48,6 +72,8 @@ describe("webhook service", () => {
it("swallows non-Error fetch rejections gracefully", async () => { it("swallows non-Error fetch rejections gracefully", async () => {
process.env["DISCORD_BOT_TOKEN"] = "tok"; process.env["DISCORD_BOT_TOKEN"] = "tok";
process.env["DISCORD_GUILD_ID"] = "g";
process.env["DISCORD_APOTHEOSIS_ROLE_ID"] = "r";
mockFetch.mockRejectedValueOnce("raw string error"); mockFetch.mockRejectedValueOnce("raw string error");
const { grantApotheosisRole } = await import("../../src/services/webhook.js"); const { grantApotheosisRole } = await import("../../src/services/webhook.js");
await expect(grantApotheosisRole("user")).resolves.toBeUndefined(); await expect(grantApotheosisRole("user")).resolves.toBeUndefined();
@@ -57,6 +83,18 @@ describe("webhook service", () => {
describe("grantElysianRole", () => { describe("grantElysianRole", () => {
it("does nothing when bot token is missing", async () => { it("does nothing when bot token is missing", async () => {
delete process.env["DISCORD_BOT_TOKEN"]; delete process.env["DISCORD_BOT_TOKEN"];
process.env["DISCORD_GUILD_ID"] = "guild123";
process.env["DISCORD_ELYSIAN_ROLE_ID"] = "role123";
const { grantElysianRole } = await import("../../src/services/webhook.js");
const result = await grantElysianRole("user123");
expect(mockFetch).not.toHaveBeenCalled();
expect(result).toBe(false);
});
it("does nothing when guild id is missing", async () => {
process.env["DISCORD_BOT_TOKEN"] = "token";
delete process.env["DISCORD_GUILD_ID"];
process.env["DISCORD_ELYSIAN_ROLE_ID"] = "role123";
const { grantElysianRole } = await import("../../src/services/webhook.js"); const { grantElysianRole } = await import("../../src/services/webhook.js");
const result = await grantElysianRole("user123"); const result = await grantElysianRole("user123");
expect(mockFetch).not.toHaveBeenCalled(); expect(mockFetch).not.toHaveBeenCalled();
@@ -65,11 +103,12 @@ describe("webhook service", () => {
it("returns true when Discord API responds with ok", async () => { it("returns true when Discord API responds with ok", async () => {
process.env["DISCORD_BOT_TOKEN"] = "bot_token"; process.env["DISCORD_BOT_TOKEN"] = "bot_token";
process.env["DISCORD_GUILD_ID"] = "guild123";
mockFetch.mockResolvedValueOnce({ ok: true, status: 200 }); mockFetch.mockResolvedValueOnce({ ok: true, status: 200 });
const { grantElysianRole } = await import("../../src/services/webhook.js"); const { grantElysianRole } = await import("../../src/services/webhook.js");
const result = await grantElysianRole("user789"); const result = await grantElysianRole("user789");
expect(mockFetch).toHaveBeenCalledWith( expect(mockFetch).toHaveBeenCalledWith(
"https://discord.com/api/v10/guilds/1354624415861833870/members/user789/roles/1486144823684628490", "https://discord.com/api/v10/guilds/guild123/members/user789/roles/1486144823684628490",
expect.objectContaining({ expect.objectContaining({
method: "PUT", method: "PUT",
headers: expect.objectContaining({ Authorization: "Bot bot_token" }), headers: expect.objectContaining({ Authorization: "Bot bot_token" }),
@@ -80,6 +119,7 @@ describe("webhook service", () => {
it("returns true when Discord API responds with 204", async () => { it("returns true when Discord API responds with 204", async () => {
process.env["DISCORD_BOT_TOKEN"] = "tok"; process.env["DISCORD_BOT_TOKEN"] = "tok";
process.env["DISCORD_GUILD_ID"] = "g";
mockFetch.mockResolvedValueOnce({ ok: false, status: 204 }); mockFetch.mockResolvedValueOnce({ ok: false, status: 204 });
const { grantElysianRole } = await import("../../src/services/webhook.js"); const { grantElysianRole } = await import("../../src/services/webhook.js");
const result = await grantElysianRole("user"); const result = await grantElysianRole("user");
@@ -88,6 +128,7 @@ describe("webhook service", () => {
it("returns false when Discord API responds with an error status", async () => { it("returns false when Discord API responds with an error status", async () => {
process.env["DISCORD_BOT_TOKEN"] = "tok"; process.env["DISCORD_BOT_TOKEN"] = "tok";
process.env["DISCORD_GUILD_ID"] = "g";
mockFetch.mockResolvedValueOnce({ ok: false, status: 403 }); mockFetch.mockResolvedValueOnce({ ok: false, status: 403 });
const { grantElysianRole } = await import("../../src/services/webhook.js"); const { grantElysianRole } = await import("../../src/services/webhook.js");
const result = await grantElysianRole("user"); const result = await grantElysianRole("user");
@@ -96,6 +137,7 @@ describe("webhook service", () => {
it("returns false and swallows fetch errors gracefully", async () => { it("returns false and swallows fetch errors gracefully", async () => {
process.env["DISCORD_BOT_TOKEN"] = "tok"; process.env["DISCORD_BOT_TOKEN"] = "tok";
process.env["DISCORD_GUILD_ID"] = "g";
mockFetch.mockRejectedValueOnce(new Error("Network error")); mockFetch.mockRejectedValueOnce(new Error("Network error"));
const { grantElysianRole } = await import("../../src/services/webhook.js"); const { grantElysianRole } = await import("../../src/services/webhook.js");
const result = await grantElysianRole("user"); const result = await grantElysianRole("user");
@@ -104,6 +146,7 @@ describe("webhook service", () => {
it("returns false and swallows non-Error fetch rejections", async () => { it("returns false and swallows non-Error fetch rejections", async () => {
process.env["DISCORD_BOT_TOKEN"] = "tok"; process.env["DISCORD_BOT_TOKEN"] = "tok";
process.env["DISCORD_GUILD_ID"] = "g";
mockFetch.mockRejectedValueOnce("raw string error"); mockFetch.mockRejectedValueOnce("raw string error");
const { grantElysianRole } = await import("../../src/services/webhook.js"); const { grantElysianRole } = await import("../../src/services/webhook.js");
const result = await grantElysianRole("user"); const result = await grantElysianRole("user");
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@elysium/web", "name": "@elysium/web",
"version": "0.3.2", "version": "0.3.1",
"private": true, "private": true,
"type": "module", "type": "module",
"scripts": { "scripts": {
+69 -16
View File
@@ -11,11 +11,10 @@
/* eslint-disable max-lines -- Boss panel with sub-component and helper function */ /* eslint-disable max-lines -- Boss panel with sub-component and helper function */
import { type JSX, useState } from "react"; import { type JSX, useState } from "react";
import { useGame } from "../../context/gameContext.js"; import { useGame } from "../../context/gameContext.js";
import { computePartyCombatPower } from "../../engine/tick.js";
import { cdnImage } from "../../utils/cdn.js"; import { cdnImage } from "../../utils/cdn.js";
import { LockToggle } from "../ui/lockToggle.js"; import { LockToggle } from "../ui/lockToggle.js";
import { ZoneSelector } from "./zoneSelector.js"; import { ZoneSelector } from "./zoneSelector.js";
import type { Boss } from "@elysium/types"; import type { Boss, GameState } from "@elysium/types";
interface BossCardProperties { interface BossCardProperties {
readonly boss: Boss; readonly boss: Boss;
@@ -158,6 +157,72 @@ const BossCard = ({
); );
}; };
/**
* Computes party DPS and HP from the current game state.
* @param state - The full game state.
* @returns The computed party DPS and HP values.
*/
const computePartyStats = (
state: GameState,
): {
partyDps: number;
partyHp: number;
} => {
const { upgrades, adventurers, equipment, prestige } = state;
let globalMultiplier = 1;
for (const upgrade of upgrades) {
const { purchased, target, multiplier } = upgrade;
if (purchased && target === "global") {
globalMultiplier = globalMultiplier * multiplier;
}
}
const prestigeBonus = prestige.count * 0.1;
const prestigeMultiplier = 1 + prestigeBonus;
const equipmentCombatMultiplier = equipment.
filter((item) => {
return item.equipped && item.bonus.combatMultiplier !== undefined;
}).
reduce((multiplier, item) => {
return multiplier * (item.bonus.combatMultiplier ?? 1);
}, 1);
let partyDps = 0;
let partyHp = 0;
for (const adventurer of adventurers) {
const { count, id: adventurerId, combatPower, level } = adventurer;
if (count === 0) {
continue;
}
let adventurerMultiplier = 1;
for (const upgrade of upgrades) {
const {
purchased,
target,
multiplier,
adventurerId: upgradeAdventurerId,
} = upgrade;
if (
purchased
&& target === "adventurer"
&& upgradeAdventurerId === adventurerId
) {
adventurerMultiplier = adventurerMultiplier * multiplier;
}
}
const dps
= combatPower
* count
* adventurerMultiplier
* globalMultiplier
* prestigeMultiplier;
partyDps = partyDps + dps;
const hp = level * 50 * count;
partyHp = partyHp + hp;
}
partyDps = partyDps * equipmentCombatMultiplier;
return { partyDps, partyHp };
};
/** /**
* Renders the boss panel with zone selection and boss list. * Renders the boss panel with zone selection and boss list.
* @returns The JSX element. * @returns The JSX element.
@@ -201,14 +266,7 @@ const BossPanel = (): JSX.Element => {
void handleChallenge(bossId); void handleChallenge(bossId);
} }
const { const { zones, bosses, quests, autoBoss, prestige: playerPrestige } = state;
adventurers,
autoBoss,
bosses,
prestige: playerPrestige,
quests,
zones,
} = state;
const activeZone = zones.find((zone) => { const activeZone = zones.find((zone) => {
return zone.id === activeZoneId; return zone.id === activeZoneId;
@@ -291,12 +349,7 @@ const BossPanel = (): JSX.Element => {
} }
const autoBossOn = autoBoss === true; const autoBossOn = autoBoss === true;
const partyDps = computePartyCombatPower(state); const { partyDps, partyHp } = computePartyStats(state);
let partyHp = 0;
for (const { level, count } of adventurers) {
// eslint-disable-next-line stylistic/no-mixed-operators -- level * 50 * count is clear
partyHp = partyHp + level * 50 * count;
}
const { count: prestigeCount } = playerPrestige; const { count: prestigeCount } = playerPrestige;
return ( return (
@@ -7,8 +7,6 @@
/* eslint-disable react/no-multi-comp -- Sub-component is tightly coupled to the panel */ /* eslint-disable react/no-multi-comp -- Sub-component is tightly coupled to the panel */
/* eslint-disable max-lines-per-function -- Complex component with many render paths */ /* eslint-disable max-lines-per-function -- Complex component with many render paths */
/* eslint-disable complexity -- UpgradeCard has many conditional render paths for states */ /* eslint-disable complexity -- UpgradeCard has many conditional render paths for states */
/* eslint-disable max-statements -- UpgradePanel builds hints from three sources */
/* eslint-disable max-lines -- Upgrade panel with sub-component exceeds line limit */
import { type JSX, useState } from "react"; import { type JSX, useState } from "react";
import { useGame } from "../../context/gameContext.js"; import { useGame } from "../../context/gameContext.js";
import { cdnImage } from "../../utils/cdn.js"; import { cdnImage } from "../../utils/cdn.js";
@@ -240,22 +238,6 @@ const UpgradePanel = (): JSX.Element => {
} }
} }
} }
for (const upgrade of locked) {
if (
!upgradeUnlockHints.has(upgrade.id)
&& upgrade.adventurerId !== undefined
) {
const adventurerForHint = adventurers.find((a) => {
return a.id === upgrade.adventurerId;
});
if (adventurerForHint !== undefined) {
upgradeUnlockHints.set(
upgrade.id,
`🗡️ Recruit: ${adventurerForHint.name}`,
);
}
}
}
function handleToggle(): void { function handleToggle(): void {
setShowLocked((current) => { setShowLocked((current) => {
+5 -16
View File
@@ -10,12 +10,7 @@
/* eslint-disable complexity -- Many conditional resource and badge render paths */ /* eslint-disable complexity -- Many conditional resource and badge render paths */
import { useState, type FocusEvent, type JSX } from "react"; import { useState, type FocusEvent, type JSX } from "react";
import { useGame } from "../../context/gameContext.js"; import { useGame } from "../../context/gameContext.js";
import { import { RESOURCE_CAP, computeGoldPerSecond } from "../../engine/tick.js";
RESOURCE_CAP,
computeEssencePerSecond,
computeGoldPerSecond,
computePartyCombatPower,
} from "../../engine/tick.js";
import type { Resource } from "@elysium/types"; import type { Resource } from "@elysium/types";
interface ResourceBarProperties { interface ResourceBarProperties {
@@ -88,11 +83,12 @@ const ResourceBar = ({
const { gold, essence, crystals } = resources; const { gold, essence, crystals } = resources;
let partyCombatPower = 0; let partyCombatPower = 0;
let goldPerSecond = 0; let goldPerSecond = 0;
let essencePerSecond = 0;
if (state !== null) { if (state !== null) {
partyCombatPower = computePartyCombatPower(state); for (const adventurer of state.adventurers) {
const contribution = adventurer.combatPower * adventurer.count;
partyCombatPower = partyCombatPower + contribution;
}
goldPerSecond = computeGoldPerSecond(state); goldPerSecond = computeGoldPerSecond(state);
essencePerSecond = computeEssencePerSecond(state);
} }
let avatarUrl: string | null = null; let avatarUrl: string | null = null;
@@ -186,13 +182,6 @@ const ResourceBar = ({
</span> </span>
<span className="resource-label">{"Gold/s"}</span> <span className="resource-label">{"Gold/s"}</span>
</div> </div>
<div className="resource">
<span className="resource-icon">{"⚡"}</span>
<span className="resource-value">
{formatNumber(essencePerSecond)}
</span>
<span className="resource-label">{"Essence/s"}</span>
</div>
<div className={`resource${essenceFull <div className={`resource${essenceFull
? " resource-full" ? " resource-full"
: ""}`}> : ""}`}>
+8 -29
View File
@@ -58,7 +58,6 @@ import {
RESOURCE_CAP, RESOURCE_CAP,
applyTick, applyTick,
calculateClickPower, calculateClickPower,
computePartyCombatPower,
} from "../engine/tick.js"; } from "../engine/tick.js";
import { updateChallengeProgress } from "../utils/dailyChallenges.js"; import { updateChallengeProgress } from "../utils/dailyChallenges.js";
import { formatNumber as formatNumberUtil } from "../utils/format.js"; import { formatNumber as formatNumberUtil } from "../utils/format.js";
@@ -738,7 +737,7 @@ export const GameProvider = ({
setSchemaOutdated(data.schemaOutdated); setSchemaOutdated(data.schemaOutdated);
setSaveSchemaVersion(data.state.schemaVersion ?? 0); setSaveSchemaVersion(data.state.schemaVersion ?? 0);
setCurrentSchemaVersion(data.currentSchemaVersion); setCurrentSchemaVersion(data.currentSchemaVersion);
setInGuild(data.inGuild); setInGuild(data.inGuild === true);
// Fetch number format preference from profile (fire-and-forget, non-blocking) // Fetch number format preference from profile (fire-and-forget, non-blocking)
void fetch(`/api/profile/${data.state.player.discordId}`). void fetch(`/api/profile/${data.state.player.discordId}`).
@@ -1079,7 +1078,11 @@ export const GameProvider = ({
return q.status === "active"; return q.status === "active";
}); });
if (!hasActiveQuest) { if (!hasActiveQuest) {
const partyCombatPower = computePartyCombatPower(next); // eslint-disable-next-line unicorn/no-array-reduce -- Need the total!
const partyCombatPower = next.adventurers.reduce((total, a) => {
const power = total + a.combatPower;
return power * a.count;
}, 0);
const zoneOrder = new Map( const zoneOrder = new Map(
next.zones.map((z, index) => { next.zones.map((z, index) => {
return [ z.id, index ]; return [ z.id, index ];
@@ -1117,31 +1120,14 @@ export const GameProvider = ({
next.autoAdventurer === true next.autoAdventurer === true
&& next.prestige.purchasedUpgradeIds.includes("auto_adventurer") && next.prestige.purchasedUpgradeIds.includes("auto_adventurer")
) { ) {
const maxAdventurerLevel = Math.max(
...next.adventurers.
filter((a) => {
return a.unlocked;
}).
map((a) => {
return a.level;
}),
);
const autoBuyCap = 100;
const [ bestAdventurer ] = next.adventurers. const [ bestAdventurer ] = next.adventurers.
filter((adventurer) => { filter((adventurer) => {
const cost const cost
= adventurer.baseCost * Math.pow(1.15, adventurer.count); = adventurer.baseCost * Math.pow(1.15, adventurer.count);
const isMaxTier = adventurer.level === maxAdventurerLevel; return adventurer.unlocked && next.resources.gold >= cost;
const withinCap
= isMaxTier || adventurer.count < autoBuyCap;
return (
adventurer.unlocked
&& next.resources.gold >= cost
&& withinCap
);
}). }).
sort((adventurerA, adventurerB) => { sort((adventurerA, adventurerB) => {
return adventurerB.level - adventurerA.level; return adventurerB.combatPower - adventurerA.combatPower;
}); });
if (bestAdventurer !== undefined) { if (bestAdventurer !== undefined) {
const purchaseCost const purchaseCost
@@ -1360,13 +1346,6 @@ export const GameProvider = ({
} }
return afterBoss; return afterBoss;
}); });
/*
* Boss fight modifies server state; clear stale signature so
* the next pre-save or auto-save does not send a mismatched one.
*/
signatureReference.current = null;
localStorage.removeItem("elysium_save_signature");
setAutoBossLastResult({ setAutoBossLastResult({
at: Date.now(), at: Date.now(),
bossName: bossName, bossName: bossName,
-145
View File
@@ -195,138 +195,6 @@ export const computeGoldPerSecond = (state: GameState): number => {
return goldPerSecond; return goldPerSecond;
}; };
/**
* Computes the current essence per second for the given game state,
* applying all relevant multipliers (upgrades, prestige, echo, crafted, companion).
* @param state - The current game state.
* @returns The total essence per second.
*/
export const computeEssencePerSecond = (state: GameState): number => {
const runestonesEssence = state.prestige.runestonesEssenceMultiplier ?? 1;
const craftedEssenceMultiplier
= state.exploration?.craftedEssenceMultiplier ?? 1;
const companionBonus = getActiveCompanionBonus(
state.companions?.activeCompanionId,
state.companions?.unlockedCompanionIds ?? [],
);
const companionEssenceMult
= companionBonus?.type === "essenceIncome"
? 1 + companionBonus.value
: 1;
let essencePerSecond = 0;
for (const adventurer of state.adventurers) {
if (!adventurer.unlocked || adventurer.count === 0) {
continue;
}
const upgradeMultiplier = state.upgrades.
filter((upgrade) => {
const isGlobal = upgrade.target === "global";
const isThisAdventurer
= upgrade.target === "adventurer"
&& upgrade.adventurerId === adventurer.id;
return upgrade.purchased && (isGlobal || isThisAdventurer);
}).
reduce((mult, upgrade) => {
return mult * upgrade.multiplier;
}, 1);
const contribution
= adventurer.essencePerSecond
* adventurer.count
* upgradeMultiplier
* state.prestige.productionMultiplier
* runestonesEssence
* craftedEssenceMultiplier
* companionEssenceMult;
essencePerSecond = essencePerSecond + contribution;
}
return essencePerSecond;
};
/**
* Computes the party's total combat power, applying all active multipliers
* (upgrades, prestige, equipment, set bonuses, echo, crafted, companion).
* This mirrors the server-side calculatePartyStats in boss.ts.
* @param state - The current game state.
* @returns The total party combat power.
*/
export const computePartyCombatPower = (state: GameState): number => {
let globalMultiplier = 1;
for (const upgrade of state.upgrades) {
if (upgrade.purchased && upgrade.target === "global") {
globalMultiplier = globalMultiplier * upgrade.multiplier;
}
}
// eslint-disable-next-line stylistic/no-mixed-operators -- prestige count * factor is clear
const prestigeMultiplier = 1 + state.prestige.count * 0.1;
const equipmentCombatMultiplier = state.equipment.
filter((item) => {
return item.equipped && item.bonus.combatMultiplier !== undefined;
}).
reduce((mult, item) => {
return mult * (item.bonus.combatMultiplier ?? 1);
}, 1);
const equippedItemIds = state.equipment.
filter((item) => {
return item.equipped;
}).
map((item) => {
return item.id;
});
const { combatMultiplier: setCombatMultiplier } = computeSetBonuses(
equippedItemIds,
EQUIPMENT_SETS,
);
const echoCombatMultiplier
= state.transcendence?.echoCombatMultiplier ?? 1;
const craftedCombatMultiplier
= state.exploration?.craftedCombatMultiplier ?? 1;
const companionBonus = getActiveCompanionBonus(
state.companions?.activeCompanionId,
state.companions?.unlockedCompanionIds ?? [],
);
const companionCombatMult
= companionBonus?.type === "bossDamage"
? 1 + companionBonus.value
: 1;
let partyCombatPower = 0;
for (const adventurer of state.adventurers) {
if (adventurer.count === 0) {
continue;
}
let adventurerMultiplier = 1;
for (const upgrade of state.upgrades) {
if (
upgrade.purchased
&& upgrade.target === "adventurer"
&& upgrade.adventurerId === adventurer.id
) {
adventurerMultiplier = adventurerMultiplier * upgrade.multiplier;
}
}
const contribution
= adventurer.combatPower
* adventurer.count
* adventurerMultiplier
* globalMultiplier
* prestigeMultiplier;
partyCombatPower = partyCombatPower + contribution;
}
return partyCombatPower
* equipmentCombatMultiplier
* setCombatMultiplier
* echoCombatMultiplier
* craftedCombatMultiplier
* companionCombatMult;
};
/** /**
* Pure function — applies one game tick to the state. * Pure function — applies one game tick to the state.
* DeltaSeconds: time elapsed since last tick. * DeltaSeconds: time elapsed since last tick.
@@ -601,19 +469,6 @@ export const applyTick = (
challengeCrystals = result.crystalsAwarded; challengeCrystals = result.crystalsAwarded;
} }
// Auto-unlock adventurer-specific upgrades when their adventurer is recruited
updatedUpgrades = updatedUpgrades.map((upgrade) => {
if (upgrade.unlocked || upgrade.adventurerId === undefined) {
return upgrade;
}
const adventurer = updatedAdventurers.find((a) => {
return a.id === upgrade.adventurerId;
});
return adventurer !== undefined && adventurer.count > 0
? { ...upgrade, unlocked: true }
: upgrade;
});
const goldValue = capResource(state.resources.gold + goldGained + questGold); const goldValue = capResource(state.resources.gold + goldGained + questGold);
const essenceValue = capResource( const essenceValue = capResource(
state.resources.essence + essenceGained + questEssence, state.resources.essence + essenceGained + questEssence,
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "elysium", "name": "elysium",
"version": "0.3.2", "version": "0.3.1",
"private": true, "private": true,
"type": "module", "type": "module",
"scripts": { "scripts": {
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@elysium/types", "name": "@elysium/types",
"version": "0.3.2", "version": "0.3.1",
"private": true, "private": true,
"type": "module", "type": "module",
"main": "./prod/src/index.js", "main": "./prod/src/index.js",