-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cc5209
commit 7d6c95d
Showing
31 changed files
with
502 additions
and
821 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
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
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,78 +1,67 @@ | ||
import { difficulty } from "./../Components/Home/Cards"; | ||
import { statistics } from "./Puzzle.Types"; | ||
import { SudokuObjectProps } from "../Functions/LocalDatabase"; | ||
import { getKeyJSON, removeData, storeData } from "../Functions/AsyncStorage"; | ||
import { Statistics } from "./Statistics"; | ||
import { | ||
GameDifficulty, | ||
returnGameOfDifficulty, | ||
} from "../Components/SudokuBoard/Functions/Difficulty"; | ||
import { Statistics } from "./Puzzle.Types"; | ||
import { getStatistics, saveStatisitics } from "./Statistics"; | ||
|
||
/** | ||
* Functions to handle puzzle related operations | ||
* Given a difficulty and an user auth token retrieves a random puzzle close to the difficulty that the user hasn't solved before | ||
* @param difficulty - difficulty number (between 0 and 1) | ||
* @param strategies - new game can have subset of these strategies | ||
* @returns promise of puzzle JSON object | ||
*/ | ||
export class Puzzles { | ||
/** | ||
* Given a difficulty and an user auth token retrieves a random puzzle close to the difficulty that the user hasn't solved before | ||
* @param difficulty - difficulty number (between 0 and 1) | ||
* @param strategies - new game can have subset of these strategies | ||
* @returns promise of puzzle JSON object | ||
*/ | ||
public static async startGame( | ||
difficulty: GameDifficulty | ||
): Promise<SudokuObjectProps> { | ||
return returnGameOfDifficulty(difficulty); | ||
// !uncomment below for dev testing | ||
// return returnGameOfDifficulty("dev"); | ||
} | ||
|
||
/** | ||
* Given an user auth token retrieves the users active game or returns null if the user doesn't have an active game | ||
* @returns promise of activeGame JSON object | ||
*/ | ||
public static async getGame(): Promise<SudokuObjectProps[]> { | ||
return await getKeyJSON("active_game"); | ||
} | ||
export const startGame = (difficulty: GameDifficulty): SudokuObjectProps => { | ||
return returnGameOfDifficulty(difficulty); | ||
// !uncomment below for dev testing | ||
// return returnGameOfDifficulty("dev"); | ||
}; | ||
|
||
/** | ||
* Given a game saves it to AsyncStorage | ||
* @param game - activeGame JSON object | ||
*/ | ||
public static async saveGame(game: SudokuObjectProps) { | ||
storeData("active_game", JSON.stringify([game])); | ||
} | ||
/** | ||
* Given an user auth token retrieves the users active game or returns null if the user doesn't have an active game | ||
* @returns promise of activeGame JSON object | ||
*/ | ||
export const getGame = (): Promise<SudokuObjectProps[]> => { | ||
return getKeyJSON("active_game"); | ||
}; | ||
|
||
/** | ||
* Given deletes the users active game and returns game score | ||
* @returns promise of game score | ||
*/ | ||
public static async finishGame( | ||
numHintsUsed: number, | ||
numWrongCellsPlayed: number, | ||
time: number, | ||
score: number | ||
) { | ||
// remove the game from storage | ||
await removeData("active_game"); | ||
/** | ||
* Given a game saves it to AsyncStorage | ||
* @param game - activeGame JSON object | ||
*/ | ||
export const saveGame = (game: SudokuObjectProps) => { | ||
storeData("active_game", JSON.stringify([game])); | ||
}; | ||
|
||
// Create or update user's statistics | ||
let statistics: statistics = await Statistics.getStatistics(); | ||
/** | ||
* Given deletes the users active game and returns game score | ||
* @returns promise of game score | ||
*/ | ||
export const finishGame = async ( | ||
numHintsUsed: number, | ||
numWrongCellsPlayed: number, | ||
time: number, | ||
score: number | ||
) => { | ||
// remove the game from storage | ||
await removeData("active_game"); | ||
|
||
statistics.totalScore += score; | ||
if ( | ||
time < statistics.fastestSolveTime || | ||
statistics.fastestSolveTime === 0 | ||
) { | ||
statistics.fastestSolveTime = time; | ||
} | ||
statistics.totalSolveTime += time; | ||
statistics.numGamesPlayed += 1; | ||
statistics.numHintsUsed += numHintsUsed; | ||
statistics.numWrongCellsPlayed += numWrongCellsPlayed; | ||
statistics.averageSolveTime = Math.round( | ||
statistics.totalSolveTime / statistics.numGamesPlayed | ||
); | ||
// Create or update user's statistics | ||
let statistics: Statistics = await getStatistics(); | ||
|
||
Statistics.saveStatisitics(statistics); | ||
statistics.totalScore += score; | ||
if (time < statistics.fastestSolveTime || statistics.fastestSolveTime === 0) { | ||
statistics.fastestSolveTime = time; | ||
} | ||
} | ||
statistics.totalSolveTime += time; | ||
statistics.numGamesPlayed += 1; | ||
statistics.numHintsUsed += numHintsUsed; | ||
statistics.numWrongCellsPlayed += numWrongCellsPlayed; | ||
statistics.averageSolveTime = Math.round( | ||
statistics.totalSolveTime / statistics.numGamesPlayed | ||
); | ||
|
||
saveStatisitics(statistics); | ||
}; |
Oops, something went wrong.