-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathACBLmergePairs.js
executable file
·352 lines (298 loc) · 13.6 KB
/
ACBLmergePairs.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
/*jslint browser: true, vars: true, plusplus: true, continue: true */
"use strict";
function dumphand(hand,showsymb,vert) {
var suitHTML = ['♠', '<span class="hs">♥<\/span>',
'<span class="ds">♦<\/span>', '♣'];
var suits = hand.split('.');
var i, ss;
for (i=0; i<4; i++) {
if (showsymb) {
if (suits[i].length === 0) { suits[i] = '-'; }
suits[i] = suitHTML[i] + suits[i];
}
}
if (vert) { ss = suits.join('<br>'); } else { ss = suits.join(' '); }
return ss;
}
function pairnameLookup(p, pair_id) {
var i;
for (i=0; i < p.length; i+= 3) {
if (p[i] === pair_id) { return [p[i+1], p[i+2]]; }
}
return ['', ''];
}
function flipSpecialScore(str) {
if (str === 'AVE+') { return 'AVE-'; }
if (str === 'AVE-') { return 'AVE+'; }
var ix = str.indexOf('|');
if (ix === -1) { return str; }
return str.substr(ix+1,str.length-ix-1) + '|' + str.substr(0,ix);
}
function scoreLookup(bb, mptop, pair_id_nodir, dir, hasContracts, hasLeads) {
var i;
var ndataPerResult = 4 + hasContracts + hasLeads;
if (dir === 'N') {
for (i=2; i < bb.length; i+= ndataPerResult) {
if (bb[i] === pair_id_nodir) { return [bb[i-2], bb[i-1], bb[i+1] + 'E', bb[i+2], bb[i+3]]; }
}
return [-1];
}
if (dir === 'E') {
for (i=3; i < bb.length; i+= ndataPerResult) {
if (bb[i] === pair_id_nodir) {
if (typeof(bb[i-3]) === 'number') {
return [-bb[i-3], (mptop === undefined) ? -bb[i-2] : mptop - bb[i-2], bb[i-1] + 'N', bb[i+1], bb[i+2]];
}
return [flipSpecialScore(bb[i-3]), mptop - bb[i-2], bb[i-1] + 'N', bb[i+1], bb[i+2]];
}
}
return [-1];
}
/* For Howell movement need to check both directions for pair number. */
for (i=2; i < bb.length; i+= ndataPerResult) {
if (bb[i] === pair_id_nodir) { return [bb[i-2], bb[i-1], bb[i+1], bb[i+2], bb[i+3]]; }
if (bb[i+1] === pair_id_nodir) {
if (typeof(bb[i-2]) === 'number') {
return [-bb[i-2], (mptop === undefined) ? - bb[i-1] : mptop - bb[i-1], bb[i], bb[i+2], bb[i+3]];
}
return [flipSpecialScore(bb[i-2]), mptop - bb[i-1], bb[i], bb[i+2], bb[i+3]];
}
}
return [-1];
}
function showhideClass(checkboxid, cname) {
// Show or hide elements with specified class name based on checkbox state.
// IE8 does not support getElementsByClassName(). However this function is
// only used for the iPhone specific format.
var cb = document.getElementById(checkboxid);
var disp = cb.checked ? '' : 'none';
document.getElementById("ACBLmerge").getElementsByClassName(cname);
var ids = document.getElementById("ACBLmerge").getElementsByClassName(cname);
var i;
for(i = 0; i < ids.length; i++) {
ids[i].style.display = disp;
}
}
function c1(hnum, west, north, east, south) {
var w, d, title, css, leftpad, centerpad, marginpad;
w = window.open('', '', 'width=480,height=720,scrollbars=yes');
if (w.focus) { w.focus(); }
d = w.document;
title = 'Board ' + hnum + ' Cut-and-Paste Aid';
css = '<style type="text/css">\n' +
'body, h1 {\n' +
' font-family: Arial, Helvetica, sans-serif;\n' +
' font-size: 11pt;\n' +
' margin-top: 0.6em;\n' +
' margin-bottom: 0.4em;\n' +
'}\n' +
'.ds, .hs { color: red; }\n' +
'.fill3x1 { width: 6em; }\n' +
'<\/style>\n';
d.write('<!DOCTYPE html>\n');
d.write('<html lang="en">\n<head>\n');
d.write('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">\n');
d.write('<title>' + title +'<\/title>\n' + css + '<\/head>\n\n<body>\n\n');
d.write('<h1>Horizontal North and South hands with and without suit symbols<\/h1>\n');
d.write(dumphand(north,1,0) + '<br>' + dumphand(south,1,0) + '<br><br>');
d.write(dumphand(north,0,0) + '<br>' + dumphand(south,0,0) + '<br>\n\n');
d.write('<h1>Horizontal East and West hands with and without suit symbols<\/h1>\n');
d.write(dumphand(east,1,0) + '<br>' + dumphand(west,1,0) + '<br><br>');
d.write(dumphand(east,0,0) + '<br>' + dumphand(west,0,0) + '<br>\n\n');
d.write('<h1>Bridgify ready format (West, North, East, South)<\/h1>\n');
d.write(west + '<br>\n' + north + '<br>\n' + east + '<br>\n' + south + '<br>\n\n');
d.write('<h1>Deal in 3x3 HTML table<\/h1>\n');
d.write('<table><tr><td><\/td><td>' + dumphand(north,1,1) +
'<\/td><td><\/td><\/tr>');
d.write('<tr><td>' + dumphand(west,1,1) + '<\/td><td><\/td><td>' +
dumphand(east,1,1) + '<\/td><\/tr>');
d.write('<tr><td><\/td><td>' + dumphand(south,1,1) +
'<\/td><td><\/td><\/tr><\/table>\n\n');
d.write('<h1>Compact preformatted deal without initial space<\/h1>\n');
leftpad = ' ';
centerpad = ' ';
d.write('<pre>\n');
d.write(leftpad + dumphand(north,1,0) + '\n\n');
d.write(dumphand(west,1,0) + centerpad + dumphand(east,1,0) + '\n\n');
d.write(leftpad + dumphand(south,1,0) +'\n');
d.write('<\/pre>\n\n');
d.write('<h1>Compact preformatted deal with initial space<\/h1>\n');
d.write('<pre>\n');
marginpad = ' ';
leftpad = marginpad + leftpad;
d.write(leftpad + dumphand(north,1,0) + '\n\n');
d.write(marginpad + dumphand(west,1,0) + centerpad +
dumphand(east,1,0) + '\n\n');
d.write(leftpad + dumphand(south,1,0) +'\n');
d.write('<\/pre>\n\n');
d.write('<h1>Vertical West and East hands in 3x1 HTML table<\/h1>\n');
d.write('<table><tr><td>' + dumphand(west,1,1) +
'<\/td><td class="fill3x1"><\/td><td>' +
dumphand(east,1,1) + '<\/td><\/tr><\/table>\n\n');
d.write('<h1>Vertical North and South hands in 3x1 HTML table<\/h1>\n');
d.write('<table><tr><td>' + dumphand(north,1,1) +
'<\/td><td class="fill3x1"><\/td><td>' +
dumphand(south,1,1) + '<\/td><\/tr><\/table>\n\n');
d.write('<\/body><\/html>');
d.close();
}
function recap(pair_id) {
var p = pairdata();
var data = boarddata();
var top = data[0], handix = data[1];
var hasContracts = data[3], hasLeads = data[4], b = data[5];
var tmp, oneSection;
var section, dir, isHowell, title_pair_id, title, pair_id_nodir, pairnames;
var i, winwidth, w, d, ix, parenturl, parentjs, js, css;
var bnum, pscore, nscore, pct, cname;
var psum = 0, nboards = 0, npos = 0, nneg = 0;
var suit, suitHTML, leadFlag, leadClass, rs, evtpct;
// var astyle = options().americanstyle;
var tricks, americanstyle = options().americanstyle;
tmp = b[0][2].charCodeAt(0);
oneSection = (tmp >= 48 && tmp < 59);
tmp = pair_id.charCodeAt(1);
section = pair_id.substr(0, (tmp >= 48 && tmp < 59) ? 1 : 2);
tmp = pair_id.charAt(pair_id.length-1);
isHowell = (tmp !== 'N' && tmp !== 'E');
if (!isHowell) { dir = tmp; } else { dir = ''; }
title_pair_id = pair_id.substring(section.length, pair_id.length - (isHowell ? 0 : 1));
title = 'Recap for pair ' + title_pair_id;
if (!isHowell) { title += (dir === 'N' ? ' North-South' : ' East-West'); }
if (!oneSection) { title += ' (Section ' + section + ')'; }
pairnames = pairnameLookup(p, pair_id);
winwidth = 600;
if (hasContracts) { winwidth += 80; }
if (hasLeads) { winwidth += 30; }
w = window.open('', '', 'width=' + winwidth + ',height=720,scrollbars=yes');
if (w.focus) { w.focus(); }
d = w.document;
parenturl = window.location.origin + window.location.pathname;
js = '<' + 'script type="text/javascript">\n' + 'function gotobnum(url, bnum) {\n' +
' var urlbnum = url + "#board_results" + bnum;\n' +
' window.open(urlbnum, url);\n' +
'}\n' +
'<\/script>\n';
css = '<style type="text/css">\n' +
'body, h1 {\n' +
' font-size: 11pt;\n' +
' margin-top: 0.6em;\n' +
' margin-bottom: 0.4em;\n' +
'}\n' +
'.center { text-align: center; }\n' +
'.right { text-align: right; }\n' +
'.ds, .hs { color: red; }\n' +
'.results { border-spacing: 1em 0em; }\n' +
'.results thead { font-weight: bold; }\n' +
'.bnum { color: blue; }\n' +
'.bnum:hover { background-color: yellow; text-decoration: underline; }\n' +
'.top { background-color: #80FF80; }\n' +
'.top30 { background-color: #C0FFC0; }\n' +
'.middle { background-color: white; }\n' +
'.bottom { background-color: #FF8080; }\n' +
'.bot30 { background-color: #FFC0C0; }\n' +
'.opplead { background-color: #FFFFAA; border: solid #606060 1px; padding-left: 1px; padding-right: 1px; }\n' +
'.badlead { background-color: #FFC0C0; border: solid #606060 1px; padding-left: 1px; padding-right: 1px; }\n' +
'<\/style>\n';
d.write('<!DOCTYPE html>\n');
d.write('<html lang="en">\n<head>\n');
d.write('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">\n');
d.write('<meta http-equiv="Content-Style-Type" content="text/css">\n');
d.write('<meta http-equiv="Content-Script-Type" content="text/javascript">\n');
d.write('<title>' + title +'<\/title>\n\n' + js + '\n' + css + '<\/head>\n<body>\n\n');
d.write('<h1 class="center">' + title + '<br>' + pairnames[0] + ' & ' + pairnames[1] + '<\/h1>\n\n');
/* Generate results for each board */
d.write('<table class="results">\n');
d.write('<thead><tr><td>Board<\/td><td class="right">Plus<\/td><td class="right">Minus<\/td><td class="right">Pts<\/td><td class="right">Pct<\/td>');
if (hasContracts) { d.write('<td>Contract<\/td>'); }
if (hasLeads) { d.write('<td>Ld<\/td>'); }
d.write('<td>Opponents<\/td><\/tr><\/thead>\n');
d.write('<tbody>\n');
pair_id_nodir = pair_id;
if (!isHowell) { pair_id_nodir = pair_id_nodir.substr(0,pair_id_nodir.length-1); }
if (oneSection) { pair_id_nodir = pair_id_nodir.substr(section.length); }
for (i=0; i < handix.length; i++) {
bnum = handix[i];
rs = scoreLookup(b[i], top, pair_id_nodir, dir, hasContracts, hasLeads);
if (rs[0] === -1) { continue; }
if (typeof rs[0] === 'number') {
if (rs[0] >= 0) { pscore = rs[0]; nscore = ''; npos++; }
else { nscore = -rs[0]; pscore = ''; nneg++; }
}
else {
if (rs[0] === "AVE-") { pscore = ''; nscore = rs[0]; nneg++; }
else if (rs[0] === "AVE+") { pscore = rs[0]; nscore = ''; npos++; }
else { pscore = rs[0]; nscore = rs[0]; }
}
pct = (undefined !== top) ? 100 * rs[1] / top : undefined;
psum += pct; nboards++;
if (oneSection) { pairnames = pairnameLookup(p, pair_id.substr(0,section.length) + rs[2]); }
else { pairnames = pairnameLookup(p, rs[2]); }
cname = pct >= 99 ? 'top' : pct >= 70.0 ? 'top30' : pct <= 1.0 ? 'bottom' :
pct <= 30.0 ? 'bot30' : 'middle';
parentjs = "gotobnum('" + parenturl + "', " + bnum + ");";
d.write('<tr><td class="bnum" onclick="'+ parentjs + '">' + bnum + '<\/td><td class="right">' +
pscore + '<\/td><td class="right">' + nscore + '<\/td><td class="right">' +
rs[1].toFixed(2) + '<\/td><td class="right ' + cname + '">' +
((pct === undefined) ? '' : pct.toFixed(2)) + '<\/td>');
if (hasContracts) {
if (rs[3] === 'PASS') { d.write('<td>' + 'Pass Out' + '<\/td>'); }
else if (rs[3] === '') { d.write('<td>' + 'Missing' + '<\/td>'); }
else {
suit = rs[3].substr(1,1);
if (suit === 'N') { suitHTML = 'N'; }
else if (suit === 'S') { suitHTML = '♠'; }
else if (suit === 'H') { suitHTML = '<span class="hs">♥</span>'; }
else if (suit === 'D') { suitHTML = '<span class="ds">♦</span>'; }
else if (suit === 'C') { suitHTML = '♣'; }
if (americanstyle) {
d.write('<td>' + rs[3].substr(0,1) + suitHTML);
if ( rs[3].substr(rs[3].length-3,1) === ' ' ) {
if ( rs[3].substr(rs[3].length-2,1) === '-' ) {
d.write(rs[3].substr(2,rs[3].length-2) + '<\/td>');
}
else {
tricks = parseInt(rs[3].substr(0,1),10) + parseInt(rs[3].substr(rs[3].length-1,1),10);
d.write(rs[3].substr(2,rs[3].length-4) + tricks + '<\/td>');
}
}
else {
d.write(rs[3].substr(2,rs[3].length-2) + ' ' + rs[3].substr(0,1) + '<\/td>');
}
}
else {
d.write('<td>' + rs[3].substr(0,1) + suitHTML + rs[3].substr(2,rs[3].length-2) + '<\/td>');
}
}
}
if (hasLeads) {
if (rs[3+hasContracts].length === 0) { d.write('<td><\/td>'); }
else {
suit = rs[3+hasContracts].substr(0,1);
if (suit === 'S') { suitHTML = '♠'; }
else if (suit === 'H') { suitHTML = '<span class="hs">♥</span>'; }
else if (suit === 'D') { suitHTML = '<span class="ds">♦</span>'; }
else if (suit === 'C') { suitHTML = '♣'; }
if (rs[3+hasContracts].length === 2) {
d.write('<td>' + suitHTML + rs[3+hasContracts].substr(1,1) + '<\/td>');
}
else {
leadFlag = rs[3+hasContracts].substr(2,1);
if (leadFlag === '=') { leadClass = 'opplead'; }
else if (leadFlag === '?') { leadClass = 'badlead'; }
d.write('<td><span class="' + leadClass + '">' + suitHTML +
rs[3+hasContracts].substr(1,1) + '<\/span><\/td>');
}
}
}
d.write('<td>' + pairnames[0] + ' - ' + pairnames[1] + '<\/td><\/tr>\n');
}
d.write('<\/tbody>\n');
d.write('<\/table>\n\n');
evtpct = psum / nboards;
d.write('<p>Session: ' + evtpct.toFixed(2) + '% Plus Scores: ' + npos +
' Minus Scores: ' + nneg + '</p>\n');
d.write('<\/body><\/html>');
d.close();
}