-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbank.js
342 lines (306 loc) · 11.8 KB
/
bank.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
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.9.3/firebase-app.js";
import {
getDatabase,
ref,
set,
onValue,
child,
push,
update,
get,
} from "https://www.gstatic.com/firebasejs/9.9.3/firebase-database.js";
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyDCdFQAJcAQqHi3d_ajETfWYZjzJNX1lZc",
authDomain: "covitrade-1ac29.firebaseapp.com",
databaseURL:
"https://covitrade-1ac29-default-rtdb.asia-southeast1.firebasedatabase.app",
projectId: "covitrade-1ac29",
storageBucket: "covitrade-1ac29.appspot.com",
messagingSenderId: "1066677743414",
appId: "1:1066677743414:web:f6f176b4eee00168700d97",
measurementId: "G-D166FP41MX",
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Realtime Database and get a reference to the service
const database = getDatabase(app);
const country_list = ["CHINA", "INDIA", "USA", "INDONESIA","BRAZIL","RUSSIA","JAPAN","TURKEY","SOUTHKOREA","FRANCE","UK","SPAIN","POLAND","UAE","AUSTRALIA","NETHERLANDS","SWITZERLAND","ITALY"];
const testcountry_list = ["BRAZIL","CHINA","INDIA","INDONESIA","POLAND","TURKEY"];
var transactionlist = [];
const prop_list = ["POPULATION","INFECTED","I_CHILD","I_ADULT","I_OLD","TAXSTAR","PCM","COF","VITC","CEF","AZE","LEVO","MONEY","C1","C2","C3","C4","C5","C6"];
var table = document.getElementsByTagName("table")[0];
var row1 = table.getElementsByTagName("tr")[1];
var row2 = table.getElementsByTagName("tr")[2];
var row3 = table.getElementsByTagName("tr")[3];
function snapshotToArray(snapshot) {
var returnArr = [];
snapshot.forEach(function(childSnapshot) {
var item = childSnapshot.val();
item.key = childSnapshot.key;
returnArr.push(item);
});
return returnArr;
};
function show_info(item,index)
{
var row = table.insertRow();
row.id = item;
var cell = row.insertCell();
cell.innerHTML = item;
var prop = item;
country_list.forEach
(
function (item,index)
{
if(cell.childNodes[0])
{
console.log("check");
}
var c = item;
item += "/" + prop;
get(child(ref(database), `${item}`)) .then((snapshot)=>{
cell = row.insertCell();
cell.classList=[c];
var node = document.createTextNode(snapshot.val());
cell.appendChild(node);
})
}
)
}
// onValue(ref(database,`AUSTRALIA/`),(golgol)=>
// {
// var top = table.insertRow();
// var cell = top.insertCell();
// cell.innerHTML = "PROPERTY";
// top.id = "property";
// for(var property in golgol.val())
// {
// cell = top.insertCell();
// cell.innerHTML=property;
// }
// })
// onValue(ref(database,`/`),(snapshot)=>{
// for(var country in snapshot.val())
// {
// if(country == 'Transaction')continue;
// var pat = country + "/";
// var row = table.insertRow();
// row.id = country;
// var head = row.insertCell();
// head.innerHTML = country;
// head.classList = ["head"];
// onValue(ref(database,`${pat}`),(golgol)=>
// {
// for(var property in golgol.val())
// {
// var cell = row.insertCell();
// cell.classList = [property];
// pat = country + "/" + property + "/";
// onValue(ref(database,`${pat}`),(golgolgol)=>{
// cell.innerHTML = golgolgol.val();
// })
// }
// })
// }
// })
prop_list.forEach(show_info);
var i = 1;
var button = document.getElementById("transaction");
button.onclick = function transaction() {
var seller;
var buyer;
var resource;
var amt;
var rate;
var buyer_money;
var seller_money;
buyer = prompt("buyer");
seller = prompt("seller");
resource = prompt("resource");
amt = parseFloat(prompt("amount"));
rate = parseFloat(prompt("rates"));
var data = { BUYER: buyer, SELLER: seller, RESOURCE: resource, AMOUNT: amt,RATE: rate};
const transaction_list = ref(database, "Transaction/");
const add_transaction = push(transaction_list);
var key_transaction = add_transaction.key;
transactionlist.push(key_transaction);
set(add_transaction, data);
var prop = document.getElementById(resource);
var mon = document.getElementById("MONEY");
var count_buy = prop.getElementsByClassName(buyer)[0];
var count_seller = prop.getElementsByClassName(seller)[0];
var mon_buy = mon.getElementsByClassName(buyer)[0];
var mon_sell = mon.getElementsByClassName(seller)[0];
buyer += "/";
seller += "/";
var taxstar=seller + "TAXSTAR";
var buyer_country_money = buyer + "MONEY";
var seller_country_money = seller + "MONEY";
get(child(ref(database),`${taxstar}`)).then((snapshot)=>{
var tax = parseInt(snapshot.val());
get(child(ref(database),`${buyer_country_money}`)).then((snapshot)=>{
buyer_money = parseFloat(snapshot.val());
buyer_money -= rate*(1+(7-tax)*0.01);
buyer_money = buyer_money.toFixed(2);
mon_buy.innerHTML = buyer_money;
const upda = {};
upda["/" + buyer_country_money] = buyer_money;
update(ref(database),upda);
})
})
get(child(ref(database),`${seller_country_money}`)).then((snapshot)=>{
seller_money = parseFloat(snapshot.val());
seller_money += rate;
seller_money = seller_money.toFixed(2);
mon_sell.innerHTML = seller_money;
const upda = {};
upda["/" + seller_country_money] = seller_money;
update(ref(database),upda);
})
buyer += resource;
seller += resource;
var buy;
var sell;
var updated = false;
get(child(ref(database), `${buyer}`))
.then((snapshot) => {
if (snapshot.exists()) {
buy = parseFloat(snapshot.val());
buy += amt;
buy = buy.toFixed(2);
const updates = {};
updates["/" + buyer] = buy;
count_buy.innerHTML = buy;
update(ref(database), updates)
.then(() => {
updated = true;
})
.catch((error) => {
updated = false;
});
} else {
console.log("No data available");
}
})
.catch((error) => {
console.error(error);
});
updated = false;
get(child(ref(database), `${seller}`))
.then((snapshot) => {
if (snapshot.exists()) {
sell = parseFloat(snapshot.val());
sell -= amt;
sell = sell.toFixed(2);
const updatess = {};
updatess["/" + seller] = sell;
count_seller.innerHTML = sell;
update(ref(database), updatess)
.then(() => {
updated = true;
})
.catch((error) => {
updated = false;
});
} else {
console.log("No data available");
}
})
.catch((error) => {
console.error(error);
});
};
var reset = document.getElementById("reset");
reset.onclick = function reset()
{
get(child(ref(database),`Transaction/`))
.then ((snapshot)=>{
var last_transactionarr = snapshotToArray(snapshot);
var last_transaction = last_transactionarr.pop();
var tax;
var key = last_transaction.key;
get(child(ref(database),`${last_transaction.SELLER}/TAXSTAR`)).then((snapshot)=>{
tax = snapshot.val();
})
get(child(ref(database),`${last_transaction.BUYER}`)).then((snapshot)=>
{
get(child(ref(database),`${last_transaction.BUYER}/${last_transaction.RESOURCE}`))
.then((snapshot)=>{
var prev = parseFloat(snapshot.val());
var res = document.getElementById(last_transaction.RESOURCE);
var cont = res.getElementsByClassName(last_transaction.BUYER)[0];
prev -= parseFloat(last_transaction.AMOUNT);
const updates = {};
updates[`${last_transaction.BUYER}/${last_transaction.RESOURCE}`] = prev;
update(ref(database),updates);
cont.innerHTML = prev;
})
get(child(ref(database),`${last_transaction.BUYER}/MONEY`)).then((snapshot)=>{
var prev = parseFloat(snapshot.val());
var rate = last_transaction.RATE;
var res = document.getElementById("MONEY");
var cont = res.getElementsByClassName(last_transaction.BUYER)[0];
prev += parseFloat(rate*(1+(7-tax)*0.01));
const updates ={};
updates[`${last_transaction.BUYER}/MONEY`] = prev;
update(ref(database),updates);
cont.innerHTML = prev;
})
}
)
get(child(ref(database),`${last_transaction.SELLER}`)).then((snapshot)=>
{
get(child(ref(database),`${last_transaction.SELLER}/${last_transaction.RESOURCE}`))
.then((snapshot)=>{
var prev = parseFloat(snapshot.val());
var res = document.getElementById(last_transaction.RESOURCE);
var cont = res.getElementsByClassName(last_transaction.SELLER)[0];
prev += parseFloat(last_transaction.AMOUNT);
const updates = {};
updates[`${last_transaction.SELLER}/${last_transaction.RESOURCE}`] = prev;
update(ref(database),updates);
cont.innerHTML = prev;
})
get(child(ref(database),`${last_transaction.SELLER}/MONEY`)).then((snapshot)=>{
var prev = parseFloat(snapshot.val());
var rate = parseFloat(last_transaction.RATE);
var res = document.getElementById("MONEY");
var cont = res.getElementsByClassName(last_transaction.SELLER)[0];
prev -= parseFloat(rate);
const updates ={};
updates[`${last_transaction.SELLER}/MONEY`] = prev;
update(ref(database),updates);
cont.innerHTML = prev;
})
}
)
const trans = {AMOUNT: null,BUYER: null,RATE: null,RESOURCE: null,SELLER: null};
const updates = {};
updates[`Transaction/${last_transaction.key}`] = trans;
update(ref(database),updates);
})
}
var history = document.getElementById("history");
history.onclick = function ()
{
var table2 = document.getElementsByTagName("table")[1];
onValue(ref(database,"Transaction/"),(snapshot) =>{
for(var key in snapshot.val())
{
var path1 = "Transaction/"+key;
var row = table2.insertRow();
onValue(ref(database,path1),
(snapshot)=>{
for(var data in snapshot.val())
{
var cell = row.insertCell();
onValue(ref(database,`Transaction/${key}/${data}`),
(snapshot)=>{
cell.innerHTML = snapshot.val();
});
}
});
}
})
};