// BElib.txt v20130324a peter.brazier@ntlworld.com var BE = {}; //BE is the Bridge Event object BE.lastPair; //pair number of last score card displayed BE.points = []; BE.hands = []; BE.thisHand; BE.simsLink = ''; //default - not a sims event var members = []; var visitor; function shortDelay() { var x = 1; var y = null; // To keep under proper scope setTimeout(function() { x = x * 3 + 2; y = x / 2; }, 100); } function getDayName(sessionDate) { switch (sessionDate.getDay()) { case 0: return "Sunday"; case 1: return "Monday"; case 2: return "Tuesday"; case 3: return "Wednesday"; case 4: return "Thursday"; case 5: return "Friday"; case 6: return "Saturday"; } } function getMonthName(sessionDate) { switch (sessionDate.getMonth()) { case 0: return "January"; case 1: return "February"; case 2: return "March"; case 3: return "April"; case 4: return "May"; case 5: return "June"; case 6: return "July"; case 7: return "August"; case 8: return "September"; case 9: return "October"; case 10: return "November"; case 11: return "December"; } } function vulnerable(board, pos) { var t = true, f = false, vul_a = [0, [f, f], [t, f], [f, t], [t, t], [t, f], [f, t], [t, t], [f, f], [f, t], [t, t], [f, f], [t, f], [t, t], [f, f], [t, f], [f, t], [f, f], [t, f], [f, t], [t, t], [t, f], [f, t], [t, t], [f, f], [f, t], [t, t], [f, f], [t, f], [t, t], [f, f], [t, f], [f, t], [f, f], [t, f], [f, t], [t, t]]; return vul_a[board][pos]; } //END vulnerable(board, pos) function round2dp(Number) { //Rounds Number to the nearest 2dp return (Math.round(100 * Number)) / 100; } function percentage(pair) { return round2dp(100 * BE.pairs[pair][3] / BE.pairs[pair][4]); } function processPage() { //Wait for the page to load before running the process and display. This funciton is called onload processData(); displayResults(); } function getMembersID(name) { //returns Id if a memeber otherwith returns undefined var id, temp; for (id = 1; id < members.length; id += 1){ if (members[id] !== undefined){ if (members[id][0] === name){ return id; } } } return 0; } function sortArray(arrayName, index) { // Sort arrayName by the index ( in range 0 to ?) in each array - see: http://stackoverflow.com/questions/4382098/javascript-multidimensional-array-sort-by-specified-index arrayName.sort(function (a, b) { if (a[index] === b[index]) { return 0; } return a[index] > b[index] ? -1 : 1; }); } function processData() { //alert("date = " + BE.date); var pn, i, r, board, sessionDate = new Date(BE.date.slice(0, 4), (BE.date.slice(4, 6) - 1), BE.date.slice(6, 8)); BE.sessionDateStr = getDayName(sessionDate) + " " + sessionDate.getDate() + " " + getMonthName(sessionDate) + " " + sessionDate.getFullYear(); BE.rank = [[],[]]; //[0] for NS or 1 winner, [1] for EW with 2 winners. [0 or 1][pair number, %] if (BE.winners === 1) { for (i = 1; i <= 2 * BE.tables; i += 1) { BE.rank[0][i - 1] = [i, BE.pairs[i][5]]; //[pair row, %] row starts at 0 } sortArray(BE.rank[0],1); //sort results array in decending % order rankResults(); } else { // 2 winners for (i = 1; i <= BE.tables; i += 1) { pn = i; BE.rank[0][i - 1] = [pn, BE.pairs[pn][5]]; //NS BE.rank[0][i - 1] = [pn, %] pn = i + BE.tables; BE.rank[1][i - 1] = [pn, BE.pairs[pn][5]]; //EW } sortArray(BE.rank[0],1); //NS rankResults(); sortArray(BE.rank[1],1); //EW rankResults(); } //alert("members.length = " + members.length); if (members.length > 0) { filterNames(); //Filter names for members, non members and pairs of couple } //Sort Travellers for (board = 1; board < BE.travellers.length ; board += 1) { travellerRank(board, 5, 7); //sorts on index 5 (EWmps) and puts EW ranks in index 7 travellerRank(board, 4, 6); //sorts on index 4 (NSmps) and puts NS ranks in index 6 ***THIS WILL BE THE ORDER USED IN ALL SUBSEQUENT OPERATIONS BUT IS NOT SAVED WHEN CLOSED***** } } //END function ProcessData() function rankResults() { // Sorts the traveller rows in order of BE.travellers[board][index1] and places the ranks in BE.travellers[board][index2] // On exit the traveller rows are in the final sorted order var r, rank_a = [[], [], [], []], pn, pc, ns = 0, ew = 1; if (BE.winners === 1){ for (r = 0; r < BE.rank[0].length; r += 1) { // initialize rank_a[r] pn = BE.rank[0][r][0]; pc = BE.rank[0][r][1]; rank_a[r] = [r, pn, r + 1, "", pc]; } for (r = 1; r < BE.rank[0].length; r += 1) { if(rank_a[r][4] === rank_a[r - 1][4]) { // same % rank_a[r][2] = rank_a[r - 1][2]; rank_a[r][3] = "="; rank_a[r - 1][3] = "="; // alert("resultsRank r = " + r); } } // alert("rank_a = " + rank_a); for (r = 0; r < BE.rank[0].length; r += 1) { // write ranks to BE.travellers[board][r][index2] pn = BE.rank[0][r][0]; BE.pairs[pn][2] = rank_a[r][2] + rank_a[r][3]; // alert("NS pn = " + pn + " rank = " + BE.pairs[pn][2]); } } else { //2 winners // NS for (r = 0; r < BE.tables; r += 1) { // initialize rank_a[r] pn = BE.rank[ns][r][0]; pc = BE.rank[ns][r][1]; rank_a[r] = [r, pn, r + 1, "", pc]; } for (r = 1; r < BE.tables; r += 1) { if(rank_a[r][4] === rank_a[r - 1][4]) { // same % rank_a[r][2] = rank_a[r - 1][2]; rank_a[r][3] = "="; rank_a[r - 1][3] = "="; //alert("NS resultsRank r = " + r); } } //alert("rank_a = " + rank_a); for (r = 0; r < BE.rank[0].length; r += 1) { // write ranks to BE.pairs[pn][2] pn = BE.rank[ns][r][0]; BE.pairs[pn][2] = rank_a[r][2] + rank_a[r][3]; } // EW for (r = 0; r < BE.tables; r += 1) { // initialize rank_a[r] pn = BE.rank[ew][r][0]; pc = BE.rank[ew][r][1]; rank_a[r] = [r, pn, r + 1, "", pc]; } //alert("A rank_a = " + rank_a); for (r = 1; r < BE.tables; r += 1) { if(rank_a[r][4] === rank_a[r - 1][4]) { // same % rank_a[r][2] = rank_a[r - 1][2]; rank_a[r][3] = "="; rank_a[r - 1][3] = "="; // alert("EW resultsRank r = " + r); } } //alert("EW rank_a = " + rank_a); for (r = 0; r < BE.rank[1].length; r += 1) { // write ranks to BE.travellers[board][r][index2] pn = BE.rank[ew][r][0]; BE.pairs[pn][2] = rank_a[r][2] + rank_a[r][3]; // alert("EW pn = " + pn + " rank = " + BE.pairs[pn][2]); } } } //END function rankResults() function filterNames(){ var id1, id2, id1spouse; //tag visitors as name - the name is then displayed in italics visitor = false; for (pn = 1; pn < BE.pairs.length; pn +=1) { if (BE.pairs[pn][0] !== undefined) { // is not a missing pair //name1 id1 = getMembersID(BE.pairs[pn][0]); if (id1 === 0) { //a visitor visitor = true; BE.pairs[pn][0] = "" + BE.pairs[pn][0] + ""; id1spouse = 0; //alert("visitor: pn = " + pn + " " + BE.pairs[pn][0]); } else { // member if (members[id1][1].length > 0) { // has nickname BE.pairs[pn][0] = members[id1][1] + BE.pairs[pn][0].substring(BE.pairs[pn][0].indexOf(' ')); //alert("BE.pairs[pn][0] = " + BE.pairs[pn][0]); } id1spouse = members[id1][2]; } //name2 id2 = getMembersID(BE.pairs[pn][1]); if (id2 === 0) { //a visitor visitor = true; BE.pairs[pn][1] = "" + BE.pairs[pn][1] + ""; //alert("visitor: pn = " + pn + " " + BE.pairs[pn][1]); } else { // member if (members[id2][1].length > 0) { // has nickname BE.pairs[pn][1] = members[id2][1] + BE.pairs[pn][1].substring(BE.pairs[pn][1].indexOf(' ')); //alert("nickname = " + members[id2][1]); //alert("surname = " + BE.pairs[pn][1].substring(BE.pairs[pn][1].indexOf(' '))); //alert("BE.pairs[pn][1] = " + BE.pairs[pn][1]); } //look for couples if (id2 === id1spouse) { // a couple BE.pairs[pn][0] = BE.pairs[pn][0].substring(0, BE.pairs[pn][0].indexOf(' ')); //alert("couple pn = " + pn + " " + BE.pairs[pn][0] + " & " + BE.pairs[pn][1]); } } } } } function travellerRank(board, index1, index2) { // Sorts the traveller rows in order of BE.travellers[board][index1] and places the ranks in BE.travellers[board][index2] // On exit the traveller rows are in the final sorted order var r, rank_a = [[], [], [], []]; sortArray(BE.travellers[board], index1); for (r = 0; r < BE.travellers[board].length; r += 1) { // initialize rank_a[r] rank_a[r] = [r, BE.travellers[board][r][index1], r + 1, ""]; } for (r = 1; r < BE.travellers[board].length; r += 1) { if(rank_a[r][1] === rank_a[r - 1][1]) { // find = positions rank_a[r][2] = rank_a[r - 1][2]; rank_a[r][3] = "="; rank_a[r - 1][3] = "="; } } for (r = 0; r < BE.travellers[board].length; r += 1) { // write ranks to BE.travellers[board][r][index2] BE.travellers[board][r][index2] = rank_a[r][2] + rank_a[r][3]; } }//END function travellerRank(board, indrx) function displayResults() { function writePairData(pair) { var url, id1, id2; if (BE.pairs[pair][0] === undefined) { divString += "missing pair"; } else { //alert(getMembersID("Peter Brazier")); divString += "" + BE.pairs[pair][2] + ""; //Rank url = "javascript: displayScoreCard(" + pair + ")"; divString += "" + (BE.pairs[pair][0] + " & " + BE.pairs[pair][1]).link(url) + ""; //pair names divString += "" + BE.pairs[pair][5] + ""; //% if (BE.points[2]) { // green points awarded divString += "" + (BE.pairs[pair][6] === undefined ? "" : BE.pairs[pair][6]) + ""; //green points } if (BE.points[1]) { // blue points awarded divString += "" + (BE.pairs[pair][7] === undefined ? "" : BE.pairs[pair][7]) + ""; //green points } if (BE.points[0]) { // local points awarded divString += "" + (BE.pairs[pair][8] === undefined ? "" : BE.pairs[pair][8]) + ""; //green points } divString += "" + BE.pairs[pair][3] + ""; //match points divString += "" + BE.pairs[pair][4] + ""; //max match points } } //END function writePairData(pair) function writePointsHeadings() { if (BE.points[2]) { divString += "green
pts"; } if (BE.points[1]) { divString += "blue
pts"; } if (BE.points[0]) { divString += "local
pts"; } } //END function writePointsHeadings var r, b, url, pair, pairNames, nCols = 5, divString = ""; divString += "
"; divString += ""; divString += "
"; divString += ""; if (BE.winners === 1) { nCols = nCols + BE.points[0] + BE.points[1] + BE.points[2] divString += ""; writePointsHeadings(); //divString += GPs ? "" : ""; //divString += ""; divString += ""; for (r = 0; r < BE.pairs.length - 1; r += 1) { pair = BE.rank[0][r][0]; divString += ""; writePairData(pair); divString += ""; } } else{ // 2 winners nCols = nCols + BE.points[0] + BE.points[1] + BE.points[2] //alert(nCols); divString += ""; divString += ""; divString += ""; writePointsHeadings(); divString += ""; divString += ""; writePointsHeadings(); divString += ""; divString += ""; for (r = 0; r <= BE.tables - 1; r += 1) { divString += ""; pair = BE.rank[0][r][0]; // NS writePairData(pair); pair = BE.rank[1][r][0]; // EW writePairData(pair); divString += ""; } } divString += "
RankPair%GPLPmatch
points
max
mps
North/SouthEast/West
RankPair%match
points
max
mps
RankPair%match
points
max
mps
"; if (visitor) { divString += "Vistors are shown in italics"; } divString += ""; divString += "
"; divString += "
"; //Set the divString to the div on the page document.getElementById('pageContent').innerHTML = divString; }// END function DisplayResults function displayTraveller(board) { function displayHand(){ function writeHand(iSeat){ return "♠ " + thisHand[iSeat][0] + "
" + "♥ " + thisHand[iSeat][1] + "
" + "♦ " + thisHand[iSeat][2] + "
" + "♣ " + thisHand[iSeat][3]; } //END function writeHand(hand) //Start of function displayHand() var thisHand, iSeat; if (BE.hands[board] === undefined){ //no hand data for this board return; } else { //generate hand[seat][suit] array where seat = 0 for N, 1 for E, 2 for S and 3 for W suit = 0 for spades, 1 for hearts, 2 for diamonds and 3 for clubs //alert("BE.hands[board].length = " + BE.hands[1].length); if (BE.hands[board].length === 67){ //exact number of characters required thisHand = BE.hands[board]; thisHand = thisHand.split(","); for (iSeat = 0; iSeat < 4; iSeat += 1){ thisHand[iSeat] = thisHand[iSeat].split(" "); } divString += ""; divString += ""; //north divString += ""; //west divString += ""; divString += "";//east divString += ""; //south divString += "
" + writeHand(0) + "
" + writeHand(3) + "
" + writeHand(1) + "
" + writeHand(2) + "
"; } } } //end of displayHand() //Start of function displayTraveller(board) var positions = ['north','east','south','west'], b, row, url, leadColumn, contract, dealer = positions[(board-1)%4], NSvul = (vulnerable(board, 0) ? true: false), EWvul = (vulnerable(board, 1) ? true: false), divString = ""; divString += ""; divString += "

