|
104 | 104 | this.curSet = null;
|
105 | 105 | Storage.saveTeam(this.curTeam);
|
106 | 106 | } else if (this.curTeam) {
|
| 107 | + this.clearCachedUserSetsIfNecessary(this.curTeam.format); |
107 | 108 | this.curTeam.team = Storage.packTeam(this.curSetList);
|
108 | 109 | this.curTeam.iconCache = '';
|
109 | 110 | var team = this.curTeam;
|
|
1766 | 1767 | },
|
1767 | 1768 | getSmogonSets: function () {
|
1768 | 1769 | this.$('.teambuilder-pokemon-import .teambuilder-import-smogon-sets').empty();
|
| 1770 | + this.$('.teambuilder-pokemon-import .teambuilder-import-user-sets').empty(); |
1769 | 1771 |
|
1770 | 1772 | var format = this.curTeam.format;
|
1771 | 1773 | // If we don't have a specific format, don't try and guess which sets to use.
|
1772 | 1774 | if (format.match(/gen\d$/)) return;
|
1773 | 1775 |
|
1774 | 1776 | var self = this;
|
1775 | 1777 | this.smogonSets = this.smogonSets || {};
|
| 1778 | + this.updateCachedUserSets(format); |
| 1779 | + this.importSetButtons(); |
| 1780 | + |
1776 | 1781 | if (this.smogonSets[format] !== undefined) {
|
1777 |
| - this.importSetButtons(); |
1778 | 1782 | return;
|
1779 | 1783 | }
|
| 1784 | + |
1780 | 1785 | // We fetch this as 'text' and JSON.parse it ourserves in order to have consistent behavior
|
1781 | 1786 | // between the localdev CORS helper and the real jQuery.get function, which would already parse
|
1782 | 1787 | // this into an object based on the content-type header.
|
|
1791 | 1796 | self.importSetButtons();
|
1792 | 1797 | }, 'text');
|
1793 | 1798 | },
|
| 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 | + }, |
1794 | 1829 | 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]; |
1796 | 1833 | var species = this.curSet.species;
|
1797 | 1834 |
|
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(); |
1800 | 1837 |
|
1801 |
| - if (!formatSets) return; |
| 1838 | + var $userSetDiv = this.$('.teambuilder-pokemon-import .teambuilder-import-user-sets'); |
| 1839 | + $userSetDiv.empty(); |
1802 | 1840 |
|
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 analysis</a>)</small>'); |
| 1848 | + } |
1804 | 1849 |
|
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>'); |
1808 | 1857 | }
|
1809 |
| - $setDiv.append(' <small>(<a target="_blank" href="' + this.smogdexLink(species) + '">Smogon analysis</a>)</small>'); |
1810 | 1858 | },
|
1811 | 1859 | importSmogonSet: function (i, button) {
|
1812 |
| - var formatSets = this.smogonSets[this.curTeam.format]; |
1813 | 1860 | var species = this.curSet.species;
|
1814 |
| - |
1815 | 1861 | 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 | + } |
1818 | 1884 |
|
1819 | 1885 | var text = Storage.exportTeam([curSet], this.curTeam.gen);
|
1820 | 1886 | this.$('.teambuilder-pokemon-import .pokemonedit').val(text);
|
|
1905 | 1971 | 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>';
|
1906 | 1972 | buf += '<textarea class="pokemonedit textbox" rows="14"></textarea>';
|
1907 | 1973 | buf += '<div class="teambuilder-import-smogon-sets"></div>';
|
| 1974 | + buf += '<div class="teambuilder-import-user-sets"></div>'; |
1908 | 1975 | buf += '</div>';
|
1909 | 1976 |
|
1910 | 1977 | this.$el.html('<div class="teamwrapper">' + buf + '</div>');
|
|
0 commit comments