Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //socket move function
- //pass in the socket session and data
- export function move(io, data, socket, bucket) {
- if (data.direction === "null") {
- return false;
- } //if no direction, ignore this function
- //destructure map collection movement
- let [x=0,y=0] = movement[data.direction];
- //check for map boundaries
- if (socket.session.player.x + x > 100 || socket.session.player.x + x < 1) x = 0;
- if (socket.session.player.y + y > 100 || socket.session.player.y + y < 1) y = 0;
- //apply modification to player object
- socket.session.player.x += x;
- socket.session.player.y += y;
- client.hmset(socket.session.player.id, socket.session.player);
- client.smembers("game-1", (err, reply) => {
- console.log("game keys", reply);
- reply.map(x => {
- client.hgetall(x, (err, reply) => {
- console.log("reply",reply);
- const msg = `${reply.user} is on ${reply.x}, ${reply.y}`;
- console.log("location emit", msg);
- io.sockets.emit("locations", {location:msg});
- });
- });
- });
- //string to return to client for event log
- const string = `${socket.session.player.user} has moved ${data.direction} to [${socket.session.player.x},${socket.session.player.y}]`;
- console.log(string);
- io.sockets.emit("somethingelse", {msg:string});
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement