MediaWiki:Common.js

From Epic Path
Revision as of 03:50, 3 August 2019 by Reese (talk | contribs)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

/* Point Buy Script -- Begin */

var ptby_pb = {
  spent: 0,
  avail: 28,
  rmods: {
    standard: [ 2, 2, -2 ],
    opt1: [ 4, -2, -2 ],
    opt2: [ 2, 1, 1, 1, -2 ],
  },
  costs: [],
};

ptby_pb.costs[7] = -4;
ptby_pb.costs[8] = -2;
ptby_pb.costs[9] = -1;
ptby_pb.costs[10] = 0;
ptby_pb.costs[11] = 1;
ptby_pb.costs[12] = 2;
ptby_pb.costs[13] = 3;
ptby_pb.costs[14] = 5;
ptby_pb.costs[15] = 7;
ptby_pb.costs[16] = 10;
ptby_pb.costs[17] = 13;
ptby_pb.costs[18] = 17;

var ptby_names = [
  'ptby_str',
  'ptby_dex',
  'ptby_con',
  'ptby_int',
  'ptby_wis',
  'ptby_cha'
];

function ptby_setNumber(val, usePlus = true) {
  var txt;

  if (val < 0) {
    txt = '\u2013';
  } else if (usePlus && (val > 0)) {
    txt = '+';
  } else {
    txt = '';
  }
  txt += Math.abs(val);
  return txt;
}

function ptby_calcMods() {
  var i, n, sel;

  for (i in ptby_names) {
    sel = document.getElementById(ptby_names[i] + 'race' + i);
    n = parseInt(document.getElementById(ptby_names[i] + 'val', 10).value) +
      parseInt(sel.value, 10);
      document.getElementById(ptby_names[i] + 'tot').innerText = n;
      document.getElementById(ptby_names[i] + 'mod').innerText =
        ptby_setNumber(Math.floor(n / 2) - 5);
  }
}

function ptby_fixMod() {
  var i, n, ss, sel;

  for (i in ptby_names) {
    sel = document.getElementById(ptby_names[i] + 'race' + i);
    if ((sel != this) && (this.selectedIndex > 0) && (sel.selectedIndex == this.selectedIndex)) {
      sel.selectedIndex = 0;
    }
  }
  ptby_calcMods();
}

function ptby_newVal() {
  var i, e, pts;

  pts = 0;
  for (i in ptby_names) {
    e = parseInt(document.getElementById(ptby_names[i] + 'val').value, 10);
    pts += ptby_pb.costs[e];
  }
  document.getElementById('ptby_used').innerText = ptby_setNumber(pts, false);
  ptby_calcMods();
}

function ptby_setRaceMods(evt) {
  var choice, i, j, n, td, input, select, option, val, label;

  choice = document.getElementById('ptby_racelist').value;

  for (i in ptby_names) {
    td = document.getElementById(ptby_names[i] + 'race');
    while (td.firstChild !== null) {
      td.removeChild(td.firstChild);
    }
    select = document.createElement('select');
    select.onchange = ptby_fixMod;
    select.id = select.name = ptby_names[i] + 'race' + i;
    option = document.createElement('option');
    option.selected = true;
    option.value = 0;
    option.appendChild(document.createTextNode('\u2013'));
    select.appendChild(option);
    for (j = 0, n = ptby_pb.rmods[choice].length; j < n; j++) {
      option = document.createElement('option');
      option.value = ptby_pb.rmods[choice][j];
      option.appendChild(document.createTextNode(String.fromCharCode(97 + j) +
        ': ' + option.value.toString().replace('-', '\u2013')));
      select.appendChild(option);
    }
    td.appendChild(select);
    td = document.getElementById(ptby_names[i] + 'tot');
    td.innerText = document.getElementById(ptby_names[i] + 'val').value;
    td = document.getElementById(ptby_names[i] + 'mod');
    td.innerText = Math.floor(parseInt(document.getElementById(ptby_names[i] + 'val').value, 10) / 2) - 5;
  }
  if (evt != null) {
    window.removeEventListener('load', ptby_setRaceMods, false);
  }
}

if (document.readyState === 'loading') {
  window.addEventListener('load', ptby_setRaceMods, false);
} else {
  ptby_setRaceMods(null);
}

/* Point Buy Script -- End */