hello173

old Boxors.io server

Apr 15th, 2022
1,343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var WebSocket = require("ws");
  2. var port = process.env.PORT; // don't change
  3.  
  4. var wss = new WebSocket.Server({
  5.   port: port
  6. });
  7.  
  8. var userCount=0
  9. var IPList=[]
  10.  
  11. var levels = require("./levels.json");
  12. var levelsRender = require("./levelsr.json");
  13. var ids = 1;
  14. var players = [
  15.   [],
  16.   [],
  17.   [],
  18.   [],
  19.   [],
  20.   [],
  21.   [],
  22.   [],
  23.   [],
  24.   []
  25. ];
  26. function nextLvl(ws,rqLvl,rqX,rqY){
  27.   players[ws.level][ws.id-1]=null
  28.         ws.level=rqLvl
  29.         ws.send(
  30.           JSON.stringify({
  31.             kind: "levelRender",
  32.             collisions: levels[ws.level],
  33.             render: levelsRender[ws.level],
  34.             "players":players[ws.level]
  35.           })
  36.         );
  37.         ws.posX=rqX;
  38.         ws.posY=rqY;
  39.         ws.send(JSON.stringify({
  40.           "kind":"clientPosUpdate",
  41.           "data":[ws.posX,ws.posY]
  42.         }));
  43.         wss.clients.forEach(function each(client) {
  44.           client.send(JSON.stringify({kind: "disconnection", id: ws.id}));
  45.         });
  46. }
  47. wss.on("connection", function(ws, req) {
  48.   if(IPList.includes(req.headers['x-forwarded-for'].split(",")[0])){/*
  49.     ws.send(JSON.stringify({
  50.       "kind":"error",
  51.       "message":"You have reached the IP limit."
  52.     }));
  53.     console.log("someone tried to have 2 connections.")
  54.     ws.close();
  55.     return*/
  56.   };
  57.   IPList[ids]=req.headers['x-forwarded-for'].split(",")[0];
  58.   ws.level = 0;
  59.   ws.posX = 400;
  60.   ws.posY = 300;
  61.   ws.id = ids;
  62.   ids++;
  63.   ws.clickRegulator=true
  64.   wss.clients.forEach(function each(client) {
  65.     if(client!=ws && client.level==0){
  66.       client.send(JSON.stringify({ kind: "userMove", id: ws.id, X: ws.posX, Y: ws.posY }));
  67.     }
  68.   });
  69.   ws.on("error", function(err) {});
  70.   ws.on("close", function(cls) {
  71.     userCount-=1;
  72.     IPList[ws.id]=undefined
  73.     wss.clients.forEach(function each(client) {
  74.       client.send(JSON.stringify({kind: "disconnection", id: ws.id}));
  75.       client.send(JSON.stringify({"kind":"userCountUpdate","count":userCount}));
  76.     });
  77.     players[ws.level][ws.id-1]=null
  78.   });
  79.   ws.on("message", async function(msg) {
  80.     var data = JSON.parse(String(msg));
  81.     if (data.kind == "move") {
  82.       switch (data.direction) {
  83.         case "left":
  84.           var cl=ws.posX-20
  85.           if (ws.posX > 0 && !levels[ws.level].includes(cl+","+ws.posY)) {ws.posX -= 20};
  86.           break;
  87.         case "up":
  88.           var cu=ws.posY-20
  89.           if (ws.posY > 0 && !levels[ws.level].includes(ws.posX+","+cu)) {ws.posY -= 20};
  90.           break;
  91.         case "right":
  92.           var cr=ws.posX+20
  93.           if (ws.posX < 780 && !levels[ws.level].includes(cr+","+ws.posY)) {ws.posX += 20};
  94.           break;
  95.         case "down":
  96.           var cd=ws.posY+20
  97.           if (ws.posY < 580 && !levels[ws.level].includes(ws.posX+","+cd)) {ws.posY += 20};
  98.           break;
  99.       };
  100.       if(ws.posX<0){ws.posX=0} else if(ws.posY<0){ws.posY=0} else if(ws.posX>780){ws.posX=780} else if(ws.posY>580){ws.posY=580}
  101.       //destinations
  102.       if(ws.posX>599 && ws.posX<681 && ws.posY>339 && ws.posY<441 && ws.level==0){
  103.         nextLvl(ws,1,400,300)
  104.       };
  105.       if(ws.posX>-1 && ws.posX<61 && ws.posY>-1 && ws.posY<61 && ws.level==0){
  106.         nextLvl(ws,9,400,300)
  107.       };
  108.       if(ws.posX>-1 && ws.posX<41 && ws.posY>-1 && ws.posY<41 && ws.level==9){
  109.         nextLvl(ws,1,400,300)
  110.       };
  111.       if(ws.posX>-1 && ws.posX<61 && ws.posY>-1 && ws.posY<61 && ws.level==1){
  112.         nextLvl(ws,2,400,300)
  113.       };
  114.       if(ws.posX>719 && ws.posX<781 && ws.posY>-1 && ws.posY<61 && ws.level==2){
  115.         nextLvl(ws,0,80,0)
  116.       };
  117.       wss.clients.forEach(function each(client) {
  118.         if(client!=ws && client.level==ws.level){
  119.           client.send(JSON.stringify({ kind: "userMove", id: ws.id, X: ws.posX, Y: ws.posY }));
  120.         }
  121.       });
  122.       players[ws.level][ws.id-1]=[ws.posX,ws.posY,ws.id]
  123.       ws.send(JSON.stringify({kind:"clientMove", id: ws.id, X: ws.posX, Y: ws.posY}))
  124.       if(true){}
  125.     };
  126.     if(data.kind == "click" && ws.clickRegulator){
  127.       wss.clients.forEach(function each(client) {
  128.         if(client.level==ws.level){
  129.           client.send(JSON.stringify({ kind: "click", X: ws.posX, Y: ws.posY }));
  130.         }
  131.       });
  132.       if(ws.posX==400 && ws.posY==300 && ws.level==9){
  133.         levelsRender[ws.level]["button"][0][4]--;
  134.         wss.clients.forEach(function each(client) {
  135.           if(client.level==ws.level){
  136.             client.send(
  137.               JSON.stringify({
  138.                 kind: "levelRender",
  139.                 collisions: levels[ws.level],
  140.                 render: levelsRender[ws.level],
  141.                 "players":players[client.level]
  142.               })
  143.             );
  144.           }
  145.         });
  146.       };
  147.       ws.clickRegulator=false;
  148.       setTimeout(function(){ws.clickRegulator=true},250)
  149.     }
  150.   });
  151.   userCount+=1;
  152.   wss.clients.forEach(function each(client) {
  153.     client.send(JSON.stringify({
  154.       "kind":"userCountUpdate",
  155.       "count":userCount
  156.     }));
  157.   });
  158.   ws.send(JSON.stringify({
  159.     "kind":"clientPosUpdate",
  160.     "data":[ws.posX,ws.posY]
  161.   }));
  162.   ws.send(JSON.stringify({ kind: "id", id: ws.id }));
  163.   ws.send(
  164.     JSON.stringify({
  165.       kind: "levelRender",
  166.       collisions: levels[ws.level],
  167.       render: levelsRender[ws.level],
  168.       "players":players[ws.level]
  169.     })
  170.   );
  171. });
Advertisement
Add Comment
Please, Sign In to add comment