Skip to content

Commit

Permalink
Fixed: Score Update functionality is working.
Browse files Browse the repository at this point in the history
  • Loading branch information
“Akhilesh committed Jan 21, 2024
1 parent 7bfe91f commit e2891a2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Rocket-Typing Multiplayer Game
This project is a Typing Multiplayer Game built using **Node.js, Express, and Socket.IO.** It allows multiple users to join a room, type messages, and interact in real-time.
This project is a Fastest Typing Multiplayer Game built using **Node.js, Express, and Socket.IO.** It allows multiple users to join a room, type messages, and interact in real-time.

### Features
- Multiplayer Typing Game
Expand Down
125 changes: 77 additions & 48 deletions public/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ socket.on('connect', function() {
// Access the socket ID directly using socket.id
const socketId = socket.id;
// Now you can use the socket ID as needed
setCookie("socketId", socket.id, 1);

setUserSocketID(socketId);
});

Expand Down Expand Up @@ -89,9 +91,14 @@ socket.on('roomData', (roomDataJSONList) => {




socket.on('startGameInitilization', (gameliveUpdate) => {

isMultiplayer = true;

// play a start butffer
buzzer_sound.play();

hideMultiplayerWindow();
hideMultiplayerButton();
showLivePlayerCardButton();
Expand All @@ -100,6 +107,7 @@ socket.on('startGameInitilization', (gameliveUpdate) => {

socket.on('getReadyContDown', (no_of_sec_to_getReady) => {

// console.log("getReadyContDown: ", socket_id);
showGetReadyCountdownWrapper(no_of_sec_to_getReady);

});
Expand All @@ -109,7 +117,8 @@ socket.on('getReadyContDown', (no_of_sec_to_getReady) => {


socket.on('gameRoomLiveUpdateDataJson', (gameUpdateData) => {



const timeLeft = gameUpdateData["time"];
var playerDataJSON = gameUpdateData["playersData"];
// console.log(gameUpdateData["playersData"]);
Expand All @@ -121,50 +130,51 @@ socket.on('gameRoomLiveUpdateDataJson', (gameUpdateData) => {



for (const [playerSocketID, playerInfo] of Object.entries(playerDataJSON)) {
// for (const [playerSocketID, playerInfo] of Object.entries(playerDataJSON)) {

// Extract player information from the received data
const playerName = playerInfo.name;
const playerScore = playerInfo.score;
const playerAvatarLink = playerInfo.avatar_link;
// // Extract player information from the received data
// const playerName = playerInfo.name;
// const playerScore = playerInfo.score;
// const playerAvatarLink = playerInfo.avatar_link;

// console.log(playerName, playerScore, playerAvatarLink);
appendLivePlayerBox(1, playerName, playerAvatarLink, playerScore)
// // console.log(playerName, playerScore, playerAvatarLink);
// appendLivePlayerBox(1, playerName, playerAvatarLink, playerScore)

}
// }


var position = 1;

// // sorting
// for (const [i, j] of Object.entries(playerDataJSON)) {
// sorting on the based on the highest score at top
for (const [i, j] of Object.entries(playerDataJSON)) {

// var maxScore = -1;
// var maxPlayerID;
var maxScore = -1;
var maxPlayerID;

// // Extract player information from the received data
// var playerName, playerScore, playerAvatarLink;
// Extract player information from the received data
var playerName, playerScore, playerAvatarLink;

// for (const [playerSocketID, playerInfo] of Object.entries(playerDataJSON)) {
for (const [playerSocketID, playerInfo] of Object.entries(playerDataJSON)) {

// if(playerInfo.score > maxScore){
if(playerInfo.score > maxScore){

// maxPlayerID = playerSocketID;
// maxScore = playerInfo.score;
maxPlayerID = playerSocketID;
maxScore = playerInfo.score;

// playerName = playerInfo.name;
// playerScore = playerInfo.score;
// playerAvatarLink = playerInfo.avatar_link;
// }
playerName = playerInfo.name;
playerScore = playerInfo.score;
playerAvatarLink = playerInfo.avatar_link;
}

// }
}

// if(maxScore >= 0){
// // console.log(playerName, playerScore, playerAvatarLink);
// appendLivePlayerBox(1, playerName, playerAvatarLink, playerScore)
// playerDataJSON[maxPlayerID]["score"] = -1;
// }
if(maxScore >= 0){
appendLivePlayerBox(position, playerName, playerAvatarLink, playerScore)
playerDataJSON[maxPlayerID]["score"] = -1;
}

// }
position += 1;
}



Expand All @@ -175,6 +185,32 @@ socket.on('gameRoomLiveUpdateDataJson', (gameUpdateData) => {
});


function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}


function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

function appendLivePlayerBox(rank, name, avatar_img_link, score){

// Create the elements
Expand Down Expand Up @@ -216,11 +252,12 @@ function appendLivePlayerBox(rank, name, avatar_img_link, score){
// Event listener for keydown
document.addEventListener('keydown', function (event) {

if(isStarted == false && isMultiplayer == false){
if(isStarted === false){
isStarted = true;
// Example usage
}

if(isMultiplayer === false){
startCountdown(numberOfSecondToCompete);
// hideStartButton();
}

const firstCharacter = un_typedTextElement.textContent.charAt(0);
Expand Down Expand Up @@ -261,22 +298,14 @@ document.addEventListener('keydown', function (event) {
score_number.innerHTML = scoreCount;


// // send score update data to server
// socket.emit('updatePlayerScore',
// {
// "playerID": socket_id,
// "score": scoreCount
// });


// SERVER UPDATE SCORE
// SERVER UPDATE SCORE
if(isMultiplayer === true){

if(isMultiplayer){
socket.emit('scoreUpdateByPlayer', {
"roomID": room_id,
"socketID": socket_id,
"socketID": getCookie('socketId'),
"scoreCount": scoreCount
});
});
}

});
Expand Down Expand Up @@ -305,7 +334,7 @@ function findWPM(sec, words) {
return (60.0/sec) * words;
}

console.log(60.0/sec) * words;
// console.log(60.0/sec) * words;
return Math.max(0, (sec/60.0) * words);
}

Expand Down Expand Up @@ -426,10 +455,10 @@ function joinMatch() {

function startTheMatchClicked(){
socket.emit('startMatchRequest',
{
{
"socket_id":socket_id,
"room_id": room_id
});
});
}


Expand Down
4 changes: 0 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,8 @@ io.on('connection', (socket) => {
var socketID = updateData["socketID"];
var scoreCount = updateData["scoreCount"];

// console.log(roomsDetailArrayList);

//? UPDATE THE ROOM DETAIL ARRAY */
updatePlayerScoreOnly(roomID, socketID, scoreCount);

console.log("scoreUpdateByPlayer: roomID: ", roomID, "| socketID:", socketID, "score:", scoreCount);
});

});
Expand Down
2 changes: 1 addition & 1 deletion views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<div class="container">
<div class="textContent leftText" id="typedText"></div>
<div id="pointer"></div>
<div class="textContent rightText" id="ToTypeText">This_is_a_sample_Text_Just_for_testing_don't_reinvent_the_wheel_just_improve_it.</div>
<div class="textContent rightText" id="ToTypeText">AI_technology_is_widely_used_throughout_industry,_government,_and_science._Some_high-profile_applications_are:_advanced_web_search_engines_(e.g.,_Google_Search),_recommendation_systems_(used_by_YouTube,_Amazon,_and_Netflix),_understanding_human_speech_(such_as_Google_Assistant,_Siri,_and_Alexa),_self-driving_cars_(e.g.,_Waymo),_generative_and_creative_tools_</div>
</div>

<div class="center_wrapper">
Expand Down

0 comments on commit e2891a2

Please sign in to comment.