Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2.32.1 #371

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 37 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "noitool",
"version": "2.32.0",
"version": "2.32.1",
"private": true,
"type": "module",
"homepage": "https://www.noitool.com/",
Expand Down Expand Up @@ -86,6 +86,7 @@
"lua2js": "^0.0.11",
"luaparse": "^0.3.1",
"nodemon": "^3.1.1",
"p-limit": "^5.0.0",
"pixelmatch": "^5.3.0",
"prettier": "^3.2.5",
"react": "^18.3.1",
Expand Down
176 changes: 77 additions & 99 deletions src/components/SeedInfo/SeedDataOutput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, useEffect, useState } from "react";
import React, { createContext, useEffect, useState, useCallback } from "react";
import { Stack } from "react-bootstrap";

import type { GameInfoProvider } from "../../services/SeedInfo/infoHandler";
Expand All @@ -7,134 +7,112 @@ import SeedInfo from "./SeedInfo";
import i18n from "../../i18n";
import { db } from "../../services/db";
import useLocalStorage from "../../services/useLocalStorage";
import { NOITA_SPELL_COUNT } from "../../static.ts";
import { NOITA_SPELL_COUNT } from "../../static";

interface ISeedDataProps {
isDaily?: boolean;
seed: string;
}

const waitToLoad = (gameInfoProvider?: GameInfoProvider): Promise<void> =>
new Promise(async res => {
if (!gameInfoProvider) {
return res();
}
await gameInfoProvider.ready();
return res();
});
interface GameInfoContextType {
gameInfoProvider: GameInfoProvider;
data: Awaited<ReturnType<GameInfoProvider["provideAll"]>>;
}

const importMain = async () => {
const GameInfoProvider = (await import("../../services/SeedInfo/infoHandler/index.ts")).default;
return GameInfoProvider;
const waitToLoad = async (gameInfoProvider?: GameInfoProvider): Promise<void> => {
if (gameInfoProvider) {
await gameInfoProvider.ready();
}
};

// Maybe something like this in the future?
// const importBeta = async () => {
// const GameInfoProvider = (await import("../../services/SeedInfo/infoHandlerBeta/index.ts")).default;
// return GameInfoProvider;
// };
const importGameInfoProvider = async (branch: string) => {
// TODO: handle beta branch
return (await import("../../services/SeedInfo/infoHandler/index.ts")).default;
};

export const createGameInfoProvider = async (branch, seed: string, unlockedSpells: boolean[], setData) => {
const GameInfoProvider = await importMain(); // TODO: handle beta
export const createGameInfoProvider = async (
branch: string,
seed: string,
unlockedSpells: boolean[],
setData: (data: Awaited<ReturnType<GameInfoProvider["provideAll"]>>) => void,
): Promise<GameInfoProvider> => {
const GameInfoProvider = await importGameInfoProvider(branch);
const gameInfoProvider = new GameInfoProvider({ seed: parseInt(seed, 10) }, unlockedSpells, i18n);
gameInfoProvider
.onRandomLoad(() => {
gameInfoProvider.randoms.SetWorldSeed(parseInt(seed, 10));
gameInfoProvider.randoms.SetUnlockedSpells(unlockedSpells);
gameInfoProvider
.provideAll()
.then(data => {
setData(data);
})
.catch(() => {
// handle this?
});
gameInfoProvider.addEventListener("update", async event => {
// Save config for resetting
const config = gameInfoProvider.config;
await db.setSeedInfo("" + config.seed, config);
await gameInfoProvider.provideAll().then(data => {
setData(data);
});
});
gameInfoProvider.addEventListener("reset", event => {
gameInfoProvider
.provideAll()
.then(data => {
setData(data);
})
.catch(e => {
console.error(e);
});
});
})
.catch(e => {
console.error(e);

const handleProvideAll = async () => {
try {
const data = await gameInfoProvider.provideAll();
setData(data);
} catch (error) {
console.error("Error providing data:", error);
}
};

gameInfoProvider.onRandomLoad(() => {
gameInfoProvider.randoms.SetWorldSeed(parseInt(seed, 10));
gameInfoProvider.randoms.SetUnlockedSpells(unlockedSpells);
handleProvideAll();

gameInfoProvider.addEventListener("update", async () => {
const config = gameInfoProvider.config;
await db.setSeedInfo("" + config.seed, config);
await handleProvideAll();
});

gameInfoProvider.addEventListener("reset", handleProvideAll);
});

return gameInfoProvider;
};

export const GameInfoContext = createContext<{
gameInfoProvider: GameInfoProvider;
data: Awaited<ReturnType<GameInfoProvider["provideAll"]>>;
}>({} as any);
// This is a hacky way to get around the default value
// We always set the value in the provider so this should never be used
// TODO: find a better way to handle this
export const GameInfoContext = createContext<GameInfoContextType>(null!);

export const useGameInfoProvider = (
seed: string,
): [GameInfoProvider?, Awaited<ReturnType<GameInfoProvider["provideAll"]>>?] => {
): [GameInfoProvider | undefined, Awaited<ReturnType<GameInfoProvider["provideAll"]>> | undefined] => {
const [data, setData] = useState<Awaited<ReturnType<GameInfoProvider["provideAll"]>>>();
const [unlockedSpells] = useLocalStorage<boolean[]>("unlocked-spells", Array(NOITA_SPELL_COUNT).fill(true));
const [branch] = useLocalStorage<string>("noita-branch", "main");
const [gameInfoProvider, setGameInfoProvider] = useState<GameInfoProvider>();

const [gameInfoProvider, setGameInfoProvider] = useState<GameInfoProvider | undefined>();
const initializeGameInfoProvider = useCallback(async () => {
setData(undefined);
setGameInfoProvider(undefined);
const config = await db.getSeedInfo(seed);
const newGameInfoProvider = await createGameInfoProvider(branch, seed, unlockedSpells, setData);

await waitToLoad(newGameInfoProvider);

newGameInfoProvider.resetConfig({
...config?.config,
seed: parseInt(seed, 10),
});
setGameInfoProvider(newGameInfoProvider);
}, [seed, unlockedSpells, branch]);

useEffect(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async () => {
setData(undefined);
setGameInfoProvider(undefined);
const config = await db.getSeedInfo(seed);
const newGameInfoProvider = await createGameInfoProvider(branch, seed, unlockedSpells, setData);
waitToLoad(newGameInfoProvider)
.then(() => {
newGameInfoProvider.resetConfig({
...config?.config,
seed: parseInt(seed, 10),
});
setGameInfoProvider(newGameInfoProvider);
})
.catch(() => {
// handle this?
});
})();
}, [seed, unlockedSpells]);
initializeGameInfoProvider();
}, [initializeGameInfoProvider]);

return [gameInfoProvider, data];
};

