diff --git a/play.pokemonshowdown.com/js/client-teambuilder.js b/play.pokemonshowdown.com/js/client-teambuilder.js index 170cf69752..072872443a 100644 --- a/play.pokemonshowdown.com/js/client-teambuilder.js +++ b/play.pokemonshowdown.com/js/client-teambuilder.js @@ -104,6 +104,7 @@ this.curSet = null; Storage.saveTeam(this.curTeam); } else if (this.curTeam) { + this.clearCachedUserSetsIfNecessary(this.curTeam.format); this.curTeam.team = Storage.packTeam(this.curSetList); this.curTeam.iconCache = ''; var team = this.curTeam; @@ -1767,6 +1768,7 @@ }, getSmogonSets: function () { this.$('.teambuilder-pokemon-import .teambuilder-import-smogon-sets').empty(); + this.$('.teambuilder-pokemon-import .teambuilder-import-user-sets').empty(); var format = this.curTeam.format; // If we don't have a specific format, don't try and guess which sets to use. @@ -1774,10 +1776,13 @@ var self = this; this.smogonSets = this.smogonSets || {}; + this.updateCachedUserSets(format); + this.importSetButtons(); + if (this.smogonSets[format] !== undefined) { - this.importSetButtons(); return; } + // We fetch this as 'text' and JSON.parse it ourserves in order to have consistent behavior // between the localdev CORS helper and the real jQuery.get function, which would already parse // this into an object based on the content-type header. @@ -1792,30 +1797,91 @@ self.importSetButtons(); }, 'text'); }, + updateCachedUserSets: function (format) { + if (this.userSets && this.userSets[format]) return; + + this.userSets = this.userSets || {}; + this.userSets[format] = {}; + + var duplicateNameIndices = {}; + for (var i = 0; i < teams.length; i++) { + var team = teams[i]; + if (team.format !== format || team.capacity !== 24) continue; + + var setList = Storage.unpackTeam(team.team); + for (var j = 0; j < setList.length; j++) { + var set = setList[j]; + var name = set.name + " " + (duplicateNameIndices[set.name] || ""); + var sets = this.userSets[format][set.species] || {}; + sets[name] = set; + this.userSets[format][set.species] = sets; + duplicateNameIndices[set.name] = 1 + (duplicateNameIndices[set.name] || 0); + } + } + }, + clearCachedUserSetsIfNecessary: function (format) { + if (!this.curTeam || !this.userSets) return; + + // clear cached user sets if we have just been in a box for given format + if (this.curTeam.capacity === 24 && this.userSets[format]) { + this.userSets[format] = undefined; + } + }, importSetButtons: function () { - var formatSets = this.smogonSets[this.curTeam.format]; + var format = this.curTeam.format; + var smogonFormatSets = this.smogonSets[format]; + var userFormatSets = this.userSets[format]; var species = this.curSet.species; - var $setDiv = this.$('.teambuilder-pokemon-import .teambuilder-import-smogon-sets'); - $setDiv.empty(); + var $smogonSetDiv = this.$('.teambuilder-pokemon-import .teambuilder-import-smogon-sets'); + $smogonSetDiv.empty(); - if (!formatSets) return; + var $userSetDiv = this.$('.teambuilder-pokemon-import .teambuilder-import-user-sets'); + $userSetDiv.empty(); - var sets = $.extend({}, formatSets['dex'][species], (formatSets['stats'] || {})[species]); + if (smogonFormatSets) { + var smogonSets = $.extend({}, smogonFormatSets['dex'][species], (smogonFormatSets['stats'] || {})[species]); + $smogonSetDiv.text('Sample sets: '); + for (var set in smogonSets) { + $smogonSetDiv.append(''); + } + $smogonSetDiv.append(' (Smogon analysis)'); + } - $setDiv.text('Sample sets: '); - for (var set in sets) { - $setDiv.append(''); + $userSetDiv.text('Box sets: '); + if (userFormatSets && userFormatSets[species]) { + for (var set in userFormatSets[species]) { + $userSetDiv.append(''); + } + } else { + $userSetDiv.append('(Sets from your boxes in this format will be available here)'); } - $setDiv.append(' (Smogon analysis)'); }, importSmogonSet: function (i, button) { - var formatSets = this.smogonSets[this.curTeam.format]; var species = this.curSet.species; - var setName = this.$(button).text(); - var smogonSet = formatSets['dex'][species][setName] || formatSets['stats'][species][setName]; - var curSet = $.extend({}, this.curSet, smogonSet); + var sampleSet; + if (this.$(button).hasClass('smogon')) { + var smogonFormatSets = this.smogonSets[this.curTeam.format]; + sampleSet = smogonFormatSets['dex'][species][setName] || smogonFormatSets['stats'][species][setName]; + } + + if (this.$(button).hasClass('box')) { + var userFormatSets = this.userSets[this.curTeam.format]; + sampleSet = userFormatSets[species][setName]; + } + + if (!sampleSet) return; + + var curSet = $.extend({}, this.curSet, sampleSet); + + // smogon samples don't usually have sample names, box samples usually do; either way, don't use them + curSet.name = this.curSet.name || undefined; + + // never preserve current set tera, even if smogon set used default + if (this.curSet.gen === 9) { + curSet.teraType = species.forceTeraType || sampleSet.teraType || species.types[0]; + } var text = Storage.exportTeam([curSet], this.curTeam.gen); this.$('.teambuilder-pokemon-import .pokemonedit').val(text); @@ -1906,6 +1972,7 @@ buf += '
'; buf += ''; buf += '
'; + buf += '
'; buf += ''; this.$el.html('
' + buf + '
');