Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor sim/ to be native Typescript #5210

Merged
merged 68 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from 62 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
a723e0a
Convert PRNG to typescript
scheibo Feb 22, 2019
40b28db
Working on import
scheibo Feb 22, 2019
043d4f8
Import the PRNG type in sim/dex.js
scheibo Feb 22, 2019
a0a60b5
Merge branch 'master' into tsc
scheibo Feb 24, 2019
175f6f0
Convert battle stream + example to ts
scheibo Feb 24, 2019
acc2324
Convert pokemon
scheibo Feb 24, 2019
13f799e
Convert side
scheibo Feb 24, 2019
61430ba
DEX DATA OMG
scheibo Feb 24, 2019
ebc493f
team-validators
scheibo Feb 24, 2019
0740f90
Dex
scheibo Feb 24, 2019
b84a5ea
Minor cleanup
scheibo Feb 24, 2019
34db689
Try using export default?
scheibo Feb 24, 2019
460cdfe
Done battle
scheibo Feb 24, 2019
8c3fdc6
Index
scheibo Feb 24, 2019
c7fee1b
Fixes
scheibo Feb 24, 2019
34d29ef
Globals
scheibo Feb 24, 2019
6f845ab
Convert data/random-team
scheibo Feb 24, 2019
f98d2ae
SSB
scheibo Feb 24, 2019
6564c18
Gen 6 random
scheibo Feb 24, 2019
9ec44d6
All random
scheibo Feb 24, 2019
34ec581
ALL COMPILE
scheibo Feb 24, 2019
1843ee7
add emit tsconfig
scheibo Feb 25, 2019
81437cf
Remove module exports
scheibo Feb 25, 2019
660772c
Fix prng test
scheibo Feb 25, 2019
71d8b88
Fix eslint
scheibo Feb 25, 2019
ffbd96f
SWC experiment
scheibo Feb 25, 2019
a3b27ed
Sucrase
scheibo Feb 25, 2019
4329baf
Revert "SWC experiment"
scheibo Feb 25, 2019
0155637
unignore
scheibo Feb 25, 2019
07f7af4
rm emit.json
scheibo Feb 25, 2019
a57efbc
Revert "ALL COMPILE"
scheibo Feb 25, 2019
e8f4825
Revert "All random"
scheibo Feb 25, 2019
fb479c3
Revert "Gen 6 random"
scheibo Feb 25, 2019
2b810ca
Revert "SSB"
scheibo Feb 25, 2019
4cb13cc
Revert "Convert data/random-team"
scheibo Feb 25, 2019
e934d68
Fix random-teams and moves
scheibo Feb 25, 2019
7776c7e
Add build commands
scheibo Feb 25, 2019
758d5c1
Merge branch 'master' into tsc
scheibo Feb 25, 2019
5fe5d5d
rm TODO
scheibo Feb 25, 2019
e7d0ce4
Install tslint
scheibo Feb 25, 2019
84eecb1
Include build script
scheibo Feb 25, 2019
9fda315
Add build
scheibo Feb 25, 2019
791e828
Fix lint rules
scheibo Feb 25, 2019
5723cfe
Switch to interface
scheibo Feb 25, 2019
0d52a7e
Non offensive
scheibo Feb 25, 2019
11c9985
Lint for battle-stream*
scheibo Feb 25, 2019
adda163
Battle
scheibo Feb 25, 2019
47e72af
Dex and team
scheibo Feb 25, 2019
2f0ed9a
Passing lint and compile
scheibo Feb 25, 2019
461ab14
Merge branch 'master' into tsc
scheibo Feb 25, 2019
9335f2e
Nits
scheibo Feb 25, 2019
acd6e38
Remove unused and self assigns (just ignore)
scheibo Feb 25, 2019
0263a56
Merge remote-tracking branch 'origin' into tsc
scheibo Feb 26, 2019
7d89d4b
Merge remote-tracking branch 'origin' into tsc
scheibo Feb 26, 2019
597f663
Add .sim-dist with warning
scheibo Feb 26, 2019
46e0fd6
Revert "Add .sim-dist with warning"
scheibo Feb 26, 2019
8e715fd
Revert "Revert "Add .sim-dist with warning""
scheibo Feb 26, 2019
b5cec60
Attempt Zarels method - still has issues
scheibo Feb 26, 2019
e280c51
Typescript require workaround
scheibo Feb 26, 2019
71e616c
Attempt to do any cast
scheibo Feb 26, 2019
e870ce5
Revert "Attempt to do any cast"
scheibo Feb 26, 2019
38d9dfa
omg it works
scheibo Feb 26, 2019
7d45d96
Fix a bunch of unnecessary @ts-ignore
Zarel Feb 26, 2019
2ef7aab
Remove tslint no-console setting
Zarel Feb 26, 2019
f1bb650
Remove 'fooLoc' pattern in favor of `any` casts
scheibo Feb 26, 2019
f9f7822
Merge branch 'master' into tsc
scheibo Feb 26, 2019
5f4ad51
Remove unnecessary cast
scheibo Feb 26, 2019
ffd4b21
Update server/team-validator-async.js to remove parens
Zarel Feb 26, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ server/tournaments/lib/
logs/
dev-tools/globals.js
node_modules/
.sim-dist/
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ npm-debug.log
.eslintcache
package-lock.json

