forked from WhitePegasis/Player-Bidder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
339 lines (290 loc) · 9.69 KB
/
script.js
File metadata and controls
339 lines (290 loc) · 9.69 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
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
'use strict'
const playerName1=document.getElementById("player-name1");
const playerName=document.getElementById("player-name2");
const dept=document.getElementById("dept");
const year=document.getElementById("year");
const bidValue=document.getElementById("bid-value");
const bidderName=document.getElementById("bidder-name");
const bidButton=document.getElementById("bid-button");
const newBidValue=document.getElementById("new-bid-value");
const newBidderName=document.getElementById("new-bidder-name");
const resetBtn=document.getElementById("reset");
const startBtn=document.getElementById("start");
const submitBtn=document.getElementById("submit");
const skillSpeciality=document.getElementById("speciality");
const skillWk=document.getElementById("skill-wk");
const playerImage = document.getElementById("player-image");
const leftColumn=document.getElementById("left-column");
const rightColumn=document.getElementById("right-column");
const soldDiv=document.getElementById("sold-div");
const footer=document.getElementById("footer");
const soldDetail=document.getElementById("sold-detail");
const gotoNextBidBtn= document.getElementById("goto-next-bid");
const teamNames=["RR","CSK","KKR","DC","RCB","SRH"];
const maxPoint=1700; //maximum bid value
//fetching data from sheet1
let playerJsonData;
let sNo;
const getData = async () => { // input sheet: https://docs.google.com/spreadsheets/d/1YbGjVNyBVm14jz-FsrWaziq7EuRvUoX8a8PFuGkaLJo/edit?usp=sharing
try {
const res = await fetch( //sheet link (from sheet.best)
"https://sheet.best/api/sheets/4af5b600-38ab-40df-8f16-0e1a652b3f86"
);
playerJsonData = await res.json();
console.log(playerJsonData); // player data
} catch (error) {
console.log(error);
}
};
getData();
//Function for Selecting random player from the list
let currentPlayerName="";
let currentPlayerDept="";
let currentPlayerYear="";
let currentPlayerTeam="";
let currentPlayerPoints="";
function randomPlayer(){
sNo=Math.floor(Math.random() * playerJsonData.length); //generating random number between 0 to length of data
currentPlayerName=playerJsonData[sNo].Name;
currentPlayerDept=playerJsonData[sNo].Dept;
currentPlayerYear=playerJsonData[sNo].Year;
playerImage.src="https://drive.google.com/uc?id="+playerJsonData[sNo].Photo;
//changing player name
playerName1.innerHTML=`${currentPlayerName}`;
playerName.innerHTML=`<i class="fa fa-user fa-fw w3-margin-right w3-xxlarge w3-text-teal"></i>
${currentPlayerName}`;
//changing player dept and year
dept.innerHTML=`${currentPlayerDept}`;
year.innerHTML=`${currentPlayerYear}`;
skillSpeciality.innerText=playerJsonData[sNo].Speciality;
if(playerJsonData[sNo].WK=="no"){
skillWk.style.display="none";
}
else{
skillWk.style.display="block";
}
}
function displayNextPlayer(){
if(confirm("Start Bidding ?")==true){
startBtn.innerText=`Next Player`;
bidValue.innerHTML="0000";
bidderName.innerHTML="Unsold";
newBidValue.value="";
newBidderName.value="select";
playerImage.src="default-image.png";
randomPlayer();
}
}
//Selecting random player from the list
startBtn.addEventListener("click",displayNextPlayer);
//Updating player bid values
let bidderIndex;
bidButton.addEventListener("click",()=>{
const val=newBidValue.value;
const currIdx=parseInt(newBidderName.value);
const bidder=teamNames[currIdx];
//const bidder=newBidderName.value;
const currPoint=parseInt(teamPointArr[currIdx])+parseInt(val);
if(val==''){
alert('Bid Value Field Empty!')
}
else if(bidder=='select'){
alert('Select the Bidder!')
}
else if(parseInt(val)<parseInt(bidValue.innerText)){
alert("Current bid value less than previous bid!");
}
else if(currPoint > maxPoint){
console.log(teamPointArr[currIdx],val,currIdx);
alert("Not enough point!")
}
else{
bidderIndex=parseInt(newBidderName.value);
//console.log(bidderIndex);
bidValue.innerHTML=val;
bidderName.innerHTML=bidder;
newBidValue.value="";
newBidderName.value="select";
}
});
//reset player bid values
resetBtn.addEventListener("click",()=>{
if(confirm("Are you sure you want to reset?")){
bidValue.innerHTML="0000";
bidderName.innerHTML="Unsold";
newBidValue.value="";
newBidderName.value="select";
}
});
//Submit the data i.e delete from sheet 1 and add to sheet 2 with bid values
submitBtn.addEventListener("click",()=>{
//let choice = prompt("Are you sure you want to submit?");
if(confirm("Are you sure you want to submit?")==true){
handleSubmit();
}
});
//Function to Add data of sold player to new sheet
const data={
"Name":"",
"Dept":"",
"Year":"",
"Team":"",
"Points":""
}
const addData=()=>{
// adding sold player data to new sheet
console.log("Data to add:", data);
fetch("https://sheet.best/api/sheets/fb70e40a-5f93-4c7d-8bb5-536bb7d3a364", {
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
})
.then((r) => r.json())
.then((data) => {
// The response comes here
console.log("Added data",data);
console.log("Added data to https://docs.google.com/spreadsheets/d/1Gr6PnoMYJn0dQHQ0nPuLSg5xIsXL0qxgZx7sNKPDOIU/edit?usp=sharing" );
})
.catch((error) => {
// Errors are reported there
console.log(error);
});
}
//Function to delete row from sheet
const deleteRow=()=>{ //input sheet: https://docs.google.com/spreadsheets/d/1YbGjVNyBVm14jz-FsrWaziq7EuRvUoX8a8PFuGkaLJo/edit?usp=sharing
fetch(`https://sheet.best/api/sheets/4af5b600-38ab-40df-8f16-0e1a652b3f86/${sNo}`, { //change last didgit to manipulate the row you want to delete
method: "DELETE",
})
.then((response) => response.json())
.then((data) => {
console.log("deleted data:",data);
console.log("Deleted from https://docs.google.com/spreadsheets/d/1YbGjVNyBVm14jz-FsrWaziq7EuRvUoX8a8PFuGkaLJo/edit?usp=sharing");
})
.catch((error) => {
console.error(error);
});
}
/*
const handleSubmit = async (e) => {
addData();
//e.preventDefault();
try {
const res = await fetch(
"https://sheet.best/api/sheets/b874102e-68f4-4005-9ef6-e9bbb10b316a",
{
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
}
);
if (res.ok) {
console.log("done");
history.replace("/");
}
} catch (error) {
console.log("HIHIHIIII");
console.log(error);
}
};*/
//Function to submit final data
const handleSubmit = async (e) => {
data.Name=currentPlayerName;
data.Dept=currentPlayerDept;
data.Year=currentPlayerYear;
if(currentPlayerName==""){
alert("Bidding is not started yet");
}
else{
if(bidValue.innerText==="0000")
{
data.Team="UNSOLD";
data.Points="UNSOLD";
deleteRow();
addData();
console.log("Unsold");
}
else{
data.Team=bidderName.innerText;
data.Points=bidValue.innerText;
addData();
deleteRow();
const newPoints=parseInt(data.Points)+parseInt(teamPointArr[bidderIndex]);
console.log("newpoints:",newPoints ," arr[i]: ",teamPointArr[bidderIndex]);
teamPointArr[bidderIndex]=parseInt(newPoints);
updateTeamPointSheet(data.Team,newPoints); //update points sheet
}
leftColumn.style.display="none";
rightColumn.style.display="none";
soldDiv.style.display="block";
footer.classList.add("footer-align");
if(bidValue.innerText=="0000"){
soldDetail.innerHTML=`${currentPlayerName} unsold`;
}
else{
soldDetail.innerHTML=`${currentPlayerName} sold to ${bidderName.innerText} for ${bidValue.innerText} points`;
}
}
}
gotoNextBidBtn.addEventListener("click",()=>{
leftColumn.style.display="block";
rightColumn.style.display="block";
soldDiv.style.display="none";
footer.classList.remove("footer-align");
displayNextPlayer();
});
/*
0 = rr
1 = csk
2 = kkr
3 = dc
4 = rcb
5 = srh
*/
//fetching points from point sheet
let teamPointsJson;
let teamPointArr=[];
const getCurrentPoints = async () => {
try {
const res = await fetch( //sheet link (from sheet.best) // team point sheet: https://sheet.best/api/sheets/1d7eb585-89cf-4925-b18a-2681b62e70d1
"https://sheet.best/api/sheets/1d7eb585-89cf-4925-b18a-2681b62e70d1"
);
teamPointsJson = await res.json();
console.log(teamPointsJson); // player data
console.log("size:"+teamPointsJson.length);
teamPointArr.push(teamPointsJson[0].total);
teamPointArr.push(teamPointsJson[1].total);
teamPointArr.push(teamPointsJson[2].total);
teamPointArr.push(teamPointsJson[3].total);
teamPointArr.push(teamPointsJson[4].total);
teamPointArr.push(teamPointsJson[5].total);
console.log(teamPointArr);
//setData(Object.keys(data).map((key) => data[key]));
} catch (error) {
console.log(error);
}
};
getCurrentPoints();
const updateTeamPointSheet=(teamName,newPoint)=>{
fetch(
`https://sheet.best/api/sheets/1d7eb585-89cf-4925-b18a-2681b62e70d1/team/*${teamName}*`,
{
method: "PATCH",
mode: "cors",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
total: `${newPoint}`,
}),
}
)
.then((r) => r.json())
.then(console.log)
.catch(console.error);
}