generated from nhcarrigan/template
feat: add auto-prestige toggle via runestone shop upgrade
Adds a new Utility category to the prestige shop with the "Autonomous Ascension" upgrade (100 runestones). Once purchased, a toggle button appears in the shop that enables automatic prestige — the client RAF loop checks the threshold on each tick and fires the prestige API automatically using the current character name. ✨ This feature was implemented with help from Hikari~ 🌸
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
||||
buyPrestigeUpgrade as buyPrestigeUpgradeApi,
|
||||
challengeBoss as challengeBossApi,
|
||||
loadGame,
|
||||
prestige as prestigeApi,
|
||||
saveGame,
|
||||
} from "../api/client.js";
|
||||
import { RESOURCE_CAP, applyTick, calculateClickPower } from "../engine/tick.js";
|
||||
@@ -73,11 +74,15 @@ interface GameContextValue {
|
||||
formatNumber: (value: number) => string;
|
||||
/** Buy a prestige upgrade from the runestone shop */
|
||||
buyPrestigeUpgrade: (upgradeId: string) => Promise<void>;
|
||||
/** Toggle the auto-prestige setting on/off (requires auto_prestige upgrade) */
|
||||
toggleAutoPrestige: () => void;
|
||||
}
|
||||
|
||||
const GameContext = createContext<GameContextValue | null>(null);
|
||||
|
||||
const AUTO_SAVE_INTERVAL_MS = 30_000;
|
||||
const AUTO_PRESTIGE_THRESHOLD_BASE = 1_000_000;
|
||||
const AUTO_PRESTIGE_THRESHOLD_SCALE = 5;
|
||||
|
||||
export const GameProvider = ({ children }: { children: React.ReactNode }): React.JSX.Element => {
|
||||
const [state, setState] = useState<GameState | null>(null);
|
||||
@@ -98,6 +103,8 @@ export const GameProvider = ({ children }: { children: React.ReactNode }): React
|
||||
const rafRef = useRef<number | null>(null);
|
||||
const newlyUnlockedRef = useRef<Achievement[]>([]);
|
||||
const signatureRef = useRef<string | null>(localStorage.getItem("elysium_save_signature"));
|
||||
const isAutoPrestigingRef = useRef(false);
|
||||
const reloadRef = useRef<() => Promise<void>>(() => Promise.resolve());
|
||||
|
||||
stateRef.current = state;
|
||||
|
||||
@@ -136,6 +143,8 @@ export const GameProvider = ({ children }: { children: React.ReactNode }): React
|
||||
}
|
||||
}, []);
|
||||
|
||||
reloadRef.current = reload;
|
||||
|
||||
useEffect(() => {
|
||||
void reload();
|
||||
}, [reload]);
|
||||
@@ -191,6 +200,23 @@ export const GameProvider = ({ children }: { children: React.ReactNode }): React
|
||||
}
|
||||
}
|
||||
|
||||
// Auto-prestige: fire when unlocked, enabled, and threshold is met
|
||||
const autoState = stateRef.current;
|
||||
if (
|
||||
!isAutoPrestigingRef.current &&
|
||||
autoState?.prestige.purchasedUpgradeIds.includes("auto_prestige") &&
|
||||
autoState.prestige.autoPrestigeEnabled &&
|
||||
autoState.player.totalGoldEarned >=
|
||||
AUTO_PRESTIGE_THRESHOLD_BASE *
|
||||
Math.pow(AUTO_PRESTIGE_THRESHOLD_SCALE, autoState.prestige.count)
|
||||
) {
|
||||
isAutoPrestigingRef.current = true;
|
||||
void prestigeApi({ characterName: autoState.player.characterName })
|
||||
.then(() => reloadRef.current())
|
||||
.catch(() => { /* silently ignore — will retry next tick */ })
|
||||
.finally(() => { isAutoPrestigingRef.current = false; });
|
||||
}
|
||||
|
||||
rafRef.current = requestAnimationFrame(tick);
|
||||
};
|
||||
|
||||
@@ -404,6 +430,19 @@ export const GameProvider = ({ children }: { children: React.ReactNode }): React
|
||||
}
|
||||
}, []);
|
||||
|
||||
const toggleAutoPrestige = useCallback(() => {
|
||||
setState((prev) => {
|
||||
if (!prev) return prev;
|
||||
return {
|
||||
...prev,
|
||||
prestige: {
|
||||
...prev.prestige,
|
||||
autoPrestigeEnabled: !prev.prestige.autoPrestigeEnabled,
|
||||
},
|
||||
};
|
||||
});
|
||||
}, []);
|
||||
|
||||
const challengeBoss = useCallback(async (bossId: string) => {
|
||||
if (!stateRef.current) return;
|
||||
const boss = stateRef.current.bosses.find((b) => b.id === bossId);
|
||||
@@ -566,6 +605,7 @@ export const GameProvider = ({ children }: { children: React.ReactNode }): React
|
||||
setNumberFormat,
|
||||
formatNumber: boundFormatNumber,
|
||||
buyPrestigeUpgrade,
|
||||
toggleAutoPrestige,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
|
||||
Reference in New Issue
Block a user