本館粉絲專頁

使用者:Digipunk/SC/func.js:修訂版本之間的差異

分享此網頁到Facebook
分享此網頁到Plurk
分享此網頁到百度搜藏
分享此網頁到Twitter
分享此網頁到Del.icio.us
台灣棒球維基館
跳轉到: 導覽搜尋
第703行: 第703行:
 
       case 'TQB':
 
       case 'TQB':
 
         html += '<TD STYLE="' + (val !== null && val !== '' ? (val < 0 ? 'color:#C00;' : 'color:#080;') : '') + stl +'">';
 
         html += '<TD STYLE="' + (val !== null && val !== '' ? (val < 0 ? 'color:#C00;' : 'color:#080;') : '') + stl +'">';
         html += dgpk_round(val, 4);
+
         html += dgpk_round(val, 4, 1);
 
         html += '</TD>';
 
         html += '</TD>';
 
         break;
 
         break;
第756行: 第756行:
 
               html += '1.000';
 
               html += '1.000';
 
             } else {
 
             } else {
               html += dgpk_round(ATTR.TEAMS[ti].wlt.W / (ATTR.TEAMS[ti].wlt.W + ATTR.TEAMS[ti].wlt.L), 3);
+
               html += dgpk_round(ATTR.TEAMS[ti].wlt.W / (ATTR.TEAMS[ti].wlt.W + ATTR.TEAMS[ti].wlt.L), 3, 1);
 
             }
 
             }
 
           }
 
           }
第893行: 第893行:
 
             html += '1.000';
 
             html += '1.000';
 
           } else {
 
           } else {
             html += dgpk_round(ATTR.SUMMARY.W / (ATTR.SUMMARY.W + ATTR.SUMMARY.L), 3);
+
             html += dgpk_round(ATTR.SUMMARY.W / (ATTR.SUMMARY.W + ATTR.SUMMARY.L), 3, 1);
 
           }
 
           }
 
         }
 
         }
第909行: 第909行:
 
     html += '</TR>';
 
     html += '</TR>';
 
   } // END OF if (TOTALS)
 
   } // END OF if (TOTALS)
 
 
   //-----------------
 
   //-----------------
 
   // end of table
 
   // end of table
 
   //-----------------
 
   //-----------------
   html += '</DIV></DIV>';
+
   html += '</TABLE></DIV>';
 +
 
 +
  if (ATTR.NOTE) {
 +
    html += '<BR><TABLE STYLE="display:inline-block;margin:0;border-left:6px solid #DDD;margin-top:6px">';
 +
    html += '<TR VALIGN="top">';
 +
    html += '<TD NOWRAP>✎備註:</TD>';
 +
    html += '<TD>' + ATTR.NOTE + '</TD>';
 +
    html += '</TR>';
 +
    html += '</TABLE>';
 +
  }
 +
  //-----------------
 +
  // end of outer div
 +
  //-----------------
 +
  html += '</DIV>';
 +
 
 
   div.outerHTML = html;
 
   div.outerHTML = html;
  

2019年12月5日 (四) 09:04的修訂版本

