-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️: Refactor the 'high-score' feature into a react context.
- Loading branch information
Showing
11 changed files
with
122 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { createContext, useRef } from "react"; | ||
import { HighScore } from "./high-score"; | ||
import { saveObject } from "@drk4/utilities"; | ||
import { getData } from "../../core/data"; | ||
import { ScoreData } from "./high-score.types"; | ||
|
||
export type HighScoreContextValue = { | ||
getScores: () => ScoreData[]; | ||
addScore: (score: ScoreData) => number | undefined; | ||
}; | ||
|
||
interface HighScoreContextProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
const STORAGE_KEY = "tetris_high_score"; | ||
|
||
export const HighScoreContext = createContext<HighScoreContextValue>({ | ||
getScores: () => [], | ||
addScore: () => { | ||
return undefined; | ||
}, | ||
}); | ||
|
||
export function HighScoreContextProvider({ | ||
children, | ||
}: HighScoreContextProviderProps) { | ||
const highScore = useRef<HighScore>(new HighScore(getData(STORAGE_KEY))); | ||
|
||
const value = { | ||
addScore: (score: ScoreData) => { | ||
const position = highScore.current.add(score); | ||
saveObject(STORAGE_KEY, highScore.current.getHighScores()); | ||
|
||
return position; | ||
}, | ||
getScores: () => highScore.current.getHighScores(), | ||
}; | ||
|
||
return ( | ||
<HighScoreContext.Provider value={value}> | ||
{children} | ||
</HighScoreContext.Provider> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,60 @@ | ||
import { getData, setData } from "../../core/data"; | ||
|
||
export interface ScoreData { | ||
score: number; | ||
linesCleared: number; | ||
time: number; | ||
} | ||
|
||
let SCORES: ScoreData[] = []; // sorted by the 'score' property | ||
const MAX_SCORES = 10; | ||
|
||
/** | ||
* Load the given high-scores. | ||
*/ | ||
export function load() { | ||
const scores = getData("tetris_high_score"); | ||
|
||
if (scores) { | ||
SCORES = scores; | ||
import { ScoreData } from "./high-score.types"; | ||
|
||
export class HighScore { | ||
private scores: ScoreData[] = []; // sorted by the 'score' property | ||
private maxScores = 10; | ||
|
||
/** | ||
* Load the given high-scores. | ||
*/ | ||
constructor(scores: ScoreData[] | undefined | null) { | ||
if (scores) { | ||
this.scores = scores; | ||
} | ||
} | ||
|
||
return SCORES; | ||
} | ||
|
||
/** | ||
* Try to add a score to the high-score list. Its only added if there's an empty spot or if the new score is higher than existing ones. | ||
* If a score was actually added, it returns the position in the high-score list. | ||
*/ | ||
export function add(score: ScoreData) { | ||
let added = false; | ||
/** | ||
* Try to add a score to the high-score list. Its only added if there's an empty spot or if the new score is higher than existing ones. | ||
* If a score was actually added, it returns the position in the high-score list. | ||
*/ | ||
add(score: ScoreData) { | ||
let added = false; | ||
|
||
// haven't reached the limit yet, so just add the score | ||
if (SCORES.length < MAX_SCORES) { | ||
SCORES.push(score); | ||
added = true; | ||
} | ||
// haven't reached the limit yet, so just add the score | ||
if (this.scores.length < this.maxScores) { | ||
this.scores.push(score); | ||
added = true; | ||
} | ||
|
||
// delete an existing score if this score happens to be better than the worse score saved so far | ||
else { | ||
const worseScore = SCORES[SCORES.length - 1]; | ||
// delete an existing score if this score happens to be better than the worse score saved so far | ||
else { | ||
const worseScore = this.scores[this.scores.length - 1]; | ||
|
||
if (worseScore.score < score.score) { | ||
SCORES.pop(); | ||
SCORES.push(score); | ||
added = true; | ||
if (worseScore.score < score.score) { | ||
this.scores.pop(); | ||
this.scores.push(score); | ||
added = true; | ||
} | ||
} | ||
} | ||
|
||
if (added) { | ||
SCORES.sort((a, b) => { | ||
return b.score - a.score; | ||
}); | ||
save(); | ||
if (added) { | ||
this.scores.sort((a, b) => { | ||
return b.score - a.score; | ||
}); | ||
|
||
// find the position of the score in the list | ||
for (let a = 0; a < SCORES.length; a++) { | ||
if (SCORES[a].score === score.score) { | ||
return a + 1; | ||
// find the position of the score in the list | ||
for (let a = 0; a < this.scores.length; a++) { | ||
if (this.scores[a].score === score.score) { | ||
return a + 1; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get the high-scores list. | ||
*/ | ||
export function getHighScores() { | ||
return SCORES; | ||
} | ||
|
||
/** | ||
* Save the current high-scores to the storage. | ||
*/ | ||
function save() { | ||
setData("tetris_high_score", SCORES); | ||
/** | ||
* Get the high-scores list. | ||
*/ | ||
getHighScores() { | ||
return this.scores; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface ScoreData { | ||
score: number; | ||
linesCleared: number; | ||
time: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./high-score"; | ||
export * from "./high-score.hooks"; | ||
export * from "./high-score.context"; | ||
export * from "./high-score.types"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters