-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #332 from govariantsteam/rating-system
Rating system
- Loading branch information
Showing
7 changed files
with
284 additions
and
9 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
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { | ||
getGlickoPlayer, | ||
applyPlayerRankingToUserResponse, | ||
supportsRatings, | ||
getGlickoResult, | ||
} from "../rating"; | ||
import { Glicko2 } from "glicko2"; | ||
import { | ||
UserRanking, | ||
UserRankings, | ||
UserResponse, | ||
} from "@ogfcommunity/variants-shared"; | ||
|
||
test("supportsRatings", () => { | ||
const supportsRating = supportsRatings("chess"); | ||
expect(supportsRating).toEqual(false); | ||
|
||
const new_supportsRating = supportsRatings("quantum"); | ||
expect(new_supportsRating).toEqual(true); | ||
}); | ||
|
||
test("getGlickoPlayer - with typical player value", () => { | ||
const db_player_ranking_quantum = { | ||
rating: 1337, | ||
rd: 290, | ||
vol: 0.0599, | ||
}; | ||
|
||
const ranking = new Glicko2({ | ||
tau: 0.5, | ||
rating: 1500, | ||
rd: 350, | ||
vol: 0.06, | ||
}); | ||
|
||
const player = getGlickoPlayer(db_player_ranking_quantum, ranking); | ||
|
||
expect(player.getRating()).toBe(1337); | ||
expect(player.getRd()).toBe(290); | ||
expect(player.getVol()).toBe(0.0599); | ||
}); | ||
|
||
test("getGlickoPlayer - when player does not have any rating", () => { | ||
const db_player_ranking_quantum: undefined = undefined; | ||
|
||
const ranking = new Glicko2({ | ||
tau: 0.5, | ||
rating: 1500, | ||
rd: 350, | ||
vol: 0.06, | ||
}); | ||
|
||
const player = getGlickoPlayer(db_player_ranking_quantum, ranking); | ||
|
||
expect(player.getRating()).toBe(1500); | ||
expect(player.getRd()).toBe(350); | ||
expect(player.getVol()).toBe(0.06); | ||
}); | ||
|
||
test("getGlickoResult", () => { | ||
expect(getGlickoResult("B")).toBe(1); | ||
expect(getGlickoResult("W")).toBe(0); | ||
expect(getGlickoResult("T")).toBe(0.5); | ||
}); | ||
|
||
test("applyPlayerRankingToUserResponse", () => { | ||
const variant = "quantum"; | ||
const glicko_ranking = new Glicko2({ | ||
tau: 0.5, | ||
rating: 1500, | ||
rd: 350, | ||
vol: 0.06, | ||
}); | ||
const db_player_initial: UserResponse = { | ||
id: "672844d2254d76r4387b8488", | ||
login_type: "persistent", | ||
username: "testUser", | ||
ranking: { | ||
quantum: { | ||
rating: 1337, | ||
rd: 290, | ||
vol: 0.0599, | ||
}, | ||
baduk: { | ||
rating: 1623, | ||
rd: 290, | ||
vol: 0.0599, | ||
}, | ||
phantom: { | ||
rating: 1762, | ||
rd: 290, | ||
vol: 0.0599, | ||
}, | ||
}, | ||
}; | ||
|
||
const new_quantum_ranking: UserRanking = { | ||
rating: 1667, | ||
rd: 295, | ||
vol: 0.0699, | ||
}; | ||
|
||
const glicko_player = getGlickoPlayer(new_quantum_ranking, glicko_ranking); | ||
|
||
const expected_player_rankings: UserRankings = { | ||
quantum: { | ||
rating: 1667, | ||
rd: 295, | ||
vol: 0.0699, | ||
}, | ||
baduk: { | ||
rating: 1623, | ||
rd: 290, | ||
vol: 0.0599, | ||
}, | ||
phantom: { | ||
rating: 1762, | ||
rd: 290, | ||
vol: 0.0599, | ||
}, | ||
}; | ||
|
||
expect( | ||
applyPlayerRankingToUserResponse(db_player_initial, glicko_player, variant), | ||
).toEqual(expected_player_rankings); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { | ||
GameResponse, | ||
AbstractGame, | ||
UserRanking, | ||
UserRankings, | ||
UserResponse, | ||
} from "@ogfcommunity/variants-shared"; | ||
import { Glicko2, Player } from "glicko2"; | ||
import { updateUserRanking, getUser } from "../users"; | ||
|
||
export async function updateRatings( | ||
game: GameResponse, | ||
game_obj: AbstractGame<object, object>, | ||
) { | ||
const ranking = new Glicko2({ | ||
tau: 0.5, | ||
rating: 1500, | ||
rd: 350, | ||
vol: 0.06, | ||
}); | ||
|
||
const variant = game.variant; | ||
|
||
const glicko_outcome = getGlickoResult(game_obj.result[0]); | ||
|
||
const player_black_id = game.players[0].id; | ||
const player_white_id = game.players[1].id; | ||
|
||
const db_player_black = await getUser(player_black_id); | ||
const db_player_white = await getUser(player_white_id); | ||
|
||
const glicko_player_black = getGlickoPlayer( | ||
db_player_black.ranking[variant], | ||
ranking, | ||
); | ||
const glicko_player_white = getGlickoPlayer( | ||
db_player_white.ranking[variant], | ||
ranking, | ||
); | ||
|
||
ranking.addResult(glicko_player_black, glicko_player_white, glicko_outcome); | ||
|
||
ranking.calculatePlayersRatings(); | ||
|
||
const player_black_new_ranking = applyPlayerRankingToUserResponse( | ||
db_player_black, | ||
glicko_player_black, | ||
variant, | ||
); | ||
const player_white_new_ranking = applyPlayerRankingToUserResponse( | ||
db_player_white, | ||
glicko_player_white, | ||
variant, | ||
); | ||
|
||
await Promise.all([ | ||
updateUserRanking(player_black_id, player_black_new_ranking), | ||
updateUserRanking(player_white_id, player_white_new_ranking), | ||
]); | ||
} | ||
|
||
export function getGlickoResult(game_result: string): number { | ||
if (game_result === "B") { | ||
return 1; | ||
} | ||
if (game_result === "W") { | ||
return 0; | ||
} | ||
return 0.5; | ||
} | ||
|
||
export function getGlickoPlayer( | ||
db_player_ranking: UserRanking, | ||
ranking: Glicko2, | ||
): Player { | ||
if (db_player_ranking == undefined) { | ||
return ranking.makePlayer(1500, 350, 0.06); | ||
} | ||
return ranking.makePlayer( | ||
db_player_ranking.rating, | ||
db_player_ranking.rd, | ||
db_player_ranking.vol, | ||
); | ||
} | ||
|
||
// this function updates player's UserRanking with new ratings | ||
export function applyPlayerRankingToUserResponse( | ||
player: UserResponse, | ||
glicko_player: Player, | ||
variant: string, | ||
): UserRankings { | ||
const ranking: UserRanking = { | ||
rating: glicko_player.getRating(), | ||
rd: glicko_player.getRd(), | ||
vol: glicko_player.getVol(), | ||
}; | ||
|
||
const player_ranking = player.ranking; | ||
player_ranking[variant] = ranking; | ||
return player_ranking; | ||
} | ||
|
||
export function supportsRatings(variant: string) { | ||
return [ | ||
"baduk", | ||
"phantom", | ||
"capture", | ||
"tetris", | ||
"pyramid", | ||
"thue-morse", | ||
"freeze", | ||
"fractional", | ||
"keima", | ||
"one color", | ||
"drift", | ||
"quantum", | ||
"sfractional", | ||
].includes(variant); | ||
} |
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
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