# Typescript build artifacts
.sim-dist/

# visual studio live share
.vs

Expand All @@ -31,7 +34,6 @@ tmp/**/*
.installed.cfg

downloads
build
dist

# egg related
Expand All @@ -53,4 +55,3 @@ local_settings.py
.svn

pip-log.txt

3 changes: 3 additions & 0 deletions .sim-dist/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**NOTE**: This folder contains the compiled output of the `sim/` directory.
You should be editting the `.ts` files there and then running `npm run build` or
`./pokemon-showdown` to force these `.js` files to be recreated.
17 changes: 17 additions & 0 deletions build
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env node

if (!require('assert').strict) {
console.error("We require Node.js version 10 or later; you're using " + process.version);
process.exit(1);
}

var execSync = require('child_process').execSync;

try {
require.resolve('sucrase');
} catch (e) {
console.log('Installing dependencies...');
execSync('npm install', {stdio: 'inherit'});
}

execSync('npx sucrase ./sim -d ./.sim-dist --transforms typescript,imports');
2 changes: 2 additions & 0 deletions data/mods/gen2/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ let BattleScripts = {
}
return damage;
},
/** @return {number | undefined | false} */
moveHit(target, pokemon, move, moveData, isSecondary, isSelf) {
/** @type {number | false | null | undefined} */
let damage = undefined;
Expand Down Expand Up @@ -339,6 +340,7 @@ let BattleScripts = {
}
if (moveData.status) {
hitResult = target.trySetStatus(moveData.status, pokemon, move);
// @ts-ignore
if (!hitResult && move.status) return hitResult;
didSomething = didSomething || hitResult;
}
Expand Down
6 changes: 5 additions & 1 deletion data/mods/ssb/moves.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

// Used for bumbadadabum and Snaquaza's move
const RandomStaffBrosTeams = require('./random-teams');
const Pokemon = require('../../../sim/pokemon');
// Typescript workaround
const pokemonLoc = '../../../.sim-dist/pokemon';
/** @type {import('../../../sim/pokemon').Pokemon} */
const Pokemon = require(pokemonLoc);

/** @type {{[k: string]: ModdedMoveData}} */
let BattleMovedex = {
Expand Down Expand Up @@ -764,6 +767,7 @@ let BattleMovedex = {
// Bit of a hack so client doesn't crash when formeChange is called for the new pokemon
let effect = this.effect;
this.effect = /** @type {Effect} */ ({id: ''});
// @ts-ignore
let pokemon = new Pokemon(set, source.side);
this.effect = effect;

Expand Down
10 changes: 8 additions & 2 deletions data/random-teams.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
'use strict';

const Dex = require('./../sim/dex');
const PRNG = require('./../sim/prng');
// Typescript workaround
const dexLoc = './../.sim-dist/dex';
const prngLoc = './../.sim-dist/prng';
/** @type {typeof import('./../sim/dex')} */
const Dex = require(dexLoc);
/** @type {import('./../sim/prng').PRNG} */
const PRNG = require(prngLoc).PRNG;

/**@type {AnyObject} */
// @ts-ignore
Expand Down Expand Up @@ -38,6 +43,7 @@ class RandomTeams extends Dex.ModdedDex {

this.factoryTier = '';
this.format = format;
// @ts-ignore
this.prng = prng && !Array.isArray(prng) ? prng : new PRNG(prng);
}

Expand Down
3 changes: 3 additions & 0 deletions data/rulesets.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,10 @@ let BattleFormats = {
name: 'STABmons Move Legality',
desc: "Allows Pokémon to use any move that they or a previous evolution/out-of-battle forme share a type with",
checkLearnset(move, template, lsetData, set) {
// @ts-ignore
const restrictedMoves = this.format.restrictedMoves || [];
if (!move.isZ && !restrictedMoves.includes(move.name)) {
// @ts-ignore
let dex = this.dex;
let types = template.types;
let baseTemplate = dex.getTemplate(template.baseSpecies);
Expand All @@ -868,6 +870,7 @@ let BattleFormats = {
}
if (types.includes(move.type)) return null;
}
// @ts-ignore
return this.checkLearnset(move, template, lsetData, set);
},
},
Expand Down
1 change: 1 addition & 0 deletions data/statuses.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ let BattleStatuses = {
}
const hitMove = new this.Data.Move(posData.moveData);

// @ts-ignore
scheibo marked this conversation as resolved.
Show resolved Hide resolved
this.tryMoveHit(target, posData.source, hitMove);

this.effectData.positions[i] = null;
Expand Down
44 changes: 22 additions & 22 deletions dev-tools/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import child_process = require('child_process');

import BattleType = require('./../sim/battle');
import BattleStreamType = require('./../sim/battle-stream');
import DataType = require('./../sim/dex-data');
import {Battle as BattleType} from './../sim/battle';
import * as BattleStreamType from './../sim/battle-stream';
import * as DataType from './../sim/dex-data';
import DexType = require('./../sim/dex');
import SimType = require('./../sim/index');
import PokemonType = require('./../sim/pokemon');
import PRNGType = require('./../sim/prng');
import SideType = require('./../sim/side');
import {Pokemon as PokemonType} from './../sim/pokemon';
import {PRNG as PRNGType, PRNGSeed as PRNGSeedType} from './../sim/prng';
import {Side as SideType} from './../sim/side';
import TeamValidatorType = require('./../sim/team-validator');

import RoomsType = require('./../server/rooms');
Expand Down Expand Up @@ -38,29 +38,29 @@ declare global {

// sim
type PlayerSlot = 'p1' | 'p2'
type PRNGSeed = [number, number, number, number]
const Battle: typeof BattleType
type PRNGSeed = PRNGSeedType
const Battle: BattleType
const ModdedDex: typeof DexType
const Pokemon: typeof PokemonType
const PRNG: typeof PRNGType
const Side: typeof SideType
const Pokemon: PokemonType
const PRNG: PRNGType
const Side: SideType
const Sim: typeof SimType
const TeamValidator: typeof TeamValidatorType
const Validator: typeof TeamValidatorType.Validator
const BattleStream: typeof BattleStreamType.BattleStream
const BattleStream: BattleStreamType.BattleStream

// dex data
const Ability: typeof DataType.Ability
const BasicEffect: typeof DataType.BasicEffect
const Format: typeof DataType.Format
const Item: typeof DataType.Item
const Move: typeof DataType.Move
const TypeInfo: typeof DataType.TypeInfo
const PureEffect: typeof DataType.PureEffect
const RuleTable: typeof DataType.RuleTable
const Template: typeof DataType.Template
const Ability: DataType.Ability
const BasicEffect: DataType.BasicEffect
const Format: DataType.Format
const Item: DataType.Item
const Move: DataType.Move
const TypeInfo: DataType.TypeInfo
const PureEffect: DataType.PureEffect
const RuleTable: DataType.RuleTable
const Template: DataType.Template
const toId: typeof DataType.Tools.getId
const Tools: typeof DataType.Tools
const Tools: DataType.Tools

// rooms
const GlobalRoom: typeof RoomsType.GlobalRoom
Expand Down
10 changes: 5 additions & 5 deletions dev-tools/globals.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
type Battle = import('./../sim/battle')
type Battle = import('./../sim/battle').Battle
type ModdedDex = typeof import('./../sim/dex')
type Pokemon = import('./../sim/pokemon')
type Side = import('./../sim/side')
type Validator = ReturnType<typeof import('./../sim/team-validator')>
type Pokemon = import('./../sim/pokemon').Pokemon
type Side = import('./../sim/side').Side
type Validator = typeof import('./../sim/team-validator')

type PageTable = import('./../server/chat').PageTable
type ChatCommands = import('./../server/chat').ChatCommands
Expand Down Expand Up @@ -782,7 +782,7 @@ interface ModdedBattleScriptsData extends Partial<BattleScriptsData> {
side?: ModdedBattleSide
boost?: (this: Battle, boost: SparseBoostsTable, target: Pokemon, source?: Pokemon | null, effect?: Effect | string | null, isSecondary?: boolean, isSelf?: boolean) => boolean | null | 0
debug?: (this: Battle, activity: string) => void
getDamage?: (this: Battle, pokemon: Pokemon, target: Pokemon, move: string | number | ActiveMove, suppressMessages: boolean) => number
getDamage?: (this: Battle, pokemon: Pokemon, target: Pokemon, move: string | number | ActiveMove, suppressMessages: boolean) => number | undefined | null | false
getEffect?: (this: Battle, name: string | Effect | null) => Effect
init?: (this: Battle) => void
modifyDamage?: (this: Battle, baseDamage: number, pokemon: Pokemon, target: Pokemon, move: ActiveMove, suppressMessages?: boolean) => void
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"version": "0.11.2",
"dependencies": {
"probe-image-size": "^4.0.0",
"sockjs": "0.3.18"
"sockjs": "0.3.18",
"sucrase": "^3.9.5"
},
"optionalDependencies": {
"cloud-env": "^0.2.2",
Expand All @@ -19,7 +20,8 @@
},
"scripts": {
"start": "node pokemon-showdown start",
"test": "eslint --cache . && tsc && mocha",
"build": "node build",
"test": "eslint --cache . && tslint --project . && node build && tsc && mocha",
"tsc": "tsc",
"lint": "eslint --cache ."
},
Expand Down Expand Up @@ -62,6 +64,7 @@
"eslint": "^5.13.0",
"husky": "^1.1.2",
"mocha": "^5.2.0",
"tslint": "^5.13.0",
"typescript": "~3.3.3333"
}
}
25 changes: 18 additions & 7 deletions pokemon-showdown
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ try {
);
}

// # Make sure the Typescript sources have been compiled into Javascript.

try {
require.resolve('./.sim-dist/dex');
} catch(err) {
if (err.code !== 'MODULE_NOT_FOUND') throw err; // should never happen

console.log('Building project...');
child_process.execSync('npm run build', {stdio: 'inherit'});
}

if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
// Start the server.
//
Expand Down Expand Up @@ -89,7 +100,7 @@ if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
console.log('pokemon-showdown simulate-battle');
console.log('');
console.log(' Simulates a battle, taking input to stdin and writing output to stdout');
console.log(' Protocol is documented in ./sim/README.md');
console.log(' Protocol is documented in ./.sim-dist/README.md');
console.log('');
console.log('pokemon-showdown unpack-team');
console.log('');
Expand All @@ -114,16 +125,16 @@ if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
}
case 'generate-team':
{
var Dex = require('./sim/dex');
var Dex = require('./.sim-dist/dex');
global.toId = Dex.getId;
var seed = process.argv[4] ? process.argv[4].split(',').map(Number) : undefined;
console.log(Dex.packTeam(Dex.generateTeam(process.argv[3], seed)));
}
break;
case 'validate-team':
{
var Dex = require('./sim/dex');
var TeamValidator = require('./sim/team-validator');
var Dex = require('./.sim-dist/dex');
var TeamValidator = require('./.sim-dist/team-validator');
var validator = TeamValidator(process.argv[3]);
var Streams = require('./lib/streams');
var stdin = new Streams.ReadStream(process.stdin);
Expand All @@ -146,7 +157,7 @@ if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
break;
case 'simulate-battle':
{
var BattleTextStream = require('./sim/battle-stream').BattleTextStream;
var BattleTextStream = require('./.sim-dist/battle-stream').BattleTextStream;
var Streams = require('./lib/streams');
var stdin = new Streams.ReadStream(process.stdin);
var stdout = new Streams.WriteStream(process.stdout);
Expand All @@ -160,7 +171,7 @@ if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
break;
case 'unpack-team':
{
var Dex = require('./sim/dex');
var Dex = require('./.sim-dist/dex');
var Streams = require('./lib/streams');
var stdin = new Streams.ReadStream(process.stdin);

Expand All @@ -178,7 +189,7 @@ if (!process.argv[2] || /^[0-9]+$/.test(process.argv[2])) {
break;
case 'pack-team':
{
var Dex = require('./sim/dex');
var Dex = require('./.sim-dist/dex');
var Streams = require('./lib/streams');
var stdin = new Streams.ReadStream(process.stdin);

Expand Down
8 changes: 4 additions & 4 deletions server/chat-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -3035,12 +3035,12 @@ const commands = {
if (lock['battles']) return this.errorReply(`Hot-patching battles has been disabled by ${lock['battles'].by} (${lock['battles'].reason})`);
if (lock['validator']) return this.errorReply(`Hot-patching the validator has been disabled by ${lock['validator'].by} (${lock['validator'].reason})`);

// uncache the sim/dex.js dependency tree
// uncache the .sim-dist/dex.js dependency tree
Chat.uncacheDir('./sim');
Chat.uncacheDir('./data');
Chat.uncache('./config/formats');
// reload sim/dex.js
global.Dex = require('../sim/dex');
// reload .sim-dist/dex.js
global.Dex = require('../.sim-dist/dex');
// rebuild the formats list
delete Rooms.global.formatList;
// respawn validator processes
Expand Down Expand Up @@ -3089,7 +3089,7 @@ const commands = {
`You can disable various hot-patches with /nohotpatch. For more information on this, see /help nohotpatch`,
`/hotpatch chat - reload chat-commands.js and the chat-plugins`,
`/hotpatch validator - spawn new team validator processes`,
`/hotpatch formats - reload the sim/dex.js tree, rebuild and rebroad the formats list, and spawn new simulator and team validator processes`,
`/hotpatch formats - reload the .sim-dist/dex.js tree, rebuild and rebroad the formats list, and spawn new simulator and team validator processes`,
`/hotpatch dnsbl - reloads Dnsbl datacenters`,
`/hotpatch punishments - reloads new punishments code`,
`/hotpatch tournaments - reloads new tournaments code`,
Expand Down
4 changes: 2 additions & 2 deletions server/chat-plugins/datasearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -1614,10 +1614,10 @@ if (!PM.isParentProcess) {
});
}

global.Dex = require('../../sim/dex');
global.Dex = require('../../.sim-dist/dex');
global.toId = Dex.getId;
Dex.includeData();
global.TeamValidator = require('../../sim/team-validator');
global.TeamValidator = require('../../.sim-dist/team-validator');

require('../../lib/repl').start('dexsearch', cmd => eval(cmd));
} else {
Expand Down
2 changes: 1 addition & 1 deletion server/chat-plugins/modlog.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ if (!PM.isParentProcess) {
require('../../lib/crashlogger')(err, 'A modlog child process');
}
});
global.Dex = require('../../sim/dex');
global.Dex = require('../../.sim-dist/dex');
global.toId = Dex.getId;

require('../../lib/repl').start('modlog', cmd => eval(cmd));
Expand Down
4 changes: 2 additions & 2 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* rooms.js. There's also a global room which every user is in, and
* handles miscellaneous things like welcoming the user.
*
* Dex - from sim/dex.js
* Dex - from .sim-dist/dex.js
*
* Handles getting data about Pokemon, items, etc.
*
Expand Down Expand Up @@ -96,7 +96,7 @@ if (Config.watchconfig) {
* Set up most of our globals
*********************************************************/

global.Dex = require('../sim/dex');
global.Dex = require('../.sim-dist/dex');
global.toId = Dex.getId;

global.LoginServer = require('./loginserver');
Expand Down
Loading