function BattleChart() { var self = this; this.firstResult = ''; this.exactResult = false; this.lastSearch = '[init]'; // I know, I know, lots of memory // but these are arrays of pointers, so it's not _horrible_ // and this let me cache sorting this.pokemon = []; for (var i in BattlePokedex) { BattlePokedex[i].name = BattlePokedex[i].species; this.pokemon.push(BattlePokedex[i]); } this.items = []; for (var i in BattleItems) { this.items.push(BattleItems[i]); } this.abilities = []; for (var i in BattleAbilities) { this.abilities.push(BattleAbilities[i]); } this.moves = []; for (var i in BattleMovedex) { this.moves.push(BattleMovedex[i]); } this.lastPokemonSort = null; this.lastItemSort = null; this.lastAbilitySort = null; this.lastMoveSort = null; this.illegalBuckets = { 'Illegal': 'Illegal' }; this.selectCallback = null; this.select = function (x) { if (self.selectCallback) self.selectCallback(x); }; this.row = function (thing, attrs, match, isFirst, dataCommand) { if (isFirst) { self.firstResult = thing.name; } attrs = attrs || ''; switch (match.thingType) { case 'pokemon': return self.pokemonRow(thing, attrs, match, isFirst, dataCommand); case 'item': return self.itemRow(thing, attrs, match, isFirst, dataCommand); case 'ability': return self.abilityRow(thing, attrs, match, isFirst, dataCommand); case 'move': return self.moveRow(thing, attrs, match, isFirst, dataCommand); } }; this.pokemonRow = function (pokemon, attrs, match, isFirst, dataCommand) { var tag = dataCommand ? 'div' : 'a'; var text = '
  • <' + tag + attrs + ' data-name="' + Tools.escapeHTML(pokemon.species) + '">'; var tier = pokemon.tier; if (!tier) tier = Tools.getTemplate(pokemon.baseSpecies || pokemon.species).tier || 'Illegal'; text += '' + tier + ' '; var name = Tools.escapeHTML(pokemon.name); if (pokemon.forme && pokemon.baseSpecies) name = pokemon.baseSpecies; if (match.name) { name = name.substr(0, match.name.start) + '' + name.substr(match.name.start, match.name.end - match.name.start) + '' + name.substr(match.name.end); } if (pokemon.forme && pokemon.baseSpecies) { if (match.name && match.name.end > pokemon.baseSpecies.length) { if (match.name.start < pokemon.baseSpecies.length + 1) match.name.start = pokemon.baseSpecies.length + 1; name += '-' + pokemon.forme.substr(0, match.name.start - (pokemon.baseSpecies.length + 1)) + '' + pokemon.name.substr(match.name.start, match.name.end - match.name.start) + '' + pokemon.name.substr(match.name.end) + ''; } else { name += '-' + pokemon.forme + ''; } } if (dataCommand) { name = '' + name + ''; } /* if (match.name) { name = name.substr(0, match.name.start) + '' + name.substr(match.name.start, match.name.end-match.name.start) + '' + name.substr(match.name.end); } */ text += ''; text += ''; text += ' '; text += '' + name + ' '; if (self.illegalBuckets[match.bucket]) { text += '' + self.illegalBuckets[match.bucket] + ' '; text += '
  • '; return text; } text += ''; if (pokemon.types) for (var i = 0; i < pokemon.types.length; i++) { text += Tools.getTypeIcon(pokemon.types[i], (match.type && match.type[i])); } text += ' '; text += ''; if (pokemon.abilities['1']) { text += ''; } else { text += ''; } for (var i in pokemon.abilities) { var ability = pokemon.abilities[i]; if (!ability) continue; if (i === '1') text += '
    '; if (match.ability && match.ability[i]) { ability = ability.substr(0, match.ability[i].start) + '' + ability.substr(match.ability[i].start, match.ability[i].end - match.ability[i].start) + '' + ability.substr(match.ability[i].end); } if (i == 'H') { ability = '
    ' + (pokemon.unreleasedHidden ? '' + ability + '' : ability) + ''; } text += ability; } if (!pokemon.abilities['H']) text += ''; text += ''; text += '
    '; text += ''; text += 'HP
    ' + pokemon.baseStats.hp + '
    '; text += 'Atk
    ' + pokemon.baseStats.atk + '
    '; text += 'Def
    ' + pokemon.baseStats.def + '
    '; text += 'SpA
    ' + pokemon.baseStats.spa + '
    '; text += 'SpD
    ' + pokemon.baseStats.spd + '
    '; text += 'Spe
    ' + pokemon.baseStats.spe + '
    '; var bst = 0; for (i in pokemon.baseStats) bst += pokemon.baseStats[i]; text += 'BST
    ' + bst + '
    '; text += '
    '; text += ''; return text; }; this.itemRow = function (item, attrs, match, isFirst) { var text = '
  • '; var url = item.name.toLowerCase().replace(/ /g, '-').replace(/[^a-z-]+/g, ''); url = '/sprites/itemicons/' + url + '.png'; text += ''; text += ''; text += ' '; var name = Tools.escapeHTML(item.name); if (match.name) { name = name.substr(0, match.name.start) + '' + name.substr(match.name.start, match.name.end - match.name.start) + '' + name.substr(match.name.end); } text += '' + name + ' '; if (self.illegalBuckets[match.bucket]) { text += '' + self.illegalBuckets[match.bucket] + ' '; text += '
  • '; return text; } text += '' + Tools.escapeHTML(item.shortDesc || item.desc) + ' '; text += ''; return text; }; this.abilityRow = function (ability, attrs, match, isFirst) { var text = '
  • '; var name = Tools.escapeHTML(ability.name); if (match.name) { name = name.substr(0, match.name.start) + '' + name.substr(match.name.start, match.name.end - match.name.start) + '' + name.substr(match.name.end); } text += '' + name + ' '; if (self.illegalBuckets[match.bucket]) { text += '' + self.illegalBuckets[match.bucket] + ' '; text += '
  • '; return text; } text += '' + Tools.escapeHTML(ability.shortDesc || ability.desc) + ' '; text += ''; return text; }; this.moveRow = function (move, attrs, match, isFirst) { var text = '
  • '; var name = Tools.escapeHTML(move.name); var hplen = 'Hidden Power'.length; if (name.substr(0, hplen) === 'Hidden Power') name = 'Hidden Power'; if (match.name) { name = name.substr(0, match.name.start) + '' + name.substr(match.name.start, match.name.end - match.name.start) + '' + name.substr(match.name.end); } if (move.name.substr(0, hplen) === 'Hidden Power') { if (match.name && match.name.end > hplen) { if (match.name.start < hplen + 1) match.name.start = hplen + 1; name += ' ' + move.name.substr(hplen + 1, match.name.start - (hplen + 1)) + '' + move.name.substr(match.name.start, match.name.end - match.name.start) + '' + move.name.substr(match.name.end) + ''; } else { name += ' ' + move.name.substr(hplen + 1) + ''; } } text += '' + name + ' '; if (self.illegalBuckets[match.bucket]) { text += '' + self.illegalBuckets[match.bucket] + ' '; text += '
  • '; return text; } text += ''; text += Tools.getTypeIcon(move.type, match.type); text += '' + move.category + ''; text += ' '; text += '' + (move.category !== 'Status' ? ('Power
    ' + (move.basePower || '—')) : '') + '
    '; text += 'Accuracy
    ' + (move.accuracy && move.accuracy !== true ? move.accuracy + '%' : '—') + '
    '; text += 'PP
    ' + ((move.pp !== 1 && !move.noPPBoosts) ? move.pp * 8 / 5 : move.pp) + '
    '; text += '' + Tools.escapeHTML(move.shortDesc || move.desc) + ' '; text += ''; return text; }; this.chart = function (searchTerm, type, init, thisArrange, thisSort) { if (!searchTerm) searchTerm = ''; else searchTerm = searchTerm.toLowerCase(); if (!init && searchTerm === self.lastSearch) return; self.lastSearch = searchTerm; var chartData = { exact: {}, start: {}, contains: {}, illegalstart: {}, illegalcontains: {}, fullstart: {}, illegalfullstart: {}, other: {} }; thisArrange = thisArrange || self.defaultArrange; thisSort = thisSort || self.defaultSort; self.firstResult = ''; self.exactResult = false; var buckets = thisArrange(); var things = []; switch (type) { case 'pokemon': if (thisSort !== self.lastPokemonSort) { self.pokemon.sort(thisSort); self.lastPokemonSort = thisSort; } things = self.pokemon; break; case 'item': if (thisSort !== self.lastItemSort) { self.items.sort(thisSort); self.lastItemSort = thisSort; } things = self.items; break; case 'ability': if (thisSort !== self.lastAbilitySort) { self.abilities.sort(thisSort); self.lastAbilitySort = thisSort; } things = self.abilities; break; case 'move': if (thisSort !== self.lastMoveSort) { self.moves.sort(thisSort); self.lastMoveSort = thisSort; } things = self.moves; break; } for (var i = 0; i < things.length; i++) { var thing = things[i]; if (type === 'pokemon' && !thing.species || thing.jp) continue; // fill our buckets! var bucket = thisArrange(thing); var match = {thing: thing, thingType: type, bucket: bucket}; var matchType = 'other'; var index = -1; if (searchTerm) { index = thing.name.toLowerCase().indexOf(searchTerm); if (index === 0) { matchType = 'start'; match.name = { start: index, end: index + searchTerm.length }; if (searchTerm.length === thing.name.length) { self.exactResult = thing.name; matchType = 'exact'; } } else if (index >= 0) { matchType = 'contains'; match.name = { start: index, end: index + searchTerm.length }; } else if (type === 'pokemon') { index = thing.types[0].toLowerCase().indexOf(searchTerm); if (index == 0) { matchType = 'fullstart'; match.type = {0:{ start: 0, end: searchTerm.length }}; } if (thing.types[1]) { index = thing.types[1].toLowerCase().indexOf(searchTerm); if (index == 0) { matchType = 'fullstart'; match.type = {1:{ start: 0, end: searchTerm.length }}; } } index = thing.abilities['0'].toLowerCase().indexOf(searchTerm); if (index == 0) { matchType = 'fullstart'; match.ability = {0:{ start: 0, end: searchTerm.length }}; } if (thing.abilities['1']) { index = thing.abilities['1'].toLowerCase().indexOf(searchTerm); if (index == 0) { matchType = 'fullstart'; match.ability = {1:{ start: 0, end: searchTerm.length }}; } } if (thing.abilities['H']) { index = thing.abilities['H'].toLowerCase().indexOf(searchTerm); if (index == 0) { matchType = 'fullstart'; match.ability = {H:{ start: 0, end: searchTerm.length }}; } } } else if (type === 'move') { index = thing.type.toLowerCase().indexOf(searchTerm); if (index == 0) { matchType = 'fullstart'; match.type = { start: 0, end: searchTerm.length }; } index = thing.category.toLowerCase().indexOf(searchTerm); if (index == 0) { matchType = 'fullstart'; match.category = { start: 0, end: searchTerm.length }; } } } if (self.illegalBuckets[bucket]) { if (matchType === 'start' || matchType === 'contains' || matchType === 'fullstart') { matchType = 'illegal' + matchType; } } if (!chartData[matchType][bucket]) chartData[matchType][bucket] = []; chartData[matchType][bucket].push(match); } // construct a chart from the filled buckets! var text = ''; return text; }; this.defaultArrange = function (thing) { if (!thing) { // we want the list of buckets return ['All']; } return 'All'; }; this.defaultSort = function (a, b) { /* if (!a.num) a.num = 1000; if (!b.num) b.num = 1000; if (a.num != b.num) { return a.num - b.num; } */ return (a.name == b.name) ? 0 : ( (a.name > b.name) ? 1 : -1 ); }; } var Chart = new BattleChart();