Skip to content

Commit 275c82f

Browse files
committed
Async await
1 parent 3d50a58 commit 275c82f

File tree

1 file changed

+44
-52
lines changed

1 file changed

+44
-52
lines changed

jam-25/src/routes/App.jsx

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ const App = () => {
9494
const turnScores = useRef([]);
9595
const [totalScore, setTotalScore] = useState(0);
9696
const [turnScore, setTurnScore] = useState(null);
97-
const scoreTimeouts = useRef([]);
98-
const scoreCount = useRef(0);
9997
const [round, setRound] = useState(0);
10098
const [roundOver, setRoundOver] = useState(false);
10199
const [gameOver, setGameOver] = useState(false);
@@ -107,10 +105,10 @@ const App = () => {
107105
const [inventory, setInventory] = useState([]);
108106
const [inventoryItems, setInventoryItems] = useState([]);
109107
const [possibleLetters, setPossibleLetters] = useState([]);
110-
const [jokers, setJokers] = useState([]); // <Joker joker={JOKERS[4]} id={JOKERS[4].id} />,<Joker joker={JOKERS[7]} id={JOKERS[7].id} />
108+
const [jokers, setJokers] = useState([]); // <Joker joker={JOKERS[15]} id={JOKERS[15].id} /><Joker joker={JOKERS[4]} id={JOKERS[4].id} />,<Joker joker={JOKERS[7]} id={JOKERS[7].id} />
111109
const [maxJokers, setMaxJokers] = useState(5);
112110

113-
const scoreWord = useCallback((word, scoreRound) => {
111+
const scoreWord = useCallback(async (word, scoreRound) => {
114112
let baseScore = word.score || word.word.length;
115113
const bonuses = bonusSpacesRef.current.map((s) => ({ id: s.id, ...BONUSES[s.bonus] }));
116114
// Tile bonuses first
@@ -139,44 +137,44 @@ const App = () => {
139137
baseScore *= b.multiplier === 0 ? 1 : b.multiplier;
140138
}
141139
});
142-
scoreTimeouts.current.push(setTimeout(() => {
140+
return new Promise((resolve) => setTimeout(async () => {
143141
setScoringTiles((old) => [...old, { id: word.tiles[0]?.props?.id, score: word?.valid ? baseScore : -baseScore, scoreRound, placement: word.orientation === 'horizontal' ? 'left' : 'top' }]);
144-
setTurnScore((old) => (old ?? 0) + (word?.valid ? baseScore : -baseScore));
145142
setTimeout(() => {
146143
setScoringTiles((old) => old.filter((t) => t.id !== word.tiles[0]?.props?.id || t.placement !== (word.orientation === 'horizontal' ? 'left' : 'top') || t.scoreRound !== scoreRound));
147144
}, 1000);
148-
}, (scoreCount.current++) * 500));
149-
return baseScore;
145+
await new Promise((resolve) => setTimeout(() => { setTurnScore((old) => (old ?? 0) + (word?.valid ? baseScore : -baseScore)); resolve(); }, Math.max(500 - (scoreRound * 200), 100)));
146+
resolve(baseScore);
147+
}, Math.max(500 - (scoreRound * 200), 200)));
150148
}, [gridArray, setScoringTiles]);
151149

152150
const getGlobalJokers = useCallback(() => {
153151
return jokers.filter((j) => j.props?.joker?.global);
154152
}, [jokers]);
155153

156-
const score = useCallback((newWords, scoreRound = 0) => {
157-
const validScore = newWords.filter((w) => w.valid).reduce((acc, w) => acc + scoreWord(w, scoreRound), 0);
158-
const invalidScore = newWords.filter((w) => !w.valid).reduce((acc, w) => acc + scoreWord(w, scoreRound), 0);
154+
const score = useCallback(async (newWords, scoreRound = 0) => {
155+
const validScore = await newWords.filter((w) => w.valid).reduce(async (promise, w) => promise.then(async (last) => last + await scoreWord(w, scoreRound)), Promise.resolve(0));
156+
const invalidScore = await newWords.filter((w) => !w.valid).reduce(async (promise, w) => promise.then(async (last) => last + await scoreWord(w, scoreRound)), Promise.resolve(0));
159157
let currentScore = validScore - invalidScore;
160-
jokers.filter((j) => j.action).forEach((j) => {
158+
await Promise.all(jokers.filter((j) => j.action).map(async (j) => {
161159
const { newScore, newMoney, delta } = j.props?.joker?.action?.({ words: [...words, newWords], grid: gridArray, totalScore: currentScore, validScore, invalidScore, target, funds }) ?? { newScore: currentScore, newMoney: 0, delta: 0 };
162-
scoreTimeouts.current.push(setTimeout(() => {
163-
setScoringTiles((old) => [...old, { id: j.props?.id, score: delta, placement: 'bottom', newMoney: newMoney ?? 0, scoreRound }]);
164-
setTurnScore((old) => (old ?? 0) + delta);
165-
setTimeout(() => {
166-
setScoringTiles((old) => old.filter((t) => t.id !== j.props?.id || t.scoreRound !== scoreRound));
167-
}, 1000);
168-
}, (scoreCount.current++) * 500));
169-
currentScore = newScore;
170-
setFunds((old) => old + (newMoney ?? 0));
171-
});
160+
await new Promise((resolve) => setTimeout(() => {
161+
setScoringTiles((old) => [...old, { id: j.props?.id, score: delta, placement: 'bottom', newMoney: newMoney ?? 0, scoreRound }]);
162+
setTurnScore((old) => (old ?? 0) + delta);
163+
setFunds((old) => old + (newMoney ?? 0));
164+
setTimeout(() => {
165+
setScoringTiles((old) => old.filter((t) => t.id !== j.props?.id || t.scoreRound !== scoreRound));
166+
}, 1000);
167+
currentScore = newScore;
168+
resolve();
169+
}, Math.max(500 - (scoreRound * 200), 200))
170+
);
171+
}));
172172
turnScores.current[currentTurnRef.current] = currentScore;
173173
return currentScore;
174174
}, [funds, gridArray, jokers, scoreWord, setScoringTiles, target, words]);
175175

176176
const start = useCallback(() => {
177177
setRetrieving([]);
178-
scoreTimeouts.current = [];
179-
scoreCount.current = 0;
180178
tilesOnBoard.current = [];
181179
initialPositions.current = {};
182180
const ttd = tilesToDrawRef.current + (getGlobalJokers().reduce((acc, j) => acc + (j.props?.joker?.global?.draws ?? 0), 0));
@@ -274,31 +272,30 @@ const App = () => {
274272
return newFoundWords;
275273
}, [blanks, fixedTiles, gridArray]);
276274

277-
const endTurn = useCallback(() => {
275+
const endTurn = useCallback(async () => {
278276
setTurnOver(true);
279277
const newWords = checkForWords();
280-
let newTurnScore = score(newWords);
278+
let newTurnScore = await score(newWords);
281279
const thisTurn = currentTurnRef.current;
282-
let numJokers = 0;
283280
if (thisTurn === turns - 1) {
284-
numJokers = getGlobalJokers()?.filter((j) => j.props?.joker?.global?.rescore).length;
285-
getGlobalJokers()?.filter((j) => j.props?.joker?.global?.rescore).forEach((j, i) => {
286-
scoreTimeouts.current.push(setTimeout(() => {
281+
await Promise.all(getGlobalJokers()?.filter((j) => j.props?.joker?.global?.rescore).map(async (j, i) => {
282+
await new Promise((resolve) => setTimeout(async () => {
287283
setScoringTiles((old) => [...old, { id: j.props?.id, placement: 'bottom', scoreRound: i, text: 'Again!' }]);
288284
setTimeout(() => {
289285
setScoringTiles((old) => old.filter((t) => t.id !== j.props?.id || t.scoreRound !== i));
290286
}, 1000);
291-
newTurnScore += score(newWords, i + 1);
292-
}, (scoreCount.current++) * 500));
293-
});
287+
newTurnScore += await score(newWords, i + 1);
288+
resolve();
289+
}, 500));
290+
}));
294291
}
295292
const newTotalScore = totalScore + newTurnScore;
296-
const getNewTotalScore = () => totalScore + newTurnScore;
297293
setWords((old) => { old[thisTurn] = newWords; return old; })
298-
scoreTimeouts.current.push(setTimeout(() => {
299-
setTotalScore(getNewTotalScore());
294+
await new Promise((resolve) => setTimeout(() => {
295+
setTotalScore(newTotalScore);
300296
setTurnScore(null);
301-
}, (((scoreCount.current * 500) + (1000 * (numJokers + 1))))));
297+
resolve();
298+
}, 1500));
302299
const newFixedTiles = [];
303300
gridArray.forEach((row) => row.forEach((col) => {
304301
if (col.tile) {
@@ -310,7 +307,7 @@ const App = () => {
310307

311308
currentTurnRef.current++;
312309
if (newTotalScore < target && currentTurnRef.current < turns) {
313-
scoreTimeouts.current.push(setTimeout(() => {
310+
await new Promise((resolve) => setTimeout(() => {
314311
setCurrentTurn((old) => old + 1);
315312
const ttd = (tilesToDrawRef.current + getGlobalJokers().reduce((acc, j) => acc + (j.props?.joker?.global?.draws ?? 0), 0)) - trayArray.length - swapArray.length;
316313
const drawn = allTiles.slice(0, ttd);
@@ -322,8 +319,6 @@ const App = () => {
322319
setBagTiles(drawn.map((t) => t.props.id));
323320
setDealing(true);
324321
setSwapArray([]);
325-
scoreTimeouts.current = [];
326-
scoreCount.current = 0;
327322
bonusSpacesRef.current.forEach((b) => {
328323
const [row, col] = b.id.split(',').map((n) => parseInt(n));
329324
setGridArray((old) => old.map((r, i) => r.map((c, j) => {
@@ -333,30 +328,29 @@ const App = () => {
333328
return c;
334329
})));
335330
});
336-
}, ((scoreCount.current * 500) + (1000 * (numJokers + 1)) + 500)));
331+
resolve();
332+
}, 800));
337333
} else {
338334
tilesOnBoard.current.push(...trayArray.map((t) => t.props.id));
339-
scoreTimeouts.current.push(setTimeout(() => {
335+
await new Promise((resolve) => setTimeout(() => {
340336
setRetrieving(tilesOnBoard.current);
341337
setGlobalRoundOver(true);
342-
}, ((scoreCount.current * 500) + (1000 * (numJokers + 1)) + 300)));
338+
resolve();
339+
}, 300));
343340
// Round over
344-
scoreTimeouts.current.push(setTimeout(() => {
345-
if (getNewTotalScore() >= target) {
341+
await new Promise((resolve) => setTimeout(() => {
342+
if (newTotalScore >= target) {
346343
setFixedTiles([]);
347344
setBlanks({});
348345
setAllTiles(tileLibrary.current);
349346
setRoundOver(true);
350347
setGlobalRoundOver(false);
351-
scoreTimeouts.current = [];
352-
scoreCount.current = 0;
353348
} else {
354349
setGameOver(true);
355350
setGlobalRoundOver(false);
356-
scoreTimeouts.current = [];
357-
scoreCount.current = 0;
358351
}
359-
}, ((scoreCount.current * 500) + (1000 * (numJokers + 1)) + 1000)));
352+
resolve();
353+
}, 800));
360354

361355
}
362356
}, [allTiles, checkForWords, getGlobalJokers, gridArray, score, setBagTiles, setBlanks, setDealing, setFixedTiles, setGlobalRoundOver, setRetrieving, setScoringTiles, setTurnOver, swapArray, target, totalScore, trayArray, turns]);
@@ -600,8 +594,6 @@ const App = () => {
600594
}, [gridSizeY, gridSizeX, handleNextRound, inventory.length]);
601595

602596
const handleGameOver = useCallback(() => {
603-
scoreTimeouts.current = [];
604-
scoreCount.current = 0;
605597
const newGridSize = 7;
606598
setGridSizeY(newGridSize);
607599
setGridSizeY(newGridSize);

0 commit comments

Comments
 (0)