Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var app = require('express')();
- var http = require('http').Server(app);
- var io = require('socket.io')(http);
- var mongoose = require('mongoose')
- mongoose.connect('mongodb://localhost/IP')
- var User = mongoose.model('User', {
- username: String,
- password: String,
- currentGoal: Object, //Coord
- visitedCoord: []
- })
- var Coord = mongoose.model('Coord', {
- latitude: Number,
- longitude: String,
- ID: Number
- })
- io.on('connection', function(socket){
- var user;
- var sessionID;
- socket.on('LOGIN', function(email, passwordC){
- User.find({
- username: email,
- password: passwordC
- }, function (err, users) {
- if (err)
- {
- console.log(res.send('Error ' + err));
- return;
- }
- if(!users || users.length!=1)
- {
- socket.emit('FAIL');
- return;
- }
- user=email;
- sessionID=Math.random();
- socket.emit('SUCCESS', sessionID);
- //start of normal connection
- socket.on('NAME', function(playername){
- console.log("" + playername + "connected.");
- user = playername;
- socket.emit('GOAL', '####coordinates of goal');
- console.log("Sending GOAL to" + user);
- });
- socket.on('POSITION', function(coord){
- console.log(user + "is sending POSITION.");
- if(true /*coordinates match*/)
- {
- socket.emit('SUCCESS', '####coordinates of reached goal + score');
- console.log("Sending SUCCESS to" + user);
- }
- });
- socket.on('GETGOALS', function(){
- console.log(user + "is sending GETGOALS.");
- socket.emit('GOAL', '####coordinates of goal');
- console.log("Sending GOAL to" + user);
- });
- });
- });
- socket.on('REGISTER', function(email, passwordC){
- User.find({
- username: email,
- password: passwordC
- }, function (err, users) {
- if (err)
- {
- console.log(res.send('Error ' + err));
- return;
- }
- if(!users || users.length!=0)
- {
- socket.emit('FAIL');
- return;
- }
- user=email;
- sessionID=Math.random();
- socket.emit('SUCCESS', sessionID);
- //start of normal connection
- socket.on('NAME', function(playername){
- console.log("" + playername + "connected.");
- user = playername;
- socket.emit('GOAL', '####coordinates of goal');
- console.log("Sending GOAL to" + user);
- });
- socket.on('POSITION', function(coord){
- console.log(user + "is sending POSITION.");
- if(true /*coordinates match*/)
- {
- socket.emit('SUCCESS', '####coordinates of reached goal + score');
- console.log("Sending SUCCESS to" + user);
- }
- });
- socket.on('GETGOALS', function(){
- console.log(user + "is sending GETGOALS.");
- socket.emit('GOAL', '####coordinates of goal');
- console.log("Sending GOAL to" + user);
- });
- });
- });
- });
- http.listen(3000, function(){
- console.log('listening on *:3000');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement