Advertisement
Guest User

Untitled

a guest
Feb 25th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. var app = require('express')();
  3. var http = require('http').Server(app);
  4. var io = require('socket.io')(http);
  5. var mongoose = require('mongoose')
  6. mongoose.connect('mongodb://localhost/IP')
  7.  
  8. var User = mongoose.model('User', {
  9.   username: String,
  10.   password: String,
  11.   currentGoal: Object, //Coord
  12.   visitedCoord: []
  13. })
  14.  
  15. var Coord = mongoose.model('Coord', {
  16.     latitude: Number,
  17.     longitude: String,
  18.     ID: Number
  19. })
  20.  
  21. io.on('connection', function(socket){
  22.     var user;
  23.     var sessionID;
  24.  
  25.   socket.on('LOGIN', function(email, passwordC){
  26.     User.find({
  27.         username: email,
  28.         password: passwordC
  29.        }, function (err, users) {
  30.             if (err)
  31.                 {
  32.                     console.log(res.send('Error ' + err));
  33.                     return;
  34.                 }
  35.             if(!users || users.length!=1)
  36.                 {
  37.                     socket.emit('FAIL');
  38.                     return;
  39.                 }
  40.             user=email;
  41.             sessionID=Math.random();
  42.             socket.emit('SUCCESS', sessionID);
  43.  
  44.             //start of normal connection
  45.  
  46.             socket.on('NAME', function(playername){
  47.                 console.log("" + playername + "connected.");
  48.                 user = playername;
  49.                 socket.emit('GOAL', '####coordinates of goal');
  50.                 console.log("Sending GOAL to" + user);
  51.               });
  52.             socket.on('POSITION', function(coord){
  53.                 console.log(user + "is sending POSITION.");
  54.                 if(true /*coordinates match*/)
  55.                 {
  56.                 socket.emit('SUCCESS', '####coordinates of reached goal + score');
  57.                 console.log("Sending SUCCESS to" + user);
  58.                 }
  59.               });
  60.             socket.on('GETGOALS', function(){
  61.                 console.log(user + "is sending GETGOALS.");
  62.                 socket.emit('GOAL', '####coordinates of goal');
  63.                 console.log("Sending GOAL to" + user);
  64.               });
  65.  
  66.         });
  67.   });
  68.   socket.on('REGISTER', function(email, passwordC){
  69.     User.find({
  70.         username: email,
  71.         password: passwordC
  72.        }, function (err, users) {
  73.             if (err)
  74.                 {
  75.                     console.log(res.send('Error ' + err));
  76.                     return;
  77.                 }
  78.             if(!users || users.length!=0)
  79.                 {
  80.                     socket.emit('FAIL');
  81.                     return;
  82.                 }
  83.             user=email;
  84.             sessionID=Math.random();
  85.             socket.emit('SUCCESS', sessionID);
  86.  
  87.             //start of normal connection
  88.            
  89.             socket.on('NAME', function(playername){
  90.                 console.log("" + playername + "connected.");
  91.                 user = playername;
  92.                 socket.emit('GOAL', '####coordinates of goal');
  93.                 console.log("Sending GOAL to" + user);
  94.               });
  95.             socket.on('POSITION', function(coord){
  96.                 console.log(user + "is sending POSITION.");
  97.                 if(true /*coordinates match*/)
  98.                 {
  99.                 socket.emit('SUCCESS', '####coordinates of reached goal + score');
  100.                 console.log("Sending SUCCESS to" + user);
  101.                 }
  102.               });
  103.             socket.on('GETGOALS', function(){
  104.                 console.log(user + "is sending GETGOALS.");
  105.                 socket.emit('GOAL', '####coordinates of goal');
  106.                 console.log("Sending GOAL to" + user);
  107.               });
  108.  
  109.         });
  110.   });
  111.  
  112. });
  113.  
  114. http.listen(3000, function(){
  115.   console.log('listening on *:3000');
  116. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement