Skip to content

Commit

Permalink
Support loading teams from server, add button to upload teams (#2157)
Browse files Browse the repository at this point in the history
  • Loading branch information
mia-pi-git authored Oct 2, 2023
1 parent 38e1eaa commit 5cf6cfd
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 23 deletions.
15 changes: 9 additions & 6 deletions js/client-chat-tournament.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,22 @@
self.room.send('/tournament leave');
});
this.$challengeChallenge.on('click', function () {
app.sendTeam(Storage.teams[self.$teamSelect.children().val()]);
self.room.send('/tournament challenge ' + self.$challengeUserMenu.children().val());
app.sendTeam(Storage.teams[self.$teamSelect.children().val()], function () {
self.room.send('/tournament challenge ' + self.$challengeUserMenu.children().val());
});
});
this.$challengeAccept.on('click', function () {
app.sendTeam(Storage.teams[self.$teamSelect.children().val()]);
self.room.send('/tournament acceptchallenge');
app.sendTeam(Storage.teams[self.$teamSelect.children().val()], function () {
self.room.send('/tournament acceptchallenge');
});
});
this.$challengeCancel.on('click', function () {
self.room.send('/tournament cancelchallenge');
});
this.$validate.on('click', function () {
app.sendTeam(Storage.teams[self.$teamSelect.children().val()]);
self.room.send('/tournament vtm');
app.sendTeam(Storage.teams[self.$teamSelect.children().val()], function () {
self.room.send('/tournament vtm');
});
});

app.user.on('saveteams', this.updateTeams, this);
Expand Down
25 changes: 16 additions & 9 deletions js/client-mainmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -918,10 +918,14 @@
app.addPopupMessage("You need to go into the Teambuilder and build a team for this format.");
return;
}
app.sendTeam(team);
app.sendTeam(team, function () {
target.disabled = true;
app.send(privacy + '/accept ' + userid);
});
} else {
target.disabled = true;
app.send(privacy + '/accept ' + userid);
}
target.disabled = true;
app.send(privacy + '/accept ' + userid);
},
rejectChallenge: function (i, target) {
var userid = $(target).closest('.pm-window').data('userid');
Expand Down Expand Up @@ -959,8 +963,9 @@
buf += '<p class="buttonbar"><button name="cancelChallenge">Cancel</button></p></form>';

$(target).closest('.challenge').html(buf);
app.sendTeam(team);
app.send(privacy + '/challenge ' + userid + ', ' + format);
app.sendTeam(team, function () {
app.send(privacy + '/challenge ' + userid + ', ' + format);
});
},
cancelChallenge: function (i, target) {
var userid = $(target).closest('.pm-window').data('userid');
Expand Down Expand Up @@ -1100,11 +1105,12 @@
$searchForm.find('button.big').html('<strong><i class="fa fa-refresh fa-spin"></i> Connecting...</strong>').addClass('disabled');
$searchForm.append('<p class="cancel buttonbar"><button name="cancelSearch">Cancel</button></p>');

app.sendTeam(team);
var self = this;
this.searchDelay = setTimeout(function () {
app.send(self.adjustPrivacy($privacyCheckbox.is(':checked')) + '/search ' + format);
}, 3000);
app.sendTeam(team, function () {
self.searchDelay = setTimeout(function () {
app.send(self.adjustPrivacy($privacyCheckbox.is(':checked')) + '/search ' + format);
}, 3000);
});
},
cancelSearch: function () {
clearTimeout(this.searchDelay);
Expand Down Expand Up @@ -1401,6 +1407,7 @@
count++;
if (count % bufBoundary === 0 && count != 0 && curBuf < 4) curBuf++;
}
console.log(teamFormat);

This comment has been minimized.

Copy link
@Zarel

Zarel Nov 3, 2023

Member

ಠ_ಠ

