function Storage() {} Storage.initialize = function() { if (window.nodewebkit) { window.fs = require('fs'); this.initDirectory(); this.startLoggingChat = this.nwStartLoggingChat; this.stopLoggingChat = this.nwStopLoggingChat; this.logChat = this.nwLogChat; } }; /********************************************************* * Teams *********************************************************/ /** * Teams are normally loaded from `localStorage`. * If the client isn't running on `play.pokemonshowdown.com`, though, * teams are received from `crossdomain.php` instead. */ Storage.teams = null; Storage.loadTeams = function() { if (window.nodewebkit) { return; } this.teams = []; if (window.localStorage) { Storage.loadPackedTeams(localStorage.getItem('showdown_teams')); app.trigger('init:loadteams'); } }; Storage.loadPackedTeams = function(buffer) { try { this.teams = Storage.unpackAllTeams(buffer); } catch (e) { app.addPopup(Popup, { type: 'modal', htmlMessage: "Your teams are corrupt and could not be loaded. :( We may be able to recover a team from this data:
" }); } }; Storage.saveTeams = function() { if (window.localStorage) { Storage.cantSave = false; try { localStorage.setItem('showdown_teams', Storage.packAllTeams(this.teams)); } catch (e) { if (e.code === DOMException.QUOTA_EXCEEDED_ERR) { Storage.cantSave = true; } else { throw e; } } } }; Storage.getPackedTeams = function() { var packedTeams = ''; try { packedTeams = localStorage.getItem('showdown_teams'); } catch (e) {} if (packedTeams) return packedTeams; return Storage.packAllTeams(this.teams); }; Storage.saveTeam = function() { this.saveTeams(); }; Storage.deleteTeam = function() { this.saveTeams(); }; Storage.saveAllTeams = function() { this.saveTeams(); }; /********************************************************* * Team importing and exporting *********************************************************/ Storage.unpackAllTeams = function(buffer) { if (!buffer) return []; if (buffer.charAt(0) === '[') { // old format return JSON.parse(buffer).map(function (oldTeam) { return { name: oldTeam.name || '', format: oldTeam.format || '', team: Storage.packTeam(oldTeam.team), iconCache: '' }; }); return; } return buffer.split('\n').map(function (line) { var pipeIndex = line.indexOf('|'); if (pipeIndex < 0) return; var bracketIndex = line.indexOf(']'); if (bracketIndex > pipeIndex) bracketIndex = -1; return { name: line.slice(bracketIndex + 1, pipeIndex), format: bracketIndex > 0 ? line.slice(0, bracketIndex) : '', team: line.slice(pipeIndex + 1), iconCache: '' }; }).filter(function (v) { return v; }); }; Storage.packAllTeams = function(teams) { return teams.map(function (team) { return (team.format ? ''+team.format+']' : '') + team.name + '|' + Storage.getPackedTeam(team); }).join('\n'); }; Storage.packTeam = function(team) { var buf = ''; if (!team) return ''; for (var i=0; i(empty team)'; return this.packedTeamNames(buf).map(function (species) { return '' + toId(species) + ''; }).join(''); }; Storage.getTeamIcons = function(team) { if (team.iconCache === '!') { // an icon cache of '!' means that not only are the icons not cached, // but the packed team isn't guaranteed to be updated to the latest // changes from the teambuilder, either. // we use Storage.activeSetList instead of reading from // app.rooms.teambuilder.curSetList because the teambuilder // room may have been closed by the time we need to get // a packed team. team.team = Storage.packTeam(Storage.activeSetList); Storage.activeSetList = null; team.iconCache = Storage.packedTeamIcons(team.team); } else if (!team.iconCache) { team.iconCache = Storage.packedTeamIcons(team.team); } return team.iconCache; }; Storage.getPackedTeam = function(team) { if (!team) return null; if (team.iconCache === '!') { // see the same case in Storage.getTeamIcons team.team = Storage.packTeam(Storage.activeSetList); Storage.activeSetList = null; team.iconCache = ''; } return team.team; }; Storage.importTeam = function(text, teams) { var text = text.split("\n"); var team = []; var curSet = null; if (teams === true) { Storage.teams = []; teams = Storage.teams; } for (var i=0; i= 0) { format = line.substr(1, bracketIndex-1); line = $.trim(line.substr(bracketIndex+1)); } if (teams.length) { teams[teams.length - 1].team = Storage.packTeam(teams[teams.length - 1].team); } teams.push({ name: line, format: format, team: team, iconCache: '' }); } else if (!curSet) { curSet = {name: '', species: '', gender: ''}; team.push(curSet); var atIndex = line.lastIndexOf(' @ '); if (atIndex !== -1) { curSet.item = line.substr(atIndex+3); if (toId(curSet.item) === 'noitem') curSet.item = ''; line = line.substr(0, atIndex); } if (line.substr(line.length-4) === ' (M)') { curSet.gender = 'M'; line = line.substr(0, line.length-4); } if (line.substr(line.length-4) === ' (F)') { curSet.gender = 'F'; line = line.substr(0, line.length-4); } var parenIndex = line.lastIndexOf(' ('); if (line.substr(line.length-1) === ')' && parenIndex !== -1) { line = line.substr(0, line.length-1); curSet.species = Tools.getTemplate(line.substr(parenIndex+2)).name; line = line.substr(0, parenIndex); curSet.name = line; } else { curSet.species = Tools.getTemplate(line).name; curSet.name = curSet.species; } } else if (line.substr(0, 7) === 'Trait: ') { line = line.substr(7); curSet.ability = line; } else if (line.substr(0, 9) === 'Ability: ') { line = line.substr(9); curSet.ability = line; } else if (line === 'Shiny: Yes') { curSet.shiny = true; } else if (line.substr(0, 7) === 'Level: ') { line = line.substr(7); curSet.level = +line; } else if (line.substr(0, 11) === 'Happiness: ') { line = line.substr(11); curSet.happiness = +line; } else if (line.substr(0, 9) === 'Ability: ') { line = line.substr(9); curSet.ability = line; } else if (line.substr(0, 5) === 'EVs: ') { line = line.substr(5); var evLines = line.split('/'); curSet.evs = {hp: 0, atk: 0, def: 0, spa: 0, spd: 0, spe: 0}; for (var j=0; j= 0) return team; team = Storage.fastUnpackTeam(team); } var text = ''; for (var i=0; i