UNPKG

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

Version:
69 lines 2.95 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DBMatchmakingCandidate = void 0; exports.getQueue = getQueue; exports.addCandidate = addCandidate; exports.removeCandidate = removeCandidate; exports.clearQueue = clearQueue; exports.getCandidate = getCandidate; const mongoose_1 = __importDefault(require("mongoose")); const MatchmakingCandidate_1 = require("../../src/models/MatchmakingCandidate"); const MatchmakingQueue_1 = require("../../src/models/MatchmakingQueue"); const Rating_1 = require("../../src/models/Rating"); // Schema for individual matchmaking candidate documents const candidateSchema = new mongoose_1.default.Schema({ username: { type: String, required: true, unique: true }, rating: { type: Number, required: true }, requestTime: { type: Date, required: true }, }); // Model for candidate documents exports.DBMatchmakingCandidate = mongoose_1.default.model('MatchmakingCandidate', candidateSchema); /** * Fetches all candidates from the database and constructs a queue */ async function getQueue() { const candidates = await exports.DBMatchmakingCandidate.find(); const queue = MatchmakingQueue_1.MatchmakingQueueFactory.create(); if (candidates.length === 0) { return queue; } candidates.forEach(candidate => { queue.add(MatchmakingCandidate_1.MatchmakingCandidateFactory.create(candidate.username, Rating_1.RatingFactory.create(candidate.rating.value), candidate.requestTime)); }); return queue; } /** * Adds a new candidate to the matchmaking system */ async function addCandidate(candidate) { // Use findOneAndUpdate with upsert to handle the case where the user is already in the queue await exports.DBMatchmakingCandidate.findOneAndUpdate({ username: candidate.username }, { $set: { rating: candidate.rating, requestTime: candidate.requestTime } }, { upsert: true }); } /** * Removes a candidate from the matchmaking system */ async function removeCandidate(candidate) { await exports.DBMatchmakingCandidate.deleteOne({ username: candidate.username }); } /** * Clears all candidates from the matchmaking system */ async function clearQueue() { await exports.DBMatchmakingCandidate.deleteMany({}); } /** * Retrieves a candidate by username * @param username the username of the candidate to retrieve * @returns the MatchmakingCandidate if found, otherwise null */ async function getCandidate(username) { const candidate = await exports.DBMatchmakingCandidate.find({ username }); if (candidate.length === 0) { return null; } return MatchmakingCandidate_1.MatchmakingCandidateFactory.create(candidate[0].username, Rating_1.RatingFactory.create(candidate[0].rating.value), candidate[0].requestTime); } //# sourceMappingURL=matchmakingQueue.js.map