Commit d024a537 authored by Manish Kumar's avatar Manish Kumar
Browse files

removing old backend

parent c6859866
const mysql = require('mysql');
var con = mysql.createConnection({
host: "10.25.10.21",
user: "g2",
password: "qP1r7rnfIybWdHKk",
database: "g2"
});
con.connect(function(err) {
if (err) throw err;
console.log('Connected!');
});
const express = require('express');
const app = express();
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:4200");
res.header("Access-Control-Allow-Headers", "Origin, X-RequestedWith, Content-Type, Accept");
next();
});
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
/*app.post('/res', function (req, res) {
var postData = req.body;
con.query('INSERT INTO Pokemon SET ?', postData,
function (error, results, fields) {
if (error) throw error;
res.send(results);
});
});*/
var session = require('express-session');
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true,
maxAge: Date.now() + 30
}));
app.all('/*', function(req, res, next) {
res.set('Access-Control-Allow-Credentials', 'true');
res.set('Access-Control-Allow-Origin', 'http://localhost:4200');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers');
next();
});
app.get('/allpokemon', function(req, res) {
con.query('SELECT * FROM pokemon',
(err, results) => {
if (err) throw err;
res.send(results);
});
})
app.get('/allpokemon', function(req, res) {
con.query('SELECT * FROM pokemon',
(err, results) => {
if (err) throw err;
res.send(results);
});
})
app.get('/getteam', function(request, response) {
console.log(request.session.loggedIn);
if (request.session.loggedIn) {
con.query('SELECT p.* FROM pokemon as p, team WHERE id_joueur= ? AND p.id_pokemon=team.id_pokemon', [request.session.uid, ], function(error, results, fields) {
if (error) throw error;
response.send({ res: true, body: results });
});
} else {
response.send({ res: false, body: 'You must be logged in to manage a team !' });
response.end();
}
})
app.post('/addpokemon', function(request, response) {
console.log(request.session.loggedIn);
if (request.session.loggedIn) {
con.query('INSERT INTO team (courant, id_joueur, id_pokemon) SELECT true, id_joueur, ? FROM joueur WHERE email= ?', [request.body.id_pokemon, request.session.username], function(error, results, fields) {
response.send({ res: true, body: 'Pokemon added to team !' });
response.end();
});
} else {
response.send({ res: false, body: 'You must be logged in to manage a team !' });
response.end();
}
});
app.post('/removepokemon', function(request, response) {
console.log(request.session.loggedIn);
if (request.session.loggedIn) {
con.query('DELETE FROM team WHERE id_joueur= ? AND id_pokemon= ? LIMIT 1', [request.session.uid, request.body.id_pokemon], function(error, results, fields) {
response.send({ res: true, body: 'Pokemon removed from team !' });
response.end();
});
} else {
response.send({ res: false, body: 'You must be logged in to manage a team !' });
response.end();
}
});
app.post('/auth', function(request, response) {
var username = request.body.email;
var password = request.body.mdp;
console.log(request.session.loggedIn);
if (username && password) {
con.query('SELECT pseudo, id_joueur FROM joueur WHERE email = ? AND mdp = ?', [username, password], function(error, results, fields) {
if (results.length > 0) {
request.session.loggedIn = true;
request.session.username = username;
request.session.pseudo = results[0]['pseudo'];
request.session.uid = results[0]['id_joueur'];
console.log(request.session.uid);
console.log(request.sessionID);
response.send({ res: true, body: results[0]['pseudo'] });
} else {
response.send({ res: false, body: 'Incorrect Username and/or Password!' });
}
response.end();
});
} else {
response.send({ res: false, body: 'Please enter Username and Password!' });
response.end();
}
});
app.get('/checkLogin', function(request, response) {
if (request.session.loggedIn) {
response.send({ res: true, body: request.session.pseudo });
} else {
response.send({ res: false, body: 'You are not logged in' });
console.log(request.sessionID);
}
response.end();
});
app.listen(3001, function() {
console.log('Example app listening on port 3000! ')
})
var connectedUsers = [];
const http = require('http')
const socketIO = require('socket.io')
const server = http.createServer(app)
const io = socketIO(server)
var numUsers = 0;
io.on('connection', socket => {
console.log('New client connected')
var addedUser = false;
socket.on('add user', (username) => {
if (addedUser) return;
console.log(username);
// we store the username in the socket session for this client
socket.username = username;
++numUsers;
addedUser = true;
connectedUsers.push({
id: socket.id,
pseudo: username
});
console.log(connectedUsers);
console.log(connectedUsers[0]);
socket.emit('send list', connectedUsers);
// echo globally (all clients) that a person has connected
io.emit('user joined', {
username: username,
numUsers: numUsers
});
});
socket.on('new-message', (message) => {
io.emit('message', { text: message });
console.log(message);
});
socket.on('disconnect', () => {
console.log('user disconnected')
})
});
server.listen(3000, function() {
console.log('comm listening on port 3001! ');
})
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment