From 69cc467810900b74353da60888077d47e81a055a Mon Sep 17 00:00:00 2001 From: Francisco Pessano Date: Mon, 27 Dec 2021 22:37:43 -0300 Subject: [PATCH] Local storage for play again done --- script/game-functions/gameLogic.js | 26 ++++++++++++++++++++----- script/game-functions/victoryActions.js | 10 ++++++++-- script/index.js | 25 +++++++++++++++++++++--- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/script/game-functions/gameLogic.js b/script/game-functions/gameLogic.js index 3034ab7..7caf9f9 100644 --- a/script/game-functions/gameLogic.js +++ b/script/game-functions/gameLogic.js @@ -2,12 +2,23 @@ import accessToCombinations from "./accessToCombinations.js" import checkVictory from "./checkVictory.js" import victoryActions from "./victoryActions.js" -export default function gameLogic(checkInputsResult) { +export default function gameLogic(checkInputsResult, replay) { function Player(position) { - this.name = checkInputsResult[position][0].value - this.symbol = checkInputsResult[position][1].value + console.log(replay) + + if (replay) { + + this.name = checkInputsResult[position].name + this.symbol = checkInputsResult[position].symbol + + } else { + + this.name = checkInputsResult[position][0].value + this.symbol = checkInputsResult[position][1].value + } + this.markedBoxes = [] } @@ -42,10 +53,15 @@ export default function gameLogic(checkInputsResult) { const checkVictoryResult = checkVictory(combinations, actualPlayer.markedBoxes) console.log(checkVictoryResult) + const data = { + + player1: player1, + player2: player2, + } + if (checkVictoryResult[0] === 'WIN') { - victoryActions(checkVictoryResult[1], actualPlayer.name) - + victoryActions(checkVictoryResult[1], actualPlayer.name, data) } if (players.indexOf(actualPlayer) === 0) { diff --git a/script/game-functions/victoryActions.js b/script/game-functions/victoryActions.js index 58c8435..c5400d8 100644 --- a/script/game-functions/victoryActions.js +++ b/script/game-functions/victoryActions.js @@ -1,4 +1,4 @@ -export default function victoryActions(combination, playerName) { +export default function victoryActions(combination, playerName, data) { const stylesActions = (() => { @@ -39,10 +39,16 @@ export default function victoryActions(combination, playerName) { game.classList.add('animate__fadeOutDown') + console.log(data) + setTimeout(() => { game.classList.add('hidden') - + + localStorage.setItem('data', JSON.stringify(data)) + + location.reload(); + }, 1000) }) } \ No newline at end of file diff --git a/script/index.js b/script/index.js index 92e2e33..17cbd3f 100644 --- a/script/index.js +++ b/script/index.js @@ -17,11 +17,30 @@ function main(game) { disableEnterInputs(game) addStartButtonFunctionality(game) + + const data = localStorage.getItem('data') + + if (data) { + + const checkInputsResult = Object.values(JSON.parse(data)) + + localStorage.removeItem('data') + + game(checkInputsResult, true) + } } -function game() { +function game(checkInputsResult, replay) { - const checkInputsResult = checkInputs() + if (!checkInputsResult) { + + checkInputsResult = checkInputs() + } + + if (!replay) { + + replay = false + } console.log(checkInputsResult) @@ -33,7 +52,7 @@ function game() { hidePlayerSelection() showTicTacToe() - gameLogic(checkInputsResult) + gameLogic(checkInputsResult, replay) } window.onload = () => main(game) \ No newline at end of file