Skip to content

Commit

Permalink
Update code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrahmanNoaman committed Aug 22, 2023
1 parent 73a4db6 commit 7c3fe4a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 28 deletions.
68 changes: 68 additions & 0 deletions Models/Users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const db = require("../utils/database.js");

class User {
constructor(username, roomId, socketId) {
this.username = username;
this.roomId = roomId;
this.socketId = socketId;
this.type = "WAITING";
}
//-------------------------------------------------------------------------------------------------------
async CreateUser() {
try {
const numberOfUsers = await this.getUser(this.username, this.roomId);
if (numberOfUsers !== 0) {
return JSON.parse({
message:
"Please change the username as the same username is in the room",
});
} else {
const query = `INSERT INTO users VALUES ( '${this.username}', '${this.roomId}' , 'WAITING' , '${this.socketId}' )`;
await db.execute(query);
return this;
}
} catch (error) {
console.log(error);
}
}
//-------------------------------------------------------------------------------------------------------
async getUser(username, roomId) {
const query = ` SELECT count(*) as cnt FROM users WHERE users.username = ? AND users.room_id = ?;`;
const [rows, fields] = await db.execute(query, [username, roomId]);
console.log(rows[0].cnt);
return rows[0].cnt;
}
//-------------------------------------------------------------------------------------------------------
static async getUserBySocketId(socketId) {
const query = ` SELECT *
FROM users
WHERE socket_id = '${socketId}'
`;
return await db.execute(query);
}
//-------------------------------------------------------------------------------------------------------
static async getSpecificUsers(roomId, type) {
const query = ` SELECT *
FROM users
WHERE room_id = '${roomId}' AND
type = '${type}'
`;
return await db.execute(query);
}
//-------------------------------------------------------------------------------------------------------
static async getCnt(roomId, type) {
const query = ` SELECT COUNT(*) as cnt
FROM USERS
WHERE room_id='${roomId}' and type ='${type}'
`;
return await db.execute(query);
}
//-------------------------------------------------------------------------------------------------------
static async updateUserType(username,roomId, type) {
const query = `UPDATE USERS SET type='${type}' WHERE username= '${username}' AND room_id='${roomId}'`;
return await db.execute(query);
}

}

module.exports = User;
2 changes: 2 additions & 0 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ addTeamOne.addEventListener("click", function () {
});
// Event of transforming a player from any type to team two
addTeamTwo.addEventListener("click", function () {
console.log("add team two");
socket.emit("updatePlayer", {
username: username,
roomId: roomId,
Expand All @@ -73,6 +74,7 @@ addTeamTwo.addEventListener("click", function () {
});
// Event of transforming a player from any type to referee
addReferee.addEventListener("click", function () {
console.log("add referee");
socket.emit("updatePlayer", {
username: username,
roomId: roomId,
Expand Down
7 changes: 5 additions & 2 deletions services/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ async function welcomeUser(userObject, socket) {
userObject.username,
userObject.roomId
);
console.log(user);
socket.join(user.roomId);
await sayWelcome(socket, process.env.BOT_NAME);
await sayJoined(socket, user.username, user.roomId);
Expand All @@ -97,8 +98,10 @@ async function leaveUser(io, socket) {
return;
}
sayGoodBye(io, rows[0].username, rows[0].room_id);
db.execute(`DELETE FROM USERS WHERE username='${rows[0].username}' AND room_id='${rows[0].room_id}'`).then((status) => {
if (status.affectedRows===0) {
db.execute(
`DELETE FROM USERS WHERE username='${rows[0].username}' AND room_id='${rows[0].room_id}'`
).then((status) => {
if (status.affectedRows === 0) {
return;
}
informUsers(io, rows[0].room_id);
Expand Down
36 changes: 10 additions & 26 deletions utils/users.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,33 @@
const db = require("../utils/database.js");
const User = require("../Models/Users.js");
// join a user to chat
function userJoin(socket_id, username, roomId) {
const user = { socket_id, username, roomId };
db.execute(
`INSERT INTO users
VALUES ( '${user.username}' , '${user.roomId}' , 'WAITING' , '${user.socket_id}' )`
);
return user;
const user = new User(username, roomId, socket_id);
return user.CreateUser();
}

// get current user
function getCurrUser(socket_id) {
return db.execute(`SELECT * FROM USERS WHERE socket_id = '${socket_id}'`);
return User.getUserBySocketId(socket_id);
}

function getWaitingUsers(roomId) {
return db.execute(
`SELECT * FROM USERS WHERE room_id='${roomId}' AND TYPE='WAITING'`
);
return User.getSpecificUsers(roomId, "WAITING");
}

function getTeamOneUsers(roomId) {
return db.execute(
`SELECT * FROM USERS WHERE room_id='${roomId}' AND TYPE='TEAMONE'`
);
return User.getSpecificUsers(roomId, "TEAMONE");
}

function getTeamTwoUsers(roomId) {
return db.execute(
`SELECT * FROM USERS WHERE room_id='${roomId}' AND TYPE='TEAMTWO'`
);
return User.getSpecificUsers(roomId, "TEAMTWO");
}

function getReferee(roomId) {
return db.execute(
`SELECT * FROM USERS WHERE room_id='${roomId}' AND TYPE='REFEREE'`
);
return User.getSpecificUsers(roomId, "REFEREE");
}

async function check_count(roomId, type) {
let cnt = await db.execute(
`SELECT COUNT(*) as cnt FROM USERS WHERE room_id='${roomId}' and type ='${type}'`
);
let cnt = await User.getCnt(roomId, type);
cnt = cnt[0][0]["cnt"];
if (type === "REFEREE" && cnt === 1) {
return false;
Expand All @@ -54,9 +40,7 @@ async function check_count(roomId, type) {
async function updateUser(username, roomId, type) {
const valid = await check_count(roomId, type);
if (valid) {
return db.execute(
`UPDATE USERS SET type='${type}' WHERE username= '${username}' AND room_id='${roomId}'`
);
return User.updateUserType(username, roomId, type);
}
return false;
}
Expand Down

0 comments on commit 7c3fe4a

Please sign in to comment.