Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
240 lines (214 loc) · 7.85 KB
/
Copy pathapp.js
File metadata and controls
240 lines (214 loc) · 7.85 KB
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
let userBalances = {};
let totalBetAmount = 0;
let commandHistory = []; // Array to store command history
let historyIndex = -1; // Index to track current position in history
// Function to handle keyup event in the command input
document.getElementById('commandInput').addEventListener('keyup', function(event) {
if (event.key === 'Enter') {
executeCommand(); // Execute command on Enter key press
} else if (event.key === 'ArrowUp') {
// Navigate up through command history
if (historyIndex < commandHistory.length - 1) {
historyIndex++;
document.getElementById('commandInput').value = commandHistory[historyIndex];
}
} else if (event.key === 'ArrowDown') {
// Navigate down through command history
if (historyIndex >= 0) {
historyIndex--;
if (historyIndex === -1) {
document.getElementById('commandInput').value = ''; // Clear input when at the end of history
} else {
document.getElementById('commandInput').value = commandHistory[historyIndex];
}
}
}
});
function executeCommand() {
const input = document.getElementById('commandInput').value.trim();
const parts = input.split(' ');
const command = parts[0];
const params = parts.slice(1);
if (input !== '') {
commandHistory.unshift(input); // Add command to history
historyIndex = -1; // Reset history index
}
switch (command) {
case 'reset_bid':
reset_bid(...params.map(param => parseInt(param)));
break;
case 'add':
addpoint(...params.map(param => parseInt(param)));
break;
case 'sub':
subpoint(...params.map(param => parseInt(param)));
break;
case 'setamt':
setamt(...params.map(param => parseInt(param)));
break;
case 'bet':
bet(...params.map(param => parseInt(param)));
break;
case 'betwon':
betwon(...params.map(param => parseInt(param)));
break;
case 'checkbal':
console.log(checkbal(...params.map(param => parseInt(param))));
break;
case 'checkall':
console.log(checkall());
break;
case 'checkbet':
console.log(checkbet());
break;
case 'refree':
console.log(refree());
break;
default:
console.log('Invalid command');
}
document.getElementById('commandInput').value = '';
}
function reset_bid(noofusers, inimon) {
userBalances = {};
totalBetAmount = 0;
const initialMoney = inimon;
for (let i = 1; i <= noofusers; i++) {
userBalances[`user${i}`] = initialMoney;
}
updateProfiles();
}
function addpoint(userno, amt) {
if (userBalances.hasOwnProperty(`user${userno}`)) {
userBalances[`user${userno}`] += amt;
} else {
alert(`User ${userno} does not exist. Cannot add balance.`);
}
updateProfiles();
}
function subpoint(userno, amt) {
if (userBalances.hasOwnProperty(`user${userno}`)) {
userBalances[`user${userno}`] -= amt;
} else {
alert(`User ${userno} does not exist. Cannot subtract balance.`);
}
updateProfiles();
}
function add(userno) {
const inputAmount = parseInt(document.getElementById(`amountInput${userno}`).value);
if (!isNaN(inputAmount) && inputAmount > 0) {
userBalances[`user${userno}`] += inputAmount;
updateProfiles();
} else {
alert('Invalid input amount. Please enter a valid positive number.');
}
}
function sub(userno) {
const inputAmount = parseInt(document.getElementById(`amountInput${userno}`).value);
if (!isNaN(inputAmount) && inputAmount > 0) {
if (userBalances[`user${userno}`] >= inputAmount) {
userBalances[`user${userno}`] -= inputAmount;
updateProfiles();
} else {
alert('Insufficient balance. Cannot subtract more than available balance.');
}
} else {
alert('Invalid input amount. Please enter a valid positive number.');
}
}
function setamt(userno, amt) {
if (userBalances.hasOwnProperty(`user${userno}`)) {
userBalances[`user${userno}`] = amt;
} else {
alert(`User ${userno} does not exist. Cannot set balance.`);
}
updateProfiles();
}
function bet(amt) {
const userCount = Object.keys(userBalances).length;
for (let i = 1; i <= userCount; i++) {
subpoint(i, amt);
}
totalBetAmount += amt * userCount;
updateProfiles()
}
function betwon(userno) {
if (userBalances.hasOwnProperty(`user${userno}`)) {
const winnings = totalBetAmount; // Each user gets the total bet amount as winnings
addpoint(userno, winnings); // Add the winnings to the user's balance
totalBetAmount = 0; // Reset the total bet amount
} else {
alert(`User ${userno} does not exist. Cannot process bet won.`);
}
updateProfiles(); // Update user profiles after processing bet won
}
function checkall() {
return userBalances;
}
function checkbet() {
return totalBetAmount;
}
function refree() {
const userCount = Object.keys(userBalances).length;
let refPocket = 0;
for (let i = 1; i <= userCount; i++) {
refPocket += checkbal(i);
}
refPocket += totalBetAmount;
const supposedValue = userCount * userBalances[`user${1}`]; // Assuming all users have the same initial balance
if (supposedValue === refPocket) {
return true;
} else {
console.error(`Refree check failed. RefPocket: ${refPocket}, SupposedValue: ${supposedValue}`);
return false;
}
}
function addBet() {
const betAmount = parseInt(document.getElementById('betAmount').value);
if (!isNaN(betAmount) && betAmount > 0) {
bet(betAmount); // Call the bet function with the entered amount
console.log(`Bet amount of ${betAmount} added successfully.`);
} else {
alert('Invalid bet amount. Please enter a valid positive number.');
}
}
function updateProfiles() {
const userCount = Object.keys(userBalances).length;
let profileHTML = '';
for (let i = 1; i <= userCount; i++) {
profileHTML += `
<div class="profile">
<div class="userInfo">User ${i}: ${userBalances[`user${i}`]}</div>
<button onclick="wonBet(${i})">Won Bid</button>
<input type="number" id="amountInput${i}" placeholder="Enter amount">
<button onclick="add(${i})">+</button>
<button onclick="sub(${i})">-</button>
</div>
`;
}
document.getElementById('userProfiles').innerHTML = profileHTML;
document.getElementById('totalBetAmount').textContent = totalBetAmount; // Update total bet amount
}
document.getElementById('commandInput').addEventListener('keyup', function(event) {
if (event.key === 'Enter') {
executeCommand();
}
});
function wonBet(userno) {
if (userBalances.hasOwnProperty(`user${userno}`)) {
const winnings = checkbet();
addpoint(userno, winnings);
totalBetAmount = 0;
} else {
alert(`User ${userno} does not exist. Cannot process bet won.`);
}
updateProfiles();
}
// Get the execute button
const executeButton = document.getElementById('executeButton');
reset_bid(4, 10000); // Call to reset_bid with parameters 4 and 10000 on page load
// [[ aux click not working ]]
// executeButton.addEventListener('auxclick', function(event) {
// event.preventDefault();
// document.getElementById('commandInput').value = 'reset_bid 4 10000';
// });