Traveller for Board " + board + "  -  " + BE.sessionDateStr + "

"; if (BE.hands.length>0) { //H file present displayHand(); } leadColumn = displayLead(board); //true if a lead on this board has been entered divString += ""; divString += ""; divString += ""; divString += ""; divString += leadColumn ? "" : ""; divString += ""; divString += ""; divString += ""; divString += ""; if (leadColumn) { divString += ""; } divString += ""; for (row = 0; row < BE.travellers[board].length; row += 1) { clrStr = parseCRLstr(board, row); contract = clrStr[0]; tricksMade = clrStr[1]; declarer = clrStr[2]; lead = clrStr[3]; divString += ""; divString += ""; //rank divString += ""; //NS pair names divString += ""; //EW rank divString += ""; //EW pair names divString += "'; //contract divString += "'; //by if (leadColumn) { divString += "'; } if (contract !== "Pass") { divString += ''; //tricks } else { divString += ''; //tricks blank } divString += ''; //NS score divString += ''; //NS match points rounded to nearest/up 2dp divString += ''; //EW match points rounded to nearest/up 2dp divString += ''; } divString += '
North/South  " + (NSvul ? "(vulnerable)" : "(not vulnerable)") + "East/West  " + (EWvul ? "(vulnerable)" : "(not vulnerable)") + "" + " Dealer " + dealer + "" + " Dealer " + dealer + "
rank on
board
pairrank on
board
paircontractbyleadtricks score
NS
match pts
NS     EW
" + BE.travellers[board][row][6] + "" + (BE.pairs[BE.travellers[board][row][0]][0] + " & " + BE.pairs[BE.travellers[board][row][0]][1]) + "" + BE.travellers[board][row][7] + "" + (BE.pairs[BE.travellers[board][row][1]][0] + " & " + BE.pairs[BE.travellers[board][row][1]][1]) + "" + contract + '" + declarer + '" + lead + '' + tricksMade + '' + BE.travellers[board][row][3] + '' + round2dp(BE.travellers[board][row][4]) + '' + round2dp(BE.travellers[board][row][5]) + '
'; divString += "

