ED3IWKTSVYYD52TWW5TOC4H63YZ7FTO3E53YO5NELPG5I3RLRAYAC
CWMLLMPJKUMHWSQFRQJETMY7STI73TK4NJA4ZZ7YQDIBP6M5OFLQC
6EXHALB3E5HP3IQVC47M4RVZE2JERJHX37GOOHPYIVAFBWSWUX7QC
DPJCZOPIKAKQIS4YRXETBSJA5ZTL2KQESSJE2TEZFLRQARXUVFXQC
HXHNGFB2VCXB6YXDND35HJI22GBJC3QTSUR2TK3M3LXGJHVNXVHAC
Y7VEWZEPUKX2WWPTQCUSIF7YMRUC6JZQNUU5OX3HQDB4GSDSTQZAC
OPUQ6NVYM2BN4H2FENH6EBGVU25XQBY2GRRZANPZEBEACZMDOGEAC
4CBMNZWFOZRTXXNX2GWVFHVYXZGCFCJEJJZGM6O73LLW5A7RZKWAC
DNNKTYMMT2SXGZDMTFYZOQPZ3IX5EF6ESQAMGO5UUDFUA44FXMGAC
TQ57VE45BHV7MOZ6GKTYZEAMAOTXLPQ3ROCWJ2FUCITQWOYVMUIAC
const updateGameState = useCallback(
(updater: (oldGameState: GameState) => GameState) => {
setGame((oldGameState) => {
let newGameState = updater(oldGameState);
return { ...newGameState };
});
}, [setGame]);
// updateGameState(oldGame => { oldGame.s = y; return oldGame } )
const updateSelectedPointNode = (updater: (old: PointNodeRef | undefined, oldState: GameState) => PointNodeRef) => {
updateGameState(oldGameState => {
oldGameState.playerUI.selectedPointNode = updater(oldGameState.playerUI.selectedPointNode, oldGameState);
return oldGameState;
})
};
const setSelectedPointNode = (newSelectedPointNode: PointNodeRef) => updateSelectedPointNode(() => newSelectedPointNode);
useEffect(() => {
console.log("game updated:");
console.log(game);
}, [game]);
const setSelectedPointNode = (newSelectedPointNode: PointNodeRef) =>
updateSelectedPointNode(() => newSelectedPointNode);
// TODO
// let updaters = doMagic();
// updateSelectedPointNode = updaters.playerUI.selectedPointNode.getUpdater();
// setSelectedPointNode = updaters.playerUI.selectedPointNode.getSetter();
// updateSelectedPointNode = updaters.playerUI.fn()
let updaters = updaterGenerator(game, setGame);
const updateSelectedPointNode = updaters.playerUI.selectedPointNode.getUpdater();
export function updaterGenerator(dataObject: any, dataUpdater: any): any {
const updaters: any = {};
updaters.getUpdater = () => dataUpdater;
if (typeof dataObject !== "object") return updaters;
const keys = Object.keys(dataObject);
keys.forEach((key) => {
function keyUpdater(newValueOrCallback) {
// console.log(newValueOrCallback);
if (typeof newValueOrCallback === "function") {
dataUpdater((oldData) => {
const newData = {
...oldData,
[key]: newValueOrCallback(oldData[key]),
};
// console.log({ newData });
return newData;
});
} else {
dataUpdater((oldData) => ({ ...oldData, [key]: newValueOrCallback }));
}
}
updaters[key] = updaterGenerator(dataObject[key], keyUpdater);
// updaters[key].getUpdater = () => keyUpdater;
});
return updaters;
}