repository to manage all files for 1_2_oder_3 interaction game for Inf2/2 Interaktionen SoSe23 from Engert, Caliskan and Bachiri
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

App.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { useEffect, useState} from "react";
  2. import './App.css';
  3. import Header from "./components/Header";
  4. import Body from "./components/Body";
  5. import {getQuestion, getScoreboard, getReset} from "./components/api";
  6. function App() {
  7. const [timeLeft, setTimeLeft] = useState(10*1000);
  8. const [question, setQuestion] = useState('?')
  9. const [ans1, setAns1] = useState('?')
  10. const [ans2, setAns2] = useState('?')
  11. const [ans3, setAns3] = useState('?')
  12. const [correctans, setCorrectAns] = useState()
  13. const [correctfield, setCorrectField] = useState()
  14. const [scoreblue, setScoreBlue] = useState()
  15. const [scoregreen, setScoreGreen] = useState()
  16. const [scorered, setScoreRed] = useState()
  17. const [timerRunning, setTimerRunning] = useState(false);
  18. const [questionCount, setQuestionCount] = useState(0);
  19. const [teamsize, setTeamSize] = useState(0);
  20. useEffect(() => {
  21. // This function will be executed when the component is loaded
  22. getReset(setAns1, setAns2, setAns3, setQuestion);
  23. getScoreboard(setScoreBlue, setScoreGreen, setScoreRed);
  24. }, []);
  25. useEffect(() => {
  26. let timer;
  27. if (timerRunning && timeLeft > 0) {
  28. timer = setTimeout(() => {
  29. setTimeLeft(timeLeft-10);
  30. }, 10);}
  31. else if (timeLeft === 0) {
  32. alert("Zeit ist um !");
  33. showcorrect();
  34. getScoreboard(setScoreBlue, setScoreGreen, setScoreRed);
  35. setTimerRunning(false);
  36. setTimeLeft(10);
  37. }
  38. return () => clearTimeout(timer);
  39. }, [timeLeft, timerRunning]);
  40. function formatTime(time) {
  41. const seconds = Math.floor((time / 1000) % 60).toString().padStart(2, '0');
  42. return `${seconds} s`;
  43. }
  44. const showcorrect = () => {
  45. alert("Richtiges Feld: " + correctfield + "\nRichtige Antwort: " + correctans)
  46. }
  47. const startQuestion = () => {
  48. if (questionCount <= 5){
  49. setQuestionCount(questionCount+1)
  50. getQuestion(setAns1, setAns2, setAns3, setQuestion, setCorrectField, setCorrectAns).then(r => console.log(r))
  51. setTimeLeft(10*1000);
  52. setTimerRunning(true);
  53. }
  54. else {
  55. alert("Spiel vorbei !");
  56. getReset(setAns1, setAns2, setAns3, setQuestion);
  57. }
  58. }
  59. const stopTimer = () => {
  60. setTimerRunning(false)
  61. setTimeLeft(10*1000);
  62. }
  63. return (<>
  64. <Header timerRunning={timerRunning} timerContent={formatTime(timeLeft)} questionCount={questionCount} question={question}/>
  65. <Body startQuestion={startQuestion} timerRunning={timerRunning} stopTimer={stopTimer} setQuestionCount={setQuestionCount}
  66. setTeamSize={setTeamSize} teamsize={teamsize}
  67. ans2={ans2} ans3={ans3} ans1={ans1}
  68. scoreblue={scoreblue} scorered={scorered} scoregreen={scoregreen}
  69. setScoreGreen={setScoreGreen} setScoreRed={setScoreRed} setScoreBlue={setScoreBlue}
  70. setAns3={setAns3} setAns1={setAns1} setAns2={setAns2} setQuestion={setQuestion}
  71. />
  72. </>
  73. );
  74. }
  75. export default App;