refining vars and refactoring
This commit is contained in:
parent
896d90121b
commit
00da8a0ca1
|
|
@ -1,13 +1,16 @@
|
||||||
import { createContext, useContext, useEffect, useState } from "react";
|
import { createContext, useContext, useEffect, useState } from "react";
|
||||||
import chantierAtmopshere from "../assets/sounds/chantier_1.mp3";
|
import chantierAtmopshere from "../assets/sounds/chantier_1.mp3";
|
||||||
import GameAnswerExplanation from "../components/game/GameAnswerExplanation.jsx";
|
import GameAnswerExplanation from "../components/game/GameAnswerExplanation.jsx";
|
||||||
import { useGame } from "../hooks/useGame.jsx";
|
|
||||||
import { useUser } from "../hooks/useUser.jsx";
|
import { useUser } from "../hooks/useUser.jsx";
|
||||||
import { calculateScore } from "../utils/gameFunctions.js";
|
import { calculateScore } from "../utils/gameFunctions.js";
|
||||||
|
import { postGameStatisticsData } from "../services/WordpressFetchData.js";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
export const GameContext = createContext();
|
export const GameContext = createContext();
|
||||||
|
|
||||||
export function GameContextProvider({ children }) {
|
export function GameContextProvider({ children }) {
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const { language, country } = useUser();
|
const { language, country } = useUser();
|
||||||
|
|
||||||
// ##### DATA #####
|
// ##### DATA #####
|
||||||
|
|
@ -16,6 +19,7 @@ export function GameContextProvider({ children }) {
|
||||||
// ##### GAME #####
|
// ##### GAME #####
|
||||||
const [answers, setAnswers] = useState(null);
|
const [answers, setAnswers] = useState(null);
|
||||||
const [score, setScore] = useState(null);
|
const [score, setScore] = useState(null);
|
||||||
|
const [gameTime, setGameTime] = useState(0);
|
||||||
|
|
||||||
// ##### INTERFACE #####
|
// ##### INTERFACE #####
|
||||||
const [hasCheckedTutorial, setHasCheckedTutorial] = useState(false);
|
const [hasCheckedTutorial, setHasCheckedTutorial] = useState(false);
|
||||||
|
|
@ -26,7 +30,7 @@ export function GameContextProvider({ children }) {
|
||||||
const [isTimeRuning, setIsTimeRuning] = useState(false);
|
const [isTimeRuning, setIsTimeRuning] = useState(false);
|
||||||
const [isGameComplete, setIsGameComplete] = useState(false);
|
const [isGameComplete, setIsGameComplete] = useState(false);
|
||||||
|
|
||||||
// INIT DATAS
|
// ##### INIT DATAS #####
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!contextGameDatas) return;
|
if (!contextGameDatas) return;
|
||||||
initAnswers();
|
initAnswers();
|
||||||
|
|
@ -58,14 +62,32 @@ export function GameContextProvider({ children }) {
|
||||||
function checkIfGameIsComplete() {
|
function checkIfGameIsComplete() {
|
||||||
const isGameFinished = answers.every((answer) => answer.userAnswer !== null);
|
const isGameFinished = answers.every((answer) => answer.userAnswer !== null);
|
||||||
if (!isGameFinished) return;
|
if (!isGameFinished) return;
|
||||||
console.warn(answers);
|
endGame();
|
||||||
alert("Game is complete");
|
}
|
||||||
|
|
||||||
|
function endGame() {
|
||||||
|
console.log("### Edning Game is complete");
|
||||||
setIsTimeRuning(false);
|
setIsTimeRuning(false);
|
||||||
setIsGameComplete(true);
|
setIsGameComplete(true);
|
||||||
|
const gameStats = buildGameStatsObject();
|
||||||
|
postGameStatisticsData(gameStats);
|
||||||
|
// navigate("/results");
|
||||||
|
navigate("/results", { state: { gameStats, contextGameDatas } });
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildGameStatsObject() {
|
||||||
|
if (!language || !country || !country.name || !contextGameDatas.gameId) return null;
|
||||||
|
return {
|
||||||
|
user_locale: language,
|
||||||
|
user_country: country.name,
|
||||||
|
level_post_id: contextGameDatas.gameId ?? null,
|
||||||
|
level_score: score ?? 0,
|
||||||
|
level_completion_time: gameTime,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function resetGame() {
|
function resetGame() {
|
||||||
isGameComplete(false);
|
setIsGameComplete(false);
|
||||||
setIsTimeRuning(false);
|
setIsTimeRuning(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -74,6 +96,9 @@ export function GameContextProvider({ children }) {
|
||||||
value={{
|
value={{
|
||||||
currentGameModal,
|
currentGameModal,
|
||||||
score,
|
score,
|
||||||
|
|
||||||
|
gameTime,
|
||||||
|
setGameTime,
|
||||||
setCurrentGameModal,
|
setCurrentGameModal,
|
||||||
contextGameDatas,
|
contextGameDatas,
|
||||||
setContextGameDatas,
|
setContextGameDatas,
|
||||||
|
|
@ -81,10 +106,12 @@ export function GameContextProvider({ children }) {
|
||||||
setIsTimeRuning,
|
setIsTimeRuning,
|
||||||
isGameComplete,
|
isGameComplete,
|
||||||
answers,
|
answers,
|
||||||
|
resetGame,
|
||||||
setAnswers,
|
setAnswers,
|
||||||
initAnswers,
|
initAnswers,
|
||||||
isSoundOn,
|
isSoundOn,
|
||||||
setIsSoundOn,
|
setIsSoundOn,
|
||||||
|
buildGameStatsObject,
|
||||||
answerQuestion,
|
answerQuestion,
|
||||||
hasCheckedTutorial,
|
hasCheckedTutorial,
|
||||||
setHasCheckedTutorial,
|
setHasCheckedTutorial,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user