diff --git a/API/db/app.db b/API/db/app.db index 5941a0ad986c88c613f7c0a0b6db12a51cf60954..7680fd1877fc4b7fcf94bea21b4a1d6b2bfed5da 100644 Binary files a/API/db/app.db and b/API/db/app.db differ diff --git a/API/db/create_db.sql b/API/db/create_db.sql index 8b5fc2f81d83856838dbd019cb18396bd5e893f8..e75fd17ae727d731ae928e261b07c594c1b394c2 100644 --- a/API/db/create_db.sql +++ b/API/db/create_db.sql @@ -1,42 +1,36 @@ PRAGMA foreign_keys = on; .mode json -CREATE TABLE users ( +CREATE TABLE Users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE, password TEXT, admin BOOLEAN ); -CREATE TABLE questions ( +CREATE TABLE Questions ( id INTEGER PRIMARY KEY AUTOINCREMENT, question TEXT UNIQUE, category TEXT, - CONSTRAINT FK_category FOREIGN KEY (category) REFERENCES category(title) ON DELETE CASCADE + CONSTRAINT FK_category FOREIGN KEY (category) REFERENCES Categories(title) ON DELETE CASCADE ); -CREATE TABLE category ( +CREATE TABLE Categories ( title TEXT PRIMARY KEY UNIQUE ); -CREATE TABLE answer ( +CREATE TABLE Answers ( id INTEGER PRIMARY KEY AUTOINCREMENT, text_answer TEXT, id_question INTEGER, correct BOOLEAN, - CONSTRAINT FK_question FOREIGN KEY (id_question) REFERENCES questions(id) ON DELETE CASCADE + CONSTRAINT FK_question FOREIGN KEY (id_question) REFERENCES Questions(id) ON DELETE CASCADE ); -CREATE TABLE room ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT UNIQUE, - -); - -INSERT INTO users (username, password, admin) +INSERT INTO Users (username, password, admin) VALUES ('alec', 'alec', true); -INSERT INTO category (title) +INSERT INTO Categories (title) VALUES ('Histoire'), ('Géographie'); diff --git a/API/src/database/Database.ts b/API/src/database/Database.ts index 7354ffea7b929f7006fa703739c384e09df464a3..3ae5e0ac56183c4774edbaa9d00fee3877688d6a 100644 --- a/API/src/database/Database.ts +++ b/API/src/database/Database.ts @@ -46,13 +46,13 @@ class DBHandler { else throw JSON.stringify({message: "Username does not exists"}); if (okay) - return await asyncdb.get("SELECT id, username, admin FROM users WHERE username='" + user.username + "'") + return await asyncdb.get("SELECT id, username, admin FROM Users WHERE username='" + user.username + "'") else throw JSON.stringify({message: "Incorrect Password"}); } async verifyUsernameExistence(username:string) : Promise<boolean> { - const usernameInDB = await asyncdb.all<User_t>("SELECT username FROM users WHERE username='" + username + "'") + const usernameInDB = await asyncdb.all<User_t>("SELECT username FROM Users WHERE username='" + username + "'") if (usernameInDB.length == 0) return false; @@ -68,19 +68,19 @@ class DBHandler { } async comparePassword(user: User_t) : Promise<boolean> { - const query = "SELECT password, admin FROM users WHERE username='"+ user.username + "'"; + const query = "SELECT password, admin FROM Users WHERE username='"+ user.username + "'"; const password = await asyncdb.all<User_t>(query) return (password[0].password == user.password); } async getUserById(id: number) : Promise<User_t> { - const query = "SELECT username, password, admin FROM users WHERE id="+id; + const query = "SELECT username, password, admin FROM Users WHERE id="+id; const user = await asyncdb.get<User_t>(query); return user; } async getUsers(res:express.Response) { - const query = "SELECT id, username, admin FROM users"; + const query = "SELECT id, username, admin FROM Users"; asyncdb.all(query) .then(result => { res.status(StatusCodes.OK).json(result).end()} @@ -94,7 +94,7 @@ class DBHandler { if (a.admin === undefined) a.admin = false; - const query = "INSERT INTO users (username, password, admin) \ + const query = "INSERT INTO Users (username, password, admin) \ VALUES ('" + a.username + "','" + a.password + "'," + a.admin +")"; asyncdb.all(query) @@ -105,7 +105,7 @@ class DBHandler { async updateUser(req:express.Request, res:express.Response) { const a = req.body as User_t; - const request = "UPDATE users \ + const request = "UPDATE Users \ SET username = '"+ a.username + "', admin =" + a.admin + " WHERE id = " + req.params.id; asyncdb.all(request) @@ -114,7 +114,7 @@ class DBHandler { } async deleteUser(req:express.Request, res:express.Response) { - asyncdb.all("DELETE FROM users WHERE id = " + req.params.id) + asyncdb.all("DELETE FROM Users WHERE id = " + req.params.id) .then(() => res.status(StatusCodes.OK).end()) .catch(err => console.log(err)); } @@ -122,7 +122,7 @@ class DBHandler { async postQuestion(req:express.Request, res:express.Response) { const a = req.body as Question_t; - const insert_question = "INSERT INTO questions (question, category) VALUES ('"+ a.question +"', '"+ a.category +"')"; + const insert_question = "INSERT INTO Questions (question, category) VALUES ('"+ a.question +"', '"+ a.category +"')"; asyncdb.all(insert_question) .then(() => res.status(StatusCodes.OK).end()) @@ -133,11 +133,11 @@ class DBHandler { const id = req.params.id; if (id === undefined) { - var query = "SELECT * FROM questions"; + var query = "SELECT * FROM Questions"; var questions = await asyncdb.all(query); } else { - query = "SELECT * FROM questions WHERE id =" + id; + query = "SELECT * FROM Questions WHERE id =" + id; questions = await asyncdb.get(query); } @@ -146,7 +146,7 @@ class DBHandler { async updateQuestion(req:express.Request, res:express.Response){ const a = req.body as Question_t; - const request = "UPDATE questions \ + const request = "UPDATE Questions \ SET question='"+ a.question +"', category='"+ a.category +"' \ WHERE id=" + req.params.id; @@ -158,20 +158,20 @@ class DBHandler { async deleteQuestion(req:express.Request, res:express.Response) { const a = req.body as Question_t; - asyncdb.all("DELETE FROM questions WHERE id =" + req.params.id) + asyncdb.all("DELETE FROM Questions WHERE id =" + req.params.id) .then(() => res.status(StatusCodes.OK).end()) .catch(e => console.log(e)); } async getCategories(req:express.Request, res:express.Response) { - asyncdb.all("SELECT * FROM category") + asyncdb.all("SELECT * FROM Categories") .then(result => res.status(StatusCodes.OK).json(result).end()) .catch(e => console.log(e)); } async postCategory(req:express.Request, res:express.Response) { const a = req.body as Category_t; - const request = "INSERT INTO Category VALUES ('" + a.category + "');" + const request = "INSERT INTO Categories VALUES ('" + a.category + "');" asyncdb.all(request) .then(() => res.status(StatusCodes.OK).end()) @@ -180,7 +180,7 @@ class DBHandler { async deleteCategory(req:express.Request, res:express.Response) { const category = req.params.category; - const request = "DELETE FROM category WHERE title = '" + category + "'"; + const request = "DELETE FROM Categories WHERE title = '" + category + "'"; asyncdb.all(request) .then(() => res.status(StatusCodes.OK).end()) .catch(e => console.log(e)); @@ -189,7 +189,7 @@ class DBHandler { async getAnwsers(req:express.Request, res:express.Response) { const question_id = req.params.questionId; - const query = "SELECT * FROM answer WHERE id_question=" + question_id; + const query = "SELECT * FROM Answers WHERE id_question=" + question_id; asyncdb.all(query) .then(ans => res.status(StatusCodes.OK).json(ans)) @@ -198,7 +198,7 @@ class DBHandler { async postAnwsers(req:express.Request, res:express.Response) { const answer = req.body as Answer_t; - const query = "INSERT INTO answer (text_answer, id_question, correct) \ + const query = "INSERT INTO Answers (text_answer, id_question, correct) \ VALUES ('" + answer.text_answer + "', " + answer.id_question + ", " + answer.correct + ")"; asyncdb.all(query) @@ -206,7 +206,7 @@ class DBHandler { } async deleteAnswer(req:express.Request, res:express.Response) { - const query = "DELETE from answer WHERE id=" + req.params.id; + const query = "DELETE from Answers WHERE id=" + req.params.id; asyncdb.all(query) .then(() => res.status(StatusCodes.OK).end()) @@ -214,7 +214,7 @@ class DBHandler { async updateAnswer(req:express.Request, res:express.Response) { const a = req.body as Answer_t; - const request = "UPDATE answer \ + const request = "UPDATE Answers \ SET text_answer='"+ a.text_answer +"', correct="+ a.correct + " \ WHERE id=" + req.params.id; @@ -225,13 +225,13 @@ class DBHandler { } async getQuestionsForGame() { - const query = "SELECT * FROM questions ORDER BY RANDOM() LIMIT 10;"; + const query = "SELECT * FROM Questions ORDER BY RANDOM() LIMIT 10;"; let questions = await asyncdb.all(query); return questions; } async getAnswersForGame(question_id: number) { - const query = "SELECT * FROM answer WHERE id_question= " + question_id + " ORDER BY RANDOM();"; + const query = "SELECT * FROM Answers WHERE id_question= " + question_id + " ORDER BY RANDOM();"; const answers = await asyncdb.all(query); console.log(answers); diff --git a/API/src/socket.io/ServerIO.ts b/API/src/socket.io/ServerIO.ts index 48e98e2cf89ddd2fa93293d47654184fb302a6c6..a2d874e43c550199512b75d902c96e1b7cdc7817 100644 --- a/API/src/socket.io/ServerIO.ts +++ b/API/src/socket.io/ServerIO.ts @@ -39,12 +39,10 @@ class ServerIO extends IO.Server { if (this.gameOngoing) return socket.emit('Room Full'); - const token = socket.handshake.query.Authorization as string; - - console.log(token); + const token = socket.handshake.query.Authorization as string; var user: User_t = this.decodeToken(token); - + logger.info(user.username + ` joined the game`); this.playerList.push(user.username); @@ -64,6 +62,7 @@ class ServerIO extends IO.Server { var index: number = this.playerList.indexOf(user.username, 0); + delete this.scoreboard[user.username] if (index != -1) { this.playerList.splice(index, 1); @@ -73,8 +72,7 @@ class ServerIO extends IO.Server { if (this.gameOngoing) this.gameEnd(); - socket.emit('Players Waiting', this.playerList.length) - socket.broadcast.emit('Players Waiting', this.playerList.length); + this.emit('Players Waiting', this.playerList.length); }) socket.on('disconnect', _ => { @@ -138,8 +136,6 @@ class ServerIO extends IO.Server { if (this.questions.length == 0) return this.gameEnd() - - console.log(this.scoreboard) let currentQuestion = this.questions.pop() @@ -161,7 +157,7 @@ class ServerIO extends IO.Server { this.playerList = []; this.scoreboard = {}; - this.emit('Players Waiting', this.playerList.length) + this.emit('Players Waiting', this.playerList.length); this.gameOngoing = false; } diff --git a/Documentation/Documentation.md b/Documentation/Documentation.md new file mode 100644 index 0000000000000000000000000000000000000000..511d11d6295d5ab77fbb4375dfaa16f4a52330ee --- /dev/null +++ b/Documentation/Documentation.md @@ -0,0 +1,25 @@ +# Application Web style Question pour un Champion + +--- + +### Introduction + +Dans le cadre des cours Architecture Web et Application Web, nous avons du créer un site web permettant à des joueurs de s'inscrire, puis de participer à des parties de jeu contre deux autres joueurs. + +Le projet se sépare donc en trois parties distinctes : +- L'API REST du projet +- Le visuel du site +- Le système de jeu à l'aide de WebSockets. + +Cette documentation va se porter sur ces parties du projet, en détaillant les aspects techniques ainsi que des requis pour pouvoir les lancer. + +--- + +### API REST + +L'API REST va être celle qui va faire le lien entre la base de données et les utilisateurs. Avec ses routes, on peut y effectuer toutes les actions du CRUD sur notre db. + + +##### Database + +Pour ce projet, on avait commencé par garder les données "In Memory". Très vite, on a rencontré des problèmes majeurs, comme la persistence des données, ou simplement les actions sur celles-ci. \ No newline at end of file diff --git a/Documentation/db_diagram.drawio b/Documentation/db_diagram.drawio new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391