-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdicefight.js
370 lines (301 loc) · 9.94 KB
/
dicefight.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
var rollDice = document.getElementById('rollDice');
var restart = document.getElementById("restart");
var highscores = document.getElementById("highscore");
var refillHp = document.getElementById("refillHp");
var maxHp = document.getElementById("maxHp");
var diceBoost = document.getElementById("diceBoost");
var hpPY = document.getElementById("hpPY");
var hpPC = document.getElementById("hpPC");
var diceCounterPY = document.getElementById("diceCounterPY");
var diceCounterPC =document.getElementById("diceCounterPC");
var hptxtPY = document.getElementById("hptxtPY");
var hptxtPC = document.getElementById("hptxtPC");
var winner = document.getElementById("winner");
var PCmulti = document.getElementById("PCmult");
var scoreForm = document.getElementById("scoreForm");
var formVis = document.getElementById("highscoreForm");
var formVisfiller = document.getElementById("formFiller");
console.log("delirdim");
formVis.style.visibility = "hidden";
formVisfiller.style.visibility = "hidden";
var PYsides = 6;
var PCsides = 3;
var PCsidesmulti = 1;
//Random number functions for dices
var dice1 = {
roll: function () {
var randomNumber1 = Math.floor(Math.random() * PYsides) + 1;
return randomNumber1;
}
}
var dice2 = {
sides: PCsides,
roll: function () {
var randomNumber2 = Math.floor(Math.random() * PCsides) + PCsidesmulti;
return randomNumber2;
}
}
//Random number for calculating coin
function wincond(){
coinCalc = (Math.random() * 0.45)+0.20;
}
//Random number for roll animation
function rolltime(min, max){
var rolling = Math.floor(Math.random() * (max-min)+min);
return rolling
}
//Main function for dice number generation and animation
function rollanimation(){
setTimeout(() =>
{
result = dice1.roll();
result2 = dice2.roll();
printNumber(result);
printNumber2(result2);
}, rolltime(700,3000));
}
//Print function for dices
function printNumber(number) {
var placeholder = document.getElementById('placeholder');
placeholder.innerHTML = number;
}
function printNumber2(number2){
var placeholder2 = document.getElementById('placeholder2');
placeholder2.innerHTML = number2;
}
//Highscore request API
function highScoreButton(){
formVis.style.visibility = "hidden";
formVisfiller.style.visibility = "hidden";
highscores.style.visibility = "visible";
var userName = document.getElementById('fname').value;
const data = {userName, score};
const option ={
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
};
fetch('/CLItoSV', option);
console.log(data);
console.log("Gonderdim!");
};
//Functions for disabling buttons(between rounds etc)
function buttonStateOff(){
refillHp.disabled = true;
maxHp.disabled = true;
diceBoost.disabled = true;
refillHp.style.color = "grey";
maxHp.style.color = "grey";
diceBoost.style.color = "grey";
}
function buttonStateOn(){
refillHp.disabled = false;
maxHp.disabled = false;
diceBoost.disabled = false;
refillHp.style.color = "";
maxHp.style.color = "";
diceBoost.style.color = "";
}
//Refresh function for output values
function refreshValues(){
diceCounterPY.innerHTML = PYsides;
diceCounterPC.innerHTML = PCsides;
PCmult.innerHTML = "x"+PCmultipli.toFixed(2);9
document.getElementById("hptxtPY").innerHTML = hpPY.value +" / "+ hpPY.max +" hp";
document.getElementById("hptxtPC").innerHTML = hpPC.value +" / "+ hpPC.max + " hp";
document.getElementById("level").innerHTML = "Level: " + level;
document.getElementById("coin").innerHTML = coin.toFixed(2);
console.log(userName);
scoreForm.innerHTML = score;
}
var coin = 0;
var sonuc = "";
var i = 1;
var level = 1;
var coinCalc = 0;
var PCmultipli = 1;
var score = 0;
var levelReset = 0;
var userName =
hpPY.max = 10;
hpPC.max = 10;
hpPY.value = hpPY.max;
hpPC.value = hpPC.max;
document.getElementById("hptxtPY").innerHTML = hpPY.value +" / "+ hpPY.max +" hp";
document.getElementById("hptxtPC").innerHTML = hpPC.value +" / "+ hpPC.max + " hp";
restart.style.visibility = "hidden";
formVis.style.visibility = "hidden";
formVisfiller.style.visibility = "hidden";
//Main game function for rolling the dices ------------------------------------------------------------------------------------------------------------
rollDice.onclick = function() {
//disables the buttons for round
winner.style.color = "";
rollDice.disabled = true;
rollDice.style.color = "grey";
rollDice.style.fontSize = "29px";
restart.style.visibility = "hidden";
highscores.style.visibility = "hidden";
buttonStateOff();
wincond(coinCalc);
//Duration of the dice roll animation
do{
rollanimation();
i++;
}
while(i < 14);
i=1;
setTimeout(() =>{
var dmg = result-result2;
var wincoin = coinCalc * level + dmg;
var loscoin = coinCalc + 1;
if ( result > result2 ) { //IF PLAYER WINS
sonuc = sonuc + "Player hits " + dmg + " dmg!" + "<br>" + "Earned + " + wincoin.toFixed(2) + " coins!" + "<br>";
hpPC.value = hpPC.value - dmg;
coin = coin + wincoin;
buttonStateOn();
refreshValues();
rollDice.disabled = false;
rollDice.style.color = "";
rollDice.style.fontSize ="";
}
else if( result < result2) { //IF PC WINS
sonuc = sonuc + "Pc hits " + dmg + " dmg!" + "<br>" + "Earned +"+ loscoin.toFixed(2) + "<br>";
hpPY.value = hpPY.value + dmg;
coin = coin + loscoin;
buttonStateOn();
refreshValues();
rollDice.disabled = false;
rollDice.style.color = "";
rollDice.style.fontSize ="";
}
else{
sonuc = sonuc +"Its a tie!" + "<br>" ;
buttonStateOn();
refreshValues();
rollDice.disabled = false;
rollDice.style.color = "";
rollDice.style.fontSize ="";
}
if (hpPY.value <= 0){ //IF PLAYER DIES
winner.style.color = "red";
sonuc ="You Lost!" + "<br>";
restart.style.visibility = "visible";
formVis.style.visibility = "visible";
formVisfiller.style.visibility = "visible";
scoreForm.innerHTML = score;
buttonStateOff();
rollDice.disabled = true;
rollDice.style.color = "grey";
rollDice.style.fontSize = "29px";
}
else if (hpPC.value <= 0){ //IF PC DIES
winner.style.color = "green";
level = level + 1;
var scoreHPmulti = hpPY.max;
if(level > 7){
level = 1;
PCsides = 3;
hpPC.max = 5 + level*5;
hpPY.value = hpPY.max;
hpPC.value = hpPC.max;
score = score + 10 + (levelReset * 5) + (scoreHPmulti * 0.25);
sonuc ="Level reset!" + "<br>" +"Enemy got stronger!"+ "<br>" + "Health regenerated" + "<br>" + "+"+wincoin.toFixed(2) + " earned" + "<br>" + "Combat multiplier increased!" + "<br>" + "Score multiplier increased!" + "<br>";
levelReset = levelReset + 1;
PCmultipli = PCmultipli*1.25;
refreshValues();
}
else{
PCsides = PCsides + 1;
sonuc ="Next level!" + "<br>" +"Enemy got stronger!"+ "<br>" + "Health regenerated" + "<br>" + "+"+wincoin.toFixed(2) + " earned" + "<br>";
hpPC.max = 5 + level * 5;
score = score + levelReset + 1;
buttonStateOn();
hpPY.value = hpPY.max;
hpPC.value = hpPC.max;
refreshValues();
}
}
//Outputing and resetting values
winner.innerHTML = sonuc;
PYsides = 6;
diceCounterPY.style.fontWeight = "100";
diceCounterPC.style.fontWeight = "100";
diceCounterPY.style.color = "";
refreshValues();
winner.scrollTop = winner.scrollHeight;
}, 3100);
//Restroe hp button
refillHp.onclick = function(){
if(coin >= 3 && hpPY.value < hpPY.max){
hpPY.value = hpPY.value + 3;
coin = coin - 3;
refreshValues();
sonuc = sonuc + "Purchase succesfull restoring 3 HP!" + "<br>";
}
else if(hpPY.value == hpPY.max && coin >= 5){
sonuc = sonuc + "Hitpoints are already at max!" + "<br>";
}
else {
sonuc = sonuc + "Not enough coins to spend!" + "<br>"
}
winner.innerHTML = sonuc;
winner.scrollTop = winner.scrollHeight;
}
//Hp upgrade button
maxHp.onclick = function(){
if(coin >= 6){
hpPY.max = hpPY.max + 2;
hpPY.value = hpPY.value + 2;
coin = coin - 6;
refreshValues();
sonuc = sonuc + "Purchase succesfull max HP raised by 2!" + "<br>";
}
else{
sonuc = sonuc + "Not enough coins to spend!" + "<br>";
}
winner.innerHTML = sonuc;
winner.scrollTop = winner.scrollHeight;
}
//Dice side boots button
diceBoost.onclick = function(){
if(coin >= 2 && PYsides < 9){
PYsides = PYsides + 1;
coin = coin - 2;
sonuc = sonuc + "Dice side expaned 1 for one round!" + "<br>";
diceCounterPY.style.fontWeight = "bold";
diceCounterPY.style.color = "rgb(255, 215, 0)";
refreshValues();
}
else if(PYsides >= 9){
sonuc = sonuc + "Dice has reached its capacity" + "<br>";
}
else{
sonuc = sonuc + "Not enough coins to spend!" + "<br>";
}
winner.innerHTML = sonuc;
winner.scrollTop = winner.scrollHeight;
}
};
//Restart button function
restart.onclick = function(){
restart.style.visibility = "hidden";
highscores.style.visibility = "hidden";
formVis.style.visibility = "hidden";
formVisfiller.style.visibility = "hidden";
rollDice.disabled = false;
rollDice.style.color = "";
rollDice.style.fontSize ="";
level = 1;
coin = 0;
hpPC.max = 10;
hpPY.max = 10;
PCsides = 3;
score = 0;
levelReset = 0;
hpPY.value = hpPY.max;
hpPC.value = hpPC.max;
refreshValues();
buttonStateOn();
};