-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameRoundScreen.tsx
91 lines (84 loc) · 3.26 KB
/
GameRoundScreen.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import {useEffect, useState} from "react";
import axios from "axios";
import {Card} from '../../models/Card';
import {GameState} from "../GameScreen";
import {GameTeams} from "../../models/Team";
import AliasCard from "../AliasCard";
type GameRoundScreenProps = {
gameStateSetter: (gs: GameState) => void
gameTeamsSetter: (gt: GameTeams) => void
gameTeams: GameTeams
}
type CardState = {
index: number;
card: Card | null;
}
export default function GameRoundScreen({gameStateSetter, gameTeams, gameTeamsSetter}: GameRoundScreenProps) {
let [maxIndex, maxIndexSetter] = useState<number>(-1)
let [currentCardState, cardSetter] = useState<CardState>({index: -1, card: null})
let [roundPoints, roundPointsSetter] = useState<number>(0)
function nextCard() {
if (maxIndex != null && currentCardState.index < maxIndex) {
axios.get(`/cards/card?index=${currentCardState.index + 1}`).then((response) => {
let newCard = response.data as Card
cardSetter({index: currentCardState.index + 1, card: newCard})
})
}
if (currentCardState.index >= maxIndex) {
endGame()
}
}
// Load initial data
useEffect(() => {
axios.get("/cards/amount").then((response) => {
maxIndex = response.data as number;
maxIndexSetter(maxIndex)
nextCard();
})
}, []);
function endGame() {
gameTeams.addPoints(roundPoints)
gameTeamsSetter(gameTeams)
gameStateSetter(GameState.END)
}
return (
<div className="App-cards-container">
<p className="App-game-p font-link-base">
Current team: {gameTeams.teams[gameTeams.currentTeamIndex].name}
</p>
<AliasCard card={currentCardState.card}/>
<div className="App-buttons-container">
<button className="App-button-base App-game-button-base App-game-button-base-skip" onClick={
() => {
nextCard()
if (roundPoints - 1 < 0) {
roundPointsSetter(0)
} else {
roundPointsSetter(roundPoints - 1)
}
}
}></button>
<button className="App-button-base App-game-button-base App-game-button-base-next" onClick={
() => {
nextCard()
roundPointsSetter(roundPoints + 1)
}
}>
</button>
</div>
<div className="App-buttons-container">
<button className="App-button-base App-game-button-bottom-base App-game-button-bottom-next-round" onClick={
() => {
gameTeams.addPoints(roundPoints)
nextCard()
roundPointsSetter(0)
}
}></button>
|
<button className="App-button-base App-game-button-bottom-base App-game-button-bottom-finish" onClick={
() => endGame()
}></button>
</div>
</div>
);
}