-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (144 loc) · 5.9 KB
/
index.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
document.addEventListener('DOMContentLoaded', function () {
// Objectives data
var objectivesData = [
["Auto Snow", 10, 1000],
["Auto Park", 5, 20],
["Teleop Snow", 5, 1000],
["Endgame Park", 5, 30],
["Rock Bonus", 15, 15],
["Pass Bonus", 10, 10]
];
// Get Alliances
var alliancesDivs = document.querySelectorAll('.alliance');
alliancesDivs.forEach(function(allianceDiv) {
// Add objectives and inputs
var objectivesDiv = allianceDiv.querySelector('.objectives');
objectivesData.forEach(function(objective) {
var objectiveDiv = document.createElement('div');
objectiveDiv.classList.add('objective');
var span = document.createElement('span');
span.textContent = objective[0];
var input = document.createElement('input');
input.type = 'text';
input.value = '0';
var plusButton = document.createElement('button');
plusButton.textContent = '+';
plusButton.classList.add('plus-button');
var minusButton = document.createElement('button');
minusButton.textContent = '-';
minusButton.classList.add('minus-button');
plusButton.addEventListener('click', function() {
var input = plusButton.previousElementSibling;
if(parseInt(input.value) < objective[2]){
input.value = parseInt(input.value) + objective[1];
}
updateTotalPoints();
});
minusButton.addEventListener('click', function() {
var input = minusButton.previousElementSibling.previousElementSibling;
if (parseInt(input.value) > 0) {
input.value = parseInt(input.value) - objective[1];
}
updateTotalPoints();
});
objectiveDiv.appendChild(span);
objectiveDiv.appendChild(input);
objectiveDiv.appendChild(plusButton);
objectiveDiv.appendChild(minusButton);
objectivesDiv.appendChild(objectiveDiv);
});
// Add total points span and input
var totalPointsDiv = allianceDiv.querySelector('.total-points');
var totalPointsSpan = document.createElement('span');
totalPointsSpan.textContent = 'Total Points:';
var totalPointsInput = document.createElement('input');
totalPointsInput.type = 'text';
totalPointsInput.value = '0';
totalPointsDiv.appendChild(totalPointsSpan);
totalPointsDiv.appendChild(totalPointsInput);
});
function updateTotalPoints(){
// Get Alliances
var alliancesDivs = document.querySelectorAll('.alliance');
alliancesDivs.forEach(function(allianceDiv) {
// Add objectives and inputs
var objectiveInputDivs = allianceDiv.querySelectorAll('.objectives input');
let total = 0
objectiveInputDivs.forEach(function(input) {
total += parseInt(input.value)
});
// Add total points span and input
var totalPointsInput = allianceDiv.querySelector('.total-points input');
totalPointsInput.value = total;
});
}
const startButton = document.querySelector('.start');
const stopButton = document.querySelector('.stop');
const replayButton = document.querySelector('.replay');
const timerNumber = document.querySelector('.timer-number');
const timerBar = document.querySelector('.timer-bar');
const audioAuto = document.getElementById('audioAuto');
const audioTeleop = document.getElementById('audioTeleop');
const audioEndgame = document.getElementById('audioEndgame');
const audioEnd = document.getElementById('audioEnd');
const audioFault = document.getElementById('audioFault');
let gameState = "prematch"; //prematch, auto, teleop, postmatch, fault
let timerInterval;
let timeLeft = 150; // Total time in seconds
startButton.addEventListener('click', startTimer);
stopButton.addEventListener('click', faultTimer);
replayButton.addEventListener('click', resetTimer);
function startTimer() {
if (gameState == "prematch") {
gameState = "auto";
playSound(audioAuto);
timerInterval = setInterval(updateTimer, 1000);
}
}
function updateTimer() {
updateTimerDisplay(--timeLeft);
if (timeLeft == 135) {
gameState = "teleop";
playSound(audioTeleop);
}
if (timeLeft == 20) {
playSound(audioEndgame);
}
if (timeLeft <= 0) {
gameState = "postmatch";
playSound(audioEnd);
clearInterval(timerInterval);
}
}
function faultTimer() {
gamestate = "fault";
playSound(audioFault);
clearInterval(timerInterval);
}
function resetTimer() {
gamestate = "prematch";
clearInterval(timerInterval);
timeLeft = 150
updateTimerDisplay(timeLeft);
let redObjectives = [redAmpInput,redSpeakerInput,redTrapInput,redOnstageInput,redHarmonyInput];
let blueObjectives = [blueAmpInput,blueSpeakerInput,blueTrapInput,blueOnstageInput,blueHarmonyInput];
redObjectives.forEach(element => {
element.value = 0;
});
blueObjectives.forEach(element => {
element.value = 0;
});
updateTotalPoints()
}
function updateTimerDisplay(newTime) {
timeLeft = newTime;
timerNumber.innerHTML = timeLeft;
let percentageLeft = (timeLeft / 150) * 100;
timerBar.style.background = `linear-gradient(to right, #4CAF50 ${percentageLeft}%, grey 0%)`;
}
function playSound(audioElement){
audioElement.pause();
audioElement.currentTime = 0;
audioElement.play();
}
});