//--------------------------------------------------
// SC (schedule) related
//--------------------------------------------------
function DGPKJS_SC() {
  // timezone conversion
  var elmts = document.getElementsByClassName('__DGPK_TZ_BTN__');
  for (var i = elmts.length - 1; i >= 0; i--) {
    elmts[i].addEventListener("click", __dgpk_tz_conv, false);
    elmts[i].title = '當地時間';
    elmts[i].className = 'DGPK-tz-btn';
  }
 
/*
  // DGPK-RNK2
  var RNK2 = document.getElementsByClassName('DGPK-RNK2-JS');
  for (var i = RNK2.length - 1; i >= 0; i--) {
    try {
      var ATTR = dgpk_arrange_raw_object(JSON.parse(RNK2[i].innerText.replace(/[\r\n]/g, '')));
      if (__dgpkjs_rnk_init(ATTR) == true) {
        __dgpkjs_rnk_populate_values(ATTR, RNK2[i].nextSibling);
      }
 
      RNK2[i].parentNode.removeChild(RNK2[i]);
      ATTR = null;
    } catch (e) {
      console.log(e);
    }
  }
*/
 
  // DGPK-RNK-AUTO
  var RNK_AUTO = document.getElementsByClassName('DGPK-RNK-AUTO-JS');
  for (var i = RNK_AUTO.length - 1; i >= 0; i--) {
    try {
      var ATTR = dgpk_arrange_raw_object(JSON.parse( RNK_AUTO[i].innerText.replace(/[\r\n]/g, '').replace(/\[\s*,/g, '[').replace(/\{\s*,/g, '{') ));
 
      if (!ATTR.SCHDID) {
        RNK_AUTO[i].outerHTML = '<DIV CLASS="DGPK-ERROR"><DIV>【賽程表/自動排行】錯誤:未指定賽程表 ID。</DIV></DIV>';
      } else {
        if (__dgpkjs_rnk_init(ATTR, 1) == true) {
          if (ATTR.SCHD.length == 0) {
            RNK_AUTO[i].outerHTML = '<DIV CLASS="DGPK-ERROR"><DIV>【賽程表/自動排行】錯誤:找不到指定的賽程表。</DIV></DIV>';
          } else {
            __dgpkjs_rnk_auto(ATTR, RNK_AUTO[i], i);
          }
        }
      }
    } catch (e) {
      console.log(e);
    }
  }
}
 
//----------------------------
// timezone conversion
//----------------------------
function __dgpk_tz_conv(event) {
  var o = event.srcElement || event.target;
 
  var p = o;
  var safe_cnt = 10;
  while ((p = p.parentNode) != null && --safe_cnt >= 0) {
    if (p.tagName == 'TBODY' || p.tagName == 'TABLE') {
      break;
    }
  }
  if (p.tagName != 'TBODY' && p.tagName != 'TABLE') {
    return false;
  }
 
  var attr = decodeURIComponent(o.getAttribute('data-str')).replace(/\s/g, '').split('!');
  var offset = __dgpk_tz_offset(attr[0]);
  if (offset === false) {
    return false;
  }
 
  var yy = attr[1];
  if (!yy || isNaN(yy)) {
    yy = (new Date()).getFullYear();
  }
 
  // seems fine
  var ATTR = [
    {c:'DGPK-tz-btn',  s:'當地時間'},
    {c:'DGPK-tz-btn1', s:'台灣時間'}
  ];
 
  var act = (o.className.indexOf(ATTR[1].c) >= 0 ? 0 : 1);
  o.className = ATTR[act].c;
  o.title = ATTR[act].s;
 
  var date = null;
  for (var i = 0, ii = p.children.length; i < ii; i++) {
    var tr = p.children[i];
    if (tr.tagName != 'TR') break; // something wrong
 
    var mat = tr.children[0].innerText.trim().match(/^\d+\/\d+/);
    if (mat != null) {
      date = yy + '/' + mat[0];
    }
    if (date == null) continue;
 
    var TD = tr.getElementsByClassName('time-val');
    for (var j = 0, jj = TD.length; j < jj; j++) {
      var td = TD[j];
      var time = td.innerText.trim();
 
      attr = td.getAttribute('data-str');
      if (!attr) {
        // do conversion
        attr = time + '!' + __dgpk_tz_conv_do(date, time, offset);
        td.setAttribute('data-str', attr);
      }
 
      td.innerHTML = attr.split('!')[act];
    }
  }
 
  // 好喵做到底,按一次就執行全部
  var BTN = document.getElementsByClassName(ATTR[(act ^ 1)].c);
  BTN[0] && BTN[0].click();  // 由於會一個呼叫一個,每個只要做一次即可
}
 
function __dgpk_tz_offset(tz) { // return tz offset in seconds
  if (!isNaN(tz) && tz.length <= 3) {
    return 3600 * (8 + parseFloat(tz));
  } else {
    var mat = tz.match(/^UTC([\+\-])([0-9:]+)$/);
    if (mat == null) return false;
    if (mat[2].indexOf(':') > 0) { // UTC+08:00
      var hm = mat[2].split(':');
      return (3600 * hm[0] + 60 * hm[1]) * (mat[1] == '+' ? 1 : -1);
    } else if (mat[2].length == 4) { // UTC+0800
      return (3600 * mat[2].substr(0, 2) + 60 * mat[2].substr(2)) * (mat[1] == '+' ? 1 : -1);
    } else {
      return false;
    }
  }
}
 
function __dgpk_tz_conv_do(date, time, offset) {
  if (time.match(/^\d+:\d+/) == null) return time;
 
  var dd = new Date(date + ' ' + time);
  var utc = dd.getTime() - 60000 * dd.getTimezoneOffset() - 1000 * offset;
  var nd = new Date(utc);
 
  var M = nd.getMonth() + 1;
  var D = nd.getDate();
  var h = nd.getHours()
  var m = nd.getMinutes();
  var ret = (h < 10 ? '0' : '') + h + ':' + (m < 10 ? '0' : '') + m;
  ret = '<U>' + ret + '</U>';
 
  var ori = date.split('/');
  if (M != parseInt(ori[1]) || D != parseInt(ori[2])) {
    ret += '<BR><SPAN STYLE="color:deeppink">' + (M < 10 ? '0' : '') + M + '/' + (D < 10 ? '0' : '') + D + '</SPAN>';
  }
 
  return ret;
}
 
 
//--------------------------------------------------
// [2019-09-10] 
// dynamically calculate the TQB values
// called by User:Digipunk/SC/TQB/Renderer2
//--------------------------------------------------
function DGPKJS_TQB() {{{
  var TQB = document.getElementsByClassName('DGPK-TQB-JS');
  for (var i = TQB.length - 1; i >= 0; i--) {
    var start_sn = TQB[i].getAttribute('data-str');
    var DESCR = TQB[i].getElementsByClassName('de');
    for (var j = DESCR.length - 1; j >= 0; j--) {
      var mat, mat2, tmp, tmp2;
      var r = '', ra = '', o = '', d = '';
      var oa = 0, ob = 0;
      var da = 0, db = 0;
      var getval = 0;
      var of = df = 0.0;
 
      var descr = DESCR[j].innerText.replace(/\s+/g, '');
      if (descr.substr(0, 7) == 'getval:') {
        getval = 1;
        descr = descr.substr(7);
      }
 
      var TOKEN = descr.split('/');
 
      if (TOKEN.length == 4) {
        //-------------------------------------
        // mode = 1 (numeric values)
        //-------------------------------------
        if (TOKEN[0] != '') eval('r  = ' + TOKEN[0] + ';');
        if (TOKEN[2] != '') eval('ra = ' + TOKEN[2] + ';');
 
        if (TOKEN[1] == '') {
        } else if (TOKEN[1].indexOf('+') < 0) {
          o = TOKEN[1] + (TOKEN[1].indexOf('.') < 0 ? '.0' : '');
        } else {
          tmp = TOKEN[1].split('+');
          for (var k = 0, kk = tmp.length; k < kk; k++) {
            tmp2 = tmp[k].split('.');
            oa += parseInt(tmp2[0]);
            if (tmp2.length == 2) {
              ob += parseInt(tmp2[1]);
              oa += parseInt(ob / 3);
              ob %= 3;
            }
          }
          o = oa + '.' + ob;
        }
 
        if (TOKEN[3] == '') {
        } else if (TOKEN[3].indexOf('+') < 0) {
          d = TOKEN[3] + (TOKEN[3].indexOf('.') < 0 ? '.0' : '');
        } else {
          tmp = TOKEN[3].split('+');
          for (var k = 0, kk = tmp.length; k < kk; k++) {
            tmp2 = tmp[k].split('.');
            da += parseInt(tmp2[0]);
            if (tmp2.length == 2) {
              db += parseInt(tmp2[1]);
              da += parseInt(ob / 3);
              db %= 3;
            }
          }
          d = da + '.' + db;
        }
      } else {
        //-------------------------------------
        // mode = 2 (sentence)
        //-------------------------------------
        if ((mat = descr.match(/得\d+分/g)) != null) {
          r = 0;
          for (var k = 0, kk = mat.length; k < kk; k++) {
            if ((mat2 = mat[k].match(/\d+/)) != null) {
              r += parseInt(mat2[0]);
            }
          }
        }
        if ((mat = descr.match(/失\d+分/g)) != null) {
          ra = 0;
          for (var k = 0, kk = mat.length; k < kk; k++) {
            if ((mat2 = mat[k].match(/\d+/)) != null) {
              ra += parseInt(mat2[0]);
            }
          }
        }
 
        if ((mat = descr.match(/攻[0-9\.]+局/g)) != null) {
          for (var k = 0, kk = mat.length; k < kk; k++) {
            if ((mat2 = mat[k].match(/(\d+)\.?(\d*)/)) != null) {
              oa += parseInt(mat2[1]);
              if (mat2[2]) {
                ob += parseInt(mat2[2]);
                oa += parseInt(ob / 3);
                ob %= 3;
              }
            }
          }
        }
        if ((mat = descr.match(/(守|備)[0-9\.]+局/g)) != null) {
          for (var k = 0, kk = mat.length; k < kk; k++) {
            if ((mat2 = mat[k].match(/(\d+)\.?(\d*)/)) != null) {
              da += parseInt(mat2[1]);
              if (mat2[2]) {
                db += parseInt(mat2[2]);
                da += parseInt(db / 3);
                db %= 3;
              }
            }
          }
        }
 
        o = oa + '.' + ob;
        d = da + '.' + db;
      }
 
      if (o) {
        var tmpf = o.split('.');
        of = parseInt(tmpf[0]) + parseInt(tmpf[1]) / 3;
      }
      if (d) {
        var tmpf = d.split('.');
        df = parseInt(tmpf[0]) + parseInt(tmpf[1]) / 3;
      }
 
      var xo = (r !== '' && o ? r / of : '');
      var xd = (ra !== '' && d ? ra / df : '');
 
      var xx = (xo && xd ? dgpk_round(xo - xd, 4) : '');
 
      if (getval == 1) {
        TQB[i].outerHTML = (xx !== '' && xx >= 0 ? '+' : '') + xx;
        break;
      } else {
        if (xx !== '') {
          if (xx >= 0) {
            xx = '<SPAN STYLE="color:blue"><B>+' + xx + '</B></SPAN>';
          } else {
            xx = '<SPAN STYLE="color:red"><B>' + xx + '</B></SPAN>';
          }
        }
 
        var TR = DESCR[j].parentElement.parentElement;
        if (start_sn) {
          TR.children[0].innerHTML = parseInt(TR.children[0].innerHTML) + parseInt(start_sn) - 1;
        }
        TR.children[2].innerHTML = r;
        TR.children[3].innerHTML = o;
        TR.children[4].innerHTML = dgpk_round(xo, 4);
        TR.children[5].innerHTML = ra;
        TR.children[6].innerHTML = d;
        TR.children[7].innerHTML = dgpk_round(xd, 4);
        TR.children[8].innerHTML = xx;
      }
    } // DESCR for
 
    if (TQB[i]) TQB[i].className = 'DGPK-TQB';
  } // TQB for
}}}
 
 
//========================================
// [2019-12-04] 
// JS-RNK : init
//========================================
function __dgpkjs_rnk_init(ATTR, auto) {{{
  var num_teams = ATTR.TEAMS.length;
 
  if (auto == 1) {
    ATTR.SCHD = [];
    ATTR.SCHDID = dgpk_to_array(ATTR.SCHDID, '/', 1);
 
    for (var i = 0; i < ATTR.SCHDID.length; i++) {
      if (document.getElementById(ATTR.SCHDID[i]) && document.getElementById(ATTR.SCHDID[i]).tagName == 'TABLE') {
        ATTR.SCHD.push(document.getElementById(ATTR.SCHDID[i]));
      }
    }
    if (ATTR.SCHD.length == 0) {
      return -1;
    }
  }
 
  //----------------------------
  // initialize TEAMS
  //----------------------------
  for (var ti = 0; ti < num_teams; ti++) {
    if (ATTR.TEAMS[ti].name) {
      switch (ATTR.TEAMS[ti].name.substr(0, 3)) {
      case 'ROC':
      case 'TWN':
      case 'TW-':
        ATTR.TEAMS[ti].sbg = 'background:#FFB;';
        break;
      case 'EX-':
        ATTR.TEAMS[ti].sbg = 'background:#FDD;';
        break;
      }
 
      if (ATTR.TEAMS[ti].alias) {  // 若有 alias 就優先使用
        ATTR.TEAMS[ti].alias = ATTR.TEAMS[ti].alias.replace(/\[\[(File|Image):([^\]\|]+)\|[^\]\|]*\|(\d+)px.*\]\]/g, function(x, a, b, c) {
          return '<IMG SRC="' + dgpk_get_fileurl(b) + '" STYLE="width:' + c + 'px;height:auto" />';
        });
      } else {
        // 若為 country code 就解開並放進 alias
        if (/^[A-Z\-]+$/.test(ATTR.TEAMS[ti].name) && (descr = DGPK_TEAMX_COUNTRIES[ATTR.TEAMS[ti].name]) != null) {
          ATTR.TEAMS[ti].alias = dgpk_teamx_country(ATTR.TEAMS[ti].name, descr);  // create TeamX if it's country code
        }
      }
    }
 
    ATTR.TEAMS[ti].chk = dgpk_strip_tags(ATTR.TEAMS[ti].alias || ATTR.TEAMS[ti].name).replace(/\s/g, '');  // 稍後當作比對用的關鍵字
    ATTR.TEAMS[ti].wlt = {W:0, L:0, T:0, X:0, N:0};  // WLTX-
    ATTR.TEAMS[ti].wls = [];  // WL Sequence
  }
 
  //----------------------------
  // calculate WLTX- and WLS
  //----------------------------
  ATTR.SUMMARY = {W:0, L:0, T:0, X:0, N:0};
 
  if (auto == 1) {
    ATTR.CTAB = [];
    for (var ti = 0; ti < num_teams; ti++) {
      ATTR.CTAB[ti] = [];
    }
 
    var num_schd = ATTR.SCHD.length;
    for (var si = 0; si < num_schd; si++) {
      var ROWS = ((ATTR.SCHD[si].getElementsByTagName('TBODY') && ATTR.SCHD[si].getElementsByTagName('TBODY')[0]) || ATTR.SCHD[si]).rows;
      var num_rows = ROWS.length;
 
      var excmark = [];
      if (si == 0) {
        for (var ti = 0; ti < num_teams; ti++) {
          excmark[ti] = 1;
        }
      } else {
        for (var ti = 0; ti < num_teams; ti++) {
          excmark[ti] = 0;
        }
      }
 
      for (var ri = 0; ri < num_rows; ri++) {
        var html = ROWS[ri].innerHTML.replace(/\s/g, '');
        var text = ROWS[ri].innerText.replace(/\s/g, '');
 
        var found = [];
        for (var ti = 0; ti < num_teams; ti++) {
          if (html.indexOf('>' + ATTR.TEAMS[ti].chk + '<') >= 0) {
            found.push(ti);
          }
          if (found.length == 2) {
            var gn_mat = html.match(/id="Game_(\d+)_.E8.B3.BD.E7.A8.8B.E8.A1.A8"/);
            var gn = (gn_mat ? '/' + gn_mat[1] : '');
 
            if (excmark[found[0]] == 0) {
              excmark[found[0]] = 1;
              ATTR.TEAMS[found[0]].wls.push('!');
            }
            if (excmark[found[1]] == 0) {
              excmark[found[1]] = 1;
              ATTR.TEAMS[found[1]].wls.push('!');
            }
            ATTR.CTAB[found[0]][found[1]] = (ATTR.CTAB[found[0]][found[1]] ? ATTR.CTAB[found[0]][found[1]] + '<BR>' : '');
            ATTR.CTAB[found[1]][found[0]] = (ATTR.CTAB[found[1]][found[0]] ? ATTR.CTAB[found[1]][found[0]] + '<BR>' : '');
 
            if (text.indexOf('取消') >= 0) {
              ATTR.TEAMS[found[0]].wlt.X++;
              ATTR.TEAMS[found[1]].wlt.X++;
              ATTR.TEAMS[found[0]].wls.push('X' + gn);
              ATTR.TEAMS[found[1]].wls.push('X' + gn);
              ATTR.CTAB[found[0]][found[1]] += 'X';
              ATTR.CTAB[found[1]][found[0]] += 'X';
              ATTR.SUMMARY.X += 2;
            } else {
              var t1 = t2 = -1;
              var regex = new RegExp(ATTR.TEAMS[found[0]].chk + '[\\D]*(\\d+):(\\d+)[\\D]*' + ATTR.TEAMS[found[1]].chk);
              var mat = text.match(regex);
              if (mat) {
                t1 = 0;
                t2 = 1;
              } else {
                regex = new RegExp(ATTR.TEAMS[found[1]].chk + '[\\D]*(\\d+):(\\d+)[\\D]*' + ATTR.TEAMS[found[0]].chk);
                mat = text.match(regex);
                if (mat) {
                  t1 = 1;
                  t2 = 0;
                }
              }
              if (mat) {
                var a = parseInt(mat[1]);
                var b = parseInt(mat[2]);
                if (a > b) {
                  ATTR.TEAMS[found[t1]].wlt.W++;
                  ATTR.TEAMS[found[t2]].wlt.L++;
                  ATTR.TEAMS[found[t1]].wls.push('W' + gn);
                  ATTR.TEAMS[found[t2]].wls.push('L' + gn);
                  ATTR.CTAB[found[t1]][found[t2]] += '○ ' + a + ':' + b;
                  ATTR.CTAB[found[t2]][found[t1]] += '● ' + b + ':' + a;
                  ATTR.SUMMARY.W++;
                  ATTR.SUMMARY.L++;
                } else if (a < b) {
                  ATTR.TEAMS[found[t1]].wlt.L++;
                  ATTR.TEAMS[found[t2]].wlt.W++;
                  ATTR.TEAMS[found[t1]].wls.push('L' + gn);
                  ATTR.TEAMS[found[t2]].wls.push('W' + gn);
                  ATTR.CTAB[found[t1]][found[t2]] += '● ' + a + ':' + b;
                  ATTR.CTAB[found[t2]][found[t1]] += '○ ' + b + ':' + a;
                  ATTR.SUMMARY.W++;
                  ATTR.SUMMARY.L++;
                } else {
                  ATTR.TEAMS[found[0]].wlt.T++;
                  ATTR.TEAMS[found[1]].wlt.T++;
                  ATTR.TEAMS[found[0]].wls.push('T' + gn);
                  ATTR.TEAMS[found[1]].wls.push('T' + gn);
                  ATTR.CTAB[found[t1]][found[t2]] += '△ ' + a + ':' + b;
                  ATTR.CTAB[found[t2]][found[t1]] += '△ ' + b + ':' + a;
                  ATTR.SUMMARY.T += 2;
                }
 
                var pad = '&nbsp;&nbsp;&nbsp;&nbsp;'.substr(0, 6 * (4 - ('' + mat[1] + mat[2]).length));
                ATTR.CTAB[found[t1]][found[t2]] += pad;
                ATTR.CTAB[found[t2]][found[t1]] += pad;
              } else {
                ATTR.TEAMS[found[0]].wlt.N++;
                ATTR.TEAMS[found[1]].wlt.N++;
                ATTR.TEAMS[found[0]].wls.push('-' + gn);
                ATTR.TEAMS[found[1]].wls.push('-' + gn);
                ATTR.SUMMARY.N += 2;
              }
            }
            break;
          }
        }  // END OF for (TEAMS)
      }  // END OF for (ROWS)
    }  // END OF for (SCHD)
 
  } else { // not auto
  }
 
  ATTR.TEAMS[0].gb = (ATTR.TEAMS[0].wlt.W - ATTR.TEAMS[0].wlt.L) / 2;  // GB base
 
  // to array
  ATTR.POINTS = dgpk_to_array(ATTR.POINTS);
  ATTR.COLNAMES = dgpk_to_array(ATTR.COLNAMES);
 
  ATTR.LN1 = dgpk_to_array(ATTR.LN1, '/', 1);
  ATTR.LN2 = dgpk_to_array(ATTR.LN2, '/', 1);
  ATTR.LN3 = dgpk_to_array(ATTR.LN3, '/', 1);
 
  //------------------------
  // columns
  //------------------------
  ATTR.COLIDX = {};
  for (var i = 0; i < ATTR.COLNAMES.length; i++) {
    if (ATTR.COLNAMES[i]) {
      ATTR.COLIDX[ATTR.COLNAMES[i]] = i;
    }
  }
 
  // magic2
  if (ATTR.COLNAMES.indexOf('晉級') >= 0) {
    var idx = ATTR.COLIDX['晉級'];
    var VAL = dgpk_to_array(ATTR.EVC[idx]);
 
    for (var ti = 0; ti < num_teams; ti++) {
      var val = '' + VAL[ti];
      if (i == 0 || val != '' + VAL[ti - 1]) {
        ATTR.TEAMS[ti].m2 = 1;
 
        for (var x = ti + 1; x < num_teams; x++) {
          if (val == '' + VAL[x]) {
            ATTR.TEAMS[ti].m2++;
          } else {
            ti += x - (ti + 1);
            break;
          }
        }
      }
    }
  }
 
  return true;
}}}
 
