-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from KartikWatts/feature-timer-countdown
Enhancement: Game plays with Clock TimeOut
- Loading branch information
Showing
17 changed files
with
355 additions
and
26 deletions.
There are no files selected for viewing
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,21 +1,18 @@ | ||
import React from "react"; | ||
import tw from "twrnc"; | ||
import { View } from "react-native"; | ||
import { StatusBar } from "expo-status-bar"; | ||
import * as NavigationBar from "expo-navigation-bar"; | ||
import Game from "./containers/Game"; | ||
import { GameProvider } from "./contexts/GameContext"; | ||
import GameWrapper from "./components/GameWrapper"; | ||
|
||
NavigationBar.setBackgroundColorAsync("rgb(71, 85, 105)"); | ||
NavigationBar.setBehaviorAsync("overlay-swipe"); | ||
NavigationBar.setVisibilityAsync("hidden"); | ||
|
||
export default function App() { | ||
return ( | ||
<View | ||
style={tw`w-full h-full bg-slate-500 flex justify-center items-center flex-col`} | ||
> | ||
<Game /> | ||
<GameProvider> | ||
<GameWrapper /> | ||
<StatusBar hidden={true} /> | ||
</View> | ||
</GameProvider> | ||
); | ||
} |
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
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import React, { useContext, useEffect, useState } from "react"; | ||
import { Audio } from "expo-av"; | ||
import tw from "twrnc"; | ||
import { Text, View } from "react-native"; | ||
import { GameContext } from "../contexts/GameContext"; | ||
|
||
function CountDown() { | ||
const [countDown, setCountDown] = useState("120"); | ||
const [countDownTime, setCountDownTime] = useState(0); | ||
const [isFinalSeconds, setIsFinalSeconds] = useState(false); | ||
|
||
const gameContext = useContext(GameContext); | ||
|
||
const [countDownTimerSound, setCountDownTimerSound] = | ||
useState<Audio.Sound>(); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
const { sound } = await Audio.Sound.createAsync( | ||
require("../assets/sounds/countdown_time.mp3") | ||
); | ||
setCountDownTimerSound(sound); | ||
})(); | ||
}, []); | ||
|
||
useEffect(() => { | ||
// TODO:: TO BE REPLACED BY TIME OF THE GAME ON THE SERVER IF EXISTS | ||
let cTime = new Date(+new Date() + 60000 * 2).getTime(); //Adding two minutes to current time | ||
if (!countDownTime) { | ||
setCountDownTime(cTime); | ||
return; | ||
} | ||
let seconds = 120; | ||
let countDownInterval = setInterval(() => { | ||
let now = new Date(+new Date() - 1000 * 2).getTime(); | ||
let difference = countDownTime - now + 1; | ||
if (difference < 0) difference = 0; | ||
let minutes = Math.floor( | ||
(difference % (1000 * 60 * 60)) / (1000 * 60) | ||
); | ||
seconds = Math.floor( | ||
minutes * 60 + (difference % (1000 * 60)) / 1000 | ||
); | ||
let secondsStr = seconds.toString(); | ||
setCountDown(secondsStr); | ||
if (seconds <= 10) { | ||
setIsFinalSeconds(true); | ||
} | ||
if (seconds == 0) { | ||
if (countDownTimerSound) countDownTimerSound.stopAsync(); | ||
clearInterval(countDownInterval); | ||
setTimeout(() => { | ||
if (gameContext) gameContext.toggleGameState(); | ||
}, 10); | ||
} | ||
}, 1000); | ||
}, [countDownTime]); | ||
|
||
useEffect(() => { | ||
if (countDownTimerSound) { | ||
if (countDown == "10") { | ||
countDownTimerSound.playAsync(); | ||
} | ||
if (countDown == "0") { | ||
countDownTimerSound.stopAsync(); | ||
} | ||
} | ||
}, [countDown]); | ||
|
||
return ( | ||
<View style={tw`w-30 px-2`}> | ||
<Text | ||
style={[ | ||
isFinalSeconds ? tw`text-red-400` : tw`text-slate-300`, | ||
tw`text-2xl`, | ||
]} | ||
> | ||
{countDown} | ||
</Text> | ||
</View> | ||
); | ||
} | ||
|
||
export default CountDown; |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, { useContext } from "react"; | ||
import tw from "twrnc"; | ||
import { Text, TouchableOpacity, View } from "react-native"; | ||
import Game from "../containers/Game"; | ||
import { GameContext } from "../contexts/GameContext"; | ||
|
||
export default function GameWrapper() { | ||
const gameContext = useContext(GameContext); | ||
|
||
return ( | ||
<View | ||
style={tw`w-full h-full bg-slate-500 flex justify-center items-center flex-col`} | ||
> | ||
{gameContext && gameContext.isGameOn ? ( | ||
<Game /> | ||
) : ( | ||
<View> | ||
<TouchableOpacity | ||
onPress={() => { | ||
if (gameContext) { | ||
gameContext.toggleGameState(); | ||
} | ||
}} | ||
style={tw`bg-yellow-300 p-8 border-8 hover:bg-amber-300 border-blue-400 rounded-xl`} | ||
> | ||
<Text style={tw`text-xl font-bold text-blue-600`}> | ||
Start Game | ||
</Text> | ||
</TouchableOpacity> | ||
</View> | ||
)} | ||
</View> | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React, { createContext, useReducer } from "react"; | ||
import { | ||
GameAction, | ||
GameActionType, | ||
GameState, | ||
} from "../assets/data/Interfaces"; | ||
import { GameContextType } from "../assets/data/Types"; | ||
|
||
const defaultState = { isGameOn: false }; | ||
|
||
const GameContext = createContext<GameContextType | null>(null); | ||
|
||
const gameReducer = (state: GameState, action: GameAction) => { | ||
const { type } = action; | ||
switch (type) { | ||
case GameActionType.TOGGLE: { | ||
return { ...state, isGameOn: !state.isGameOn }; | ||
} | ||
default: { | ||
throw new Error(`Unhandled action type: ${action.type}`); | ||
} | ||
} | ||
}; | ||
|
||
const GameProvider: React.FC = (props) => { | ||
const [state, dispatch] = useReducer(gameReducer, defaultState); | ||
const toggleGameState = () => { | ||
dispatch({ type: GameActionType.TOGGLE }); | ||
}; | ||
const value = { isGameOn: state.isGameOn, toggleGameState }; | ||
|
||
return ( | ||
<GameContext.Provider value={value}> | ||
{props.children} | ||
</GameContext.Provider> | ||
); | ||
}; | ||
|
||
export { GameContext, GameProvider }; |
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
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
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React, { useContext, useEffect, useState } from "react"; | ||
import countdown_timer from "../assets/sounds/countdown_time.mp3"; | ||
import useSound from "use-sound"; | ||
import { GameContext } from "../contexts/GameContext"; | ||
|
||
function CountDown() { | ||
const [countDown, setCountDown] = useState("120"); | ||
const [countDownTime, setCountDownTime] = useState(0); | ||
const [isFinalSeconds, setIsFinalSeconds] = useState(false); | ||
|
||
const gameContext = useContext(GameContext); | ||
|
||
const [playCountDownTimer, { stop: stopCountDownTimer }] = | ||
useSound(countdown_timer); | ||
|
||
useEffect(() => { | ||
// TODO:: TO BE REPLACED BY TIME OF THE GAME ON THE SERVER IF EXISTS | ||
let cTime = new Date(+new Date() + 60000 * 2).getTime(); //Adding two minutes to current time | ||
if (!countDownTime) { | ||
setCountDownTime(cTime); | ||
return; | ||
} | ||
let seconds = 120; | ||
let countDownInterval = setInterval(() => { | ||
let now = new Date(+new Date() - 1000 * 2).getTime(); | ||
let difference = countDownTime - now + 1; | ||
if (difference < 0) difference = 0; | ||
let minutes = Math.floor( | ||
(difference % (1000 * 60 * 60)) / (1000 * 60) | ||
); | ||
seconds = Math.floor( | ||
minutes * 60 + (difference % (1000 * 60)) / 1000 | ||
); | ||
let secondsStr = seconds.toString(); | ||
setCountDown(secondsStr); | ||
if (seconds <= 10) { | ||
setIsFinalSeconds(true); | ||
} | ||
if (seconds == 0) { | ||
clearInterval(countDownInterval); | ||
stopCountDownTimer(); | ||
setTimeout(() => { | ||
if (gameContext) gameContext.toggleGameState(); | ||
}, 10); | ||
} | ||
}, 1000); | ||
}, [countDownTime]); | ||
|
||
useEffect(() => { | ||
if (countDown == "10") { | ||
playCountDownTimer(); | ||
} | ||
if (countDown == "0") { | ||
stopCountDownTimer(); | ||
} | ||
}, [countDown]); | ||
|
||
return ( | ||
<div | ||
id="countdown_timer" | ||
className={`${ | ||
isFinalSeconds ? "text-red-400" : "text-slate-300" | ||
} text-2xl`} | ||
> | ||
{countDown} | ||
</div> | ||
); | ||
} | ||
|
||
export default CountDown; |
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
Oops, something went wrong.