-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
linting changes, as well as removed an import that wasn't being used …
…in the spec file, no functional changes. looks good!
- Loading branch information
Showing
3 changed files
with
72 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters