Local storage for play again done

This commit is contained in:
2021-12-27 22:37:43 -03:00
parent 658c5b2419
commit 69cc467810
3 changed files with 51 additions and 10 deletions

View File

@@ -2,12 +2,23 @@ import accessToCombinations from "./accessToCombinations.js"
import checkVictory from "./checkVictory.js" import checkVictory from "./checkVictory.js"
import victoryActions from "./victoryActions.js" import victoryActions from "./victoryActions.js"
export default function gameLogic(checkInputsResult) { export default function gameLogic(checkInputsResult, replay) {
function Player(position) { function Player(position) {
this.name = checkInputsResult[position][0].value console.log(replay)
this.symbol = checkInputsResult[position][1].value
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 = [] this.markedBoxes = []
} }
@@ -42,10 +53,15 @@ export default function gameLogic(checkInputsResult) {
const checkVictoryResult = checkVictory(combinations, actualPlayer.markedBoxes) const checkVictoryResult = checkVictory(combinations, actualPlayer.markedBoxes)
console.log(checkVictoryResult) console.log(checkVictoryResult)
const data = {
player1: player1,
player2: player2,
}
if (checkVictoryResult[0] === 'WIN') { if (checkVictoryResult[0] === 'WIN') {
victoryActions(checkVictoryResult[1], actualPlayer.name) victoryActions(checkVictoryResult[1], actualPlayer.name, data)
} }
if (players.indexOf(actualPlayer) === 0) { if (players.indexOf(actualPlayer) === 0) {

View File

@@ -1,4 +1,4 @@
export default function victoryActions(combination, playerName) { export default function victoryActions(combination, playerName, data) {
const stylesActions = (() => { const stylesActions = (() => {
@@ -39,10 +39,16 @@ export default function victoryActions(combination, playerName) {
game.classList.add('animate__fadeOutDown') game.classList.add('animate__fadeOutDown')
console.log(data)
setTimeout(() => { setTimeout(() => {
game.classList.add('hidden') game.classList.add('hidden')
localStorage.setItem('data', JSON.stringify(data))
location.reload();
}, 1000) }, 1000)
}) })
} }

View File

@@ -17,11 +17,30 @@ function main(game) {
disableEnterInputs(game) disableEnterInputs(game)
addStartButtonFunctionality(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) console.log(checkInputsResult)
@@ -33,7 +52,7 @@ function game() {
hidePlayerSelection() hidePlayerSelection()
showTicTacToe() showTicTacToe()
gameLogic(checkInputsResult) gameLogic(checkInputsResult, replay)
} }
window.onload = () => main(game) window.onload = () => main(game)