Skip to content

Commit

Permalink
re formatting code + Adding additional comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrahmanNoaman committed Jul 25, 2023
1 parent b3881d6 commit 9bf4843
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 56 deletions.
95 changes: 42 additions & 53 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ const addTeamOne = document.getElementById("add-team-one");
const addTeamTwo = document.getElementById("add-team-two");
const addReferee = document.getElementById("add-referee");
const addWaiting = document.getElementById("add-waiting");

const { outputMessage, outputNames } = require("./utils.js");

//Get username and room from URL
const { username, roomId } = Qs.parse(location.search, {
ignoreQueryPrefix: true,
});
//Defining the socket
const socket = io();

//Adding the room id to be able to copy it
const p = document.createElement("p");
p.innerHTML = `${roomId}`;
document.getElementById("room-name").appendChild(p);

const socket = io();
//join chatroom
// --------------------------------------------------------------------------------------
// Dealing with sockets

//join chatroom event, which must happen when you enter any room
socket.emit("joinRoom", { username, roomId });
//Informing that this user will start in the waiting list
socket.emit("updatePlayer", {
username: username,
roomId: roomId,
Expand All @@ -24,96 +33,76 @@ socket.emit("updatePlayer", {

// Receiving the message from the server
socket.on("message", (message) => {
//displaying the message
outputMessage(message);
//every time we get a message we want to scroll down
chatMessages.scrollTop = chatMessages.scrollHeight;
});

// Case of receiving an updated version of waited users
socket.on("waitedUsers", (users) => {
outputNames(users, "waiting-users");
});

// Case of receiving an updated version of team one
socket.on("teamOne", (username) => {
outputNames(username, "team-one");
});

// Case of receiving an updated version of team two
socket.on("teamTwo", (username) => {
outputNames(username, "team-two");
});

// Case of receiving an updated version of referee
socket.on("referee", (username) => {
outputNames(username, "referee");
});

// --------------------------------------------------------------------------------------
// Event Listeners

//Adding a message
document.addEventListener("submit", (e) => {
e.preventDefault();
//Getting the text of the message
const msg = {
username: username,
};
msg.text = e.target.elements.msg.value;
//Sending the msg to the server
socket.emit("chatMessage", msg);
//Everytime we get a message we want to clear the input field
e.target.elements.msg.value = "";
e.target.elements.msg.focus();
});

//Adding the message to the client side
function outputMessage(message) {
const div = document.createElement("div");
div.classList.add("message");
div.innerHTML = `
<div class="message">
<p class="meta">${message.username}<span> ${message.time}</span></p>
<p class="text">${message.text}</p>
</div>`;
document.querySelector(".chat-messages").appendChild(div);
}

//Adding the name of users in waiting names
function outputNames(users, listName) {
const element = document.getElementById(listName);
while (element.firstChild) {
element.removeChild(element.firstChild);
}
for (let i = 0; i < users.length; i++) {
const li = document.createElement("li");
li.innerHTML = `${users[i].username}`;
document.getElementById(listName).appendChild(li);
}
}

addTeamOne.addEventListener("click", function() {
// Event of transforming a player from any type to team one
addTeamOne.addEventListener("click", function () {
socket.emit("updatePlayer", {
username: username,
roomId: roomId,
type: "TEAMONE",
});
});

addTeamTwo.addEventListener("click", function() {
// Event of transforming a player from any type to team two
addTeamTwo.addEventListener("click", function () {
socket.emit("updatePlayer", {
username: username,
roomId: roomId,
type: "TEAMTWO",
});
});

addReferee.addEventListener("click", function() {
// Event of transforming a player from any type to referee
addReferee.addEventListener("click", function () {
socket.emit("updatePlayer", {
username: username,
roomId: roomId,
type: "REFEREE",
});
});

addWaiting.addEventListener("click", function() {
// Event of transforming a player from any type to waiting
addWaiting.addEventListener("click", function () {
socket.emit("updatePlayer", {
username: username,
roomId: roomId,
type: "WAITING",
});
});

// Event of Adding a message
document.addEventListener("submit", (e) => {
e.preventDefault();
const msg = {
username: username,
roomId: roomId,
};
// Getting the text of the message
msg.text = e.target.elements.msg.value;
// Sending the msg to the server
socket.emit("chatMessage", msg);
// Every time we get a message we want to clear the input field, and focus on it
e.target.elements.msg.value = "";
e.target.elements.msg.focus();
});
32 changes: 32 additions & 0 deletions public/js/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// --------------------------------------------------------------------------------------
// Helper Functions

// Adding the message to the client side
const outputMessage = function (message) {
const div = document.createElement("div");
div.classList.add("message");
div.innerHTML = `
<div class="message">
<p class="meta">${message.username}<span> ${message.time}</span></p>
<p class="text">${message.text}</p>
</div>`;
document.querySelector(".chat-messages").appendChild(div);
};

// Adding the name of users in any field
const outputNames = function (users, listName) {
const element = document.getElementById(listName);
while (element.firstChild) {
element.removeChild(element.firstChild);
}
for (let i = 0; i < users.length; i++) {
const li = document.createElement("li");
li.innerHTML = `${users[i].username}`;
document.getElementById(listName).appendChild(li);
}
};

module.exports = {
outputMessage,
outputNames,
};
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ io.on("connection", (socket) => {

// Getting the chat message
socket.on("chatMessage", (msg) => {
io.to(user.roomId).emit("message", formatMessage(msg));
io.to(msg.roomId).emit("message", formatMessage(msg));
});
});

Expand Down
1 change: 0 additions & 1 deletion services/users.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const {
getCurrUser,
userJoin,
leaveChat,
getWaitingUsers,
getTeamOneUsers,
getReferee,
Expand Down
1 change: 0 additions & 1 deletion utils/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ async function updateUser(username, roomId, type) {
module.exports = {
userJoin,
getCurrUser,
leaveChat,
getReferee,
getTeamOneUsers,
getTeamTwoUsers,
Expand Down

0 comments on commit 9bf4843

Please sign in to comment.