Difference between revisions of "MediaWiki:MobTaxonomy.js"
BrianFreud (talk | contribs) (Created page with "→Create the needed elements to hold the data.: $('h2:contains("Taxonomy")').after('<div id="taxonomyContainer"><canvas id="relatedMobs" width="800" height="600"></canvas><...") |
BrianFreud (talk | contribs) m |
||
Line 1: | Line 1: | ||
/* Create the needed elements to hold the data. */ | /* Create the needed elements to hold the data. */ | ||
− | $('h2:contains("Taxonomy")').after('< | + | $('h2:contains("Taxonomy")').after('<br><br><h2 id="notableNPCs" style="display:none;"><span>Notable NPCs</span></h2>') |
− | .after('< | + | .after('<div id="taxonomyContainer"><canvas id="relatedMobs" width="800" height="600"></canvas></div>'); |
Line 10: | Line 10: | ||
mobType = document.location.search.split(':')[1].replace(/ \((?:Centaur|creature|monster)\)/, ''); | mobType = document.location.search.split(':')[1].replace(/ \((?:Centaur|creature|monster)\)/, ''); | ||
} | } | ||
− | let family = | + | let family = mobs[mobType].family; |
Line 24: | Line 24: | ||
/* Populate the orgchart. */ | /* Populate the orgchart. */ | ||
let countMobs = (name, info) => { | let countMobs = (name, info) => { | ||
− | if (! | + | if (!mobs[info.parent].kids) { |
− | + | mobs[info.parent].kids = 0; | |
} | } | ||
− | + | mobs[info.parent].kids++; | |
}; | }; | ||
Line 43: | Line 43: | ||
} | } | ||
if (info.parent.length && name !== info.parent) { // Climb the family tree; stop if we're at the top. | if (info.parent.length && name !== info.parent) { // Climb the family tree; stop if we're at the top. | ||
− | addMob(info.parent, | + | addMob(info.parent, mobs[info.parent]); |
} | } | ||
}; | }; | ||
for (let x in mobs) { | for (let x in mobs) { | ||
− | if ( | + | if (mobs[x].family.match(matcher)) { |
− | if (parseInt( | + | if (parseInt(mobs[x].bluename, 10)) { |
− | NPCs[x] = | + | NPCs[x] = mobs[x]; |
} else { | } else { | ||
− | countMobs(x, | + | countMobs(x, mobs[x]); |
} | } | ||
} | } | ||
Line 58: | Line 58: | ||
for (let x in mobs) { | for (let x in mobs) { | ||
− | if ( | + | if (mobs[x].family.match(matcher)) { |
− | if (!parseInt( | + | if (!parseInt(mobs[x].bluename, 10)) { |
− | addMob(x, | + | addMob(x, mobs[x]); |
} | } | ||
} | } | ||
Line 69: | Line 69: | ||
let y = mobTreeData[x], | let y = mobTreeData[x], | ||
dir = 'u'; | dir = 'u'; | ||
− | if (!! | + | if (!!mobs[y.parent] && mobs[y.parent].kids > 1) { |
− | if (! | + | if (!mobs[y.parent].counter) { |
− | + | mobs[y.parent].counter = 0; | |
} | } | ||
− | let counter = ( | + | let counter = (mobs[y.parent].counter++ % 2); |
dir = !counter ? 'r' : 'l'; | dir = !counter ? 'r' : 'l'; | ||
} | } |
Revision as of 20:16, 19 April 2017
/* Create the needed elements to hold the data. */
$('h2:contains("Taxonomy")').after('<br><br><h2 id="notableNPCs" style="display:none;"><span>Notable NPCs</span></h2>')
.after('<div id="taxonomyContainer"><canvas id="relatedMobs" width="800" height="600"></canvas></div>');
/* What mob are we dealing with? */
let mobs = stratics.data.mobs,
mobType = $('#mobileName').text();
if (!mobType) {
mobType = document.location.search.split(':')[1].replace(/ \((?:Centaur|creature|monster)\)/, '');
}
let family = mobs[mobType].family;
/* Create the orgchart. */
let o = new orgChart();
o.setColor('#111', '#FFFF99', '#000', '#1E90FF');
o.setNodeStyle(1, 10, 2);
o.setFont('arial', 14, '#000', 'c');
o.setSize(120, 40, 40, 30);
o.setColor('#3388DD', '#DAA520');
/* Populate the orgchart. */
let countMobs = (name, info) => {
if (!mobs[info.parent].kids) {
mobs[info.parent].kids = 0;
}
mobs[info.parent].kids++;
};
let mobTreeData = {},
NPCs = {},
matcher = new RegExp(`(?:,|^)${family}(?:,|$)`);
let addMob = (name, info) => {
if (!mobTreeData[name]) {
mobTreeData[name] = {
name,
parent: info.parent,
page: !!info.page.length ? info.page : name
};
}
if (info.parent.length && name !== info.parent) { // Climb the family tree; stop if we're at the top.
addMob(info.parent, mobs[info.parent]);
}
};
for (let x in mobs) {
if (mobs[x].family.match(matcher)) {
if (parseInt(mobs[x].bluename, 10)) {
NPCs[x] = mobs[x];
} else {
countMobs(x, mobs[x]);
}
}
}
for (let x in mobs) {
if (mobs[x].family.match(matcher)) {
if (!parseInt(mobs[x].bluename, 10)) {
addMob(x, mobs[x]);
}
}
}
for (let x in mobTreeData) {
if (mobTreeData.hasOwnProperty(x)) {
let y = mobTreeData[x],
dir = 'u';
if (!!mobs[y.parent] && mobs[y.parent].kids > 1) {
if (!mobs[y.parent].counter) {
mobs[y.parent].counter = 0;
}
let counter = (mobs[y.parent].counter++ % 2);
dir = !counter ? 'r' : 'l';
}
// Tweaks to improve the look of the resulting trees.
if (['Red Solen', 'Gray Goblin', 'Gray Goblin Mage', 'Leather Wolf'].includes(x)) dir = 'r';
if (y.parent == 'Korpre') dir = 'u';
if (mobType === x) { // 'Highlight' if the current mob being handled is the same as the mob for this page
o.setColor('#3388DD', '#1E90FF');
}
o.addNode(y.name, y.parent, dir, y.name, 0, `http://community.stratics.com/w/index.php?title=UO:${y.page}`);
o.setColor('#3388DD', '#DAA520'); // Reset the colors
}
}
/* Render the orgchart. */
o.drawChart('relatedMobs', 'c', true);
/* Scale the orgchart to fit the contents. */
let tc = '#taxonomyContainer';
let $tc = $(tc);
let $canvas = $(`${tc} canvas`);
let ratio = $(tc).width() / $canvas.width();
if (ratio < 1) {
$tc.css({
transform: `scale(${ratio})`,
'image-rendering': '-webkit-optimize-contrast',
'-ms-interpolation-mode': 'nearest-neighbor'
}).css({
'image-rendering': '-moz-crisp-edges'
}).css({
'image-rendering': '-o-crisp-edges'
}).css({
'image-rendering': 'optimize-contrast'
}).css({
'image-rendering': 'crisp-edges',
marginLeft: `${$tc.parent().offset().left - $tc.offset().left}px`
});
}