Advertisement
az4521

Discord Pong

Jan 5th, 2018
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var script = document.createElement('script');
  2. script.type = 'text/javascript';
  3. script.src = 'https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js';
  4. document.head.appendChild(script);
  5.  
  6. String.prototype.count=function(s1) {
  7.     return (this.length - this.replace(new RegExp(s1,"g"), '').length) / s1.length;
  8. }
  9.  
  10. var token = eval(document.body.appendChild(document.createElement("iframe")).contentWindow.localStorage.token);
  11.  
  12. seq = null;
  13. ackd= true;
  14. pongchannel= null;
  15. userid = null;
  16. room=null;
  17. server=null;
  18. ballx=320;bally=290;ballxspeed=0;ballyspeed=0;p1y=250;p2y=250;p1s=0;p2s=0;keys_down=[0,0];
  19. botid="388844842182246411";
  20.  
  21. websocket = new WebSocket("wss://gateway.discord.gg/?v=6&encoding=json");
  22. console.log(websocket);
  23. websocket.onmessage= function(message){
  24.     data=JSON.parse(message.data);
  25.     if (data.op == 0){
  26.         seq=data.s;
  27.         if (data.t=="READY") {
  28.             userid = data.d.user.id;
  29.         }
  30.         else if (data.t=="MESSAGE_CREATE") {
  31.             if (data.d.author.id == userid && data.d.content == "!pong") {
  32.                 pongchannel = data.d.channel_id;
  33.             }
  34.             else if (pongchannel != null && data.d.channel_id == pongchannel) {
  35.                 if (data.d.author.id == botid && data.d.content.startsWith("<@"+userid+"> PONG _")){
  36.                     server = data.d.content.replace("<@"+userid+"> PONG _","").indexOf(" _");
  37.                     room = data.d.content.count(" ")-server-4;
  38.                     console.log(room);console.log(server);
  39.                     socket=io('https://lolur.ga:4009');socket.on("connect", function(){socket.emit("join", {"room":room});pong_add();pong_animate(pong_update);})
  40.                 }
  41.             }
  42.         }
  43.     }
  44.     else if (data.op == 1){
  45.         websocket.send(JSON.stringify({op:1, d:seq}));
  46.     }
  47.     else if (data.op == 10){
  48.         websocket.send(JSON.stringify({"op":2, "d":{
  49.             "token": token,
  50.             "properties": {
  51.                 "$os": "windows",
  52.                 "$browser": "pong.js",
  53.                 "$device": "pong.js"
  54.             },
  55.             "compress": false,
  56.             "large_threshold": 250,
  57.             "shard": [0, 1],
  58.             "presence": {
  59.                 "game": {
  60.                     "name": "Pong",
  61.                     "type": 0
  62.                 },
  63.                 "status": "online",
  64.                 "since": null,
  65.                 "afk": false
  66.             }
  67.         }}));
  68.         heartbeat = setInterval(function(){
  69.             if (ackd == false) {
  70.                 websocket.close();
  71.             } else {
  72.                 ackd=false;
  73.                 websocket.send(JSON.stringify({op:1, d:seq}));
  74.             }
  75.         }, data.d.heartbeat_interval);
  76.     }
  77.     else if (data.op==11) {
  78.         ackd=true;
  79.     }
  80. }
  81. websocket.onclose= function() {
  82.     clearInterval(heartbeat);
  83. }
  84. function pong_add(){
  85.     //var canv = $("<canvas id='pong' width=640 height=480 style='width:100%'></canvas>");
  86.     canv= document.createElement("canvas");
  87.     canv.id="pong";
  88.     canv.width=640;
  89.     canv.height=480;
  90.     canv.style="width:100%";
  91.     el = document.querySelector("#pong");
  92.     if (el){el.parentNode.removeChild(el);}
  93.     parent=document.querySelector(".channels-3g2vYe.vertical-3X17r5.flex-3B1Tl4.directionColumn-2h-LPR");
  94.     parent.insertBefore(canv, parent.firstChild);
  95. }
  96. function pong_remove(){
  97.     el = document.querySelector("#pong");
  98.     if (el){
  99.         el.parentNode.removeChild(el);
  100.     }
  101.     socket.close()
  102. }
  103. function pong_draw(b_x,b_y,p1_y,p2_y,p1_s,p2_s){
  104.     canvas=document.getElementById("pong");
  105.     canvas.tabIndex = 1;
  106.     ctx=canvas.getContext("2d");
  107.     ctx.font="90px Arial";
  108.     ctx.fillStyle="black";
  109.     ctx.fillRect(0,0,640,480);
  110.     ctx.fillStyle="white";
  111.     ctx.fillRect(20,p1_y,20,80);
  112.     ctx.fillRect(600,p2_y,20,80);
  113.     ctx.fillRect(b_x-10,b_y-10,20,20);
  114.     ctx.fillRect(0,80,640,20);
  115.     ctx.fillText(" "+p1_s+"                "+p2_s,40,70);
  116.     for(i=0;i<5;i++){
  117.         ctx.fillRect(315,i*80+111,10,40);
  118.     };
  119. };
  120.  
  121. pong_animate=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(callback){window.setTimeout(callback, 1000/60)};
  122.  
  123. function pong_update(){ //key controller
  124.     document.onkeydown=function(e){switch(e.keyCode){
  125.             case 87:
  126.                 keys_down[0]=1;break;
  127.             case 83:
  128.                 keys_down[1]=1;break;}}
  129.     document.onkeyup=function(e){switch(e.keyCode){
  130.             case 87:
  131.                 keys_down[0]=0;break;
  132.             case 83:
  133.                 keys_down[1]=0;break;}}
  134.     if(keys_down[0]==1){p1y-=4;}
  135.     if(keys_down[1]==1){p1y+=4;}
  136.     if (p1y<100){p1y=100;}
  137.     if (p1y>400){p1y=400;}
  138.     if(!server){//client commands
  139.         if (keys_down[0]||keys_down[1]){
  140.             socket.emit("event",{data:"cy"+p1y});
  141.         }
  142.         socket.on("event",function(data){
  143.             if(data.data.indexOf("sy")===0){
  144.                 p2y=parseInt(data.data.replace("sy",""));
  145.             }
  146.             else if(data.data.indexOf("bx")===0){
  147.                 ballx=640-parseInt(data.data.replace("bx",""));
  148.             }
  149.             else if(data.data.indexOf("by")===0){
  150.                 bally=parseInt(data.data.replace("by",""));
  151.             }
  152.             else if(data.data.indexOf("ss")===0){
  153.                 p1s=parseInt(data.data.replace("ss",""));
  154.             }
  155.             else if(data.data.indexOf("cs")===0){
  156.                 p2s=parseInt(data.data.replace("cs",""));
  157.             }
  158.         });
  159.     } else {
  160.         //server commands
  161.         if (bally>=470||bally<=110){ballyspeed*=-1;}
  162.         ballx+=ballxspeed
  163.         bally+=ballyspeed
  164.         if (ballx<50 && bally>=p1y && bally<=p1y+80 && ballxspeed<0){ballxspeed*=-1;ballyspeed=((bally-p1y)/8)-5}
  165.         if (ballx>590 && bally>=p2y && bally<=(p2y+80) && ballxspeed>0){ballxspeed*=-1;ballyspeed=((bally-p2y)/8)-5}
  166.         socket.emit("event",{data:"bx"+(ballx)});socket.emit("event",{data:"by"+(bally)});
  167.         if (!(ballxspeed||ballyspeed)){
  168.             ballxspeed = Math.random() < 0.5 ? 1*4 : -1*4;
  169.             ballyspeed = 1.6*(Math.floor(Math.random()*(3+3+1))-3 );
  170.             socket.emit("event",{data:"bx"+(ballx)});socket.emit("event",{data:"by"+(bally)});
  171.         }
  172.         if (ballx>650){ballx=320;bally=290;ballxspeed=0;ballyspeed=0;p1s+=1;socket.emit("event",{data:"cs"+p1s});}
  173.         if (ballx<-10){ballx=320;bally=290;ballxspeed=0;ballyspeed=0;p2s+=1;socket.emit("event",{data:"ss"+p2s});}
  174.         if(keys_down[0]||keys_down[1]){
  175.             socket.emit("event",{data:"sy"+p1y});
  176.         }
  177.         socket.on("event",function(data){
  178.             if(data.data.indexOf("cy")===0){
  179.                 p2y=parseInt(data.data.replace("cy",""));
  180.             }
  181.         });
  182.     }
  183.     pong_draw(ballx,bally,p1y,p2y,p1s,p2s);
  184.     pong_animate(pong_update);
  185.     if (p1s>10){alert("You Win");ballx=320;bally=290;ballxspeed=0;ballyspeed=0;p1y=250;p2y=250;p1s=0;p2s=0;keys_down=[0,0];pong_remove()}
  186.         if (p2s>10){alert("You Lose");ballx=320;bally=290;ballxspeed=0;ballyspeed=0;p1y=250;p2y=250;p1s=0;p2s=0;keys_down=[0,0];pong_remove()}
  187. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement