-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
155 lines (133 loc) · 4.74 KB
/
script.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
// Retrieve the necessary elements
let inputName = document.getElementById("inputName");
let teamSizeInput = document.getElementById("teamSizeInput");
let pickButton = document.getElementById("pickButton");
let clearButton = document.getElementById("clearButton");
let randomButton = document.getElementById("randomButton");
let teamOneResult = document.getElementById("teamOneResult");
let teamTwoResult = document.getElementById("teamTwoResult");
let out = document.getElementById("out");
let gunLine = document.getElementById("gunLine");
let mapLine = document.getElementById("mapLine");
inputName.value = "";
out.innerHTML = "";
teamOneResult.innerHTML = "";
teamTwoResult.innerHTML = "";
const guns = [
"Operator",
"Phantom",
"Vandal",
"Sheriff",
"Classic",
"Judge",
"Guardian",
"Odin",
"Spectre",
"Ares",
"Bulldog",
"Marshall",
"Ghost",
"Stinger",
"Bucky",
"Knife",
"Shorty",
"Frenzy",
];
// Object to keep track of which player has picked which gun
let playerGunMap = {};
// Add event listeners to the buttons
pickButton.addEventListener("click", pickPlayerButton);
clearButton.addEventListener("click", clearInputAndOutput);
randomButton.addEventListener("click", pickRandomGun);
inputName.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
pickPlayerButton();
}
});
function pickPlayerButton() {
let input = inputName.value;
// Generate a random number (0 or 1)
let randomNum = Math.floor(Math.random() * 2);
// Check the current number of players in each team
let teamOnePlayers = teamOneResult.querySelectorAll("p").length;
let teamTwoPlayers = teamTwoResult.querySelectorAll("p").length;
// Check if either team has reached the maximum limit of three players
if (teamOnePlayers === 3 && teamTwoPlayers === 3) {
alert("Both teams already have three players each.");
return; // Stop further execution of the function
}
// Decide which team to assign the name based on the random number
if (randomNum === 0) {
// Check if Team 1 can have more players
if (teamOnePlayers < 3) {
// Assign to Team 1
teamOneResult.innerHTML += `<p class="out">${input}</p>`;
} else {
// If Team 1 is full, assign to Team 2
teamTwoResult.innerHTML += `<p class="out">${input}</p>`;
}
} else {
// Check if Team 2 can have more players
if (teamTwoPlayers < 3) {
// Assign to Team 2
teamTwoResult.innerHTML += `<p class="out">${input}</p>`;
} else {
// If Team 2 is full, assign to Team 1
teamOneResult.innerHTML += `<p class="out">${input}</p>`;
}
}
// Clear the input field after reading the value
inputName.value = "";
}
function clearInputAndOutput() {
inputName.value = "";
out.innerHTML = "";
teamOneResult.innerHTML = "";
teamTwoResult.innerHTML = "";
gunLine.innerHTML = "";
// Preserve mapLine and gunLine elements, just clear their content
if (mapLine) {
mapLine.querySelector("h4").textContent = "";
}
if (gunLine) {
gunLine.querySelector("h4").textContent = "";
}
}
function pickRandomGun() {
let teamOnePlayers = Array.from(teamOneResult.querySelectorAll("p"));
let teamTwoPlayers = Array.from(teamTwoResult.querySelectorAll("p"));
let allPlayers = teamOnePlayers.concat(teamTwoPlayers);
if (allPlayers.length === 0) {
alert("No players added to either Team 1 or Team 2.");
return;
}
// Filter out players who have already picked a gun
let availablePlayers = allPlayers.filter(
(player) => !playerGunMap[player.innerText]
);
if (availablePlayers.length === 0) {
alert("All players have already picked guns.");
return;
}
let randomPlayerIndex = Math.floor(Math.random() * availablePlayers.length);
let randomPlayer = availablePlayers[randomPlayerIndex].innerText;
let randomGunIndex = Math.floor(Math.random() * guns.length);
let randomGun = guns[randomGunIndex];
// Update the player-gun mapping
playerGunMap[randomPlayer] = randomGun;
gunLine.innerText = `${randomPlayer} picks ${randomGun}`;
}
const maps = ["ascent", "pearl", "lotus", "haven", "bind", "fracture"];
// Add event listener to the "Pick random Map" button
let randomMapButton = document.getElementById("randomMap");
randomMapButton.addEventListener("click", pickRandomMap);
function pickRandomMap() {
let randomMapIndex = Math.floor(Math.random() * maps.length);
let randomMap = maps[randomMapIndex];
let mapLineElement = document.getElementById("mapLine"); // Get mapLine element
if (mapLineElement) {
mapLineElement.querySelector("h4").textContent = randomMap;
} else {
console.error("mapLine element not found!");
}
}