Skip to content

Commit

Permalink
Support importing teams
Browse files Browse the repository at this point in the history
We now have a `Teams.import` function. This supports importing teams
in any format, allowing it to be the backbone of a new series of
commandline functions, which support teams in any format.
  • Loading branch information
Zarel committed Jun 10, 2021
1 parent 5d3b758 commit ddb6010
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 18 deletions.
84 changes: 80 additions & 4 deletions COMMANDLINE.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Note: Commands that ask for a team want the team in [packed team format][packed-

`./pokemon-showdown validate-team [FORMAT-ID]`

: Reads a team from stdin, and validates it
: Reads a team in any format from stdin, and validates it
: - If valid: exits with code 0
: - If invalid: writes errors to stderr, exits with code 1

Expand All @@ -51,14 +51,90 @@ Note: Commands that ask for a team want the team in [packed team format][packed-
: Using Pokémon Showdown as a command-line simulator is documented at:
: https://github.com/smogon/pokemon-showdown/blob/master/sim/README.md

`./pokemon-showdown unpack-team`
`./pokemon-showdown json-team`

: Reads a team from stdin, writes the unpacked JSON to stdout
: Reads a team in any format from stdin, writes the unpacked JSON to stdout

`./pokemon-showdown pack-team`

: Reads a JSON team from stdin, writes the packed team to stdout
: Reads a team in any format from stdin, writes the packed team to stdout

`./pokemon-showdown export-team`

: Reads a team in any format from stdin, writes the exported (human-readable) team to stdout

`./pokemon-showdown help`

: Displays this reference


Piping
------

These commands are very unixy (using stdin and stdout), so you can of course pipe them together:

```
$ ./pokemon-showdown generate-team gen8randombattle | ./pokemon-showdown export-team
Kartana @ Choice Band
Ability: Beast Boost
Level: 74
EVs: 85 HP / 85 Atk / 85 Def / 85 SpA / 85 SpD / 85 Spe
- Smart Strike
- Sacred Sword
- Knock Off
- Leaf Blade
Rotom (Rotom-Heat) @ Heavy-Duty Boots
Ability: Levitate
Level: 82
EVs: 85 HP / 85 Def / 85 SpA / 85 SpD / 85 Spe
IVs: 0 Atk
- Defog
- Will-O-Wisp
- Thunderbolt
- Overheat
Kingler @ Life Orb
Ability: Sheer Force
Level: 84
EVs: 85 HP / 85 Atk / 85 Def / 85 SpA / 85 SpD / 85 Spe
- Liquidation
- X-Scissor
- Superpower
- Rock Slide
Abomasnow @ Light Clay
Ability: Snow Warning
Level: 82
EVs: 85 HP / 85 Atk / 85 Def / 85 SpA / 85 SpD / 85 Spe
- Ice Shard
- Aurora Veil
- Earthquake
- Blizzard
Goodra @ Assault Vest
Ability: Sap Sipper
Level: 82
EVs: 85 HP / 85 Atk / 85 Def / 85 SpA / 85 SpD / 85 Spe
- Earthquake
- Power Whip
- Draco Meteor
- Fire Blast
Raikou @ Choice Specs
Ability: Pressure
Level: 80
EVs: 85 HP / 85 Def / 85 SpA / 85 SpD / 85 Spe
IVs: 0 Atk
- Scald
- Aura Sphere
- Thunderbolt
- Volt Switch
```

```
$ ./pokemon-showdown generate-team gen8randombattle | ./pokemon-showdown validate-team gen8ou
Your set for Coalossal is flagged as Gigantamax, but Gigantamaxing is disallowed
(If this was a mistake, disable Gigantamaxing on the set.)
Octillery's ability Moody is banned.
```
51 changes: 39 additions & 12 deletions pokemon-showdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ try {
build();
}

function readTeam(stream) {
return stream.readLine().then(line => {
if (line.startsWith('[') || line.includes('|')) return line;
return stream.readAll().then(all => (line + '\n' + all));
});
}

if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
// Start the server.
//
Expand Down Expand Up @@ -68,16 +75,17 @@ if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
console.log(' Simulates a battle, taking input to stdin and writing output to stdout');
console.log(' Protocol is documented in ./.sim-dist/README.md');
console.log('');
console.log('pokemon-showdown unpack-team');
console.log('pokemon-showdown json-team');
console.log('');
console.log(' Reads a team from stdin, writes the unpacked JSON to stdout');
console.log(' Reads a team in any format from stdin, writes the unpacked JSON to stdout');
console.log('');
console.log('pokemon-showdown pack-team');
console.log('');
console.log(' Reads a JSON team from stdin, writes the packed team to stdout');
console.log(' NOTE for all team-processing functions: We can only handle JSON teams');
console.log(' and packed teams; the PS server is incapable of processing exported');
console.log(' teams.');
console.log(' Reads a team in any format from stdin, writes the packed team to stdout');
console.log('');
console.log('pokemon-showdown export-team');
console.log('');
console.log(' Reads a team in any format from stdin, writes the exported (human-readable) team to stdout');
console.log('');
console.log('pokemon-showdown help');
console.log('');
Expand Down Expand Up @@ -108,9 +116,9 @@ if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
var Streams = require('./.lib-dist/streams');
var stdin = Streams.stdin();

stdin.readLine().then(function (textTeam) {
readTeam(stdin).then(function (textTeam) {
try {
var team = Teams.unpack(textTeam);
var team = Teams.import(textTeam);
var result = validator.validateTeam(team);
if (result) {
console.error(result.join('\n'));
Expand Down Expand Up @@ -175,14 +183,15 @@ if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
}
break;
case 'unpack-team':
case 'json-team':
{
var Teams = require('./.sim-dist/teams').Teams;
var Streams = require('./.lib-dist/streams');
var stdin = Streams.stdin();

stdin.readLine().then(function (packedTeam) {
readTeam(stdin).then(function (team) {
try {
var unpackedTeam = Teams.unpack(packedTeam);
var unpackedTeam = Teams.unpack(Teams.import(team));
console.log(JSON.stringify(unpackedTeam));
process.exit(0);
} catch (e) {
Expand All @@ -198,9 +207,9 @@ if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
var Streams = require('./.lib-dist/streams');
var stdin = Streams.stdin();

stdin.readLine().then(function (unpackedTeam) {
readTeam(stdin).then(function (team) {
try {
var packedTeam = Teams.pack(JSON.parse(unpackedTeam));
var packedTeam = Teams.pack(Teams.import(team));
console.log(packedTeam);
process.exit(0);
} catch (e) {
Expand All @@ -210,6 +219,24 @@ if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
});
}
break;
case 'export-team':
{
var Teams = require('./.sim-dist/teams').Teams;
var Streams = require('./.lib-dist/streams');
var stdin = Streams.stdin();

readTeam(stdin).then(function (team) {
try {
var exportedTeam = Teams.export(Teams.import(team));
console.log(exportedTeam);
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
});
}
break;
default:
console.error('Unrecognized command: ' + process.argv[2]);
console.error('Use `pokemon-showdown help` for help');
Expand Down
6 changes: 5 additions & 1 deletion sim/TEAMS.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ API:

- Converts a JSON team to a packed team

`Teams.import(exportedTeam: string): PokemonSet[]`

- Converts a team in any string format (JSON, exported, or packed) to a JSON team

`Teams.export(team: PokemonSet[]): string`

- Converts a JSON team to an export team
Expand All @@ -181,7 +185,7 @@ API:

- Converts a JSON set to export format

(Import is not available in this version; we'll add it to a future version.)
To convert from export to packed (or vice versa), just round-trip through PokemonSet: `Teams.export(Teams.unpack(packedTeam))` will produce an exported team.

Example use:

Expand Down
17 changes: 17 additions & 0 deletions sim/dex-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ export class DexTypes {
}

const idsCache: readonly StatID[] = ['hp', 'atk', 'def', 'spa', 'spd', 'spe'];
const reverseCache: {readonly [k: string]: StatID} = {
__proto: null as any,
"hitpoints": 'hp',
"attack": 'atk',
"defense": 'def',
"specialattack": 'spa', "spatk": 'spa', "spattack": 'spa', "specialatk": 'spa',
"special": 'spa', "spc": 'spa',
"specialdefense": 'spd', "spdef": 'spd', "spdefense": 'spd', "specialdef": 'spd',
"speed": 'spe',
};
export class DexStats {
readonly shortNames: {readonly [k in StatID]: string};
readonly mediumNames: {readonly [k in StatID]: string};
Expand Down Expand Up @@ -334,6 +344,13 @@ export class DexStats {
} as any;
}
}
getID(name: string) {
if (name === 'Spd') return 'spe' as StatID;
const id = toID(name);
if (reverseCache[id]) return reverseCache[id];
if (idsCache.includes(id as StatID)) return id as StatID;
return null;
}
ids(): typeof idsCache {
return idsCache;
}
Expand Down

0 comments on commit ddb6010

Please sign in to comment.