For another traveller click on the board number below

"; if(board > 1) { b = board - 1; divString += "Previous  "; } else { divString += "              "; } for (b = 1; b < BE.travellers.length; b += 1) { divString += "" + b + ""; divString += "  "; } if(board < BE.travellers.length - 1) { b = board + 1; divString += " Next "; } divString += "

Back to Results"; if(BE.lastPair !== undefined) { divString += "    Back to Score Card"; } divString += "

"; document.getElementById('pageContent').innerHTML = divString; } //End displayTraveller(board) function getTravellerRow(board, pair) {// finds the row in travN in which pair occurs and returns the array[row][0 if sitting NS or 1 for EW][opposition pair number] var r = 0; for (r = 0; r < BE.travellers[board].length; r += 1) { if (pair === BE.travellers[board][r][0] || pair === BE.travellers[board][r][1]) { return r; } } return undefined; // pair did not play board } function parseCRLstr(board, row) { var crlStr, // the Contract/Score/Lead string from PairsScorer i, //index starting ar 0 of character in crlStr contract, tricksBid, tricksMade, declarer, lead; crlStr = BE.travellers[board][row][2]; if(crlStr.length > 0) { //not an average tricksBid = parseInt(crlStr,10); if (tricksBid > 0 && tricksBid < 8) { i = 1; if (crlStr.charAt(i) === 'N') { declarer = 'NT'; i = 3; } else { declarer = crlStr.charAt(i); i = 2; } while (crlStr.charAt(i) === 'x') { //skip over x or xx i += 1; } contract = crlStr.slice(0, i); //alert("contract = " + contract); switch(crlStr.charAt(i)) { case '=': tricksMade = tricksBid + 6; break; case '+': i += 1; tricksMade = tricksBid + 6 + parseInt(crlStr.charAt(i), 10); break; case '-': i += 1; tricksMade = tricksBid + 6 - parseInt(crlStr.charAt(i), 10); break; } i += 1; declarer = crlStr.charAt(i); i += 1; if (crlStr.length > i) { //lead has been entered lead = crlStr.slice(i); } else { lead = ' '; } return [contract, tricksMade, declarer, lead]; } if (crlStr.charAt(0) === 'P') { //Passed out return ['Pass',' ',' ',' ']; } else return [' ',' ',' ',' ']; //average } } //END function parseCRLstr(board, row) function displayLead(board) { var row, lead; for (row = 0; row < BE.travellers[board].length; row += 1) { lead = parseCRLstr(board, row)[3]; if (lead.length > 1) { return true; } } return false; } function displayScoreCard(pair) { var NSvul, EWvul, clrStr, contract, score, matchPoints, rank, sitting, board, url, row, oppPair, tricksMade, divString = ""; BE.lastPair = pair; divString += "
"; divString += ""; divString += "
"; divString += ""; divString += ""; divString += ""; divString += ""; divString += ""; divString += ""; for (board = 1; board < BE.travellers.length; board += 1) { row = getTravellerRow(board, pair); if (row !== undefined) { clrStr = parseCRLstr(board, row); contract = clrStr[0]; tricksMade = clrStr[1]; declarer = clrStr[2]; lead = clrStr[3]; NSvul = (vulnerable(board, 0) ? true: false); EWvul = (vulnerable(board, 1) ? true: false); divString += ""; url = "javascript: displayTraveller(" + board.toString() + ")"; board_str = board.toString(); divString += ""; sitting = (pair === BE.travellers[board][row][0]) ? "NS" : "EW"; if (sitting === "NS") { divString += ""; oppPair = BE.travellers[board][row][1]; divString += ""; rank = BE.travellers[board][row][6]; } else { divString += ""; oppPair = BE.travellers[board][row][0]; divString += ""; rank = BE.travellers[board][row][7]; } divString += ""; if (contract === "Pass") { divString += ""; //by divString += ""; //tricks made divString += ""; //score } else if (contract ===' ') { // average divString += ""; //by divString += ""; //tricks made divString += ""; //score } else { divString += ""; //by divString += ""; //tricks made score = BE.travellers[board][row][3]; if (sitting === "EW") { score *= -1; } divString += ""; //score } if (sitting === "NS") { matchPoints = BE.travellers[board][row][4]; } else { matchPoints = BE.travellers[board][row][5]; } divString += ""; //match points divString += ""; //rank divString += ""; } } divString += "
Board Sitting Opponentscontractbytricksscorematch
points
rank on
board
" + board.toString().link(url) + "NS" + (BE.pairs[oppPair][0] + " & " + BE.pairs[oppPair][1]) + "EW" + (BE.pairs[oppPair][0] + " & " + BE.pairs[oppPair][1]) + "" + contract + "" + 0 + "" + BE.travellers[board][row][3] + "" + declarer + "" + tricksMade + "" + score + "" + round2dp(matchPoints) + "" + rank + "
"; divString += "
vulnerable  not vulnerable                                        
"; divString += ""; divString += "
"; divString += "
"; document.getElementById('pageContent').innerHTML = divString; } //End displayScoreCard