Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //socket move function
  2. //pass in the socket session and data
  3. export function move(io, data, socket, bucket) {
  4.     if (data.direction === "null") {
  5.         return false;
  6.     } //if no direction, ignore this function
  7.     //destructure map collection movement
  8.     let [x=0,y=0] = movement[data.direction];
  9.  
  10.     //check for map boundaries
  11.     if (socket.session.player.x + x > 100 || socket.session.player.x + x < 1) x = 0;
  12.     if (socket.session.player.y + y > 100 || socket.session.player.y + y < 1) y = 0;
  13.    
  14.     //apply modification to player object
  15.     socket.session.player.x += x;
  16.     socket.session.player.y += y;
  17.  
  18.     client.hmset(socket.session.player.id, socket.session.player);
  19.  
  20.     client.smembers("game-1", (err, reply) => {
  21.         console.log("game keys", reply);
  22.         reply.map(x => {
  23.         client.hgetall(x, (err, reply) => {
  24.                 console.log("reply",reply);
  25.                 const msg = `${reply.user} is on ${reply.x}, ${reply.y}`;
  26.                 console.log("location emit", msg);
  27.                 io.sockets.emit("locations", {location:msg});
  28.             });
  29.         });
  30.     });
  31.  
  32.     //string to return to client for event log
  33.     const string = `${socket.session.player.user} has moved ${data.direction} to [${socket.session.player.x},${socket.session.player.y}]`;
  34.     console.log(string);
  35.     io.sockets.emit("somethingelse", {msg:string});
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement