Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor-project #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions Final Report/Type-Go-master/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const express = require('express');
const app = express();
const socket = require('socket.io');
// const mongoose = require('mongoose');
const path = require('path');
// const { render } = require('ejs');
// const Game = require('./models/game');
// const quotableAPI = require('./quotableAPI');
const port = process.env.PORT || 4000 ;

app.use(express.urlencoded())

// mongoose.connect('mongodb://localhost:27017/typego',{useNewUrlParser : true, useUnifiedTopology : true}, ()=>{ console.log('successfully connected')})

app.use(express.static(path.join(__dirname, 'public')));

// set view engine
app.set('view engine', 'ejs')

// template engin route
app.get('/', (req, res) => {
res.render('home');
})

app.post('/creategame', (req, res) => {
// console.log(req.body);
res.render('creategame');
})

app.post('/joingame', (req, res) => {
// console.log(req.body);
res.render('joingame');
})

app.get('/about', (req, res) => {
res.render('about');
})


//server setup
const server = app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})


//socket io setup
const io = socket(server);

var rooms = [];


io.on('connection',(socket)=>{

socket.on('createRoom',(roomCode)=>{
rooms.push(roomCode.roomCode);
socket.join(roomCode.roomCode);
console.log('created',roomCode.roomCode);
})

socket.on('joinRoom',(roomCode)=>{
if(rooms.includes(roomCode.roomCode)){
socket.join(roomCode.roomCode);
// console.log('room joined having id:',roomCode.roomCode);

let joinedUserId = socket.id;
io.sockets.in(roomCode.roomCode).emit('joinedRoom',joinedUserId,roomCode);
socket.broadcast.to(roomCode.roomCode).emit('newlyJoinedUser', roomCode, joinedUserId);
}

else{
console.log('Room not exits');
socket.emit('joinError', roomCode.roomCode);
}
})

socket.on('thenIamSendingMyDataToJoinedUser',(recived)=>{
let createrId = socket.id;
socket.broadcast.to(recived.idOfJoinedUser).emit('okISendedMyDataToJoinedUser',recived,createrId);
})

socket.on('brodcastMyDataToOnlyNewlyJoinedUser',(dataFromJoinUser)=>{
let joinedUserId = socket.id;
socket.broadcast.to(dataFromJoinUser.roomCode).emit('okISendMyDataToNewlyJoinedUser',dataFromJoinUser,joinedUserId);
})

socket.on('startGame',(startCredentials)=>{
// console.log(startCredentials.roomCode);
io.sockets.in(startCredentials.roomCode).emit('startGame',startCredentials);
// io.sockets.emit('startGame',startCredentials);
})

socket.on('updateProgressBar', (myData)=>{
socket.broadcast.to(myData.roomCode).emit('updatingBar',myData.calculateWidthOfProgressBar,socket.id);
})

socket.on('result',(myData)=>{
io.sockets.in(myData.roomCode).emit('result',myData);
})

socket.on('disconnecting', () => {
console.log("leaved ",Array.from(socket.rooms)[1]);
socket.broadcast.to(Array.from(socket.rooms)[1]).emit('left',socket.id);
socket.broadcast.to(Array.from(socket.rooms)[1]).emit('Adminleft',socket.id);
});

socket.on('deleteRoom', ()=>{
let roomName = Array.from(socket.rooms)[1];
const index = rooms.indexOf(roomName);
if (index > -1) {
rooms.splice(index, 1);
console.log('room deleted');
}
})

});
Loading