UNPKG

@andreabiagini5/applicazioni-e-servizi-web-project

Version:
72 lines 3.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findSuitableOpponent = exports.findMatchAndCreate = exports.findMatchOrQueue = exports.findMatch = void 0; const matchmakingQueue_1 = require("../../repositories/matchmakingQueue"); const logic_1 = require("./logic"); const MatchmakingCandidate_1 = require("../../models/MatchmakingCandidate"); require("../../utils/array.extensions"); const match_1 = require("../match"); const account_1 = require("../../repositories/account"); /** * Creates a new match if possible for the given player. * If no match is found, the player will be added to the matchmaking queue. * If no player is given, it will try to find a match for any player in the queue. * @param requestingUsername The username of the player. * @returns An object containing the usernames of the matched players and the match ID, or undefined if no match was found. */ const findMatch = async (requestingUsername) => { if (!requestingUsername) return await (0, exports.findMatchAndCreate)(); else return await (0, exports.findMatchOrQueue)(requestingUsername); }; exports.findMatch = findMatch; /** * Finds a suitable opponent for the given player. If no opponent is found, adds the player to the matchmaking queue. * @param requestingUsername The username of the player. * @returns An object containing the usernames of the matched players and the match ID, or undefined if no match was found. */ const findMatchOrQueue = async (requestingUsername) => { const requestingPlayer = await (0, account_1.readAccountByUsername)(requestingUsername); const requestingCandidate = MatchmakingCandidate_1.MatchmakingCandidateFactory.create(requestingUsername, requestingPlayer.rating); const suitableOpponent = await (0, exports.findSuitableOpponent)(requestingCandidate); if (suitableOpponent) { await (0, matchmakingQueue_1.removeCandidate)(suitableOpponent); const matchId = await (0, match_1.newMatch)(requestingUsername, suitableOpponent.username, new Date()); return [requestingUsername, suitableOpponent.username, matchId]; } await (0, matchmakingQueue_1.addCandidate)(requestingCandidate); return undefined; }; exports.findMatchOrQueue = findMatchOrQueue; /** * Finds a suitable opponent for the given player. If no opponent is found, adds the player to the matchmaking queue. * @param requestingUsername The username of the player. * @returns An object containing the usernames of the matched players and the match ID, or undefined if no match was found. */ const findMatchAndCreate = async () => { const queue = await (0, matchmakingQueue_1.getQueue)(); for (const candidate of queue) { const suitableOpponent = await (0, exports.findSuitableOpponent)(candidate); if (suitableOpponent) { await (0, matchmakingQueue_1.removeCandidate)(candidate); await (0, matchmakingQueue_1.removeCandidate)(suitableOpponent); const matchId = await (0, match_1.newMatch)(candidate.username, suitableOpponent.username, new Date()); return [candidate.username, suitableOpponent.username, matchId]; } } return undefined; }; exports.findMatchAndCreate = findMatchAndCreate; /** * Finds a suitable opponent for the requesting player and removes them from the queue. * @param requestingPlayerUsername the id of the requesting player * @returns the suitable opponent, or undefined if no suitable opponent is found */ const findSuitableOpponent = async (requestingPlayerUsername) => { const queue = await (0, matchmakingQueue_1.getQueue)(); const suitableOpponent = await Array.from(queue).findAsync(async (candidateOpponent) => await (0, logic_1.isValidMatch)(requestingPlayerUsername, candidateOpponent)); return suitableOpponent ? suitableOpponent : undefined; }; exports.findSuitableOpponent = findSuitableOpponent; //# sourceMappingURL=matchmaking.js.map