@andreabiagini5/applicazioni-e-servizi-web-project
Version:
Project for Applicazioni e Servizi Web.
91 lines • 3.79 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.notifyNewMatch = exports.requestMatchWithBot = exports.requestMatch = void 0;
const matchmaking_1 = require("../services/matchmaking/matchmaking");
const match_1 = require("../services/match");
const ioHandler = __importStar(require("../sockets/socket"));
/**
* Requests a match and notifies the player if a match has been found, adds them to the queue otherwise.
* @param playerSocket the socket of the player requesting the match
* @param username the username of the player requesting the match
*/
const requestMatch = async (playerSocket, username) => {
ioHandler.registerPlayerSocket(username, playerSocket);
const result = await (0, matchmaking_1.findMatch)(username);
if (!result)
return;
const [usernameA, usernameB, matchId] = result;
(0, exports.notifyNewMatch)(usernameA, usernameB, matchId);
};
exports.requestMatch = requestMatch;
/**
* Requests a match with a bot and notifies the player that a match has been found.
* @param playerSocket the socket of the player requesting the match
* @param username the username of the player requesting the match
*/
const requestMatchWithBot = async (playerSocket, username) => {
ioHandler.registerPlayerSocket(username, playerSocket);
const matchId = await (0, match_1.newMatch)(username, 'bot', new Date());
notifyPlayer(username, matchId);
return matchId;
};
exports.requestMatchWithBot = requestMatchWithBot;
// TODO: MOVE THIS TO MATCH CONTROLLER
/**
* Notifies the players that a match has been found and adds them to the match room.
* @param usernameA the username of the first player
* @param usernameB the username of the second player
* @param matchId the id of the match
*/
const notifyNewMatch = async (usernameA, usernameB, matchId) => {
[usernameA, usernameB].forEach(username => {
notifyPlayer(username, matchId);
});
};
exports.notifyNewMatch = notifyNewMatch;
/**
* Notifies the player that a match has been found and adds them to the match room.
* @param username the username of the player to notify
* @param matchId the id of the match
*/
const notifyPlayer = async (username, matchId) => {
const socket = ioHandler.getPlayerSocket(username);
if (socket) {
await socket.join(matchId);
socket.emit('matchFound', matchId);
}
};
//# sourceMappingURL=matchmaking.js.map