function __dgpk_wls(arr) {{{
  var ret = '';
  ret += '<DIV CLASS="WLS">';
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == '!') {
      ret += '<tt> </tt>';
    } else {
      var tok = arr[i].split('/');
      ret += '<DIV CLASS="DGPK-BN-' + tok[0] + '"';
      if (tok[1]) {
        ret += ' STYLE="cursor:pointer"';
        ret += ' TITLE="Game ' + tok[1] + '"';
        ret += ' onclick="document.getElementById(\'Game_' + tok[1] + '_.E8.B3.BD.E7.A8.8B.E8.A1.A8\').scrollIntoView();return false"';
      }
      ret += '>' + tok[0] + '</DIV>';
    }
  }
  ret += '</DIV>';
  return ret;
}}}
 
 
//========================================
// [2019-12-04] 
// JS-RNK : create the ranking table
//========================================
function __dgpkjs_rnk_auto(ATTR, div, di) {{{
  var div_id = 'DGPK-RNKTAB-' + di;
  var num_cols = ATTR.COLNAMES.length;
  var num_teams = ATTR.TEAMS.length;
 
  //-----------------
  // init
  //-----------------
  var html = '';
  html += '<DIV ID="' + div_id + '" CLASS="DGPK-RNK" STYLE="opacity:0;' + ATTR.STL_DIV + '"><DIV>';
  html += '<TABLE CLASS="DGPK-rows mover" BORDER="1" CELLSPACING="0" CELLPADDING="' + (num_teams > 6 ? 2 : 4) + '" STYLE="' + (ATTR.THEME == 1 ? 'border-color:#AAA;' : '') + ATTR.STL_TAB + '">';
 
  //-----------------
  // title
  //-----------------
  html += '<TR CLASS="title" STYLE="' + (ATTR.THEME == 1 ? 'background:#666;color:white;' : '') + ATTR.STL_TTL + '">';
  html += '<TH COLSPAN="' + (num_cols + (ATTR.COLIDX['對戰比分'] ? num_teams : 0)) + '">' + ATTR.TITLE + '</TH>';
  html += '</TR>';
 
  //-----------------
  // columns
  //-----------------
  html += '<TR CLASS="column"' + (ATTR.THEME == 1 ? ' STYLE="background:#888;color:white"' : '') + '>';
  for (var ci = 0; ci < num_cols; ci++) {
    var col = ATTR.COLNAMES[ci];
    if (!col) continue;
 
    var idx = ATTR.COLIDX[col];
    var stl = ATTR.STH[idx] || '';
 
    if (ATTR.LN1.indexOf(col) >= 0) {
      stl = 'border-left:1px solid black;' + stl;
    } else if (ATTR.LN2.indexOf(col) >= 0) {
      stl = 'border-left:2px solid black;' + stl;
    } else if (ATTR.LN3.indexOf(col) >= 0) {
      stl = 'border-left:3px double black;' + stl;
    }
 
    html += '<TH STYLE="';
    switch (col) {
    case '#':
      html += 'width:15px;';
      break;
    case '排行':
    case '排名':
      html += 'width:30px;';
      break;
    case 'TQB':
      html += 'width:55px;';
      break;
    case '對戰比分':
      html += 'width:1px;background:#CCC;color:black;';
      break;
    }
    html += (ATTR.STL_COL || '') + ';' + stl + '">';
 
    switch (col) {
    case 'ENO':
    case '淘汰數字':
    case '淘汰指數':
      html += '<SPAN STYLE="font-family:Consolas,monospace;color:' + (ATTR.THEME == 1 ? 'yellow' : 'blue') + '" TITLE="淘汰數字">E#</SPAN>';
      break;
    case 'AENO':
    case '晉級淘汰數字':
    case '晉級淘汰指數':
      html += '<SPAN STYLE="font-family:Consolas,monospace;color:' + (ATTR.THEME == 1 ? 'yellow' : 'blue') + '" TITLE="晉級淘汰數字">AE#</SPAN>';
      break;
    case 'TQB':
    case 'W-L-T':
    case 'W-T-L':
    case 'TQB':
      html += '<SPAN STYLE="font-family:Consolas,monospace">' + col + '</SPAN>';
      break;
    case '對戰比分':
      for (var ti = 0; ti < num_teams; ti++) {
        html += ATTR.TEAMS[ti].alias || ATTR.TEAMS[ti].name;
        if (ti < num_teams - 1) {
          html += '</TH><TH STYLE="width:1px;background:#CCC;color:black;' + stl + ';border-left:1px solid #AAA">';
        }
      }
      break;
    default:
      html += col;
      break;
    }
    html += '</TH>';
  }
  html += '</TR>';
 
  //-----------------
  // body
  //-----------------
  for (var ti = 0; ti < num_teams; ti++) {
    html += '<TR CLASS="body"' + (ti == 0 || ATTR.TEAMS[ti].m2 || ti == ATTR.ADVN ? ' STYLE="border-top:1px solid black"' : '') + '>';
 
    for (var ci = 0; ci < num_cols; ci++) {
      var col = ATTR.COLNAMES[ci];
      if (!col) continue;
 
      var idx = ATTR.COLIDX[col];
      var stl = ATTR.STC[idx] || '';
      var VAL = dgpk_to_array(ATTR.EVC[idx]);
      var val = VAL[ti] || null;
 
      if (ATTR.LN1.indexOf(col) >= 0) {
        stl = 'border-left:1px solid black;' + stl;
      } else if (ATTR.LN2.indexOf(col) >= 0) {
        stl = 'border-left:2px solid black;' + stl;
      } else if (ATTR.LN3.indexOf(col) >= 0) {
        stl = 'border-left:3px double black;' + stl;
      }
 
      var eno_base = 0;
 
      switch (col) {
      case '晉級':
        if (ATTR.TEAMS[ti].m2) {
          html += '<TD ROWSPAN="' + (ti + ATTR.TEAMS[ti].m2 > num_teams ? num_teams - ti : ATTR.TEAMS[ti].m2) + '" STYLE="' + (val !== null ? 'background:#FFF;' : 'background:#EEE;') + stl + '}">' + (val !== null ? val.replace('/\-/g', '<BR>') : '') + '</TD>';
        }
        break;
      case '球隊':
      case '對戰球隊':
      case '對戰層級':
        var descr;
        html += '<TD CLASS="team" STYLE="' + ATTR.TEAMS[ti].sbg + stl + '">';
        html += (ATTR.TEAMS[ti].alias || ATTR.TEAMS[ti].name || '');
        html += '</TD>';
        break;
      case 'TQB':
        html += '<TD STYLE="' + (val !== null && val !== '' ? (val < 0 ? 'color:#C00;' : 'color:#080;') : '') + stl +'">';
        html += dgpk_round(val, 4, 1);
        html += '</TD>';
        break;
      case '對戰比分':
        for (var tii = 0; tii < num_teams; tii++) {
          html += '<TD CLASS="ctab" STYLE="';
          if (ti == tii) {
            html += 'background:#DDD;' + stl + ';' + (tii > 0 ? 'border-left:1px solid #AAA;' : '') + '"></TD>';
          } else {
            html += stl + ';' + (tii > 0 ? 'border-left:1px solid #AAA;' : '') + '">';
            html += ATTR.CTAB[ti][tii] || '';
            html += '</TD>';
          }
        }
        break;
      default:
        html += '<TD STYLE="' + stl + '">';
        switch (col) {
        case '#':
        case '排行':
          html += (ti + 1);
          break;
        case '應賽':
          html += Object.values(ATTR.TEAMS[ti].wlt).reduce((a,b)=>a+b);  // sum values
          break;
        case '已賽':
          html += ATTR.TEAMS[ti].wlt.W + ATTR.TEAMS[ti].wlt.L + ATTR.TEAMS[ti].wlt.T;
          break;
        case '勝場':
          html += ATTR.TEAMS[ti].wlt.W || '<SPAN STYLE="color:#CCC">0</SPAN>';
          break;
        case '敗場':
          html += ATTR.TEAMS[ti].wlt.L || '<SPAN STYLE="color:#CCC">0</SPAN>';
          break;
        case '和局':
          html += ATTR.TEAMS[ti].wlt.T || '<SPAN STYLE="color:#CCC">0</SPAN>';
          break;
        case '取消':
        case '延賽':
          html += ATTR.TEAMS[ti].wlt.X || '<SPAN STYLE="color:#CCC">0</SPAN>';
          break;
        case '未賽':
          html += ATTR.TEAMS[ti].wlt.N || '<SPAN STYLE="color:#CCC">0</SPAN>';
          break;
        case '勝率':
          if (ATTR.TEAMS[ti].wlt.W + ATTR.TEAMS[ti].wlt.L == 0) {
            html += '-';
          } else {
            if (ATTR.TEAMS[ti].wlt.W == 0) {
              html += '.000';
            } else if (ATTR.TEAMS[ti].wlt.L == 0) {
              html += '1.000';
            } else {
              html += dgpk_round(ATTR.TEAMS[ti].wlt.W / (ATTR.TEAMS[ti].wlt.W + ATTR.TEAMS[ti].wlt.L), 3, 1);
            }
          }
          break;
        case '勝差':
          var gb = ATTR.TEAMS[0].gb - (ATTR.TEAMS[ti].wlt.W - ATTR.TEAMS[ti].wlt.L) / 2;
          html += (gb == 0 ? '-' : gb);
          break;
        case '積分':
          html += (ATTR.TEAMS[ti].wlt.W * ATTR.POINTS[0] + ATTR.TEAMS[ti].wlt.L * ATTR.POINTS[1] + ATTR.TEAMS[ti].wlt.T * ATTR.POINTS[2]);
          break;
        case '勝負記錄':
          html += __dgpk_wls(ATTR.TEAMS[ti].wls);
          break;
        case '勝負場數':
          html += ATTR.TEAMS[ti].wlt.W + 'W-' + ATTR.TEAMS[ti].wlt.L + 'L' + (ATTR.TEAMS[ti].wlt.T ? '-' + ATTR.TEAMS[ti].wlt.T + 'T' : '');
          break;
        case '勝負和':
        case '勝-負-和':
        case 'W-L-T':
          html += ATTR.TEAMS[ti].wlt.W + '-' + ATTR.TEAMS[ti].wlt.L + '-' + ATTR.TEAMS[ti].wlt.T;
          break;
        case '勝和負':
        case '勝-和-負':
        case 'W-T-L':
          html += ATTR.TEAMS[ti].wlt.W + '-' + ATTR.TEAMS[ti].wlt.T + '-' + ATTR.TEAMS[ti].wlt.L;
          break;
        case 'AENO':
        case '晉級淘汰數字':
        case '晉級淘汰指數':
          eno_base = (!ATTR.ADVN ? -1 : ATTR.ADVN - 1);
          // follow thru
        case 'ENO':
        case '淘汰數字':
        case '淘汰指數':
          if (eno_base < 0 || ti <= eno_base || JSON.stringify(ATTR.TEAMS[eno_base].wlt) == JSON.stringify(ATTR.TEAMS[ti].wlt)) {
            html += '-';
          } else {
            var v1 = ATTR.TEAMS[eno_base].wlt.W + ATTR.TEAMS[eno_base].wlt.L + ATTR.TEAMS[eno_base].wlt.N;
            var v2 = ATTR.TEAMS[ti].wlt.W + ATTR.TEAMS[ti].wlt.L + ATTR.TEAMS[ti].wlt.N;
            var v3 = ATTR.TEAMS[ti].wlt.W + ATTR.TEAMS[ti].wlt.N;
            var v4 = ATTR.TEAMS[ti].wlt.W;
            var vx = v2 == 0 ? 'NaN' : Math.floor((v1 / v2) * v3 - v4 + 1);
            if (vx == 'NaN') {
              html += '';
            } else if (vx <= 0) {
              html += '<SPAN STYLE="color:red">e</SPAN>';
            } else {
              html += vx;
            }
          }
          break;
        default: // custom values
          html += val || '';
          break;
        }
        html += '</TD>';
        break;
      }
    }
    html += '</TR>';
  }
 
  //-----------------
  // summary
  //-----------------
  if (ATTR.TOTALS) {
    if (ATTR.TOTALS < 0 || ATTR.TOTALS > 2) ATTR.TOTALS = 1;  // 防呆
 
    html += '<TR CLASS="footer"' + (ATTR.THEME == 1 ? ' STYLE="background:#888;color:white"' : '') + '>';
    for (var ci = 0; ci < num_cols; ci++) {
      var col = ATTR.COLNAMES[ci];
      if (!col) continue;
 
      var idx = ATTR.COLIDX[col];
      var stl = ATTR.STH[idx] || '';
 
      if (ATTR.LN1.indexOf(col) >= 0) {
        stl = 'border-left:1px solid black;' + stl;
      } else if (ATTR.LN2.indexOf(col) >= 0) {
        stl = 'border-left:2px solid black;' + stl;
      } else if (ATTR.LN3.indexOf(col) >= 0) {
        stl = 'border-left:3px double black;' + stl;
      }
 
      html += '<TH STYLE="' + (ATTR.STL_COL || '') + ';' + stl + '">';
 
      switch (col) {
      case '球隊':
      case '對戰球隊':
      case '對戰層級':
        html += '總計或平均';
        break;
      case '應賽':
        html += (Object.values(ATTR.SUMMARY).reduce((a,b)=>a+b)) / ATTR.TOTALS;  // sum values
        break;
      case '已賽':
        html += (ATTR.SUMMARY.W + ATTR.SUMMARY.L + ATTR.SUMMARY.T) / ATTR.TOTALS;
        break;
      case '取消':
      case '延賽':
        html += (ATTR.SUMMARY.X) / ATTR.TOTALS;
        break;
      case '未賽':
        html += (ATTR.SUMMARY.N) / ATTR.TOTALS;
        break;
      case '勝場':
        html += ATTR.SUMMARY.W;
        break;
      case '敗場':
        html += ATTR.SUMMARY.L;
        break;
      case '和局':
        html += ATTR.SUMMARY.T;
        break;
      case '勝負場數':
        html += ATTR.SUMMARY.W + 'W-' + ATTR.SUMMARY.L + 'L' + (ATTR.SUMMARY.T ? '-' + ATTR.SUMMARY.T + 'T' : '');
        break;
      case '勝負和':
      case '勝-負-和':
      case 'W-L-T':
        html += ATTR.SUMMARY.W + '-' + ATTR.SUMMARY.L + '-' + ATTR.SUMMARY.T;
        break;
      case '勝和負':
      case '勝-和-負':
      case 'W-T-L':
        html += ATTR.SUMMARY.W + '-' + ATTR.SUMMARY.T + '-' + ATTR.SUMMARY.L;
        break;
      case '勝率':
        if (ATTR.SUMMARY.W + ATTR.SUMMARY.L == 0) {
          html += '-';
        } else {
          if (ATTR.SUMMARY.W == 0) {
            html += '.000';
          } else if (ATTR.SUMMARY.L == 0) {
            html += '1.000';
          } else {
            html += dgpk_round(ATTR.SUMMARY.W / (ATTR.SUMMARY.W + ATTR.SUMMARY.L), 3, 1);
          }
        }
        break;
      case '對戰比分':
        for (var ti = 0; ti < num_teams; ti++) {
          if (ti < num_teams - 1) {
            html += '</TH><TH STYLE="' + stl + ';border-left:1px solid #AAA">';
          }
        }
        break;
      }
      html += '</TH>';
    }
    html += '</TR>';
  } // END OF if (TOTALS)
  //-----------------
  // end of table
  //-----------------
  html += '</TABLE></DIV>';
 
  if (ATTR.NOTE) {
    html += '<BR><TABLE STYLE="display:inline-block;margin:0;border-left:6px solid #DDD;margin-top:6px">';
    html += '<TR VALIGN="top">';
    html += '<TD NOWRAP>✎備註:</TD>';
    html += '<TD>' + ATTR.NOTE + '</TD>';
    html += '</TR>';
    html += '</TABLE>';
  }
  //-----------------
  // end of outer div
  //-----------------
  html += '</DIV>';
 
  div.outerHTML = html;
 
  setTimeout(function() {
    document.getElementById(div_id).style.opacity = 1;
  }, 1);
 
}}}