Skip to content

Commit

Permalink
Version 0.1 Release
Browse files Browse the repository at this point in the history
- Basic Functionalities are added: calculates the average SP if admin
pushes the calculate SP button
  • Loading branch information
Dulguun committed Apr 13, 2020
1 parent 0439dc6 commit 5921156
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 56 deletions.
7 changes: 3 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// Populate the room-names
for(let i=0;i<10;i++){
let newRoom = {
id: i,
name: `room-${i}`,
id: 0,
name: `room-0`,
occupied: false,
users:[]
}
dbHandler.insert(newRoom);
}


app.use('/', indexRouter);
app.use('/users', usersRouter);
Expand Down
92 changes: 66 additions & 26 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

var app = require('../app');
var socket = require('../socket/socket')
var dbHandler = require('../socket/dbHandler')
var debug = require('debug')('poker-app:server');
var http = require('http');
Expand Down Expand Up @@ -37,53 +36,94 @@ io.of(`/poker`).on('connection', (socket) => {
userId = socket.id;
socket.auth = auth;
console.log(`Server: join room ${roomId}`);
dbHandler.database.findOne({_id: roomId}, (err, room) => {
if(err){
dbHandler.database.findOne({ _id: roomId }, (err, room) => {
if (err) {
console.log('error has been occured during database findOne:roomId');
console.log(err);
}
if(room.users!=undefined){
if (room.users != undefined) {
io.of('/poker').in(roomId).emit("joinRoom", room.users);
}
else {

}
});
dbHandler.database.findOne({_id: roomId}, (err, room) => {
dbHandler.database.findOne({ _id: roomId }, (err, room) => {
if (err) {
console.log('error occured');
console.log(err);
}
console.log(`username: ${userName} | userId ${userId} has joined to room ${roomId}`);
socket.join(roomId);
console.log(`room: `);
console.log(room)
if(room.users == undefined){
room.users = [];
console.log(`username: ${userName} | userId ${userId} has joined to room ${roomId}`);
socket.join(roomId);
let admin = false
console.log(`room: `);
console.log(room)
if (room.users == undefined) {
room.users = [];
admin = true;
}
room.users.push({ name: userName, id: userId, admin: admin, score: 0 });
// Update the joined users
dbHandler.database.update({ _id: roomId }, { users: room.users }, (err, numReplaced) => {
if (err) {
console.log(err);
} else {
console.log(`num of Replaced : ${numReplaced}`);
}
room.users.push({name: userName,id:userId});
// Update the joined users
dbHandler.database.update({ _id: roomId }, { users: room.users }, (err, numReplaced) => {
});
io.of('/poker').in(roomId).emit("newUser", room.users);
return socket.emit('success', `You have succesfully joined to the room: ${roomId}`);
});
// socket.disconnect();
socket.on('voteScore', modifiedUser => {
console.log('modified User: ');
dbHandler.database.findOne({ _id: roomId }, (err, room) => {
let i = room.users.findIndex(user => user.id == modifiedUser.id);
room.users[i] = modifiedUser;
console.log(`modified user name: ${modifiedUser.name}`);
io.of('/poker').in(roomId).emit("updateUsers", room.users);
dbHandler.database.update({ _id: roomId }, { users: room.users }, (err, numReplaced) => {
if (err) {
console.log('An Error occured during update of modified User');
console.log(err);
} else {
console.log(`num of Replaced : ${numReplaced}`);
}
else {
console.log('DB is updated - Modified User');
console.log(numReplaced)
}
});
io.of('/poker').in(roomId).emit("newUser", room.users);
return socket.emit('success', `You have succesfully joined to the room: ${roomId}`);
})
});
// socket.disconnect();
socket.on('calculate', roomId => {
console.log('calculate socket is called');
dbHandler.database.findOne({_id:roomId},(err,room) => {
if(room && room.users){
let lengthOfUsers = room.users.length;
let sum = 0;
room.users.forEach(user => {
sum+=user.score;
});
let average = sum/lengthOfUsers;
io.of('/poker').in(roomId).emit('calculate',average);
}
});
})
});
socket.on('disconnect',() => {
console.log(`user: ${userName}| userId:${userId} is disconnected from ${roomId}`);
dbHandler.database.findOne({_id:roomId},(err,room) => {
if(err){
socket.on('disconnect', () => {
console.log(`user: ${userName} | userId:${userId} is disconnected from ${roomId}`);
dbHandler.database.findOne({ _id: roomId }, (err, room) => {
if (err) {
console.log(`socket on disconnect; Error occured during dbHandler: `);
console.log(err);
}
if (room && room.users){
if (room && room.users) {
let onlineUsers = room.users.filter(user => user.id != userId);
dbHandler.database.update({_id:roomId},{users:onlineUsers},(err,numReplaced) => {

dbHandler.database.update({ _id: roomId }, { users: onlineUsers }, (err, numReplaced) => {
});
if(room.users.length == 0 ){
dbHandler.database.update({ _id: roomId }, { occupied: false }, (err, numReplaced) => {
})
}
io.of('/poker').in(roomId).emit("newUser", onlineUsers);
}
});
Expand Down
77 changes: 62 additions & 15 deletions public/javascripts/room.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,90 @@
const socket = io(`${address}/poker`);

console.log(room);
socket.on('connection',(msg)=>{
socket.on('connection', (msg) => {
console.log(`connection event msg: ${msg}`);
})

console.log(`room name: ${roomName}`)
console.log(`room id: ${roomId}`)
let myUser;


document.getElementById('nameButton').addEventListener('click',function(){
document.getElementById('nameSubmitButton').addEventListener('click', function () {
console.log(document.getElementById('nameOfUser').value);
let userName = document.getElementById('nameOfUser').value;
console.log(`DOM read: ${userName}`);
let auth = {
userName: userName,
roomId: roomId
}
socket.emit('joinRoom',auth);
socket.on("newUser",(joinedUsers)=>{
socket.emit('joinRoom', auth);
socket.on("newUser", (joinedUsers) => {
console.log('joined Users: ');
console.log(joinedUsers);
joinedUsers.forEach(user => {
if (user.name == userName)
{ myUser = user }
});
const joinedUsersHtml = createJoinedUsersHtml(joinedUsers);
document.getElementById('joinedUsers').innerHTML = joinedUsersHtml;
document.getElementById('nameSubmitButton').disabled = true;
let submitButton = document.createElement('input');
if (myUser.admin) {
submitButton.type = 'button'
submitButton.value = 'Calculate The SP';
let divCalculateScore = document.getElementById('calculateTheScore');
divCalculateScore.innerHTML = '';
divCalculateScore.appendChild(submitButton);
}
submitButton.addEventListener('click', () => {
socket.emit('calculate',roomId);
});
const storyPoints = createPokerScores();
document.getElementById('sps').innerHTML = storyPoints;
});
});
socket.on('joinRoom',(users) => {
socket.on('calculate',averageScore => {
document.getElementById('averageScore').innerHTML = `<h3> Average Score: ${averageScore} </h3>`
});
socket.on('updateUsers', (users) => {
const joinedUsersHtml = createJoinedUsersHtml(users);
document.getElementById('joinedUsers').innerHTML = joinedUsersHtml;
});

let createJoinedUsersHtml = (users) =>{
let createJoinedUsersHtml = (users) => {
let joinedUsersHtml = '<ul>'
users.forEach( user =>{
joinedUsersHtml+=`<li> ${user.name}: ${user.id}</li>`
})
joinedUsersHtml+='</ul>'
users.forEach(user => {
if (user.score == 0) {
joinedUsersHtml += `<li> ${user.name}: NOT VOTED</li>`
}
else {
joinedUsersHtml += `<li> ${user.name}: VOTED</li>`
}
});
joinedUsersHtml += '</ul>'
return joinedUsersHtml
}

socket.on(roomId,(res)=>{console.log(res)});
socket.on('welcome',(msg) => console.log(msg));
socket.on('err',(err)=>console.log(err));
socket.on('success',(res)=>{console.log(res);});
let createPokerScores = () => {
let buttonsHTML = ''
let pokerScores = [1, 2, 3, 5, 8, 13, 21, 40, 100];
pokerScores.forEach(score => {
let button = `<input type="button" onclick=sendScore(${score}) class="${score} score-btn" value="${score}"/>`;
buttonsHTML += button;
console.log(button);
});
return buttonsHTML;
}

let sendScore = (score) => {
console.log(score);
myUser.score = score;
console.log('modified User: ');
console.log(myUser);
socket.emit('voteScore', myUser);
}

socket.on(roomId, (res) => { console.log(res) });
socket.on('welcome', (msg) => console.log(msg));
socket.on('err', (err) => console.log(err));
socket.on('success', (res) => { console.log(res); });
17 changes: 10 additions & 7 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ var router = express.Router();
let dbHandler = require('../socket/dbHandler');

router.get('/', function (req, res, next) {
dbHandler.database.find({ occupied: false }, (err, docs) => {
dbHandler.database.find({ occupied: false }, (err, rooms) => {
if (err) {
console.log(err);
}
else {
console.log('index.js get(/) function');
console.log(docs);
let newRoom = {
id:rooms.length+1,
name: `room-${rooms.length+1}`,
occupied: false,
users:[]
}
let size = docs.length
dbHandler.insert(newRoom);
let size = rooms.length
let rIndex = Math.floor(Math.random() * (size - 1));
console.log(`rIndex: ${rIndex}`);
console.log('random room: ');
console.log(docs[rIndex]);
let assignedRoom = docs[rIndex];
console.log(rooms[rIndex]);
let assignedRoom = rooms[rIndex];
assignedRoom.occupied = true;
// update the database after it's randomly selected
dbHandler.database.update({ _id: assignedRoom._id }, { occupied: true }, function (err, numReplaced) {
Expand Down
10 changes: 6 additions & 4 deletions views/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ block content
p Room name: #{room.name}
h2 SEND this link to your teammates to connect
code #{address}/room/?roomId=#{room._id}
div#averageScore
div#joinedUsers

div
label Name
label My Name is:
br
input#nameOfUser(type="text")
br
input#nameButton(type="submit",value="submit")

input#nameSubmitButton(type="submit",value="submit")
div#sps
div#calculateTheScore
script.
let room = "#{room}"
let roomName = "#{room.name}"
Expand Down

0 comments on commit 5921156

Please sign in to comment.