const SeedDataOutput = (props: ISeedDataProps) => {
const { seed, isDaily } = props;
const SeedDataOutput: React.FC<ISeedDataProps> = ({ seed, isDaily = false }) => {
const [gameInfoProvider, data] = useGameInfoProvider(seed);

if (!gameInfoProvider || !data) {
return <p>Loading</p>;
}

return (
<>
{gameInfoProvider && data ? (
<GameInfoContext.Provider value={{ gameInfoProvider, data }}>
<Stack className="seed-info">
{data && (
<p className="my-2">
Seed: {seed} {isDaily && ` (Daily)`}
</p>
)}
{data && <SeedInfo isDaily={isDaily || false} seed={seed} infoProvider={gameInfoProvider} data={data} />}
</Stack>
</GameInfoContext.Provider>
) : (
<p>Loading</p>
)}
</>
<GameInfoContext.Provider value={{ gameInfoProvider, data }}>
<Stack className="seed-info">
<p className="my-2">
Seed: {seed} {isDaily && ` (Daily)`}
</p>
<SeedInfo isDaily={isDaily} seed={seed} infoProvider={gameInfoProvider} data={data} />
</Stack>
</GameInfoContext.Provider>
);
};

Expand Down
16 changes: 4 additions & 12 deletions src/components/SeedInfo/SeedInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,18 @@ import ExcavationsiteCubeChamber from "./SeedInfoViews/ExcavationsiteCubeChamber
import SnowcaveSecretChamber from "./SeedInfoViews/SnowcaveSecretChamber";
import SnowcastleSecretChamber from "./SeedInfoViews/SnowcastleSecretChamber";

const WithShow = (props: { id: string; children: React.ReactNode }): JSX.Element => {
const config = useLiveQuery(() => db.configItems.get({ key: `panel-${props.id}-config` }));
const WithShow = ({ id, children }) => {
const config = useLiveQuery(() => db.configItems.get({ key: `panel-${id}-config` }));
const hasConfig = !!config;

if (hasConfig && !config.val) {
return <div></div>;
}

return <div>{props.children}</div>;
return <div>{children}</div>;
};

interface ISeedInfoProps {
seed: string;
data: Awaited<ReturnType<GameInfoProvider["provideAll"]>>;
infoProvider: GameInfoProvider; // This should be a context in the future
isDaily: boolean;
}

const SeedInfo = (props: ISeedInfoProps) => {
const { data, infoProvider, seed, isDaily } = props;
const SeedInfo = ({ data, infoProvider, seed, isDaily }) => {
const searchParams = new URLSearchParams(document.location.search);
const showMap = !!searchParams.get("map");

Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ class MaterialPicker
"magic_liquid_mana_regeneration", "magic_liquid_movement_faster", "magic_liquid_protection_all",
"magic_liquid_teleportation", "magic_liquid_unstable_polymorph", "magic_liquid_unstable_teleportation",
"magic_liquid_worm_attractor", "material_confusion", "mud",
"oil", "poison", "radioactive_liquid_yellow",
"oil", "poison", "radioactive_liquid",
"swamp", "urine", "water",
"water_ice", "water_swamp", "magic_liquid_random_polymorph"};

vector<string> ALCHEMY = {
"bone_box2d", "brass", "coal",
"bone", "brass", "coal",
"copper", "diamond", "fungi",
"gold", "grass", "gunpowder",
"gunpowder_explosive", "rotten_meat", "sand_petrify",
"silver", "slime", "snow_b2",
"gunpowder_explosive", "rotten_meat", "sand",
"silver", "slime", "snow",
"soil", "wax", "honey"};

NollaPrng *PRNG;
Expand Down
Loading