/* The person who associated a work with this deed has dedicated the
work to the public domain by waiving all of his or her rights to
the work worldwide under copyright law, including all related and
neighboring rights, to the extent allowed by law.
You can copy, modify, distribute and perform the work, even for
commercial purposes, all without asking permission.
See https://creativecommons.org/publicdomain/zero/1.0/ for details.
*/
"use strict";
if (!String.prototype.repeat)
Object.defineProperty(String.prototype, "repeat", {
value: function (count) {
var string = this.toString();
var result = '';
var n = count | 0;
while (n) {
if (n % 2 === 1)
result += string;
if (n > 1)
string += string;
n >>= 1;
}
return result;
}
});
if (!Array.prototype.fill)
Object.defineProperty(Array.prototype, "fill", {
value: function (value) {
var beg = arguments.length > 1 ? +arguments[1] : 0;
var end = arguments.length > 2 ? +arguments[2] : this.length;
if (beg < 0) beg += this.length;
if (end < 0) end += this.length;
for (var i = beg; i < end; ++i)
this[i] = value;
return this;
}
});
if (!Element.prototype.matches)
Object.defineProperty(Element.prototype, "matches", {
value: Element.prototype.matchesSelector
|| Element.prototype.mozMatchesSelector
|| Element.prototype.webkitMatchesSelector
});
function removeUnit (element) {
element.parentNode.removeChild(element);
}
var EMPTY = "☐";
var CHECK = "☒";
var FIRST_EMPTY = /☐/;
var LAST_TICKED = /☒([^☒]*)$/;
function choice (a) {
return a[(Math.random() * a.length) | 0];
}
function cap (s) {
return s && s[0].toUpperCase() + s.slice(1);
}
function letters (n) {
var r = "";
n = n || 1;
while (n-- > 0)
r += choice("abcdefghijklmnopqrstuvwxyz");
return r;
}
function range (a, b) {
return a + (Math.random() * (b - a)) | 0;
}
function numerals (n) {
var r = "";
n = n || 1;
while (n-- > 0)
r += choice("0123456789");
return r;
}
function pid () {
return letters(range(1, 4)).toUpperCase()
+ choice(["‑", "", ".", "/"])
+ numerals(range(1, 4));
}
function roman () {
return choice(["I", "II", "III", "IV", "V", "VI",
"VII", "VIII", "IX", "X", "XI", "XII",
"XIV"]);
}
function oid () {
return choice([letters(1).toUpperCase() + choice("-./") + numerals(2)]);
}
function randomName (scheme) {
switch (scheme) {
case "id":
return choice([pid() + " " + cap(choice(NOUNS))],
[cap(choice(NOUNS)) + " " + pid()]);
case "air":
return choice([
oid() + " " + cap(choice(ADJECTIVES)) + " " + cap(choice(BIRDS)),
cap(choice(ADJECTIVES)) + " " + cap(choice(BIRDS)) + " " + roman()]);
default:
return choice([
oid() + " " + cap(choice(ADJECTIVES)) + " " + cap(choice(NOUNS)),
cap(choice(ADJECTIVES)) + " " + cap(choice(NOUNS)) + " " + roman()]);
}
}
var STATS = ["attack", "range", "defense", "aside"];
function createUnit (dfnName) {
var dfn = UNITS[dfnName];
var weapons = dfn.weapons || [];
var unit = document.getElementById("unit-template").cloneNode(true);
unit.removeAttribute('id');
unit.setAttribute("data-dfn", dfnName);
unit.querySelector(".name").textContent = randomName(dfn.nameScheme);
unit.querySelector(".type").textContent = dfnName;
if (dfn.aside) {
var aside = unit.appendChild(document.createElement("div"));
aside.className = "aside";
aside.innerHTML = dfn.aside;
}
if (weapons.length) {
var weaponList = unit.appendChild(document.createElement('ul'));
weaponList.className = "weapons";
}
weapons.forEach(function (weaponRef) {
var count = parseInt(weaponRef, 10) || 1;
var weaponName = weaponRef.replace(/^[0-9 ]*/, "");
var weapon = WEAPONS[weaponName]
|| WEAPONS[weaponName.replace(/s+$/, "")];
var weaponItem = document.createElement("li");
weaponItem.setAttribute('data-name', weaponName);
weaponItem.setAttribute('data-count', count);
weaponItem.setAttribute('data-remaining', count);
var stats = document.createElement('ul');
stats.className = "stats";
for (var j = 0; j < STATS.length; ++j) {
if (weapon[STATS[j]] !== undefined) {
var stat = document.createElement('li');
stat.className = STATS[j];
stat.innerHTML = weapon[STATS[j]];
stats.appendChild(stat);
}
}
if (stats.children.length)
weaponItem.appendChild(stats);
var ticks = document.createElement('div');
ticks.className = "ticks";
ticks.innerHTML = ticksText(count);
weaponItem.appendChild(ticks);
weaponList.appendChild(weaponItem);
});
if (dfn.tread) {
var move = dfn.move || 3;
var per = dfn.tread / move;
var treads = document.createElement('ol');
treads.className = "treads";
treads.appendChild(document.createElement("li"));
treads.setAttribute('data-count', dfn.tread);
treads.setAttribute('data-remaining', dfn.tread);
for (var i = 0; i < move; ++i) {
var tread = treads.appendChild(document.createElement("li"));
tread.className = "ticks";
tread.innerHTML = ticksText(per);
}
unit.appendChild(treads);
}
if (dfn.propulsion) {
var move = dfn.move || 3;
var per = dfn.propulsion / move;
var treads = document.createElement('ol');
treads.className = "treads propulsion";
treads.appendChild(document.createElement("li"));
treads.setAttribute('data-count', dfn.propulsion);
treads.setAttribute('data-remaining', dfn.propulsion);
for (var i = 0; i < move; ++i) {
var tread = treads.appendChild(document.createElement("li"));
tread.className = "ticks";
tread.innerHTML = ticksText(per);
}
unit.appendChild(treads);
}
return unit;
}
function addUnit (dfnName) {
document.querySelector('main').appendChild(createUnit(dfnName));
}
function spre (c) {
return new RegExp("(^|\\s+)" + c + "(\\s+|$)");
}
function hasClass (e, n) {
return !!e.className.match(spre(n));
}
function addClass (e, n) {
if (!hasClass(e, n))
e.className += " " + n;
}
function removeClass (e, n) {
e.className = e.className.replace(spre(n), "");
}
function show (id) {
addClass(document.getElementById(id.id || id), "visible");
}
function hide (id) {
removeClass(document.getElementById(id.id || id), "visible");
}
function handleTray (evt) {
var id = this.getAttribute('data-tray');
var el = document.getElementById(id);
var f = (hasClass(el, "visible") ? hide : show)
autoClose();
f(el);
evt.stopPropagation();
}
function ticksText (n) {
var blocks = [EMPTY.repeat(n)];
for (var i = 5; i >= 2; --i) {
if (n > i && n % i === 0) {
blocks = (new Array(n / i)).fill(EMPTY.repeat(i));
break;
}
}
return "" + blocks.join("") + "";
}
function rub (content) {
return content.replace(LAST_TICKED, EMPTY + "$1");
}
function tick (content) {
return content.replace(FIRST_EMPTY, CHECK);
}
function findParent (el, selector) {
while (el && el !== document && !el.matches(selector))
el = el.parentNode;
return el === document ? null : el;
}
function boxes (event) {
var target = event.target;
if (!target.innerHTML.match(/☐|☒/))
return;
var par = findParent(target, '[data-count]');
var ticks = findParent(target, '.ticks');
if (!ticks || !par)
return;
var content = target.innerHTML;
var rect = target.getBoundingClientRect();
var total = target.innerHTML.match(/☐|☒/g);
var ticked = target.innerHTML.match(/☒/g);
var p = (event.clientX - rect.left) / rect.width;
var pr = (total && total.length)
? (ticked ? ticked.length : 0) / total.length : 0;
par.innerHTML = ((p < pr) ? rub : tick)(par.innerHTML);
var rem = par.innerHTML.match(/☐/g);
par.setAttribute('data-remaining', rem ? rem.length : 0);
event.preventDefault();
event.stopPropagation();
}
function fade (p) {
return p * p * p * (p * (p * 6.0 - 15.0) + 10.0);
}
function scroll (y1, t) {
var y0 = document.body.scrollTop || document.documentElement.scrollTop;
var n = (t || 150) / 15;
var i = 0;
clearInterval(scroll.owner);
scroll.owner = setInterval(function () {
var p = Math.max(0, Math.min(++i / n, 1));
document.body.scrollTop
= document.documentElement.scrollTop
= y0 + (y1 - y0) * fade(p);
if (i >= n) clearInterval(scroll.owner);
}, 15);
}
function next (el) {
scroll(el.nextElementSibling.offsetTop - el.parentNode.offsetTop);
}
function previous (el) {
scroll(el.previousElementSibling.offsetTop - el.parentNode.offsetTop);
}
function autoClose () {
var open = document.querySelectorAll(".tray.visible");
for (var i = 0; i < open.length; ++i)
hide(open[i]);
return open && open.length;
}
window.addEventListener("DOMContentLoaded", function () {
if (navigator.standalone)
document.body.className += " standalone";
var units = document.getElementById("addUnit");
Object.keys(UNITS).sort(function (a, b) {
return (b.indexOf("Ogre Mk") - a.indexOf("Ogre Mk"))
|| (a > b) - (a < b);
}).forEach(function (unitName) {
var unit = units.appendChild(document.createElement("li"));
unit.textContent = unitName;
unit.addEventListener("click", function () {
addUnit(unitName);
hide('addUnit');
});
});
addUnit('Ogre Mk. V');
FastClick.attach(document.body, { tapDelay: 50 });
var trays = document.querySelectorAll('[data-tray]');
for (var i = 0; i < trays.length; ++i)
trays[i].addEventListener('click', handleTray);
window.addEventListener('click', function (evt) {
if (!findParent(evt.target, ".tray.visible"))
autoClose();
});
});