Skip to content

Commit

Permalink
Teambuilder: Generate user samples from boxes (#2226)
Browse files Browse the repository at this point in the history
  • Loading branch information
im-tofa committed Apr 10, 2024
1 parent e1643c5 commit 4d4816c
Showing 1 changed file with 81 additions and 14 deletions.
95 changes: 81 additions & 14 deletions play.pokemonshowdown.com/js/client-teambuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1766,17 +1767,21 @@
},
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.
if (format.match(/gen\d$/)) return;

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.
Expand All @@ -1791,30 +1796,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('<button name="importSmogonSet" class="button smogon">' + BattleLog.escapeHTML(set) + '</button>');
}
$smogonSetDiv.append(' <small>(<a target="_blank" href="' + this.smogdexLink(species) + '">Smogon&nbsp;analysis</a>)</small>');
}

$setDiv.text('Sample sets: ');
for (var set in sets) {
$setDiv.append('<button name="importSmogonSet" class="button">' + BattleLog.escapeHTML(set) + '</button>');
$userSetDiv.text('Box sets: ');
if (userFormatSets && userFormatSets[species]) {
for (var set in userFormatSets[species]) {
$userSetDiv.append('<button name="importSmogonSet" class="button box">' + BattleLog.escapeHTML(set) + '</button>');
}
} else {
$userSetDiv.append('<small>(Sets from your boxes in this format will be available here)</small>');
}
$setDiv.append(' <small>(<a target="_blank" href="' + this.smogdexLink(species) + '">Smogon&nbsp;analysis</a>)</small>');
},
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);
Expand Down Expand Up @@ -1905,6 +1971,7 @@
buf += '<div class="pokemonedit-buttons"><button name="closePokemonImport" class="button"><i class="fa fa-chevron-left"></i> Back</button> <button name="savePokemonImport" class="button"><i class="fa fa-floppy-o"></i> Save</button></div>';
buf += '<textarea class="pokemonedit textbox" rows="14"></textarea>';
buf += '<div class="teambuilder-import-smogon-sets"></div>';
buf += '<div class="teambuilder-import-user-sets"></div>';
buf += '</div>';

this.$el.html('<div class="teamwrapper">' + buf + '</div>');
Expand Down

0 comments on commit 4d4816c

Please sign in to comment.