mirror of
https://github.com/smogon/pokemon-showdown.git
synced 2026-04-19 17:17:22 -05:00
I'm currently pretty annoyed at TypeScript and TC39 for default exports
being a mess.
The goal here is to be able to type
import Dex from './dex';
instead of any of
import Dex = require('./dex');
import {Dex} from './dex';
import * as Dex from './dex';
This part involves a significant amount of struggle.
First, you can't automatically package up all your exports as your
default export. This leads to extremely un-DRY code, like in sim/index:
export {
Pokemon,
Side,
Battle,
PRNG,
Dex,
TeamValidator,
BattleStream,
};
export const Sim = {
Pokemon,
Side,
Battle,
PRNG,
Dex,
TeamValidator,
BattleStream,
};
(Both of these exports would be entirely unnecessary if you could just
automatically declare the file's exports as a default namespace.)
Second, a default export can't easily be a namespace. And TypeScript
doesn't allow types to exist in objects. Take the example from earlier:
export const Sim = {
Pokemon,
};
If we were to try to use it:
import Sim from './sim';
let pokemon: Sim.Pokemon;
you'll get this error:
Cannot find namespace 'Sim'. ts(2503)
You can, of course, fix this by making Sim a namespace:
const PokemonT = Pokemon;
type PokemonT = Pokemon;
export namespace Sim {
export const Pokemon = PokemonT;
type Pokemon = PokemonT;
}
But this quickly gets ridiculous the more classes you try to export.
You'd think there'd be a better way to do this. But I, at least,
haven't found one.
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
/**
|
|
* Battle Stream Example
|
|
* Pokemon Showdown - http://pokemonshowdown.com/
|
|
*
|
|
* Example of how to create AIs battling against each other.
|
|
* Run this using `node build && node .sim-dist/examples/battle-stream-example`.
|
|
*
|
|
* @license MIT
|
|
* @author Guangcong Luo <guangcongluo@gmail.com>
|
|
*/
|
|
|
|
import {BattleStream, getPlayerStreams} from '../battle-stream';
|
|
import Dex from '../dex';
|
|
import RandomPlayerAI from '../tools/random-player-ai';
|
|
|
|
/*********************************************************************
|
|
* Run AI
|
|
*********************************************************************/
|
|
// tslint:disable:no-floating-promises
|
|
|
|
const streams = getPlayerStreams(new BattleStream());
|
|
|
|
const spec = {
|
|
formatid: "gen7customgame",
|
|
};
|
|
const p1spec = {
|
|
name: "Bot 1",
|
|
team: Dex.packTeam(Dex.generateTeam('gen7randombattle')),
|
|
};
|
|
const p2spec = {
|
|
name: "Bot 2",
|
|
team: Dex.packTeam(Dex.generateTeam('gen7randombattle')),
|
|
};
|
|
|
|
const p1 = new RandomPlayerAI(streams.p1);
|
|
const p2 = new RandomPlayerAI(streams.p2);
|
|
|
|
console.log("p1 is " + p1.constructor.name);
|
|
console.log("p2 is " + p2.constructor.name);
|
|
|
|
p1.start();
|
|
p2.start();
|
|
|
|
(async () => {
|
|
let chunk;
|
|
// tslint:disable-next-line no-conditional-assignment
|
|
while ((chunk = await streams.omniscient.read())) {
|
|
console.log(chunk);
|
|
}
|
|
})();
|
|
|
|
streams.omniscient.write(`>start ${JSON.stringify(spec)}
|
|
>player p1 ${JSON.stringify(p1spec)}
|
|
>player p2 ${JSON.stringify(p2spec)}`);
|