forked from Adhyyan1252/Pokernow-Legder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
397 lines (339 loc) · 9.5 KB
/
content.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
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
//entry point
var checkExist = setInterval(function() {
var log_button = document.getElementsByClassName("button-1 show-log-button small-button dark-gray");
if (log_button.length) {
log_button[0].addEventListener("click", buttonClicked, false);
console.log("EXISTS");
clearInterval(checkExist);
}
}, 100); // check every 100ms
function buttonClicked(){
setTimeout(function(){
//add the button
console.log("BUTTON CLICKED");
var footer = document.querySelector(".modal-footer").children[0];
var button = document.createElement('button');
console.log(footer);
button.type = "button";
button.className = "button-1 green small-button";
button.innerHTML = "Ledger";
var space = document.createTextNode( '\u00A0' )
footer.append(space);
footer.append(button);
button.addEventListener("click", displayLedger, false);
}, 10)
//add the button the modal thing
}
function displayLedger(){
let b = document.getElementsByClassName("modal-body")[0]
document.getElementsByClassName("modal log-modal")[0].removeChild(document.getElementsByClassName("modal-footer")[0]);
b.innerHTML = ""
var p1 = document.createElement("p");
p1.innerHTML = "Loading Ledger...";
b.append(p1);
getLog();
}
var log = [];
function getLog(lastTime, minTime){
if(minTime == undefined)
log = [];
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(xhttp.readyState === XMLHttpRequest.DONE) {
rjs = JSON.parse(this.responseText);
for(var i = 0; i < rjs.logs.length; i++){
var row = rjs.logs[i];
log.push(row.msg.slice(0, row.msg.length-1));
}
if(minTime == undefined){
minTime = rjs['infos']['min'];
}
lastTime = rjs.logs[rjs.logs.length-1]['created_at'];
if(lastTime > minTime){
getLog(lastTime, minTime);
}else{
log = log.reverse();
process(log, false, 1);
}
}
}
if(minTime == undefined){
xhttp.open("GET", window.location.href + "/log?after_at=&before_at=&mm=true");
}else{
xhttp.open("GET", window.location.href + "/log?after_at=&before_at=" + lastTime + "&mm=true");
}
xhttp.send();
}
class User{
constructor(){
this.buyin = 0;
this.buyout = 0;
this.names = new Set();
this.id = new Set();
this.quit = true;
this.current = 0;
}
net(){
return this.buyout - this.buyin + this.current;
}
get_names(){
let ret = "";
for(let name of this.names){
ret += name + "; ";
}
return ret.slice(0, ret.length-2);
}
get_one_name(){
for(let name of this.names){
return name;
}
}
get_ids(){
let ret = "";
for(let name of this.id){
ret += name + "; ";
}
return ret.slice(0, ret.length-2);
}
}
function get_user(user_id, name, users, merge=true){
if(user_id in users){
users[user_id].names.add(name);
users[user_id].id.add(user_id);
return users[user_id];
}else if(merge == true){
for(var uid in users){
if(users[uid].names.has(name) || users[uid].id.has(user_id)){
users[uid].names.add(name);
users[uid].id.add(user_id);
return users[uid];
}
}
}
users[user_id] = new User();
users[user_id].names.add(name);
users[user_id].id.add(user_id);
return users[user_id];
}
function get_user_by_name(name, users){
for(var uid in users){
if(users[uid].names.has(name)){
return users[uid];
}
}
return undefined;
}
function process(log, verbose=false, multiplier=1){
if(verbose) console.log(log);
var users = {};
var sum = 0;
for(let ent of log){
filter = ['WARNING', 'sit', 'stand', 'ownership']
words = ent.split(" ");
var found = false;
for(let word of words){
if(filter.includes(word)){
found = true;
break;
}
}
if(found){
continue;
}
if(!(ent.indexOf("player") == -1 && ent.indexOf("admin") == -1)){
var name_and_id = ent.split("\"")[1];
var name = name_and_id.split("@")[0];
var id = name_and_id.split("@")[1];
name = name.trim();
id = id.trim();
var user = get_user(id, name, users);
if(ent.indexOf("approved") != -1 || ent.indexOf("created") != -1){
var value = parseInt(words[words.length-1], 10);
if(user.buyout == value){
sum += value;
if(verbose){
console.log("REJOIN " + name + " : " + value);
}
user.buyout = 0;
}else{
sum += value;
if(verbose){
console.log("JOIN " + name + " : " + value);
}
user.buyin += value;
}
user.quit = false;
}else if(ent.indexOf("quits") != -1){
var value = parseInt(words[words.length-1], 10);
sum -= value;
if(verbose){
console.log("QUIT: " + name + " : " + value);
}
user.buyout += value;
user.quit = true;
}else if(ent.indexOf("updated") != -1){
var new_value = parseInt(words[words.length-1], 10);
var old_value = parseInt(words[words.length-3], 10);
if(verbose){
console.log("UPDATE " + name + " : " + old_value + " -> " + new_value);
}
sum += new_value - old_value;
if(old_value - new_value <= user.buyin){
user.buyin += new_value - old_value;
}else{
user.buyout += old_value - new_value;
}
user.quit = false;
}
}
}
var names = document.getElementsByClassName("table-player-name");
var stacks = document.getElementsByClassName("table-player-stack");
if(names.length == stacks.length){
for(let i = 0; i < names.length; i++){
let curn = names[i].innerText;
let curv = 0;
var numb = stacks[i].innerText.match(/\d+/g);
for(let c of numb) curv += parseInt(c, 10);
var u = get_user_by_name(curn, users);
if(u == undefined){
console.log("Couldnt find user by name: " + curn);
}else{
u.current = curv;
}
}
}
for(var uid in users){
users[uid].buyout *= multiplier;
users[uid].buyin *= multiplier;
users[uid].current *= multiplier;
}
sum *= multiplier;
if(verbose){
console.log("SUM : " + sum);
}
addToTable(users);
}
function getTableData(users){
var data = [];
var total_buyin = 0;
var total_buyout = 0;
var total_net = 0;
var total_current = 0;
var total_players = 0;;
for(let uid in users){
var u = users[uid];
let id_str = "<abbr title=\"" + u.get_ids() + "\">" + uid + "</abbr>";
let name_str = "<abbr title=\"" + u.get_names() + "\">" + u.get_one_name() + "</abbr>";
var cur = [id_str, name_str, u.buyin, u.buyout, u.current, u.net(), u.quit?0:1];
data.push(cur);
total_buyin += u.buyin;
total_buyout += u.buyout;
total_net += u.net();
total_players += (u.quit == false)?1:0;
total_current += u.current;
}
var currentHand = document.getElementsByClassName("table-player-bet-value");
var curSum = 0;
for(let c of currentHand){
if(c.innerHTML != "check"){
curSum += parseInt(c.innerHTML, 10);
}
}
curSum += parseInt(document.querySelector(".table-pot-size").innerHTML);
total_current += curSum;
total_net += curSum;
var cur = ["", "<b>Current Hand</b>", "", "", curSum, curSum, "-"]
var total = ["", "<b>Total</b>", "<b>"+total_buyin+"</b>", "<b>"+total_buyout+"</b>",
"<b>"+total_current +"</b>", "<b>"+total_net+"</b>", "<b>"+total_players+"</b>"];
data.push(cur);
data.push(total);
return {data : data, inHand : curSum};
}
function addToTable(users){
var table = document.createElement("table");
var ret = getTableData(users);
var data = ret.data;
var inHand = ret.inHand;
table.style = "width: 100%";
for(let ele of data){
let row = table.insertRow();
for(let v of ele){
let cell = row.insertCell();
cell.style = "margin-top:6px; margin-bottom: 6px; text-align: left; border-bottom: 1px solid #ddd";
cell.innerHTML = String(v);
}
}
let header = ["ID", "Name", "Buy In", "Buy Out", "Current", "Net", "Playing"];
let thead = table.createTHead();
let row = thead.insertRow();
for(let key of header){
let th = document.createElement("th");
let text = document.createTextNode(key);
th.style = "padding-bottom:7px; text-align: left; border-bottom: 1px solid #ddd";
th.appendChild(text);
row.appendChild(th);
}
let b = document.getElementsByClassName("modal-body")[0]
b.style = "height : 100%;";
b.innerHTML = "";
b.appendChild(table);
if(inHand == 0){
var button = document.createElement('button');
button.type = "button";
button.className = "button-1 green small-button";
button.innerHTML = "Generate Transactions";
b.appendChild(button);
button.users = users;
button.addEventListener("click", generateTransactions, false);
}
}
function generateTransactions(evt){
var credit = [];
var debit = [];
var users = evt.currentTarget.users;
evt.currentTarget.parentNode.removeChild(evt.currentTarget);
var sum = 0;
for(let uid in users){
let u = users[uid];
if(u.net() > 0)
credit.push({name : u.get_one_name(), amt : u.net()});
else if(u.net() < 0)
debit.push({name : u.get_one_name(), amt : -u.net()});
sum += u.net();
}
if(sum != 0){
let b = document.getElementsByClassName("modal-body")[0]
b.innerHTML += "<p>Sum doesn't add upto 0</p>";
return;
}
credit.sort(function(a, b){
return a.amt - b.amt;
});
debit.sort(function(a, b){
return a.amt - b.amt;
})
var t = [];
while(credit.length > 0 && debit.length > 0){
var c = credit.pop();
var d = debit.pop();
var m = Math.min(c.amt, d.amt);
t.push({sender: d.name, receiver : c.name, amt : m});
c.amt -= m;
d.amt -= m;
if(c.amt > 0) credit.push(c);
if(d.amt > 0) debit.push(d);
}
var text = document.createElement("div");
text.className = "transactions";
console.log(t);
for (let a of t){
console.log(a);
var p = document.createElement("p");
p.innerHTML += a.sender + " owes " + a.receiver + " " + a.amt;
text.appendChild(p);
}
let b = document.getElementsByClassName("modal-body")[0]
b.appendChild(document.createElement("br"));
b.appendChild(text);
}