pokemon-showdown/server/ladders-remote.ts
Guangcong Luo 87ef0cd5c4 TypeScript: export default
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.
2019-05-14 20:33:33 +10:00

147 lines
4.1 KiB
TypeScript

/**
* Main server ladder library
* Pokemon Showdown - http://pokemonshowdown.com/
*
* This file handles ladders for the main server on
* play.pokemonshowdown.com.
*
* Ladders for all other servers is handled by ladders.js.
*
* Matchmaking is currently still implemented in rooms.js.
*
* @license MIT license
*/
'use strict';
export class LadderStore {
formatid: string;
static formatsListPrefix = '';
constructor(formatid: string) {
this.formatid = formatid;
}
/**
* Returns [formatid, html], where html is an the HTML source of a
* ladder toplist, to be displayed directly in the ladder tab of the
* client.
*/
async getTop(): Promise<[string, string] | null> {
return null;
}
/**
* Returns a Promise for the Elo rating of a user
*/
async getRating(userid: string) {
const formatid = this.formatid;
const user = Users.getExact(userid);
if (user && user.mmrCache[formatid]) {
return user.mmrCache[formatid];
}
const [data] = await LoginServer.request('mmr', {
format: formatid,
user: userid,
});
let mmr = NaN;
if (data && !data.errorip) {
mmr = Number(data);
}
if (isNaN(mmr)) return 1000;
if (user && user.userid === userid) {
user.mmrCache[formatid] = mmr;
}
return mmr;
}
/**
* Update the Elo rating for two players after a battle, and display
* the results in the passed room.
*/
async updateRating(p1name: string, p2name: string, p1score: number, room: AnyObject) {
if (Ladders.disabled) {
room.addRaw(`Ratings not updated. The ladders are currently disabled.`).update();
return [p1score, null, null];
}
const formatid = this.formatid;
room.update();
room.send(`||Ladder updating...`);
const [data, , error] = await LoginServer.request('ladderupdate', {
p1: p1name,
p2: p2name,
score: p1score,
format: formatid,
});
if (error) {
if (error.message === 'stream interrupt') {
room.add(`||Ladder updated, but score could not be retrieved.`);
} else {
room.add(`||Ladder (probably) updated, but score could not be retrieved (${error.message}).`);
}
return [p1score, null, null];
}
if (!room.battle) {
Monitor.warn(`room expired before ladder update was received`);
return [p1score, null, null];
}
if (!data) {
room.add(`|error|Unexpected response ${data} from ladder server.`);
room.update();
return [p1score, null, null];
}
if (data.errorip) {
room.add(`|error|This server's request IP ${data.errorip} is not a registered server.`);
room.add(`|error|You should be using ladders.js and not ladders-remote.js for ladder tracking.`);
room.update();
return [p1score, null, null];
}
let p1rating;
let p2rating;
try {
p1rating = data.p1rating;
p2rating = data.p2rating;
let oldelo = Math.round(p1rating.oldelo);
let elo = Math.round(p1rating.elo);
let act = (p1score > 0.9 ? `winning` : (p1score < 0.1 ? `losing` : `tying`));
let reasons = `${elo - oldelo} for ${act}`;
if (reasons.charAt(0) !== '-') reasons = '+' + reasons;
room.addRaw(Chat.html`${p1name}'s rating: ${oldelo} &rarr; <strong>${elo}</strong><br />(${reasons})`);
let minElo = elo;
oldelo = Math.round(p2rating.oldelo);
elo = Math.round(p2rating.elo);
act = (p1score > 0.9 || p1score < 0 ? `losing` : (p1score < 0.1 ? `winning` : `tying`));
reasons = `${elo - oldelo} for ${act}`;
if (reasons.charAt(0) !== '-') reasons = '+' + reasons;
room.addRaw(Chat.html`${p2name}'s rating: ${oldelo} &rarr; <strong>${elo}</strong><br />(${reasons})`);
if (elo < minElo) minElo = elo;
room.rated = minElo;
const p1 = Users.getExact(p1name);
if (p1) p1.mmrCache[formatid] = +p1rating.elo;
const p2 = Users.getExact(p2name);
if (p2) p2.mmrCache[formatid] = +p2rating.elo;
room.update();
} catch (e) {
room.addRaw(`There was an error calculating rating changes.`);
room.update();
}
return [p1score, p1rating, p2rating];
}
/**
* Returns a Promise for an array of strings of <tr>s for ladder ratings of the user
*/
static async visualizeAll(username: string) {
return [`<tr><td><strong>Please use the official client at play.pokemonshowdown.com</strong></td></tr>`];
}
}
export default LadderStore;