Skip to content

Commit 4d4816c

Browse files
authored
Teambuilder: Generate user samples from boxes (#2226)
1 parent e1643c5 commit 4d4816c

File tree

1 file changed

+81
-14
lines changed

1 file changed

+81
-14
lines changed

play.pokemonshowdown.com/js/client-teambuilder.js

+81-14
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
this.curSet = null;
105105
Storage.saveTeam(this.curTeam);
106106
} else if (this.curTeam) {
107+
this.clearCachedUserSetsIfNecessary(this.curTeam.format);
107108
this.curTeam.team = Storage.packTeam(this.curSetList);
108109
this.curTeam.iconCache = '';
109110
var team = this.curTeam;
@@ -1766,17 +1767,21 @@
17661767
},
17671768
getSmogonSets: function () {
17681769
this.$('.teambuilder-pokemon-import .teambuilder-import-smogon-sets').empty();
1770+
this.$('.teambuilder-pokemon-import .teambuilder-import-user-sets').empty();
17691771

17701772
var format = this.curTeam.format;
17711773
// If we don't have a specific format, don't try and guess which sets to use.
17721774
if (format.match(/gen\d$/)) return;
17731775

17741776
var self = this;
17751777
this.smogonSets = this.smogonSets || {};
1778+
this.updateCachedUserSets(format);
1779+
this.importSetButtons();
1780+
17761781
if (this.smogonSets[format] !== undefined) {
1777-
this.importSetButtons();
17781782
return;
17791783
}
1784+
17801785
// We fetch this as 'text' and JSON.parse it ourserves in order to have consistent behavior
17811786
// between the localdev CORS helper and the real jQuery.get function, which would already parse
17821787
// this into an object based on the content-type header.
@@ -1791,30 +1796,91 @@
17911796
self.importSetButtons();
17921797
}, 'text');
17931798
},
1799+
updateCachedUserSets: function (format) {
1800+
if (this.userSets && this.userSets[format]) return;
1801+
1802+
this.userSets = this.userSets || {};
1803+
this.userSets[format] = {};
1804+
1805+
var duplicateNameIndices = {};
1806+
for (var i = 0; i < teams.length; i++) {
1807+
var team = teams[i];
1808+
if (team.format !== format || team.capacity !== 24) continue;
1809+
1810+
var setList = Storage.unpackTeam(team.team);
1811+
for (var j = 0; j < setList.length; j++) {
1812+
var set = setList[j];
1813+
var name = set.name + " " + (duplicateNameIndices[set.name] || "");
1814+
var sets = this.userSets[format][set.species] || {};
1815+
sets[name] = set;
1816+
this.userSets[format][set.species] = sets;
1817+
duplicateNameIndices[set.name] = 1 + (duplicateNameIndices[set.name] || 0);
1818+
}
1819+
}
1820+
},
1821+
clearCachedUserSetsIfNecessary: function (format) {
1822+
if (!this.curTeam || !this.userSets) return;
1823+
1824+
// clear cached user sets if we have just been in a box for given format
1825+
if (this.curTeam.capacity === 24 && this.userSets[format]) {
1826+
this.userSets[format] = undefined;
1827+
}
1828+
},
17941829
importSetButtons: function () {
1795-
var formatSets = this.smogonSets[this.curTeam.format];
1830+
var format = this.curTeam.format;
1831+
var smogonFormatSets = this.smogonSets[format];
1832+
var userFormatSets = this.userSets[format];
17961833
var species = this.curSet.species;
17971834

1798-
var $setDiv = this.$('.teambuilder-pokemon-import .teambuilder-import-smogon-sets');
1799-
$setDiv.empty();
1835+
var $smogonSetDiv = this.$('.teambuilder-pokemon-import .teambuilder-import-smogon-sets');
1836+
$smogonSetDiv.empty();
18001837

1801-
if (!formatSets) return;
1838+
var $userSetDiv = this.$('.teambuilder-pokemon-import .teambuilder-import-user-sets');
1839+
$userSetDiv.empty();
18021840

1803-
var sets = $.extend({}, formatSets['dex'][species], (formatSets['stats'] || {})[species]);
1841+
if (smogonFormatSets) {
1842+
var smogonSets = $.extend({}, smogonFormatSets['dex'][species], (smogonFormatSets['stats'] || {})[species]);
1843+
$smogonSetDiv.text('Sample sets: ');
1844+
for (var set in smogonSets) {
1845+
$smogonSetDiv.append('<button name="importSmogonSet" class="button smogon">' + BattleLog.escapeHTML(set) + '</button>');
1846+
}
1847+
$smogonSetDiv.append(' <small>(<a target="_blank" href="' + this.smogdexLink(species) + '">Smogon&nbsp;analysis</a>)</small>');
1848+
}
18041849

1805-
$setDiv.text('Sample sets: ');
1806-
for (var set in sets) {
1807-
$setDiv.append('<button name="importSmogonSet" class="button">' + BattleLog.escapeHTML(set) + '</button>');
1850+
$userSetDiv.text('Box sets: ');
1851+
if (userFormatSets && userFormatSets[species]) {
1852+
for (var set in userFormatSets[species]) {
1853+
$userSetDiv.append('<button name="importSmogonSet" class="button box">' + BattleLog.escapeHTML(set) + '</button>');
1854+
}
1855+
} else {
1856+
$userSetDiv.append('<small>(Sets from your boxes in this format will be available here)</small>');
18081857
}
1809-
$setDiv.append(' <small>(<a target="_blank" href="' + this.smogdexLink(species) + '">Smogon&nbsp;analysis</a>)</small>');
18101858
},
18111859
importSmogonSet: function (i, button) {
1812-
var formatSets = this.smogonSets[this.curTeam.format];
18131860
var species = this.curSet.species;
1814-
18151861
var setName = this.$(button).text();
1816-
var smogonSet = formatSets['dex'][species][setName] || formatSets['stats'][species][setName];
1817-
var curSet = $.extend({}, this.curSet, smogonSet);
1862+
var sampleSet;
1863+
if (this.$(button).hasClass('smogon')) {
1864+
var smogonFormatSets = this.smogonSets[this.curTeam.format];
1865+
sampleSet = smogonFormatSets['dex'][species][setName] || smogonFormatSets['stats'][species][setName];
1866+
}
1867+
1868+
if (this.$(button).hasClass('box')) {
1869+
var userFormatSets = this.userSets[this.curTeam.format];
1870+
sampleSet = userFormatSets[species][setName];
1871+
}
1872+
1873+
if (!sampleSet) return;
1874+
1875+
var curSet = $.extend({}, this.curSet, sampleSet);
1876+
1877+
// smogon samples don't usually have sample names, box samples usually do; either way, don't use them
1878+
curSet.name = this.curSet.name || undefined;
1879+
1880+
// never preserve current set tera, even if smogon set used default
1881+
if (this.curSet.gen === 9) {
1882+
curSet.teraType = species.forceTeraType || sampleSet.teraType || species.types[0];
1883+
}
18181884

18191885
var text = Storage.exportTeam([curSet], this.curTeam.gen);
18201886
this.$('.teambuilder-pokemon-import .pokemonedit').val(text);
@@ -1905,6 +1971,7 @@
19051971
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>';
19061972
buf += '<textarea class="pokemonedit textbox" rows="14"></textarea>';
19071973
buf += '<div class="teambuilder-import-smogon-sets"></div>';
1974+
buf += '<div class="teambuilder-import-user-sets"></div>';
19081975
buf += '</div>';
19091976

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

0 commit comments

Comments
 (0)