@andreabiagini5/applicazioni-e-servizi-web-project
Version:
Project for Applicazioni e Servizi Web.
38 lines • 1.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMaxDiff = exports.isValidMatch = void 0;
/**
* Checks if two candidates are a valid match based on their ratings and the time since their requests.
* The longer the time since the request, the larger the allowed difference.
* @param candidate1 The first candidate.
* @param candidate2 The second candidate.
* @returns True if the candidates are a valid match, false otherwise.
*/
const isValidMatch = async (candidate1, candidate2) => {
if (candidate1.equals(candidate2)) {
return false; // Players cannot match with themselves
}
// setting max difference to the one of the player with the lowest one
const maxDiff1 = (0, exports.getMaxDiff)(candidate1.requestTime);
const maxDiff2 = (0, exports.getMaxDiff)(candidate2.requestTime);
const maxDiff = Math.min(maxDiff1, maxDiff2);
return Math.abs(candidate1.rating.value - candidate2.rating.value) <= maxDiff;
};
exports.isValidMatch = isValidMatch;
/**
* Calculates the maximum allowed rating difference based on the time since the request was made.
* The longer the time since the request, the larger the allowed difference.
* @param requestTime The time when the matchmaking request was made.
* @returns The maximum allowed rating difference.
*/
const getMaxDiff = (requestTime) => {
const baseDiff = 100;
const currentTime = new Date();
const timeDiff = Math.abs(currentTime.getTime() - requestTime.getTime());
// add 100 to baseDiff for every 10 seconds
const diffInSeconds = Math.floor(timeDiff / 1000);
const maxDiff = Math.floor(diffInSeconds / 10) * 100 + baseDiff;
return maxDiff;
};
exports.getMaxDiff = getMaxDiff;
//# sourceMappingURL=logic.js.map