diff --git a/src/server/index.js b/src/server/index.js index 5c3c8a8..cb2b020 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -51,6 +51,7 @@ api.get("/tournament/:tournamentId", (req, res) => { // .then(console.log("lol")) }); + api.get("/tournament/:tournamentId/getMatches", (req, res) => { let tournamentId = req.params.tournamentId; if (isNaN(tournamentId)) { @@ -63,6 +64,18 @@ api.get("/tournament/:tournamentId/getMatches", (req, res) => { .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) => { let matchId = req.params.matchId; if (isNaN(matchId)) { diff --git a/src/server/management/initDB.sql b/src/server/management/initDB.sql index 392dc4d..73ae385 100644 --- a/src/server/management/initDB.sql +++ b/src/server/management/initDB.sql @@ -30,6 +30,7 @@ CREATE TABLE matches ( team1Id INTEGER, team2Id INTEGER, winnerId INTEGER, + tier INTEGER, FOREIGN KEY (tournamentId) REFERENCES tournaments (id), FOREIGN KEY (team1Id) REFERENCES teams (id), @@ -46,7 +47,7 @@ CREATE TABLE players ( -- 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 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, '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, 'Team Vitality'); -- 7 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 -- -- 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 -INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id) VALUES (1, 1, 1, 2); -- 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, 1, 2, 1); -- 2 +INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (1, 1, 3, 4, 1); -- 3 -- tournament 2 -- -- 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 -INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id) VALUES (2, 4, 5, 7); -- 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); -- 5 +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 INSERT INTO players (name, teamId) VALUES ('Player 1', 1); @@ -88,4 +98,12 @@ INSERT INTO players (name, teamId) VALUES ('Player 12', 6); 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 15', 8); -INSERT INTO players (name, teamId) VALUES ('Player 16', 8); \ No newline at end of file +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); diff --git a/src/server/tmdb.js b/src/server/tmdb.js index ad42b56..8aed35b 100644 --- a/src/server/tmdb.js +++ b/src/server/tmdb.js @@ -8,6 +8,7 @@ module.exports = { getMatch: getMatch, setMatchWinner: setMatchWinner, createTournament: createTournament, + getTeamsByTournamentId: getTeamsByTournamentId, } 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. // Used to initialize and manage the database by management tools, not by the main application.