Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var script = document.createElement('script');
- script.type = 'text/javascript';
- script.src = 'https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.slim.js';
- document.head.appendChild(script);
- String.prototype.count=function(s1) {
- return (this.length - this.replace(new RegExp(s1,"g"), '').length) / s1.length;
- }
- var token = eval(document.body.appendChild(document.createElement("iframe")).contentWindow.localStorage.token);
- seq = null;
- ackd= true;
- pongchannel= null;
- userid = null;
- room=null;
- server=null;
- ballx=320;bally=290;ballxspeed=0;ballyspeed=0;p1y=250;p2y=250;p1s=0;p2s=0;keys_down=[0,0];
- botid="388844842182246411";
- websocket = new WebSocket("wss://gateway.discord.gg/?v=6&encoding=json");
- console.log(websocket);
- websocket.onmessage= function(message){
- data=JSON.parse(message.data);
- if (data.op == 0){
- seq=data.s;
- if (data.t=="READY") {
- userid = data.d.user.id;
- }
- else if (data.t=="MESSAGE_CREATE") {
- if (data.d.author.id == userid && data.d.content == "!pong") {
- pongchannel = data.d.channel_id;
- }
- else if (pongchannel != null && data.d.channel_id == pongchannel) {
- if (data.d.author.id == botid && data.d.content.startsWith("<@"+userid+"> PONG _")){
- server = data.d.content.replace("<@"+userid+"> PONG _","").indexOf(" _");
- room = data.d.content.count(" ")-server-4;
- console.log(room);console.log(server);
- socket=io('https://lolur.ga:4009');socket.on("connect", function(){socket.emit("join", {"room":room});pong_add();pong_animate(pong_update);})
- }
- }
- }
- }
- else if (data.op == 1){
- websocket.send(JSON.stringify({op:1, d:seq}));
- }
- else if (data.op == 10){
- websocket.send(JSON.stringify({"op":2, "d":{
- "token": token,
- "properties": {
- "$os": "windows",
- "$browser": "pong.js",
- "$device": "pong.js"
- },
- "compress": false,
- "large_threshold": 250,
- "shard": [0, 1],
- "presence": {
- "game": {
- "name": "Pong",
- "type": 0
- },
- "status": "online",
- "since": null,
- "afk": false
- }
- }}));
- heartbeat = setInterval(function(){
- if (ackd == false) {
- websocket.close();
- } else {
- ackd=false;
- websocket.send(JSON.stringify({op:1, d:seq}));
- }
- }, data.d.heartbeat_interval);
- }
- else if (data.op==11) {
- ackd=true;
- }
- }
- websocket.onclose= function() {
- clearInterval(heartbeat);
- }
- function pong_add(){
- //var canv = $("<canvas id='pong' width=640 height=480 style='width:100%'></canvas>");
- canv= document.createElement("canvas");
- canv.id="pong";
- canv.width=640;
- canv.height=480;
- canv.style="width:100%";
- el = document.querySelector("#pong");
- if (el){el.parentNode.removeChild(el);}
- parent=document.querySelector(".channels-3g2vYe.vertical-3X17r5.flex-3B1Tl4.directionColumn-2h-LPR");
- parent.insertBefore(canv, parent.firstChild);
- }
- function pong_remove(){
- el = document.querySelector("#pong");
- if (el){
- el.parentNode.removeChild(el);
- }
- socket.close()
- }
- function pong_draw(b_x,b_y,p1_y,p2_y,p1_s,p2_s){
- canvas=document.getElementById("pong");
- canvas.tabIndex = 1;
- ctx=canvas.getContext("2d");
- ctx.font="90px Arial";
- ctx.fillStyle="black";
- ctx.fillRect(0,0,640,480);
- ctx.fillStyle="white";
- ctx.fillRect(20,p1_y,20,80);
- ctx.fillRect(600,p2_y,20,80);
- ctx.fillRect(b_x-10,b_y-10,20,20);
- ctx.fillRect(0,80,640,20);
- ctx.fillText(" "+p1_s+" "+p2_s,40,70);
- for(i=0;i<5;i++){
- ctx.fillRect(315,i*80+111,10,40);
- };
- };
- pong_animate=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(callback){window.setTimeout(callback, 1000/60)};
- function pong_update(){ //key controller
- document.onkeydown=function(e){switch(e.keyCode){
- case 87:
- keys_down[0]=1;break;
- case 83:
- keys_down[1]=1;break;}}
- document.onkeyup=function(e){switch(e.keyCode){
- case 87:
- keys_down[0]=0;break;
- case 83:
- keys_down[1]=0;break;}}
- if(keys_down[0]==1){p1y-=4;}
- if(keys_down[1]==1){p1y+=4;}
- if (p1y<100){p1y=100;}
- if (p1y>400){p1y=400;}
- if(!server){//client commands
- if (keys_down[0]||keys_down[1]){
- socket.emit("event",{data:"cy"+p1y});
- }
- socket.on("event",function(data){
- if(data.data.indexOf("sy")===0){
- p2y=parseInt(data.data.replace("sy",""));
- }
- else if(data.data.indexOf("bx")===0){
- ballx=640-parseInt(data.data.replace("bx",""));
- }
- else if(data.data.indexOf("by")===0){
- bally=parseInt(data.data.replace("by",""));
- }
- else if(data.data.indexOf("ss")===0){
- p1s=parseInt(data.data.replace("ss",""));
- }
- else if(data.data.indexOf("cs")===0){
- p2s=parseInt(data.data.replace("cs",""));
- }
- });
- } else {
- //server commands
- if (bally>=470||bally<=110){ballyspeed*=-1;}
- ballx+=ballxspeed
- bally+=ballyspeed
- if (ballx<50 && bally>=p1y && bally<=p1y+80 && ballxspeed<0){ballxspeed*=-1;ballyspeed=((bally-p1y)/8)-5}
- if (ballx>590 && bally>=p2y && bally<=(p2y+80) && ballxspeed>0){ballxspeed*=-1;ballyspeed=((bally-p2y)/8)-5}
- socket.emit("event",{data:"bx"+(ballx)});socket.emit("event",{data:"by"+(bally)});
- if (!(ballxspeed||ballyspeed)){
- ballxspeed = Math.random() < 0.5 ? 1*4 : -1*4;
- ballyspeed = 1.6*(Math.floor(Math.random()*(3+3+1))-3 );
- socket.emit("event",{data:"bx"+(ballx)});socket.emit("event",{data:"by"+(bally)});
- }
- if (ballx>650){ballx=320;bally=290;ballxspeed=0;ballyspeed=0;p1s+=1;socket.emit("event",{data:"cs"+p1s});}
- if (ballx<-10){ballx=320;bally=290;ballxspeed=0;ballyspeed=0;p2s+=1;socket.emit("event",{data:"ss"+p2s});}
- if(keys_down[0]||keys_down[1]){
- socket.emit("event",{data:"sy"+p1y});
- }
- socket.on("event",function(data){
- if(data.data.indexOf("cy")===0){
- p2y=parseInt(data.data.replace("cy",""));
- }
- });
- }
- pong_draw(ballx,bally,p1y,p2y,p1s,p2s);
- pong_animate(pong_update);
- 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()}
- 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()}
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement