generated from EnclaveGames/Enclave-Phaser-Template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
betting.html
500 lines (460 loc) · 19 KB
/
betting.html
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NBA Fantasy Betting</title>
<style>
body {
font-family: 'Roboto', sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
color: #333;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
max-width: 900px;
margin: auto;
}
h1 {
text-align: center;
color: #4CAF50;
margin-bottom: 30px;
}
h2 {
text-align: center;
color: #333;
}
.game, .bet-log {
margin: 20px 0;
padding: 15px;
background-color: #e9e9e9;
border-radius: 8px;
}
.bet-button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
margin: 5px;
transition: background-color 0.3s;
}
.bet-button:hover {
background-color: #45a049;
}
.cash-display {
text-align: center;
margin-top: 20px;
font-size: 1.2em;
}
form {
text-align: center;
margin-bottom: 20px;
}
input[type="text"], input[type="number"] {
padding: 10px;
margin: 10px;
border: 1px solid #ddd;
border-radius: 5px;
width: calc(100% - 44px);
max-width: 400px;
box-sizing: border-box;
}
button[type="button"] {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button[type="button"]:hover {
background-color: #45a049;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
padding: 10px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
cursor: pointer;
}
footer {
text-align: center;
padding: 20px;
background-color: #4CAF50;
color: white;
border-radius: 10px;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>NBA Fantasy Betting</h1>
<div class="cash-display">Cash Remaining: <span id="cashAmount">$500</span></div>
<div id="playerInfo" style="display: none;">
<!-- Display player information and statistics here -->
</div>
<form id="searchForm">
<label for="playerName">Enter Player Name:</label>
<input type="text" id="playerName" name="playerName" required>
<button type="button" onclick="searchPlayerByName()">Search</button>
</form>
<div class="game" id="game" style="display: none;">
<h3>Player Fantasy Points Betting</h3>
<label for="betAmount">Bet Amount: </label>
<input type="number" id="betAmount" name="betAmount" min="1" max="500" required>
<button class="bet-button" onclick="placeBet('over')">Over Predicted Fantasy Points</button>
<button class="bet-button" onclick="placeBet('under')">Under Predicted Fantasy Points</button>
</div>
<div class="bet-log" id="betLog">
<h2>Betting Log</h2>
<label for="sortAlgorithm">Select Sorting Algorithm:</label>
<select id="sortAlgorithm">
<option value="bubble">Bubble Sort</option>
<option value="selection">Selection Sort</option>
<option value="insertion">Insertion Sort</option>
<option value="merge">Merge Sort</option>
<option value="quick">Quick Sort</option>
</select>
<table>
<thead>
<tr>
<th onclick="sortTable(0)">Player ID</th>
<th onclick="sortTable(1)">Bet Amount</th>
<th onclick="sortTable(2)">Bet Option</th>
<th onclick="sortTable(3)">Predicted Points</th>
<th onclick="sortTable(4)">Actual Points</th>
<th onclick="sortTable(5)">Outcome</th>
</tr>
</thead>
<tbody id="betLogBody">
<!-- Log entries will be added here -->
</tbody>
</table>
</div>
</div>
<footer>
Designed by Raunak Mondal
</footer>
<script>
async function searchPlayerByName() {
var playerName = document.getElementById("playerName").value.trim().toLowerCase();
var playerId = 1; // Start with the first player ID
var found = false;
while (!found) {
try {
const response = await fetch(`http://localhost:6942/api/nba/players/${playerId}`);
if (!response.ok) {
throw new Error('Player not found');
}
const data = await response.json();
var retrievedPlayerName = `${data.data.first_name} ${data.data.last_name}`.toLowerCase();
if (retrievedPlayerName === playerName) {
found = true;
displayPlayerInfo(data.data);
document.getElementById("game").style.display = "block";
} else {
playerId++;
}
} catch (error) {
console.error('Error:', error);
playerId++;
}
}
if (!found) {
alert('Player not found. Please try again.');
}
}
function displayPlayerInfo(playerInfo) {
var playerInfoContainer = document.getElementById("playerInfo");
// Calculate fantasy points based on height and weight
var fantasyPointsPredicted = calculateFantasyPoints(playerInfo.height, playerInfo.weight);
fantasyPointsPredicted=37.5;
// Extract relevant information from the provided structure
var playerName = `${playerInfo.first_name} ${playerInfo.last_name}`;
var position = playerInfo.position || 'N/A';
var height = playerInfo.height || 'N/A';
var weight = playerInfo.weight || 'N/A';
var teamName = playerInfo.team ? playerInfo.team.full_name : 'Unknown Team';
// Display player information and characteristics
var tableHtml = `
<h2>Player Information</h2>
<table>
<tr>
<th>Name</th>
<th>Position</th>
<th>Height</th>
<th>Weight</th>
<th>Team</th>
<th>Predicted Fantasy Points</th>
</tr>
<tr>
<td>${playerName}</td>
<td>${position}</td>
<td>${height}</td>
<td>${weight} lbs</td>
<td>${teamName}</td>
<td>${fantasyPointsPredicted}</td>
</tr>
</table>
`;
playerInfoContainer.innerHTML = tableHtml;
playerInfoContainer.style.display = "block";
}
function calculateFantasyPoints(height, weight) {
// Default height and weight if undefined or null
if (!height) height = "6-0"; // Default height
if (!weight) weight = "200"; // Default weight
// Split height string into feet and inches
var heightParts = height.split("-");
var feet = parseInt(heightParts[0], 10) || 0; // Use 0 if feet is not a valid number
var inches = parseInt(heightParts[1], 10) || 0; // Use 0 if inches is not a valid number
// Convert height to inches
var totalHeightInches = feet * 12 + inches;
// Parse weight to integer
var totalWeight = parseInt(weight, 10) || 0; // Use 0 if weight is not a valid number
// Example calculation (you can adjust this formula)
var heightFactor = totalHeightInches / 12; // Convert height to feet
var weightFactor = totalWeight / 200; // Normalize weight
var fantasyPoints = Math.round((heightFactor + weightFactor) * 10);
fantasyPoints = fantasyPoints / 2; // Example calculation
return fantasyPoints > 0 ? fantasyPoints : 1; // Ensure fantasy points are not zero
}
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
async function placeBet(betOption) {
try {
var playerId = 1; // This is a placeholder, replace with actual player ID retrieval logic
const response = await fetch(`http://localhost:6942/api/nba/players/${playerId}`);
const data = await response.json();
var fantasyPointsPredicted = calculateFantasyPoints(data.data.height, data.data.weight);
var fantasyPointsActual = calculateActualFantasyPoints(data.data.stats);
let cash = parseInt(document.getElementById('cashAmount').textContent.replace('$', ''), 10);
let betAmountElement = document.getElementById('betAmount');
if (betAmountElement && betAmountElement.value) {
let betAmount = parseInt(betAmountElement.value, 10);
if (cash >= betAmount) {
cash -= betAmount;
document.getElementById('cashAmount').textContent = `$${cash}`;
let outcome;
if ((betOption === 'over' && fantasyPointsActual > fantasyPointsPredicted) ||
(betOption === 'under' && fantasyPointsActual < fantasyPointsPredicted)) {
cash += betAmount * 2; // Win double the bet amount
document.getElementById('cashAmount').textContent = `$${cash}`;
outcome = `Win $${betAmount}`;
} else {
outcome = `Lose $${betAmount}`;
}
// Log the bet
logBet(playerId, betAmount, betOption, fantasyPointsPredicted, fantasyPointsActual, outcome);
alert(`You ${outcome}. Actual fantasy points: ${fantasyPointsActual}. Remaining cash: $${cash}`);
} else {
throw new Error('Insufficient cash to place this bet.');
}
} else {
throw new Error('Bet amount is invalid.');
}
} catch (error) {
console.error('Error:', error.message);
alert(error.message);
}
}
function calculateActualFantasyPoints(stats) {
if (!stats) {
return getRandomNumber(20, 60); // Return a random number if stats are not available
}
// Example calculation based on stats
const points = stats.points || 0;
const rebounds = stats.rebounds || 0;
const assists = stats.assists || 0;
const steals = stats.steals || 0;
const blocks = stats.blocks || 0;
const turnovers = stats.turnovers || 0;
// Example fantasy points calculation
return points + rebounds * 1.2 + assists * 1.5 + steals * 3 + blocks * 3 - turnovers * 2;
}
function logBet(playerId, betAmount, betOption, predictedPoints, actualPoints, outcome) {
var betLogBody = document.getElementById("betLogBody");
var newRow = document.createElement("tr");
newRow.innerHTML = `
<td>${playerId}</td>
<td>${betAmount}</td>
<td>${betOption}</td>
<td>${predictedPoints}</td>
<td>${actualPoints}</td>
<td>${outcome}</td>
`;
betLogBody.appendChild(newRow);
}
function sortTable(columnIndex) {
var table = document.querySelector(".bet-log table tbody");
var rows = Array.from(table.rows);
var sortedRows;
var algorithm = document.getElementById("sortAlgorithm").value;
function bubbleSort(arr) {
let n = arr.length;
let i = 0;
let j = 0;
function bubble() {
if (i < n - 1) {
if (j < n - i - 1) {
if (compare(arr[j], arr[j + 1])) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
j++;
setTimeout(bubble, 100); // Adjust the delay here (in milliseconds)
} else {
i++;
j = 0;
setTimeout(bubble, 0); // No delay between iterations
}
} else {
displaySortedRows(arr);
}
}
bubble();
}
function selectionSort(arr) {
let n = arr.length;
let i = 0;
function selection() {
if (i < n - 1) {
let minIdx = i;
let j = i + 1;
while (j < n) {
if (compare(arr[minIdx], arr[j])) {
minIdx = j;
}
j++;
}
[arr[minIdx], arr[i]] = [arr[i], arr[minIdx]];
i++;
setTimeout(selection, 100); // Adjust the delay here (in milliseconds)
} else {
displaySortedRows(arr);
}
}
selection();
}
function insertionSort(arr) {
let n = arr.length;
let i = 1;
function insertion() {
if (i < n) {
let key = arr[i];
let j = i - 1;
while (j >= 0 && compare(arr[j], key)) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
i++;
setTimeout(insertion, 100); // Adjust the delay here (in milliseconds)
} else {
displaySortedRows(arr);
}
}
insertion();
}
function mergeSort(arr) {
function merge(left, right) {
let result = [];
while (left.length && right.length) {
if (compare(left[0], right[0])) {
result.push(right.shift());
} else {
result.push(left.shift());
}
}
return result.concat(left, right);
}
function mergeSortHelper(arr) {
if (arr.length <= 1) {
return arr;
}
const mid = Math.floor(arr.length / 2);
const left = mergeSortHelper(arr.slice(0, mid));
const right = mergeSortHelper(arr.slice(mid));
return merge(left, right);
}
let sorted = mergeSortHelper(arr);
setTimeout(() => {
sortedRows = sorted;
displaySortedRows(sorted);
}, 100); // Adjust the delay here (in milliseconds)
}
function quickSort(arr) {
function quickSortHelper(arr, left, right) {
if (left < right) {
const pivot = arr[right];
let i = left - 1;
for (let j = left; j <= right - 1; j++) {
if (compare(arr[j], pivot)) {
i++;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
[arr[i + 1], arr[right]] = [arr[right], arr[i + 1]];
const partitionIndex = i + 1;
quickSortHelper(arr, left, partitionIndex - 1);
quickSortHelper(arr, partitionIndex + 1, right);
}
}
quickSortHelper(arr, 0, arr.length - 1);
setTimeout(() => {
sortedRows = arr;
displaySortedRows(arr);
}, 100); // Adjust the delay here (in milliseconds)
}
function compare(rowA, rowB) {
if (columnIndex === 0 || columnIndex === 1 || columnIndex === 3 || columnIndex === 4) {
return parseFloat(rowA.cells[columnIndex].innerText) > parseFloat(rowB.cells[columnIndex].innerText);
} else {
return rowA.cells[columnIndex].innerText.localeCompare(rowB.cells[columnIndex].innerText) > 0;
}
}
function displaySortedRows(sortedRows) {
// Remove existing rows
while (table.firstChild) {
table.removeChild(table.firstChild);
}
// Append sorted rows
sortedRows.forEach(row => table.appendChild(row));
}
switch (algorithm) {
case 'bubble':
bubbleSort(rows);
break;
case 'selection':
selectionSort(rows);
break;
case 'insertion':
insertionSort(rows);
break;
case 'merge':
mergeSort(rows);
break;
case 'quick':
quickSort(rows);
break;
}
}
</script>
</body>
</html>