Skip to content

Commit

Permalink
linting changes, as well as removed an import that wasn't being used …
Browse files Browse the repository at this point in the history
…in the spec file, no functional changes. looks good!
  • Loading branch information
ivarcode committed Feb 19, 2021
1 parent ef2bd66 commit d515a97
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 65 deletions.
43 changes: 21 additions & 22 deletions src/app/lib/elo.library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,30 @@

import { Elo } from './elo.library';
import { Score } from './util.library';
import { TestBed } from '@angular/core/testing';

/// Test results are calculated with the FIDE calculator:
/// https://ratings.fide.com/calculator_rtd.phtml

describe('Elo system', () => {
it('should be able to calcuate the right (low-level) elo', () => {
const playerA = new Elo(1456, 40);
const playerB = new Elo(1790, 40);
const result = 1491;
playerA.update(playerB.rating, Score.win);
expect(playerA.rating).toBe(result);
});
it('should be able to calcuate the right (mid-level) elo', () => {
const playerA = new Elo(1732, 20);
const playerB = new Elo(2047, 20);
const result = 1739;
playerA.update(playerB.rating, Score.draw);
expect(playerA.rating).toBe(result);
});
it('should be able to calcuate the right (high-level) elo', () => {
const playerA = new Elo(2761, 10);
const playerB = new Elo(2622, 10);
const result = 2754;
playerA.update(playerB.rating, Score.loss);
expect(playerA.rating).toBe(result);
});
it('should be able to calcuate the right (low-level) elo', () => {
const playerA = new Elo(1456, 40);
const playerB = new Elo(1790, 40);
const result = 1491;
playerA.update(playerB.rating, Score.win);
expect(playerA.rating).toBe(result);
});
it('should be able to calcuate the right (mid-level) elo', () => {
const playerA = new Elo(1732, 20);
const playerB = new Elo(2047, 20);
const result = 1739;
playerA.update(playerB.rating, Score.draw);
expect(playerA.rating).toBe(result);
});
it('should be able to calcuate the right (high-level) elo', () => {
const playerA = new Elo(2761, 10);
const playerB = new Elo(2622, 10);
const result = 2754;
playerA.update(playerB.rating, Score.loss);
expect(playerA.rating).toBe(result);
});
});
92 changes: 50 additions & 42 deletions src/app/lib/elo.library.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,54 @@
import { Score } from './util.library';

export class Elo {

private _rating: number;
private _K: number;
private _games: number;

constructor(rating?: number, k?: number) {
this._rating = rating ?? 1500;
this._K = k ?? 40;
if (this._K !== 10 &&
this._K !== 20 &&
this._K !== 40) this._K = 40;
this._games = this._K === 40 ? 0 : 30;
}

private expected(opponent: number): number {
let p = opponent - this._rating;
if (Math.abs(p) > 400) p = p < 0 ? - 400 : 400;
return 1 / (1 + Math.pow(10, p / 400));
}

/// Updates the current Elo according to
/// the opponent's Elo and the game score.
public update(opponent: number, score: Score) {
const E = this.expected(opponent);
this._rating += this._K * (score - E);
if (this._rating < 100) this._rating = 100;
if (this._rating > 3000) this._rating = 3000;
this._games++;
if (this._K === 10) return;
if (this._rating < 2400) {
this._K = this._games < 30 ? 40 : 20;
} else this._K = 10;
}

get rating(): number {
return Math.round(this._rating);
}

get games(): number {
return this._games;
}

private _rating: number;
private _K: number;
private _games: number;

constructor(rating?: number, k?: number) {
this._rating = rating ?? 1500;
this._K = k ?? 40;
if (this._K !== 10 && this._K !== 20 && this._K !== 40) {
this._K = 40;
}
this._games = this._K === 40 ? 0 : 30;
}

private expected(opponent: number): number {
let p = opponent - this._rating;
if (Math.abs(p) > 400) {
p = p < 0 ? -400 : 400;
}
return 1 / (1 + Math.pow(10, p / 400));
}

/// Updates the current Elo according to
/// the opponent's Elo and the game score.
public update(opponent: number, score: Score) {
const E = this.expected(opponent);
this._rating += this._K * (score - E);
if (this._rating < 100) {
this._rating = 100;
}
if (this._rating > 3000) {
this._rating = 3000;
}
this._games++;
if (this._K === 10) {
return;
}
if (this._rating < 2400) {
this._K = this._games < 30 ? 40 : 20;
} else {
this._K = 10;
}
}

get rating(): number {
return Math.round(this._rating);
}

get games(): number {
return this._games;
}
}
2 changes: 1 addition & 1 deletion src/app/lib/util.library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const enum RANK {
export const enum Score {
loss = 0,
draw = 0.5,
win = 1,
win = 1
}

export function fileToString(file: FILE): string {
Expand Down

0 comments on commit d515a97

Please sign in to comment.