GorkemsCafe/backend.js

123 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2023-09-26 04:40:05 -05:00
const express = require('express')
const app = express()
let timeoutHandle;
//socket.io setup
const http = require('http')
const server = http.createServer(app)
const {Server} = require('socket.io')
const io = new Server(server, {
// custom ping timer for duplicates
pingInterval:2000,
pingTimeout:5000
})
var Filter = require('./bad-words-hacked');
filter = new Filter();
// app.use(requireHTTPS); // Set 'SET NODE_ENV=development' in local computer before use
2023-09-27 05:56:42 -05:00
function requireHTTPS(req, res, next) {
// The 'x-forwarded-proto' check is for Heroku
if (!req.secure && req.get('x-forwarded-proto') !== 'https' && process.env.NODE_ENV !== "development") {
return res.redirect('https://' + req.get('host') + req.url);
}
next();
}
2023-09-26 04:52:17 -05:00
// const port = 3000
2023-09-26 04:40:05 -05:00
app.use(express.static('public'))
app.get('/',(req,res)=>{
res.sendFile(__dirname + 'index.html')
})
const bPlayers = {
}
io.on('connection', (socket)=>{
console.log('user connected')
resetDisconnectTimer();
2023-09-26 04:40:05 -05:00
bPlayers[socket.id] = {
2023-09-26 06:57:39 -05:00
x:615,
y:136,
clickX:615,
clickY:136,
roomId:1,
2023-09-26 04:40:05 -05:00
message:'',
2023-09-26 10:40:27 -05:00
username:"Visitor",
skin:"Green",
2023-09-26 10:40:27 -05:00
timeoutId:0
2023-09-26 04:40:05 -05:00
} //create player object with new socket id property
io.emit('updatePlayers',bPlayers)
socket.on('disconnect',(reason)=>{
console.log(reason)
delete bPlayers[socket.id]
io.emit('updatePlayers',bPlayers)
})
socket.on('clickPosition',(coordinates)=>{
resetDisconnectTimer();
2023-09-26 04:40:05 -05:00
bPlayers[socket.id].clickX = coordinates[0]
bPlayers[socket.id].clickY = coordinates[1]
bPlayers[socket.id].x = bPlayers[socket.id].clickX
bPlayers[socket.id].y = bPlayers[socket.id].clickY
2023-09-26 04:40:05 -05:00
console.log(bPlayers)
})
socket.on('username',(username)=>{
bPlayers[socket.id].username = username
2023-09-26 11:03:59 -05:00
console.log("updated username for: "+bPlayers[socket.id].username)
2023-09-26 04:40:05 -05:00
})
socket.on('skin',(skin)=>{
bPlayers[socket.id].skin = skin
console.log("updated skin for: "+bPlayers[socket.id].username)
})
2023-09-26 04:40:05 -05:00
socket.on('message',(message)=>{
2023-09-26 10:40:27 -05:00
let timeoutId;
clearTimeout(bPlayers[socket.id].timeoutId);
bPlayers[socket.id].message = filter.cleanHacked(message)
// bPlayers[socket.id].message = message
2023-09-26 04:40:05 -05:00
function startTimer() {
2023-09-26 10:40:27 -05:00
timeoutId = setTimeout(() => {
2023-09-26 04:40:05 -05:00
bPlayers[socket.id].message = ''
}, 4000);
}
2023-09-26 10:40:27 -05:00
bPlayers[socket.id].timeoutId = timeoutId
2023-09-26 04:40:05 -05:00
startTimer();
console.log(bPlayers)
})
function resetDisconnectTimer(){
clearTimeout(socket.inactivityTimeout);
socket.inactivityTimeout = setTimeout(() => socket.disconnect(true), 1000 * 60 * 15);
}
2023-09-26 04:40:05 -05:00
// socket.on('clickPositionX',(coordinates)=>{
// bPlayers[socket.id].x = coordinates
// console.log(coordinates)
// })
// socket.on('clickPositionY',(coordinates)=>{
// bPlayers[socket.id].y = coordinates
// })
console.log(bPlayers)
})
setInterval(() =>{
io.emit('updatePlayers',bPlayers)
},15)
2023-09-26 04:52:17 -05:00
server.listen((process.env.PORT || 3000),() =>{
console.log(`app listening on port ${(process.env.PORT || 3000)}`)
})