generated from github/codespaces-react
-
-
Notifications
You must be signed in to change notification settings - Fork 117
Add feature to review matches #339
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
Open
odrail
wants to merge
7
commits into
samimsu:main
Choose a base branch
from
odrail:resume-games
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d909fa2
Add feature to review matches
odrail 64b0d54
add migration completed levels local storage
odrail f094ba5
fix unwanted redraw board
odrail 68ed561
refactor
odrail 1d696f2
refactor Timer
odrail 73f7297
refactor localStorage
odrail cc86268
add background color for LevelButton when is in progress
odrail File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,59 +1,137 @@ | ||
| export const markLevelAsCompleted = (levelNumber) => { | ||
| let completedLevels = | ||
| JSON.parse(localStorage.getItem("completedLevels")) || []; | ||
| import { createEmptyBoard } from "@/utils/board"; | ||
| import { levels } from "@/utils/levels"; | ||
|
|
||
| if (!completedLevels.includes(levelNumber)) { | ||
| completedLevels.push(levelNumber); | ||
| localStorage.setItem("completedLevels", JSON.stringify(completedLevels)); | ||
| const LOCAL_STORAGE = { | ||
| completedLevels: { | ||
| key: "completedLevels", | ||
| defaultValue: {} | ||
| }, | ||
| clashingQueensEnabled: { | ||
| key: "clashingQueensEnabled", | ||
| defaultValue: true | ||
| }, | ||
| showInstructions: { | ||
| key: "showInstructions", | ||
| defaultValue: true | ||
| }, | ||
| showClock: { | ||
| key: "showClock", | ||
| defaultValue: true | ||
| }, | ||
| autoPlaceXs: { | ||
| key: "autoPlaceXs", | ||
| defaultValue: false | ||
| }, | ||
| groupBySize: { | ||
| key: "groupBySize", | ||
| defaultValue: false | ||
| }, | ||
| } | ||
|
|
||
| const migrateStoredLevelsLocalStorage = (storedLevels) => { | ||
| if (!Array.isArray(storedLevels)) return storedLevels; | ||
| const levelsMigrated = {} | ||
|
|
||
| storedLevels.forEach(completedLevel => { | ||
| const level = levels[`level${completedLevel}`] | ||
| const levelSize = level?.size | ||
| levelsMigrated[completedLevel] = { | ||
| completed: true, | ||
| board: levelSize && createEmptyBoard(levelSize), | ||
| time: 0 | ||
| } | ||
| }) | ||
| storeLevels(levelsMigrated); | ||
| return levelsMigrated; | ||
| } | ||
|
|
||
| const getStoredLevels = () => { | ||
| try { | ||
| const storedLevels = JSON.parse(localStorage.getItem(LOCAL_STORAGE.completedLevels.key)) ?? LOCAL_STORAGE.completedLevels.defaultValue; | ||
| return migrateStoredLevelsLocalStorage(storedLevels); | ||
| } catch (error) { | ||
| return LOCAL_STORAGE.completedLevels.defaultValue; | ||
| } | ||
| } | ||
|
|
||
| const storeLevels = (completedLevels) => { | ||
| localStorage.setItem(LOCAL_STORAGE.completedLevels.key, JSON.stringify(completedLevels)); | ||
| } | ||
|
|
||
| export const getStoredLevel = (levelNumber) => { | ||
| const storedLevels = getStoredLevels(); | ||
| return storedLevels[levelNumber]; | ||
| } | ||
|
|
||
| export const saveLevelAsCompleted = (levelNumber, time, board) => { | ||
| saveLevel(levelNumber, time, board, true); | ||
| } | ||
|
|
||
| export const saveLevelAsNotCompleted = (levelNumber, time, board) => { | ||
| saveLevel(levelNumber, time, board, false); | ||
| } | ||
|
|
||
| const saveLevel = (levelNumber, time, board, completed) => { | ||
| const storedLevels = getStoredLevels(); | ||
|
|
||
| storedLevels[levelNumber] = { | ||
| completed, | ||
| time, | ||
| board | ||
| }; | ||
| storeLevels(storedLevels); | ||
| }; | ||
|
|
||
| export const isLevelCompleted = (levelNumber) => { | ||
| const completedLevels = | ||
| JSON.parse(localStorage.getItem("completedLevels")) || []; | ||
| return completedLevels.includes(levelNumber); | ||
| const storedLevels = getStoredLevels(); | ||
| return storedLevels[levelNumber]?.completed; | ||
| }; | ||
|
|
||
| export const isLevelInProgress = (levelNumber) => { | ||
| const storedLevels = getStoredLevels(); | ||
| return storedLevels[levelNumber]?.completed === false && storedLevels[levelNumber]?.time > 0; | ||
| } | ||
|
|
||
| export const resetCompletedLevels = () => { | ||
| localStorage.setItem("completedLevels", JSON.stringify([])); | ||
| storeLevels(JSON.stringify(LOCAL_STORAGE.completedLevels.defaultValue)); | ||
| }; | ||
|
|
||
| export const setClashingQueensPreference = (enabled) => { | ||
| localStorage.setItem("clashingQueensEnabled", JSON.stringify(enabled)); | ||
| localStorage.setItem(LOCAL_STORAGE.clashingQueensEnabled.key, JSON.stringify(enabled)); | ||
| }; | ||
|
|
||
| export const getClashingQueensPreference = () => { | ||
| return JSON.parse(localStorage.getItem("clashingQueensEnabled")) ?? true; // Default to true | ||
| return JSON.parse(localStorage.getItem(LOCAL_STORAGE.clashingQueensEnabled.key)) ?? LOCAL_STORAGE.clashingQueensEnabled.defaultValue; | ||
| }; | ||
|
|
||
| export const setShowInstructionsPreference = (enabled) => { | ||
| localStorage.setItem("showInstructions", JSON.stringify(enabled)); | ||
| localStorage.setItem(LOCAL_STORAGE.showInstructions.key, JSON.stringify(enabled)); | ||
| }; | ||
|
|
||
| export const getShowInstructionsPreference = () => { | ||
| return JSON.parse(localStorage.getItem("showInstructions")) ?? true; // Default to true | ||
| return JSON.parse(localStorage.getItem(LOCAL_STORAGE.showInstructions.key)) ?? LOCAL_STORAGE.showInstructions.defaultValue; | ||
| }; | ||
|
|
||
| export const setShowClockPreference = (enabled) => { | ||
| localStorage.setItem("showClock", JSON.stringify(enabled)); | ||
| localStorage.setItem(LOCAL_STORAGE.showClock.key, JSON.stringify(enabled)); | ||
| }; | ||
|
|
||
| export const getShowClockPreference = () => { | ||
| return JSON.parse(localStorage.getItem("showClock")) ?? true; // Default to true | ||
| return JSON.parse(localStorage.getItem(LOCAL_STORAGE.showClock.key)) ?? LOCAL_STORAGE.showClock.defaultValue; | ||
| }; | ||
|
|
||
| export const setAutoPlaceXsPreference = (enabled) => { | ||
| localStorage.setItem("autoPlaceXs", JSON.stringify(enabled)); | ||
| localStorage.setItem(LOCAL_STORAGE.autoPlaceXs.key, JSON.stringify(enabled)); | ||
| }; | ||
|
|
||
| export const getAutoPlaceXsPreference = () => { | ||
| return JSON.parse(localStorage.getItem("autoPlaceXs")) ?? false; // Default to false | ||
| return JSON.parse(localStorage.getItem(LOCAL_STORAGE.autoPlaceXs.key)) ?? LOCAL_STORAGE.autoPlaceXs.defaultValue; | ||
| }; | ||
|
|
||
| export const setGroupingPreference = (enabled) => { | ||
| localStorage.setItem("groupBySize", JSON.stringify(enabled)); | ||
| localStorage.setItem(LOCAL_STORAGE.groupBySize.key, JSON.stringify(enabled)); | ||
| }; | ||
|
|
||
| export const getGroupingPreference = () => { | ||
| return JSON.parse(localStorage.getItem("groupBySize")) ?? false; // Default to false | ||
| return JSON.parse(localStorage.getItem(LOCAL_STORAGE.groupBySize.key)) ?? LOCAL_STORAGE.groupBySize.defaultValue; | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@samimsu I've placed all the game-saving logic in this file to avoid altering the project structure too much. However, I believe it would be better to use a class and implement the desired storage solution. This way, switching between storage options (like from localStorage to a database, for example) would be easier.