@andreabiagini5/applicazioni-e-servizi-web-project
Version:
Project for Applicazioni e Servizi Web.
93 lines • 3.91 kB
JavaScript
;
/**
* Rating service
* This service is responsible for calculating the rating of players based on their game results.
* It uses the Elo rating system to calculate the new ratings.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNewRating = exports.GameResult = exports.getPlayerRating = void 0;
const account_1 = require("../repositories/account");
const round_1 = require("../utils/round");
/**
* getPlayerRating
* This function retrieves the rating of a player by their username.
* @param {string} username - The username of the player.
* @returns {Promise<number | undefined>} - The rating of the player, or undefined if the player does not exist.
*/
const getPlayerRating = async (username) => {
const account = await (0, account_1.readAccountByUsername)(username);
return account === null || account === void 0 ? void 0 : account.rating.value;
};
exports.getPlayerRating = getPlayerRating;
/**
* enum GameResult
* This enum represents the possible results of a game.
* - WinA: Player A wins
* - WinB: Player B wins
* @enum {string}
* @property {string} WinA - Player A wins
* @property {string} WinB - Player B wins
*/
var GameResult;
(function (GameResult) {
GameResult["WinA"] = "WinA";
GameResult["WinB"] = "WinB";
})(GameResult || (exports.GameResult = GameResult = {}));
const DEFAULT_K_FACTOR = 32;
/**
* updateRating
* This function calculates the new ratings for two players based on their current ratings and the result of the game.
* It uses the Elo rating system to calculate the new ratings.
* @param {number} ratingA - The current rating of Player A.
* @param {number} ratingB - The current rating of Player B.
* @param {GameResult} result - The result of the game.
* @param {number} kFactor - The K-factor used in the Elo rating system. Default is 32.
* @returns {[number, number]} - The new ratings for Player A and Player B, rounded to 2 decimal places.
*/
const getNewRating = (ratingA, ratingB, result, kFactor = DEFAULT_K_FACTOR) => {
const ratingChange = getRatingChange(ratingA, ratingB, result, kFactor);
const newRatingA = ratingA + ratingChange;
const newRatingB = ratingB - ratingChange;
const roundedNewRatingA = (0, round_1.roundToDecimal)(newRatingA, 1);
const roundedNewRatingB = (0, round_1.roundToDecimal)(newRatingB, 1);
return [roundedNewRatingA, roundedNewRatingB];
};
exports.getNewRating = getNewRating;
/**
* Calculate the rating change for a player based on the game result.
* @param ratingA - The current rating of Player A.
* @param ratingB - The current rating of Player B.
* @param result - The result of the game.
* @param kFactor - The K-factor used in the Elo rating system. Default is 32.
* @returns The rating change for Player A, rounded to 1 decimal place.
*/
const getRatingChange = (ratingA, ratingB, result, kFactor) => {
const expectedScoreA = getExpectedScore(ratingA, ratingB);
let scoreA;
let scoreB;
switch (result) {
case GameResult.WinA:
scoreA = 1;
scoreB = 0;
break;
case GameResult.WinB:
scoreA = 0;
scoreB = 1;
break;
}
const ratingUpdateA = kFactor * (scoreA - expectedScoreA);
const roundedRatingUpdate = (0, round_1.roundToDecimal)(ratingUpdateA, 1);
return roundedRatingUpdate;
};
/**
* Calculate the expected score for Player A based on two player ratings.
* The expected score is a value between 0 and 1, representing the probability of Player A winning.
* @param ratingA - The current rating of Player A.
* @param ratingB - The current rating of Player B.
* @returns The expected score for Player A.
*/
const getExpectedScore = (ratingA, ratingB) => {
const expectedScoreA = 1 / (1 + Math.pow(10, (ratingB - ratingA) / 400));
return expectedScoreA;
};
//# sourceMappingURL=rating.js.map