@andreabiagini5/applicazioni-e-servizi-web-project
Version:
Project for Applicazioni e Servizi Web.
147 lines • 5.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.deleteMatch = exports.saveMatch = exports.addMove = exports.getEndedMatchesByPlayer = exports.getMatch = exports.newMatch = void 0;
const Match_1 = require("../models/Match");
const endedMatchRepo = __importStar(require("../repositories/endedMatch"));
const inProgressMatchRepo = __importStar(require("../repositories/inProgressMatch"));
/**
* Creates a new match and returns its ID.
*
* @param player1 the first player.
* @param player2 the second player.
* @param creationDate the match creation date.
* @returns the ID of the just created match.
*/
const newMatch = async (player1, player2, creationDate) => {
const match = Match_1.MatchFactory.createWithDefaultInitialState(player1, player2, creationDate);
// These lines are For debug purposes only. They create a match ready to be ended.
// const match = MatchFactory.createWithCustomInitialState(
// player1,
// player2,
// creationDate,
// BoardFactory.createCustom(9, 9, [
// { x: 1, y: 1, pile: PileFactory.create(player1, 1) },
// { x: 1, y: 2, pile: PileFactory.create(player2, 1) },
// { x: 2, y: 1, pile: PileFactory.create(player1, 1) },
// { x: 2, y: 2, pile: PileFactory.create(player2, 1) },
// ]),
// );
return await inProgressMatchRepo.createMatch(match);
};
exports.newMatch = newMatch;
/**
* Returns the match corresponding to the provided ID, searching in both
* "in progress" and "ended" matches repositories.
*
* @param matchId the desired match ID
* @returns the match corresponding to the provided ID
*/
const getMatch = async (matchId) => {
var _a;
let match = (_a = (await inProgressMatchRepo.findMatch(matchId))) !== null && _a !== void 0 ? _a : (await endedMatchRepo.findMatch(matchId));
return match;
};
exports.getMatch = getMatch;
/**
* Returns a list of matches played by the provided player, searching in both
* "in progress" and "ended" matches repositories.
*
* @param player the player by which the matches are played.
* @returns a list of matches played by the provided player.
*/
const getEndedMatchesByPlayer = async (player) => {
return await endedMatchRepo.findMatchesByPlayer(player);
};
exports.getEndedMatchesByPlayer = getEndedMatchesByPlayer;
/**
* Adds a move to the match corresponding to the provided ID, if it is valid.
*
* @param matchId the ID of the match in which the move has to be added.
* @param movingPlayer the player who is making the move.
* @param newMove the move to be added.
* @returns true if the move has been successfully added, false otherwise.
*/
const addMove = async (matchId, movingPlayer, newMove) => {
let match = await inProgressMatchRepo.findMatch(matchId);
if (match == null) {
return false;
}
let ret = false;
if ((movingPlayer == match.player1 && match.moves.length % 2 == 0) ||
(movingPlayer == match.player2 && match.moves.length % 2 == 1)) {
ret = match.addMove(newMove);
await inProgressMatchRepo.updateMatch(matchId, match);
}
return ret;
};
exports.addMove = addMove;
/**
* Permanently saves a match in the ended match repository.
*
* Note: considering the current implementation of the two repositories,
* the ID does not change.
*
* @param matchId the ID of the match to be saved permanently.
* @param ratingDelta the absolute value of the rating score exchanged between players.
* @returns the new ID of the permanently saved match.
*/
const saveMatch = async (matchId, ratingDelta) => {
const match = await inProgressMatchRepo.findMatch(matchId);
if (match == null) {
return null;
}
match.ratingDelta = ratingDelta;
await inProgressMatchRepo.deleteMatch(matchId);
return await endedMatchRepo.createMatch(match, matchId);
};
exports.saveMatch = saveMatch;
/**
* Deletes the match corresponding to the provided ID, searching in both
* "in progress" and "ended" matches repositories.
*
* @param matchId the ID of the match to be deleted.
* @returns true if the match has been deleted, false otherwise.
*/
const deleteMatch = async (matchId) => {
if (await inProgressMatchRepo.deleteMatch(matchId)) {
return true;
}
else {
return await endedMatchRepo.deleteMatch(matchId);
}
};
exports.deleteMatch = deleteMatch;
//# sourceMappingURL=match.js.map