Long development session

This commit is contained in:
Felix Albrigtsen 2022-03-24 21:45:47 +01:00
parent 665ad0dd38
commit db3e7ed932
3 changed files with 53 additions and 8 deletions

View File

@ -51,6 +51,7 @@ api.get("/tournament/:tournamentId", (req, res) => {
// .then(console.log("lol")) // .then(console.log("lol"))
}); });
api.get("/tournament/:tournamentId/getMatches", (req, res) => { api.get("/tournament/:tournamentId/getMatches", (req, res) => {
let tournamentId = req.params.tournamentId; let tournamentId = req.params.tournamentId;
if (isNaN(tournamentId)) { if (isNaN(tournamentId)) {
@ -63,6 +64,18 @@ api.get("/tournament/:tournamentId/getMatches", (req, res) => {
.catch(err => res.send({"status": "error", "data": err})); .catch(err => res.send({"status": "error", "data": err}));
}); });
api.get("/tournament/:tournamentId/getTeams", (req, res) => {
let tournamentId = req.params.tournamentId;
if (!tournamentId || isNaN(tournamentId)) {
res.json({"status": "error", "data": "tournamentId must be a number"});
return
}
tournamentId = parseInt(tournamentId);
tmdb.getTeamsByTournamentId(tournamentId)
.then(teams => res.send({"status": "OK", "data": teams}))
.catch(err => res.send({"status": "error", "data": err}));
});
api.get("/match/:matchId/getMatch", (req, res) => { api.get("/match/:matchId/getMatch", (req, res) => {
let matchId = req.params.matchId; let matchId = req.params.matchId;
if (isNaN(matchId)) { if (isNaN(matchId)) {

View File

@ -30,6 +30,7 @@ CREATE TABLE matches (
team1Id INTEGER, team1Id INTEGER,
team2Id INTEGER, team2Id INTEGER,
winnerId INTEGER, winnerId INTEGER,
tier INTEGER,
FOREIGN KEY (tournamentId) REFERENCES tournaments (id), FOREIGN KEY (tournamentId) REFERENCES tournaments (id),
FOREIGN KEY (team1Id) REFERENCES teams (id), FOREIGN KEY (team1Id) REFERENCES teams (id),
@ -46,7 +47,7 @@ CREATE TABLE players (
-- Example data (Two tournaments, 4 teams, single elimination) -- Example data (Two tournaments, 4 teams, single elimination)
INSERT INTO tournaments (name, description, startTime, endTime, teamLimit) VALUES ('Tournament 1', 'First tournament, single elimination', '2022-04-01 16:00:00', '2022-04-01 20:00:00', 4); INSERT INTO tournaments (name, description, startTime, endTime, teamLimit) VALUES ('Tournament 1', 'First tournament, single elimination', '2022-04-01 16:00:00', '2022-04-01 20:00:00', 4);
INSERT INTO tournaments (name, description, startTime, endTime, teamLimit) VALUES ('Tournament 2', 'Second tournament, four teams', '2022-04-03 17:30:00', '2022-04-04 21:30:00', 4); INSERT INTO tournaments (name, description, startTime, endTime, teamLimit) VALUES ('Tournament 2', 'Second tournament, four teams', '2022-04-03 17:30:00', '2022-04-04 21:30:00', 8);
INSERT INTO teams (tournamentId, name) VALUES (1, 'Fnatic'); -- 1 INSERT INTO teams (tournamentId, name) VALUES (1, 'Fnatic'); -- 1
INSERT INTO teams (tournamentId, name) VALUES (1, 'Cloud 9'); -- 2 INSERT INTO teams (tournamentId, name) VALUES (1, 'Cloud 9'); -- 2
@ -57,20 +58,29 @@ INSERT INTO teams (tournamentId, name) VALUES (2, 'Astralis'); -- 5
INSERT INTO teams (tournamentId, name) VALUES (2, 'Entropiq'); -- 6 INSERT INTO teams (tournamentId, name) VALUES (2, 'Entropiq'); -- 6
INSERT INTO teams (tournamentId, name) VALUES (2, 'Team Vitality'); -- 7 INSERT INTO teams (tournamentId, name) VALUES (2, 'Team Vitality'); -- 7
INSERT INTO teams (tournamentId, name) VALUES (2, 'Godsent'); -- 8 INSERT INTO teams (tournamentId, name) VALUES (2, 'Godsent'); -- 8
INSERT INTO teams (tournamentId, name) VALUES (2, 'Team Secret'); -- 9
INSERT INTO teams (tournamentId, name) VALUES (2, 'Virtus.pro'); -- 10
INSERT INTO teams (tournamentId, name) VALUES (2, 'Natus Vincere'); -- 11
INSERT INTO teams (tournamentId, name) VALUES (2, 'FaZe'); -- 12
-- tournament 1 -- -- tournament 1 --
-- Final match -- Final match
INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id) VALUES (1, NULL, NULL, NULL); -- 1 INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (1, NULL, NULL, NULL, 0); -- 1
-- Semi-finals -- Semi-finals
INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id) VALUES (1, 1, 1, 2); -- 2 INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (1, 1, 1, 2, 1); -- 2
INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id) VALUES (1, 1, 3, 4); -- 3 INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (1, 1, 3, 4, 1); -- 3
-- tournament 2 -- -- tournament 2 --
-- Final match -- Final match
INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id) VALUES (2, NULL, NULL, NULL); -- 4 INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (2, NULL, NULL, NULL, 0); -- 4
-- Semi-finals -- Semi-finals
INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id) VALUES (2, 4, 5, 7); -- 5 INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (2, 4, NULL, NULL, 1); -- 5
INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id) VALUES (2, 4, 6, 8); -- 6 INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (2, 4, NULL, NULL, 1); -- 6
-- Quarter-finals
INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (2, 5, 5, 6, 2); -- 7
INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (2, 5, 7, 8, 2); -- 8
INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (2, 6, 9, 10, 2); -- 9
INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (2, 6, 11, 12, 2); -- 10
-- Players -- Players
INSERT INTO players (name, teamId) VALUES ('Player 1', 1); INSERT INTO players (name, teamId) VALUES ('Player 1', 1);
@ -89,3 +99,11 @@ INSERT INTO players (name, teamId) VALUES ('Player 13', 7);
INSERT INTO players (name, teamId) VALUES ('Player 14', 7); INSERT INTO players (name, teamId) VALUES ('Player 14', 7);
INSERT INTO players (name, teamId) VALUES ('Player 15', 8); INSERT INTO players (name, teamId) VALUES ('Player 15', 8);
INSERT INTO players (name, teamId) VALUES ('Player 16', 8); INSERT INTO players (name, teamId) VALUES ('Player 16', 8);
INSERT INTO players (name, teamId) VALUES ('Player 17', 9);
INSERT INTO players (name, teamId) VALUES ('Player 18', 9);
INSERT INTO players (name, teamId) VALUES ('Player 19', 10);
INSERT INTO players (name, teamId) VALUES ('Player 20', 10);
INSERT INTO players (name, teamId) VALUES ('Player 21', 11);
INSERT INTO players (name, teamId) VALUES ('Player 22', 11);
INSERT INTO players (name, teamId) VALUES ('Player 23', 12);
INSERT INTO players (name, teamId) VALUES ('Player 24', 12);

View File

@ -8,6 +8,7 @@ module.exports = {
getMatch: getMatch, getMatch: getMatch,
setMatchWinner: setMatchWinner, setMatchWinner: setMatchWinner,
createTournament: createTournament, createTournament: createTournament,
getTeamsByTournamentId: getTeamsByTournamentId,
} }
const mysql = require("mysql"); const mysql = require("mysql");
@ -164,6 +165,19 @@ function createTournament(name, description, startDate, endDate, teamLimit) {
}); });
} }
function getTeamsByTournamentId(tournamentId) {
return new Promise(function(resolve, reject) {
connection.query("SELECT * FROM teams WHERE tournamentId = ?", [mysql.escape(tournamentId)], (err, teams) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(teams);
}
});
});
}
// Dangerous function, use with caution. // Dangerous function, use with caution.
// Used to initialize and manage the database by management tools, not by the main application. // Used to initialize and manage the database by management tools, not by the main application.