Skip to content

Commit

Permalink
Merge pull request #143 from osutcj/Gauge-Meter-Timer
Browse files Browse the repository at this point in the history
Hopefully working Gauge Clock
  • Loading branch information
Alex7734 committed Sep 29, 2023
2 parents f45787e + c198f44 commit de1f2cd
Show file tree
Hide file tree
Showing 20 changed files with 197 additions and 50 deletions.
Empty file modified .husky/common.sh
100644 → 100755
Empty file.
Empty file modified .husky/pre-commit
100644 → 100755
Empty file.
49 changes: 49 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/firebase/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { initializeApp } from 'firebase/app';
import { getDatabase } from 'firebase/database';
import { getFirestore } from 'firebase/firestore';
import { getAuth } from 'firebase/auth';
import { environmentType } from '../utils/contants';
import { environmentType } from '../utils/constants';

// Your web app's Firebase configuration for local development
const firebaseConfigDev = {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useFlashRound.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { doc, onSnapshot } from 'firebase/firestore';
import React, { useEffect, useState } from 'react';
import { FlashRound } from '../types/flashRound';
import { FLASH_ROUND_COLLECTION } from '../utils/contants';
import { FLASH_ROUND_COLLECTION } from '../utils/constants';
import { firestore } from '../firebase/firebase';

function useFlashRound(docId: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useGame.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react';
import { onSnapshot, doc } from 'firebase/firestore';
import { firestore } from '../firebase/firebase';
import { GAMES_COLLECTION } from '../utils/contants';
import { GAMES_COLLECTION } from '../utils/constants';
import { NormalGame } from '../types/game';
import { EmptyGame } from '../models/game';

Expand Down
2 changes: 1 addition & 1 deletion src/screens/Admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { currEnvironment, firestore } from '../firebase/firebase';
import { auth } from '../firebase/firebase';
import { signInWithPopup, GoogleAuthProvider } from 'firebase/auth';
import { collection, getDocs, query, where } from 'firebase/firestore';
import { environmentType } from '../utils/contants';
import { environmentType } from '../utils/constants';

const Admin = () => {
const [loggedIn, setLoggedIn] = useState(false);
Expand Down
37 changes: 9 additions & 28 deletions src/screens/FlashRound.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { FlashRoundAnswers } from '../types/flashRound';
import wrongAnswerSound from '../static/x.mp3';
import { useSounds } from '../hooks/useSounds.hook';
import wrongAnswerPng from '../static/x.png';
import { TIMER_LENGTH, TIMER_BONUS } from '../utils/contants';
import { TIMER_LENGTH, TIMER_BONUS } from '../utils/constants';
import CountDownTimer from '../shared/FlashRound/CountDownTimer';

const FlashRoundClient = () => {
const [answers, setAnswers] = useState<FlashRoundAnswers[]>([]);
Expand All @@ -37,8 +38,6 @@ const FlashRoundClient = () => {
return () => clearInterval(interval);
}, []);

console.log(currentTime);

const Item = styled(Paper)(({ theme }) => ({
background:
'linear-gradient(to bottom,#cedbe9 0%,#aac5de 17%, #6199c7 50%, #3a84c3 51%, #419ad6 59%,#4bb8f0 71%, #3A8BC2 84%, #26558B 100%)',
Expand Down Expand Up @@ -115,32 +114,14 @@ const FlashRoundClient = () => {
};

const RenderCountdownCircle = () => {
const percentage = (currentTime / TIMER_LENGTH) * 100;

const countdownCircleStyles: React.CSSProperties = {
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: '200px',
height: '200px',
borderRadius: '50%',
backgroundColor: 'transparent',
border: '10px solid #fff'
};

return (
<div style={countdownCircleStyles}>
<div
style={{
fontSize: '36px',
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)'
}}
></div>
</div>
<CountDownTimer
seconds={currentTime}
size={200}
strokeBgColor="#ffffff"
strokeColor="#000000"
strokeWidth={10}
/>
);
};

Expand Down
4 changes: 1 addition & 3 deletions src/screens/Game.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React, { useEffect, useState } from 'react';
import Box from '@mui/material/Box';
import Paper from '@mui/material/Paper';
import Grid from '@mui/material/Grid';
import Container from '@mui/material/Container';
import { styled } from '@mui/material';
import { DBAnswer, DBQuestion } from '../types/questions';
import { NormalGame } from '../types/game';
Expand All @@ -12,7 +10,7 @@ import useGame from '../hooks/useGame';
import QuestionsService from '../services/questions.service';
import wrongAnswerPng from '../static/x.png';
import { truncateQuestion } from '../helpers/truncate-question';
import { WRONG_ANSWER_TIME } from '../utils/contants';
import { WRONG_ANSWER_TIME } from '../utils/constants';

const Game = () => {
const [currentQuestion, setCurrentQuestion] = useState<DBQuestion>();
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/addToFirestore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { firestore } from '../firebase/firebase';
import { addDoc, collection } from 'firebase/firestore';
import { FLASH_ROUND_COLLECTION } from '../utils/contants';
import { FLASH_ROUND_COLLECTION } from '../utils/constants';

const arrayOfObjects = [
{
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/seedFirestore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { addDoc, collection } from 'firebase/firestore';
import { QUESTIONS_COLLECTION } from '../utils/contants';
import { QUESTIONS_COLLECTION } from '../utils/constants';
import { firestore } from '../firebase/firebase';
import { DBAnswer } from '../types/questions';

Expand Down
2 changes: 1 addition & 1 deletion src/services/flash.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { addDoc, collection, deleteDoc, doc, getDoc, getDocs, updateDoc } from 'firebase/firestore';
import { FlashRound } from '../types/flashRound';
import { FLASH_ROUND_COLLECTION } from '../utils/contants';
import { FLASH_ROUND_COLLECTION } from '../utils/constants';
import { firestore } from '../firebase/firebase';

const FlashRoundService = {
Expand Down
2 changes: 1 addition & 1 deletion src/services/games.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { firestore, db } from '../firebase/firebase';
import { collection, addDoc, getDoc, doc, getDocs, updateDoc, deleteDoc } from 'firebase/firestore';
import { GAMES_COLLECTION } from '../utils/contants';
import { GAMES_COLLECTION } from '../utils/constants';
import { NormalGame } from '../types/game';

const GamesService = {
Expand Down
2 changes: 1 addition & 1 deletion src/services/questions.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { firestore, db } from '../firebase/firebase';
import { collection, addDoc, getDoc, doc, getDocs, updateDoc, deleteDoc } from 'firebase/firestore';
import { QUESTIONS_COLLECTION } from '../utils/contants';
import { QUESTIONS_COLLECTION } from '../utils/constants';
import { DBQuestion } from '../types/questions';

const QuestionsService = {
Expand Down
101 changes: 101 additions & 0 deletions src/shared/FlashRound/CountDownTimer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import React from 'react';
import { TIMER_BONUS, TIMER_LENGTH } from "../../utils/constants";

type CountdownTimerProps = {
seconds: number;
size: number;
strokeBgColor: string;
strokeColor: string;
strokeWidth: number;
};

const CountDownTimer: React.FC<CountdownTimerProps> = ({
seconds,
size,
strokeBgColor,
strokeColor,
strokeWidth
}) => {
const radius = size / 2;
const localStorageIsStarted = localStorage.getItem('isStarted')
const maxSeconds = localStorage.getItem('isFirstRound') === 'true' ? TIMER_LENGTH : (TIMER_LENGTH+TIMER_BONUS)
const circumference = size * Math.PI;

const countdownSizeStyles: React.CSSProperties = {
height: size,
width: size
};

const textStyles: React.CSSProperties = {
color: strokeColor,
fontSize: size * 0.3
};


const strokeDashoffset = () => {
if (localStorageIsStarted != 'true') {
return 0;
}
const fractionElapsed = seconds / maxSeconds;
console.log(fractionElapsed)
console.log(circumference, circumference * (1 - fractionElapsed))
return circumference * (1 - fractionElapsed);
};

return (
<div>
<div
style={{
opacity: localStorageIsStarted ? 0.4 : 1
}}
></div>
<div style={{ ...styles.countdownContainer, ...countdownSizeStyles }}>
<p style={textStyles}>{seconds}s</p>
<svg style={styles.svg}>
<circle
cx={radius}
cy={radius}
r={radius}
fill="none"
stroke={strokeBgColor}
strokeWidth={strokeWidth}
></circle>
</svg>
<svg style={styles.svg}>
<circle
strokeDasharray={circumference}
strokeDashoffset={strokeDashoffset()}
r={radius}
cx={radius}
cy={radius}
fill="none"
strokeLinecap="round"
stroke={strokeColor}
strokeWidth={strokeWidth}
></circle>
</svg>
</div>
</div>
);
};

const styles = {
countdownContainer: {
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
position: 'relative' as 'relative',
margin: 'auto'
},
svg: {
position: 'absolute' as 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
transform: 'rotateY(-180deg) rotateZ(-90deg)',
overflow: 'visible'
} as React.CSSProperties
};

export default CountDownTimer;
1 change: 1 addition & 0 deletions src/shared/FlashRound/RoundAnswer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const RoundAnswer = (props: any) => {
if (props.reset) {
setAnswer('');
setPoints(0);
setShowPoints(false);
}
}, [[props.reset]]);

Expand Down
16 changes: 15 additions & 1 deletion src/shared/FlashRound/Timer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ const Timer = () => {
} = useTimerStore();
const [storedCurrentTime, setStoredCurrentTime] = useLocalStorage('currentTime', currentTime);
const [storedIsFirstRound, storedSetIsFirstRound] = useLocalStorage('isFirstRound', true);
const [storedIsStarted, setStoredIsStarted] = useLocalStorage('isStarted', false);

useEffect(() => {
localStorage.setItem('startAnimation', 'false');
}, []);

useEffect(() => {
let timer: any;
Expand All @@ -35,6 +40,11 @@ const Timer = () => {
setStoredCurrentTime(currentTime);
}, [currentTime, setStoredCurrentTime]);

useEffect(() => {
setStoredIsStarted(isRunning)
}, [currentTime, setStoredIsStarted]);


useEffect(() => {
storedSetIsFirstRound(isFirstRound);
}, [isFirstRound, storedSetIsFirstRound]);
Expand All @@ -49,6 +59,10 @@ const Timer = () => {
storedSetIsFirstRound(false);
};

const handleButtonClick = () => {
startTimer();
};

return (
<div style={{ marginBottom: '20px;' }}>
<h2 style={{ fontFamily: 'Popins', fontSize: '30px;', marginBottom: '10px' }}>
Expand All @@ -60,7 +74,7 @@ const Timer = () => {
xs={12}
style={{ justifyContent: 'space-around', display: 'flex', width: '50%', margin: '0 auto' }}
>
<Button variant="contained" onClick={startTimer} disabled={isRunning}>
<Button variant="contained" onClick={handleButtonClick} disabled={isRunning}>
Start
</Button>
<Button
Expand Down
Loading

0 comments on commit de1f2cd

Please sign in to comment.