-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
264 lines (197 loc) · 6.74 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
Rock Paper Scissors
Copyright (C) 2022 Alessandro Proto - I3a
https://alexdevs.me
*/
const data = {
sets: [],
objects: {},
};
let currentSetIndex;
let userChoice;
let customSetIndex;
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function switchScene(from, to) {
// animation fuckery
document.getElementById(from).classList.add("fadeout");
document.getElementById(from).classList.remove("fadein");
document.getElementById(to).classList.add("fadein");
document.getElementById(to).classList.remove("fadeout");
await sleep(500);
document.getElementById(from).style.display = "none";
document.getElementById(to).style.display = "";
}
function sanitize(name) {
return name.replace(/[^A-Z]+/ig, '').toLowerCase();
}
async function main() {
loadData();
insertSetButtons();
}
function loadData() {
data.objects = gameData.objects;
for (let rawSetData of gameData.sets) {
let setData = {
name: "Unnamed",
set: [],
};
// if wildcard is at first pos, include all objects into the set
if (rawSetData.set[0] === "*") {
setData.set = Object.keys(data.objects);
// if blacklist is present, remove values from set of all objects
if (rawSetData.blacklist) {
for (let val of rawSetData.blacklist) {
setData.set = setData.set.filter(x => x !== val);
}
}
} else {
setData.set = rawSetData.set;
}
setData.name = rawSetData.name ?? `${setData.set.length} objects`;
data.sets.push(setData)
document.getElementById("arbitrary-input").max = Object.keys(data.objects).length;
}
}
function insertSetButtons() {
let setsSelection = document.getElementById("set-selection");
for (let i = 0; i < data.sets.length; i++) {
let set = data.sets[i];
let btn = document.createElement("button");
btn.type = "button";
btn.innerText = set.name;
btn.addEventListener("click", () => loadGame(i));
setsSelection.appendChild(btn);
}
}
function getCard(name, interactive) {
let obj = data.objects[name];
let div = document.createElement("div");
div.classList.add("card");
if (interactive) {
div.classList.add("interactive");
}
let img = document.createElement("img");
img.src = "assets/" + name + ".png";
img.alt = obj.name;
div.appendChild(img);
let p = document.createElement("p");
p.innerText = obj.name;
div.appendChild(p);
return div;
}
async function loadGame(index) {
console.log("Loading game with set of index ", index)
currentSetIndex = index;
// create all cards from the chosen set
let selection = document.getElementById("card-selection");
selection.innerHTML = "";
let gameSet = data.sets[currentSetIndex].set;
for (const name of gameSet) {
let div = getCard(name, true);
div.onclick = () => playCard(name);
selection.appendChild(div);
}
switchScene("main-menu", "game");
}
async function makeArbitrarySet() {
let setLength = Number.parseInt(document.getElementById("arbitrary-input").value);
if(document.getElementById("arbitrary-input").value == "-0") {
window.location = "https://www.youtube.com/watch?v=dQw4w9WgXcQ";
return;
}
let errorP = document.getElementById("arbitrary-error");
if(!setLength) {
errorP.style.display = "";
errorP.innerText = "Invalid input!"
return;
}
if(setLength <= 0) {
errorP.style.display = "";
errorP.innerText = "Must be at least 1!"
return;
}
let possibleObjects = Object.keys(data.objects);
setLength = Math.min(setLength, possibleObjects.length)
let customSet = {
name: "Custom set",
set: [],
};
for(let i = 0; i < setLength; i++) {
let objIndex = randomInt(0, possibleObjects.length);
let objName = possibleObjects.splice(objIndex, 1);
customSet.set.push(objName);
}
if(!customSetIndex) {
customSetIndex = data.sets.length;
}
data.sets[customSetIndex] = customSet;
loadGame(customSetIndex);
}
function playCard(playerKey) {
let gameSet = data.sets[currentSetIndex].set;
let cpuChoice = randomInt(0, gameSet.length);
let cpuKey = gameSet[cpuChoice];
showResult(playerKey, cpuKey);
}
async function showResult(playerKey, cpuKey) {
switchScene("game", "result");
let playerCard = getCard(playerKey);
document.getElementById("result-player").appendChild(playerCard);
let cpuCard = getCard(cpuKey);
cpuCard.classList.add("flipped");
document.getElementById("result-cpu").appendChild(cpuCard);
// wait for the fade in animation to finish
await sleep(750);
playerCard.classList.add("wobble");
cpuCard.classList.add("wobble-reverse");
let countdown = document.getElementById("result-status");
for (let i = 3; i > 0; i--) {
countdown.innerText = i;
await sleep(1000);
}
playerCard.classList.remove("wobble");
cpuCard.classList.remove("wobble-reverse");
await sleep(100);
cpuCard.classList.remove("flipped", "wobble-reverse");
await sleep(100);
let resultStatus = document.getElementById("result-status");
document.getElementById("reset-button").style.display = "";
if (playerKey == cpuKey) {
resultStatus.innerText = "TIE!";
resultStatus.classList.add("tie");
return;
}
let playerObj = data.objects[playerKey];
if (playerObj.beats.includes(cpuKey)) {
resultStatus.innerText = "You won!";
resultStatus.classList.add("win");
playerCard.classList.add("glow");
cpuCard.classList.add("emotional-damage");
console.log("Player won");
} else {
resultStatus.innerText = "You lost!";
resultStatus.classList.add("lose");
cpuCard.classList.add("glow");
playerCard.classList.add("emotional-damage");
console.log("CPU won");
}
}
async function reset() {
document.getElementById("arbitrary-error").style.display = "none";
await switchScene("result", "main-menu");
currentSetIndex = undefined;
let status = document.getElementById("result-status");
status.innerText = "3";
status.classList.remove("win");
status.classList.remove("lose");
status.classList.remove("tie");
document.getElementById("result-player").children[1].remove();
document.getElementById("result-cpu").children[1].remove();
document.getElementById("reset-button").style.display = "none";
}
window.onload = main;