function BattleChart()
{
var self = this;
this.firstResult = '';
this.exactResult = false;
// 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) {
if (isFirst)
{
self.firstResult = thing.name;
}
attrs = attrs || '';
switch (match.thingType)
{
case 'pokemon':
return self.pokemonRow(thing, attrs, match, isFirst);
break;
case 'item':
return self.itemRow(thing, attrs, match, isFirst);
break;
case 'ability':
return self.abilityRow(thing, attrs, match, isFirst);
break;
case 'move':
return self.moveRow(thing, attrs, match, isFirst);
break;
}
};
this.pokemonRow = function(pokemon, attrs, match, isFirst) {
var text = '
';
text += ''+(pokemon.num)+' ';
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 (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 += '';
for (var i=0; i ';
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 == 'DW') ability = ''+ability+'';
text += ability;
}
if (!pokemon.abilities['DW']) 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 += '
';
text += ' ';
text += ''+(move.category!=='Status'?('Power
'+(move.basePower||'—')):'')+' ';
text += 'Accuracy
'+(move.accuracy && move.accuracy!==true?move.accuracy+'%':'—')+' ';
text += ''+Tools.escapeHTML(move.shortDesc || move.desc)+' ';
text += '';
return text;
};
this.chart = function(searchTerm, type, init, thisArrange, thisSort) {
var chartData = {
exact: {},
start: {},
contains: {},
illegalstart: {},
illegalcontains: {},
fullstart: {},
illegalfullstart: {},
other: {}
};
thisArrange = thisArrange || self.defaultArrange;
thisSort = thisSort || self.defaultSort;
self.firstResult = '';
self.exactResult = false;
if (!searchTerm) searchTerm = '';
else searchTerm = searchTerm.toLowerCase();
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= 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['DW'])
{
index = thing.abilities['DW'].toLowerCase().indexOf(searchTerm);
if (index == 0)
{
matchType = 'fullstart';
match.ability = {DW:{
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 = '';
var firstMatch = true;
var noNameMatch = false;
var matchTypes = ['exact', 'start', 'contains', 'illegalstart', 'illegalcontains'];
for (var mtIndex = 0; mtIndex < 5; mtIndex++)
{
var matchType = matchTypes[mtIndex];
for (var i in chartData[matchType])
{
if (!chartData[matchType][i]) continue;
for (var j=0; jMatches
';
}
text += self.row(match.thing, '', match, firstMatch);
firstMatch = false;
}
}
}
if (searchTerm && firstMatch)
{
text += 'Matches
- No matches
';
firstMatch = false;
noNameMatch = true;
}
firstMatch = true;
for (var i in chartData.fullstart)
{
if (!chartData.fullstart[i]) continue;
for (var j=0; jDetails Matches
';
}
text += self.row(match.thing, '', match, firstMatch && noNameMatch);
firstMatch = false;
}
}
for (var i in chartData.illegalfullstart)
{
if (!chartData.illegalfullstart[i]) continue;
for (var j=0; jDetails Matches
';
}
text += self.row(match.thing, '', match, firstMatch && noNameMatch);
firstMatch = false;
}
}
if (!searchTerm || init)
{
for (var i=0; i'+buckets[i]+'
';
firstMatch = false;
}
text += self.row(match.thing, '', match);
}
}
}
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();