File size: 6,083 Bytes
5c2ed06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
 * 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.ts.
 *
 * Matchmaking is currently still implemented in rooms.ts.
 *
 * @license MIT
 */
import { Utils } from '../lib';

export class LadderStore {
	formatid: string;
	static readonly 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.
	 */
	// This requires to be `async` because it must conform with the `LadderStore` interface
	// eslint-disable-next-line @typescript-eslint/require-await
	async getTop(prefix?: string): 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?.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.id === 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): Promise<[
		number, AnyObject | undefined | null, AnyObject | undefined | null,
	]> {
		if (Ladders.disabled) {
			room.addRaw(`Ratings not updated. The ladders are currently disabled.`).update();
			return [p1score, null, null];
		}

		const formatid = this.formatid;
		const p1 = Users.getExact(p1name);
		const p2 = Users.getExact(p2name);
		const p1id = toID(p1name);
		const p2id = toID(p2name);

		const ladderUpdatePromise = LoginServer.request('ladderupdate', {
			p1: p1name,
			p2: p2name,
			score: p1score,
			format: formatid,
		});

		// calculate new Elo scores and display to room while loginserver updates the ladder
		const [p1OldElo, p2OldElo] = (await Promise.all([this.getRating(p1id), this.getRating(p2id)])).map(Math.round);
		const p1NewElo = Math.round(this.calculateElo(p1OldElo, p1score, p2OldElo));
		const p2NewElo = Math.round(this.calculateElo(p2OldElo, 1 - p1score, p1OldElo));

		const p1Act = (p1score > 0.9 ? `winning` : (p1score < 0.1 ? `losing` : `tying`));
		let p1Reasons = `${p1NewElo - p1OldElo} for ${p1Act}`;
		if (!p1Reasons.startsWith('-')) p1Reasons = '+' + p1Reasons;
		room.addRaw(Utils.html`${p1name}'s rating: ${p1OldElo} &rarr; <strong>${p1NewElo}</strong><br />(${p1Reasons})`);

		const p2Act = (p1score > 0.9 || p1score < 0 ? `losing` : (p1score < 0.1 ? `winning` : `tying`));
		let p2Reasons = `${p2NewElo - p2OldElo} for ${p2Act}`;
		if (!p2Reasons.startsWith('-'))	p2Reasons = '+' + p2Reasons;
		room.addRaw(Utils.html`${p2name}'s rating: ${p2OldElo} &rarr; <strong>${p2NewElo}</strong><br />(${p2Reasons})`);

		room.rated = Math.min(p1NewElo, p2NewElo);

		if (p1) p1.mmrCache[formatid] = +p1NewElo;
		if (p2) p2.mmrCache[formatid] = +p2NewElo;

		room.update();

		const [data, error] = await ladderUpdatePromise;

		let problem = false;
		if (error) {
			if (error.message !== 'stream interrupt') {
				room.add(`||Ladder isn't responding, score probably updated but might not have (${error.message}).`);
				problem = true;
			}
		} else if (!room.battle) {
			problem = true;
		} else if (!data) {
			room.add(`|error|Unexpected response ${data} from ladder server.`);
			room.update();
			problem = true;
		} else 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();
			problem = true;
		}

		if (problem) {
			// We used to clear mmrCache for the format to get the users updated rating next search
			// we now no longer do that because that results in the user getting paired with other users as though they have 1000 elo
			// if the next query times out, which happens very frequently. This results in a lot of confusion, so we're just
			// going to not clear this cache. If the user gets the proper rating later - great. If they don't,
			// this will ensure they still get matched up in a much more accurate fashion.
			return [p1score, null, null];
		}

		return [p1score, data?.p1rating, data?.p2rating];
	}

	/**
	 * Returns a Promise for an array of strings of <tr>s for ladder ratings of the user
	 */
	// This requires to be `async` because it must conform with the `LadderStore` interface
	// eslint-disable-next-line @typescript-eslint/require-await
	static async visualizeAll(username: string) {
		return [`<tr><td><strong>Please use the official client at play.pokemonshowdown.com</strong></td></tr>`];
	}
	/**
	 * Calculates Elo based on a match result
	 */
	calculateElo(oldElo: number, score: number, foeElo: number): number {
		// see lib/ntbb-ladder.lib.php in the pokemon-showdown-client repo for the login server implementation
		// *intentionally* different from calculation in ladders-local, due to the high activity on the main server

		// The K factor determines how much your Elo changes when you win or
		// lose games. Larger K means more change.
		// In the "original" Elo, K is constant, but it's common for K to
		// get smaller as your rating goes up
		let K = 50;

		// dynamic K-scaling (optional)
		if (oldElo < 1100) {
			if (score < 0.5) {
				K = 20 + (oldElo - 1000) * 30 / 100;
			} else if (score > 0.5) {
				K = 80 - (oldElo - 1000) * 30 / 100;
			}
		} else if (oldElo > 1300) {
			K = 40;
		}

		// main Elo formula
		const E = 1 / (1 + 10 ** ((foeElo - oldElo) / 400));

		const newElo = oldElo + K * (score - E);

		return Math.max(newElo, 1000);
	}
}