X-Git-Url: https://git.yukkurigames.com/?p=labelle.git;a=blobdiff_plain;f=acnl-outfit.js;h=018fe03fc11413ad7edf6776a5f35ce042b4b44a;hp=c09c3a251c5e411a4dc8b95ff03f9429b3abe1b6;hb=b41e44e3108d40958ea2f9bab8f9907b296e213f;hpb=8db94b5a2c286493f5a5120e4505bd3965de9a5d diff --git a/acnl-outfit.js b/acnl-outfit.js index c09c3a2..018fe03 100644 --- a/acnl-outfit.js +++ b/acnl-outfit.js @@ -6,12 +6,6 @@ "use strict"; -Array.prototype.firstWhere = function (callback, thisArg) { - for (var i = 0; i < this.length; ++i) - if (callback.call(thisArg, this[i])) - return this[i]; -}; - Array.prototype.contains = function (o) { return this.indexOf(o) >= 0; }; @@ -47,56 +41,51 @@ function choice (os) { return os[Math.floor(os.length * Math.random())]; } -function category (item) { - return Object.keys(ACNL_WEARABLES).firstWhere(function (type) { - return ACNL_WEARABLES[type].contains(item); - }); -} +var flatten1 = Array.prototype.concat.apply.bind(Array.prototype.concat, []); -function notLewd (outfit) { - var categories = outfit.map(category); - return (categories.contains("wetsuits") - || categories.contains("dresses") - || categories.containsSubset(["tops", "bottoms"])); +function get (attr) { + return this[attr]; } -function buildArray (a, l) { - return (a || []).concat(l); +var category = get.bind(ACNL_WEARABLES_CATEGORY); +var wearables = get.bind(ACNL_WEARABLES); + +function isLewd (outfit) { + return ![["wetsuits"], ["dresses"], ["tops", "bottoms"]].some( + Array.prototype.containsSubset, outfit.map(category)); } -function impossibilities (categories) { - return categories.map(function (c) { return BLOCKS[c]; } - ).reduce(buildArray, []); +function impossibilities (outfit) { + return flatten1(outfit.map(category).map(get, BLOCKS)); } function possibilities (outfit) { - return Object.keys(ACNL_WEARABLES - ).filter(Array.prototype.lacks, impossibilities(outfit.map(category)) - ).map(function (k) { return ACNL_WEARABLES[k] } - ).reduce(buildArray, []); + return Object.keys(ACNL_WEARABLES).filter( + Array.prototype.lacks, impossibilities(outfit)); } -function isFinished(outfit) { - return notLewd(outfit) && Math.random() > 0.5; +function outfit () { + var ps = []; + while (isLewd(ps)) + ps.push(choice(flatten1(possibilities(ps).map(wearables)))); + return ps; } -function nextItem (outfit) { - return !isFinished(outfit) && choice(possibilities(outfit)); +function isPosessive (item) { + return item[0].toUpperCase() === item[0] && item.indexOf("'s") >= 0; } -function outfit () { - var ps = []; - var next; - while (next = nextItem(ps)) - ps.push(next); - return ps; +function isPlural (item) { + return item.substr(-8) === "headgear" + || (item.substr(-1) === "s" + && item.substr(-2) !== "ss" + && item.substr(-6) !== "cosmos"); // :( } function article (item) { if (item[0].toUpperCase() === item[0] && item.indexOf("'s") >= 0) return ""; - else if (item.substr(-1) === "s" && item.substr(-2) !== "ss" - && item.substr(-6) !== "cosmos") // :( + else if (isPlural(item)) return ""; else if ("aeiouAEIO".indexOf(item[0]) >= 0 || ("FHLMNRSX".indexOf(item[0]) >= 0 @@ -135,7 +124,7 @@ function litanize (names, link) { return names.join(names.length == 2 ? " " : ", "); } -function litanizeElement (eid, tid) { +function litanizeElement (eid, tid, ev) { var o = outfit(); document.getElementById(eid).innerHTML = litanize(o, true) + "?"; // TODO: This often generates > 140 characters. Do I care? @@ -146,4 +135,6 @@ function litanizeElement (eid, tid) { choice(["Going out in ", "Labelle suggested ", "I'll wear "]) + litanize(o, false) + ".")); + if (ev) + ev.preventDefault(); }