user_hp = req.session.cookie.user_hp - boss_damage;
req.session.cookie.user_hp = user_hp;
var express = require('express');
var app = express();
var http = require('http').Server(app);
var session = require('express-session');
var mysql = require('mysql');
var connections=0,max_connections=5,port = 3000,maxhp=300000,hp = maxhp,x,msg,db;
var hour = 3600000;
let io = require('socket.io')(http, {
});
app.use(express.static(__dirname + '/client'));
app.set('trust proxy', 1); // trust first proxy
app.use(session({
secret: 'boss',
resave: false,
saveUninitialized: true,
cookie: {
secure: false,
maxAge: hour
}
}));
function user(req,res) {
console.log(req.session.cookie.originalMaxAge + '/' + req.session.id);
};
user();
app.get('/', function(req,res) {
res.sendFile(__dirname + '/client/public/auth.html');
});
app.get('/boss', function(req,res) {
res.sendFile(__dirname + '/client/public/boss.html');
/*req.session.cookie.expires = new Date(Date.now() + hour);
req.session.cookie.maxAge = hour;*/
console.log(req.session.cookie.originalMaxAge + '/' + req.session.id);
});
http.listen(port, function() {
console.log('listening on *:' + port);
});
db = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'game'
});
db.connect((err) => {
if(err) console.log(err);
console.log("Mysql connected");
});
io.on('connection', function(socket) {
//Контроль подключений
connections+=1;
io.emit('connections', connections);
console.log('connected client:' + connections);
if(connections > max_connections) {
socket.on('forceDisconnect', () => {
socket.disconnect(true);
console.log('Server full!' + max_connections + '/' + max_connections);
});
}
socket.on( 'disconnect', () => {
connections -= 1;
console.log( 'client disconnected:' + connections);
io.emit('connections', connections);
io.emit('server full', 'Вы отключены от сервера!');
});
//io.set("reconnection limit", 5000);
//end
//Хп босса
socket.on('boss fight', function(data) {
hp = hp-data;
if(hp<=0) {
hp = maxhp;
io.emit('win');
}
x = hp/maxhp*100;
io.emit('realtime hp', hp);
if(hp>0) io.emit('hp line width', x);
if(hp<=0) io.emit('hp line width', 0);
if(hp+user_hp <= 0) {
io.emit('draw');
}
});
socket.on('hp line for all', function(data) {
x = hp/maxhp*100;
io.emit('realtime hp', hp);
io.emit('hp line width', x);
if(hp<=0) {
io.emit('hp line width', 0);
io.emit('win');
}
if(hp+user_hp <= 0) {
io.emit('draw');
}
});
//end
var max_user_hp = 30000,user_hp,x2,email,nick;
//Хп игрока
socket.on('user fight', function(data) {
user_hp = data;
if(user_hp<=0) {
user_hp = max_user_hp;
socket.emit('lose');
}
x2 = user_hp/max_user_hp*100;
socket.emit('user hp', user_hp);
if(user_hp>0) socket.emit('user hp line width', x2);
if(user_hp<=0) socket.emit('user hp line width', 0);
if(hp+user_hp <= 0) {
socket.emit('draw');
}
});
//io.sockets.connected[socket.id].emit('class', 'alert');
socket.on('hp line for all', function(data) {
x2 = user_hp/max_user_hp*100;
socket.emit('user hp line width', x2);
if(user_hp<=0) {
socket.emit('user hp line width', 0);
socket.emit('lose');
}
if(hp+user_hp <= 0) {
socket.emit('draw');
}
});
//end
//Чат
socket.on('chat message', function(data) {
msg = data;
io.emit('chat msg',msg);
});
//end
//Блокировка пользователей
socket.on('banned', function(data) {
var ban = '';
if(data == ban) {
io.emit('ban', 'Вы забанены!');
socket.disconnect(true);
}
});
//end
//Регистрация
socket.on('email', function(data) {
email = data;
});
socket.on('nick', function(data) {
nick = data;
console.log(data);
});
socket.on('query', function(data) {
db.query("INSERT INTO players (id,nickname,email) VALUES('','" + nick + "','" + email + "')", (err,result) => {
if(err) console.log(err);
console.log("Insert");
});
});
//end
});