-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpokerLogic.js
More file actions
360 lines (315 loc) · 12.5 KB
/
pokerLogic.js
File metadata and controls
360 lines (315 loc) · 12.5 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
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
//Created by Ole Halvor Dahl
console.log('Loading script');
var tid = 0; //The main time variable - conatins total milliseconds played
var start = false; //Contains whether the clock is running or not
var startTid = new Date();//The time when you last started the clock
var stopTid = new Date(); //the time when you last stopped the clock
var upTid = 0;
var diffTid = 0; //the difference in time between each time the clock function is run - each time this is added to "tid"
var dispTid = 0; //the time that will be shown
var tidS;
var nyStreng;
var tidSlen;
var myVar;
var remaining = 0;
var tempBlindTid = 0;
var currLev = 0; //current level
var levCheck = 0;
var sound = false; //stores if you have clicked to enable sound checkbox
var pause = false; //stores if you have clicked to enable pause checkbox
var minutesPlayed; //if you input a "minutes played" in config - it is stored here
// ------- Configuration Variables (not in use (only for defining names now))------ //
var blinds = [1,1,1,1,1,1,1,1, 1, 1, 1, 1,1,1]; //What you put here is irrelevant, but it must be the same size as blindsLengde[]
var blindsLengde = [45,45,60,60,60,60,60,60,60,60, 60, 60, 60, 60]; //Hvo many minutes each level is
var ante = [0,0,2,5,10,10,25,25, 50, 50, 100, 100, 200, 300];
var comment = ["","","","Remove white","","","Remove red","", "", "", "Remove blue", "", "", ""];
var blindVerdi = ["2-4","5-10","10-20","25-50","40-80","50-100","75-150","100-200", "150-300", "200-400", "300-600", "400-800", "500-1000", "750-1500"];
var chipvalue = [2,5,25,100,500];
var chipcolor = ["white","blue","red","green","black"];
var chipamount = [10,11,9,7,0];
// ------- Configuration Variables (not in use)------ //
// ------- Getting and parsing Variables from url ------ //
var varArr= [1,1];
var mottat=get_query(); //getting variables
var c =0;
for (m in mottat){
varArr[c] = mottat[m];
c++;
}
for (a in varArr){ //Replace URL symbols '%2C' and '%22' with a comma or a ' " '
varArr[a] = varArr[a].replace(/%2C/g, ",");
varArr[a] = varArr[a].replace(/%22/g, '"');
}
//console.log(varArr);
//Splitting the array on the commas and storing them in individual variables
blindsLengde = varArr[0].split(',');
blinds = varArr[0].split(','); // "blinds" ends up storing the milliseconds since start at each level change.
ante = varArr[1].split(',');
comment = varArr[3].split(',');
blindVerdi = varArr[2].split(',');
chipvalue = varArr[4].split(',');
minutesPlayed = varArr[5];
//console.log("minutes played: "+minutesPlayed);
//console.log("Chip values: "+chipvalue);
// ------- Getting and parsing Variables from url ------ //
//This loops generates the total time until each level and saves it in blinds[]
for (var i=0; i<blinds.length; i++){
blinds[i] = (blindsLengde[i]*60000) + tempBlindTid; //multiplied by 60000, minutes to milliseconds
tempBlindTid += (blindsLengde[i]*60000);
//if each level is 60 minutes, blinds[0] = 0, blinds[1] = 60*60000, blinds[2] = blinds [1] + 60*60000 etc.
}
function removeAllCurrent(){ //this removes the colored cell from the table - used at level change
for (var i = 0; i<blindsLengde.length; i++){
// document.getElementById("c"+(i+1)).innerHTML =" ";
}
}
function setCurrent(current){
try{
removeAllCurrent();
// document.getElementById(current).innerHTML ="_";
// document.getElementById(current).style.background="#111111"; //This makes the "current"-cell in the current row black.
}catch(err){console.log(err.message);}
}
//This adds a row to the table, "divnr" is an ID to manipulate the cell later, same with "row.id"
function addRow(content1,content2,content3,content4,content5,divnr){
if (!document.getElementsByTagName) return;
tabBody=document.getElementsByTagName("tbody").item(0);
row=document.createElement("tr");
row.id="r"+content1;
cell1 = document.createElement("td");
cell2 = document.createElement("td");
cell3 = document.createElement("td");
cell4 = document.createElement("td");
cell5 = document.createElement("td");
// cell6 = document.createElement("td");
div = document.createElement("div");
div.id = divnr;
textnode1=document.createTextNode(content1);
textnode2=document.createTextNode(content2);
textnode3=document.createTextNode(content3);
textnode4=document.createTextNode(content4);
textnode5=document.createTextNode(content5);
//textnode6=document.createTextNode(content6);
cell1.appendChild(textnode1);
cell2.appendChild(textnode2);
cell3.appendChild(textnode3);
cell4.appendChild(textnode4);
cell5.appendChild(textnode5);
//cell6.appendChild(textnode6);
// cell6.appendChild(div);
// div.appendChild(textnode6);
row.appendChild(cell1);
row.appendChild(cell2);
row.appendChild(cell3);
row.appendChild(cell4);
row.appendChild(cell5);
// row.appendChild(cell6);
tabBody.appendChild(row);
}
function pad(n){return n<10 ? '0'+n : n;} //Makes 1 into 01.. used by the timer/clock
function nyVisTid(tid){ //converts from JS Date format(or milliseconds) to a more readeable one (HH:MM:SS)
var d = new Date(tid);
//console.log(tid);
var text = pad(d.getUTCHours()) + ":" + pad(d.getMinutes()) + ":" + pad(d.getSeconds());
return text;
}
//Makes the current row bigger, and the rest smaller
function resizeTable(){
var on = true;
for (i=0; i<blindsLengde.length; i++){
var rowId = "#r"+(i-1)+" td";
if (i==currLev+2 ){
$(rowId).css('padding', '8px');
$(rowId).css('font-size', '24px');
}
else {
$(rowId).css('padding', '4px');
$(rowId).css('font-size', '14px');
}
}
}
var prevColor = $('#r1').css('background');
color = '#92FF5C';
$('#r1').css('background',color);
function update(){ //this function is run over and over with x ms delay - This is the core of the clock.
diffTid = Date.now() - startTid; //calculate how long the clock has been running since last start
dispTid = diffTid +tid; //calculate the stored time + the currently running time
createCookie("tid",dispTid,2); //creates a cookie, used in case the browser is closed while playing
var tempLevel = 0; //this loop checks/counts what blind level we are on
for (blind in blinds){
if (dispTid > blinds[blind]){
tempLevel++;
}
}
if (currLev < tempLevel){ //new level
console.log("New level");
$('#r')
if(sound==true)play_single_sound();
if(pause==true)main(); //if the "pause at new level" is toggeled, it simulates a click on the "start/pause" button
console.log("row: #r"+(currLev+2));
$('#r'+(currLev+1)).css('background',prevColor);
prevColor = $('#r'+(currLev+2)).css('background');
$('#r'+(currLev+2)).css('background',color);
}
currLev = tempLevel;
resizeTable();
document.getElementById('currentlevel').innerHTML = currLev+1; //prints the current level to the HTML page
setCurrent("c"+(currLev+1)); //sets the colored cell in the right place in the table
//next level in:
remaining = blinds[currLev] - dispTid;
document.getElementById('display-area').innerHTML = nyVisTid(remaining);
}
function main(){ //main() is called when Start/stop is pushed
if (start == false){ // start == false indicates that the clock was not running
$('#start').text('Pause');
if (sound==true){
var audio = new Audio('lyd.mp3');
audio.play();
}
startTid = Date.now();
myVar = setInterval(update,500); //starts looping the update() function with a given delay in ms.
start = true; //the clock is running
document.getElementById('info').innerHTML = "On";
}
else{ //Else contains the code that's executed when the clock is paused
$('#start').text('Start');
clearInterval(myVar); //Stops the update() function
start = false;
tid = tid + diffTid; //Adds the current time on the clock to the time variable
document.getElementById('info').innerHTML = "Off";
}
}
function reset(){ //runs if the reset button is clicked
if(confirm('Are you sure you want to reset?')){
$('#r'+(currLev+1)).css('background',prevColor);
console.log('#r'+(currLev+1));
tid = 0;
clearInterval(myVar);
start = false;
createCookie("tid",0,2);
eraseCookie("tid");
console.log("cookies deleted");
removeAllCurrent();
document.getElementById('info').innerHTML = "Off";
document.getElementById('display-area').innerHTML = "00:00:00";
document.getElementById('currentlevel').innerHTML = "0";
currLev = 0;
resizeTable();
}
}
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
function getLevel(){
var tempLevel = 0; //this loop checks/counts what blind level we are on
for (blind in blinds){
if (tid > blinds[blind]){
tempLevel++;
}
}
currLev = tempLevel;
document.getElementById('currentlevel').innerHTML = currLev+1; //prints the current level to the HTML page
setCurrent("c"+(currLev+1));
return currLev;
}
function play_single_sound() {
document.getElementById('audiotag1').play();
}
//gets the variables from the url
function get_query(){
var url = location.href;
var qs = url.substring(url.indexOf('?') + 1).split('&');
for(var i = 0, result = {}; i < qs.length; i++){
qs[i] = qs[i].split('=');
result[qs[i][0]] = qs[i][1];
}
return result;
}
function setVariables(lengthG, antesG, blindsG, commentsG){
blinds = lengthG;
blindsLengde = lengthG;
ante = antesG;
comment = commentsG;
blindVerdi = blindsG;
console.log("lenght: "+lengthG +" Antes: " + antesG +" comments: "+commentsG+" blinds: "+blindsG);
}
function checkSound(){
if (sound==true){
sound=false;
}else {sound=true;}
console.log("Sound is now: "+sound);
}
function checkPause(){
if (pause==true){
pause=false;
}else {pause=true;}
console.log("Pause is now: "+pause);
}
function saveTab(){
window.prompt("If you want to save this table: copy and save this address", location.href);
}
//The following lines binds the functions to the HTML buttons on pageload, and reads cookie or "minutesPlayed" if they exists
window.addEventListener('load',function(){
// Set the correct values of chips in the HTML:
$('#chip1').text(chipvalue[0]);
$('#chip2').text(chipvalue[1]);
$('#chip3').text(chipvalue[2]);
$('#chip4').text(chipvalue[3]);
$('#chip5').text(chipvalue[4]);
document.getElementById('display-area').innerHTML = nyVisTid(remaining);
var ctid = parseInt(readCookie("tid"));
if (ctid>0){
console.log('milliseconds from cookie: ' + readCookie("tid"));
tid = ctid;
}else ctid=0;
//Since the coockie is deleted when you enter minutesPlayed in config -
//This is only entered if you have not started playing
if (minutesPlayed>0 && (ctid==0)){
tid = minutesPlayed * 60 * 1000;
remaining = blinds[currLev] - tid;
document.getElementById('display-area').innerHTML = nyVisTid(remaining);
}
//tid = 100000; //If you need to start at a specific time, this is the place. set tid = milliseconds since start ---------------------
var count = 0;
//Generating the rows in the table
for (blind in blindsLengde){
addRow(count+1,blindVerdi[blind],ante[blind],blindsLengde[blind],comment[blind]," ", "c"+(count+1));
count++;
}
if (ctid > 0){
getLevel();
remaining = blinds[currLev] - ctid;
document.getElementById('display-area').innerHTML = nyVisTid(remaining);
}
document.getElementById('start').addEventListener('click',function(){
main();
});
resizeTable();
});
window.addEventListener('load',function(){
document.getElementById('reset').addEventListener('click',reset);
document.getElementById('pausevalg').addEventListener('click',checkPause);
document.getElementById('lydvalg').addEventListener('click',checkSound);
document.getElementById('savebtn').addEventListener('click',saveTab);
});
console.log('script done!');
//Created by Ole Halvor Dahl