if (!isNoFolder) {
for (var i = 0; i < teams.length; i++) {
if ((!teams[i].format && !teamFormat) || teams[i].format === teamFormat) {
Expand Down
63 changes: 58 additions & 5 deletions js/client-teambuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@
update: function () {
teams = Storage.teams;
if (this.curTeam) {
if (this.curTeam.loaded === false || (this.curTeam.teamid && !this.curTeam.loaded)) {
this.loadTeam();
return this.updateTeamView();
}
this.ignoreEVLimits = (this.curTeam.gen < 3 ||
((this.curTeam.format.includes('hackmons') || this.curTeam.format.endsWith('bh')) && this.curTeam.gen !== 6) ||
this.curTeam.format.includes('metronomebattle'));
Expand All @@ -147,6 +151,20 @@
return this.updateTeamInterface();
},

loadTeam: function () {
if (this.loadingTeam) return false;
this.loadingTeam = true;
var teambuilder = this;
app.loadTeam(this.curTeam, function (team) {
window.builderTeam = team;
teambuilder.loadingTeam = false;
teambuilder.curSetList = Storage.unpackTeam(team.team);
Storage.activeSetList = teambuilder.curSetList;
teambuilder.curTeam.team = Storage.packTeam(teambuilder.curSetList);
teambuilder.updateTeamView();
});
},

/*********************************************************
* Team list view
*********************************************************/
Expand Down Expand Up @@ -460,17 +478,21 @@
}
}

buf += '</ul>';
buf += '</ul><p>';
if (atLeastOne) {
buf += '<p><button name="new" value="team" class="button"><i class="fa fa-plus-circle"></i> ' + newTeamButtonText + '</button> <button name="new" value="box" class="button"><i class="fa fa-archive"></i> New Box</button></p>';
buf += '<button name="new" value="team" class="button"><i class="fa fa-plus-circle"></i> ' + newTeamButtonText + '</button> <button name="new" value="box" class="button"><i class="fa fa-archive"></i> New Box</button> ';
}
buf += '<button class="button" name="send" value="/teams">View teams uploaded to server</button>';
buf += '</p>';

if (window.nodewebkit) {
buf += '<button name="revealFolder" class="button"><i class="fa fa-folder-open"></i> Reveal teams folder</button> <button name="reloadTeamsFolder" class="button"><i class="fa fa-refresh"></i> Reload teams files</button> <button name="backup" class="button"><i class="fa fa-upload"></i> Backup/Restore all teams</button>';
} else if (this.curFolder) {
buf += '<button name="backup" class="button"><i class="fa fa-upload"></i> Backup all teams from this folder</button>';
} else if (atLeastOne) {
buf += '<p><strong>Clearing your cookies (specifically, <code>localStorage</code>) will delete your teams.</strong> <span class="storage-warning">Browsers sometimes randomly clear cookies - you should back up your teams or use the desktop client if you want to make sure you don\'t lose them.</span></p>';
buf += '<p><strong>Clearing your cookies (specifically, <code>localStorage</code>) will delete your teams.</strong> ';
buf += '<span class="storage-warning">Browsers sometimes randomly clear cookies - you should upload your teams to the Showdown database ';
buf += 'or make a backup yourself if you want to make sure you don\'t lose them.</span></p>';
buf += '<button name="backup" class="button"><i class="fa fa-upload"></i> Backup/Restore all teams</button>';
buf += '<p>If you want to clear your cookies or <code>localStorage</code>, you can use the Backup/Restore feature to save your teams as text first.</p>';
var self = this;
Expand Down Expand Up @@ -832,6 +854,24 @@
this.exportMode = true;
this.update();
},
psExport: function () {
var cmd = '/teams ';
cmd += this.curTeam.teamid ? 'update' : 'save';
// teamName, formatid, rawPrivacy, rawTeam
var buf = [];
if (this.curTeam.teamid) buf.push(this.curTeam.teamid);
buf.push(this.curTeam.name);
buf.push(this.curTeam.format);
buf.push(this.curTeam.privacy ? 1 : 0);
var team = Storage.exportTeam(this.curSetList, this.curTeam.gen, false);
if (!team) return app.addPopupMessage("Add a Pokémon to your team before uploading it!");
buf.push(team);
app.send(cmd + " " + buf.join(', '));
this.exported = true;
$('button[name=psExport]').addClass('disabled');
$('button[name=psExport]')[0].disabled = true;
$('label[name=editMessage]').hide();
},
pokepasteExport: function (type) {
var team = Storage.exportTeam(this.curSetList, this.curTeam.gen, type === 'openteamsheet');
if (!team) return app.addPopupMessage("Add a Pokémon to your team before uploading it!");
Expand Down Expand Up @@ -950,6 +990,10 @@
if (edited) {
Storage.saveTeam(team);
app.user.trigger('saveteams');
this.exported = false;
$('button[name=psExport]').removeClass('disabled');
$('button[name=psExport]')[0].disabled = false;
$('label[name=editMessage]').show();
}

// We're going to try to animate the team settling into its new position
Expand Down Expand Up @@ -1134,6 +1178,9 @@
if (/^gen\d+$/.test(formatName)) return true;
return false;
};
if (this.loadingTeam) buf += '<div style="message-error">Downloading team from server...</strong><br />';
buf += '<label name="editMessage" style="display: none">';
buf += 'Remember to click the upload button below to sync your changes to the server!</label><br />';
if (exports.BattleFormats) {
buf += '<li class="format-select">';
buf += '<label class="label">Format:</label><button class="select formatselect teambuilderformatselect" name="format" value="' + this.curTeam.format + '">' + (isGenericFormat(this.curTeam.format) ? '<em>Select a format</em>' : BattleLog.escapeFormat(this.curTeam.format)) + '</button>';
Expand Down Expand Up @@ -1164,6 +1211,8 @@
buf += '<input type="hidden" name="paste" id="pasteData">';
buf += '<input type="hidden" name="author" id="pasteAuthor">';
buf += '<input type="hidden" name="notes" id="pasteNotes">';
buf += '<button name="psExport" type="submit" class="button exportbutton"> <i class="fa fa-upload"></i> Upload to Showdown database (saves across devices)</button>';
buf += '<br />';
buf += '<button name="pokepasteExport" type="submit" class="button exportbutton"><i class="fa fa-upload"></i> Upload to PokePaste</button></form>';
if (this.curTeam.format.includes('vgc')) {
buf += '<button name="pokepasteExport" value="openteamsheet" type="submit" class="button exportbutton"><i class="fa fa-upload"></i> Upload to PokePaste (Open Team Sheet)</button></form>';
Expand Down Expand Up @@ -1451,6 +1500,9 @@
}
},
validate: function () {
if (!this.curTeam.loaded) {
return app.loadTeam(this.curTeam, this.validate.bind(this));
}
var format = this.curTeam.format || 'gen7anythinggoes';

if (!this.curSetList.length) {
Expand All @@ -1461,8 +1513,9 @@
if (window.BattleFormats && BattleFormats[format] && BattleFormats[format].battleFormat) {
format = BattleFormats[format].battleFormat;
}
app.sendTeam(this.curTeam);
app.send('/vtm ' + format);
app.sendTeam(this.curTeam, function () {
app.send('/vtm ' + format);
});
},
teamNameChange: function (e) {
var name = ($.trim(e.currentTarget.value) || 'Untitled ' + (this.curTeamLoc + 1));
Expand Down
46 changes: 45 additions & 1 deletion js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ function toId() {
} else if (assertion.indexOf('\n') >= 0 || !assertion) {
app.addPopupMessage("Something is interfering with our connection to the login server.");
} else {
app.trigger('loggedin');
app.send('/trn ' + name + ',0,' + assertion);
}
},
Expand Down Expand Up @@ -553,6 +554,15 @@ function toId() {
self.addPopup(LoginPasswordPopup, {username: name, special: special});
});

this.on('loggedin', function () {
Storage.loadRemoteTeams(function () {
if (app.rooms.teambuilder) {
// if they have it open, be sure to update so it doesn't show 'no teams'
app.rooms.teambuilder.update();
}
});
});

this.on('response:savereplay', this.uploadReplay, this);

this.on('response:rooms', this.roomsResponse, this);
Expand Down Expand Up @@ -910,16 +920,50 @@ function toId() {
e.stopPropagation();
}
},
loadingTeam: null,
loadingTeamQueue: [],
loadTeam: function (team, callback) {
if (!team.teamid) return;
if (!this.loadingTeam) {
var app = this;
this.loadingTeam = true;
$.get(app.user.getActionPHP(), {
act: 'getteam',
teamid: team.teamid,
}, Storage.safeJSON(function (data) {
app.loadingTeam = false;
if (data.actionerror) {
return app.addPopupMessage("Error loading team: " + data.actionerror);
}
team.privacy = data.privacy;
team.team = data.team;
team.loaded = true;
callback(team);
var entry = app.loadingTeamQueue.shift();
if (entry) {
app.loadTeam(entry[0], entry[1]);
}
}));
} else {
this.loadingTeamQueue.push([team, callback]);
}
},
/**
* Send team to sim server
*/
sendTeam: function (team) {
sendTeam: function (team, callback) {
if (team.teamid && !team.loaded) {
return this.loadTeam(team, function (team) {
app.sendTeam(team, callback);
});
}
var packedTeam = '' + Storage.getPackedTeam(team);
if (packedTeam.length > 25 * 1024 - 6) {
alert("Your team is over 25 KB. Please use a smaller team.");
return;
}
this.send('/utm ' + packedTeam);
callback();
},
/**
* Receive from sim server
Expand Down
47 changes: 45 additions & 2 deletions js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,40 @@ Storage.loadTeams = function () {
} catch (e) {}
};

Storage.loadRemoteTeams = function (after) {
$.get(app.user.getActionPHP(), {act: 'getteams'}, Storage.safeJSON(function (data) {
if (data.actionerror) {
return app.addPopupMessage('Error loading uploaded teams: ' + data.actionerror);
}
for (var i = 0; i < data.teams.length; i++) {
var team = data.teams[i];
var matched = false;
for (var j = 0; j < Storage.teams.length; j++) {
var curTeam = Storage.teams[j];
if (curTeam.teamid === team.teamid) {
// prioritize locally saved teams over remote
// as so to not overwrite changes
matched = true;
break;
}
}
team.loaded = false;
if (!matched) {
// hack so that it shows up in the format selector list
team.folder = '';
// team comes down from loginserver as comma-separated list of mons
// to save bandwidth
var mons = team.team.split(',').map(function (mon) {
return {species: mon};
});
team.team = Storage.packTeam(mons);
Storage.teams.unshift(team);
}
}
if (typeof after === 'function') after();
}));
};

Storage.loadPackedTeams = function (buffer) {
try {
this.teams = Storage.unpackAllTeams(buffer);
Expand Down Expand Up @@ -669,16 +703,20 @@ Storage.unpackAllTeams = function (buffer) {
};

Storage.unpackLine = function (line) {
var leftBracketIndex = line.indexOf('[');
if (leftBracketIndex < 0) leftBracketIndex = 0;
var pipeIndex = line.indexOf('|');
if (pipeIndex < 0) return null;
if (leftBracketIndex > pipeIndex) leftBracketIndex = 0;
var bracketIndex = line.indexOf(']');
if (bracketIndex > pipeIndex) bracketIndex = -1;
var isBox = line.slice(0, bracketIndex).endsWith('-box');
var slashIndex = line.lastIndexOf('/', pipeIndex);
if (slashIndex < 0) slashIndex = bracketIndex; // line.slice(slashIndex + 1, pipeIndex) will be ''
var format = bracketIndex > 0 ? line.slice(0, isBox ? bracketIndex - 4 : bracketIndex) : 'gen9';
var format = bracketIndex > 0 ? line.slice((leftBracketIndex ? leftBracketIndex + 1 : 0), isBox ? bracketIndex - 4 : bracketIndex) : 'gen9';
if (format && format.slice(0, 3) !== 'gen') format = 'gen6' + format;
return {
teamid: leftBracketIndex > 0 ? Number(line.slice(0, leftBracketIndex)) : undefined,
name: line.slice(slashIndex + 1, pipeIndex),
format: format,
gen: parseInt(format[3], 10) || 6,
Expand All @@ -691,7 +729,12 @@ Storage.unpackLine = function (line) {

Storage.packAllTeams = function (teams) {
return teams.map(function (team) {
return (team.format ? '' + team.format + (team.capacity === 24 ? '-box]' : ']') : '') + (team.folder ? '' + team.folder + '/' : '') + team.name + '|' + Storage.getPackedTeam(team);
return (
(team.teamid ? '' + team.teamid + '[' : '') +
(team.format ? '' + team.format + (team.capacity === 24 ? '-box]' : ']') : '') +
(team.folder ? '' + team.folder + '/' : '') + team.name + '|' +
Storage.getPackedTeam(team)
);
}).join('\n');
};

Expand Down

0 comments on commit 5cf6cfd

Please sign in to comment.