Fooksie

emjack2.5

Aug 23rd, 2014
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name            EMjack
  3. // @version         2.5
  4. // @description     Some crap you may find useful.
  5. // @match           https://epicmafia.com/game/*
  6. // @match           http://userscripts.org/scripts/source/*
  7. // ==/UserScript==
  8.  
  9. //Welcome back
  10. function EMjack() {
  11.  
  12.     //public
  13.     window.ej={
  14.         name: "EMjack",
  15.         version: 2.5,
  16.         meta: new Object()
  17.         }
  18.     ej.fname=ej.name+ej.version;
  19.  
  20.     //variables
  21.     var afk=false,
  22.         meet=null,
  23.         master=null,
  24.         bubble=null,
  25.         roulette=null,
  26.         rouletteOn=null,
  27.         kicktimer=0,
  28.         antidc=false,
  29.         autokick=!ranked,
  30.         autobomb=false,
  31.         autowill=false,
  32.         auth=false,
  33.         morse=false,
  34.         users=new Object();
  35.     window.development=false;
  36.  
  37.     //setup
  38.     var sock=null,
  39.         cmds=$("#cmds").scope(),
  40.         log=function(a,classes) {
  41.             cmds.log(a,(classes||"")+" emjack");
  42.             }
  43.  
  44.     //avoid collision
  45.     if(!window.SockJS) {
  46.         log("EMjack disabled.");
  47.         log("Socket library changed. Again.");
  48.         return;
  49.         }
  50.  
  51.     //plug in
  52.     cmds.execute_cmds=function(initial) {
  53.         return function(pkg) {
  54.             initJack(pkg);
  55.             initial(pkg);
  56.             execJack(pkg);
  57.             };
  58.         }(cmds.execute_cmds);
  59.     SockJS.prototype._dispatchOpen=function(initial) {
  60.         return function() {
  61.             sock=this;
  62.             initial.apply(this,arguments);
  63.             }
  64.         }(SockJS.prototype._dispatchOpen);
  65.     SockJS.prototype._dispatchMessage=function(initial) {
  66.         return function() {
  67.             if(sock===null) {
  68.                 sock=this;
  69.                 }
  70.             initial.apply(this,arguments);
  71.             }
  72.         }(SockJS.prototype._dispatchMessage);
  73.  
  74.     //sock wrapper
  75.     var socket=new Object();
  76.  
  77.     //sock cmd
  78.     socket.cmd=function(cmd,data) {
  79.         sock.send(JSON.stringify([cmd,data]));
  80.         };
  81.  
  82.     //sock chat
  83.     socket.chat=function(message,direct) {
  84.         if(direct!==undefined) {
  85.             message="@"+direct+" "+message;
  86.             }
  87.         socket.cmd("<",{
  88.             meet:meet,
  89.             msg:message
  90.             });
  91.         };
  92.  
  93.     //sock vote
  94.     socket.vote=function(vote,meeting) {
  95.         socket.cmd("point",{
  96.             meet:meeting===undefined?meet:meeting,
  97.             target:vote
  98.             });
  99.         };
  100.  
  101.     //sock dc wrapper
  102.     socket.dcfunc=function(callback) {
  103.         cmds.redirect_back=callback;
  104.         socket.cmd("leave");
  105.         };
  106.  
  107.     //the management
  108.     function initJack(pkg) {
  109.         for(var i=0,cmd,data;i<pkg.length;i++) {
  110.             cmd=pkg[i][0];
  111.             data=pkg[i][1];
  112.             switch(cmd) {
  113.                 case "redirect":
  114.                     if(antidc) {
  115.                         pkg[i][0]=null;
  116.                         }
  117.                     break;
  118.                 }
  119.             }
  120.         }
  121.     function execJack(pkg) {
  122.         for(var i=0;i<pkg.length;i++) {
  123.             if(ej.cmd[pkg[i][0]]) {
  124.                 ej.cmd[pkg[i][0]].call(pkg[i][1],pkg[i][1]);
  125.                 }
  126.             }
  127.         }
  128.  
  129.     //morse code
  130.     ej.mor=function(user) {
  131.         if(ej.mor[user.morse.id]) {
  132.             ej.mor[user.morse.id](user,user.morse.code);
  133.             }
  134.         }
  135.     ej.mor.txt=function(id,code) {
  136.         return "["+Array(id+1).join(".")+" "+Array(code+1).join(".")+"]";
  137.         }
  138.     ej.mor.log=function(user,id,message) {
  139.         if(morse) {
  140.             if(!message) {
  141.                 message=user.morse.code;
  142.                 }
  143.             log("> "+user.name+" "+this.txt(user.morse.id,user.morse.code)+": "+id+": "+message);
  144.             }
  145.         }
  146.     ej.mor.send=function(id,code,message) {
  147.         socket.cmd("u");
  148.         for(var i=0;i<id;i++) {
  149.             socket.cmd("k");
  150.             }
  151.         socket.cmd("u");
  152.         for(var i=0;i<code;i++) {
  153.             socket.cmd("k");
  154.             }
  155.         socket.cmd("u");
  156.         if(morse) {
  157.             log("< * "+this.txt(id,code)+" "+(message||""));
  158.             }
  159.         }
  160.  
  161.     //morse script version
  162.     ej.mor[2]=function(user) {
  163.         this.log(user,"get version");
  164.         this.send(3,ej.version*10-20,"send version");
  165.         }
  166.     ej.mor[3]=function(user,code) {
  167.         this.log(user,"send version",2+code/10);
  168.         }
  169.  
  170.     //morse roll call
  171.     ej.mor[4]=function(user) {
  172.         this.log(user,"roll call");
  173.         }
  174.  
  175.     //standard cmds
  176.     ej.cmd={
  177.         "a": function() {
  178.             auth=true;
  179.             if(!options.fastgame) {
  180.                 socket.cmd("option",{
  181.                     field:"fastgame"
  182.                     });
  183.                 }
  184.             if(!options.nospectate) {
  185.                 socket.cmd("option",{
  186.                     field:"nospectate"
  187.                     });
  188.                 }
  189.             ej.mor.send(4,0,"roll call");
  190.             },
  191.         "o": function() {
  192.             if(this.data.lastwill&&autowill) {
  193.                 cmds.lastwill=user+"/"+u(user).role;
  194.                 socket.cmd("will",{
  195.                     msg:cmds.lastwill
  196.                     });
  197.                 }
  198.             },
  199.         "ro": function() {
  200.             for(var x in users) {
  201.                 users[x].voted=false;
  202.                 }
  203.             },
  204.         "uu": function(data) {
  205.             for(var x in data.users) {
  206.                 new User(data.users[x]);
  207.                 }
  208.             requestAnimationFrame(function() {
  209.                 for(x in data.users) {
  210.                     $(".user_li[data-uname="+x+"]")
  211.                         .attr("title", notes[x]||"no notes for "+x);
  212.                     }
  213.                 });
  214.             me=u(user);
  215.             },
  216.         "anonymous_players": function() {
  217.             for(var x in users) {
  218.                 delete users[x];
  219.                 }
  220.             for(var i=0;i<this.players.length;i++) {
  221.                 new User(this.players[i]);
  222.                 }
  223.             },
  224.         "anonymous_reveal": function() {
  225.             if(this.user===user) {
  226.                 me=u(this.mask);
  227.                 }
  228.             },
  229.         "join": function() {
  230.             new User(this.data);
  231.             log(this.user+" has joined");
  232.             if(/autokick/.test(notes[this.user])) {
  233.                 socket.cmd("ban",{
  234.                     uid:this.data.id
  235.                     });
  236.                 notes[this.user]=notes[this.user]
  237.                     .replace(/autokick x (\d+)/, function(full, x) {
  238.                         if(--x<=0) {
  239.                             return "";
  240.                             }
  241.                         return "autokick x "+x;
  242.                         });
  243.                 }
  244.             else {
  245.                 var user=this.user;
  246.                 requestAnimationFrame(function() {
  247.                     $(".user_li.ng-scope[data-uname="+user+"]")
  248.                         .attr("title", notes[user]||"no notes for "+user);
  249.                     });
  250.                 }
  251.             },
  252.         "leave": function() {
  253.             delete users[this.user];
  254.             log(this.user+" has left");
  255.             },
  256.         "kick": function() {
  257.             u(this.user).dead=true;
  258.             },
  259.         "kk": function() {
  260.             u(this.target).dead=true;
  261.             },
  262.         "k": function() {
  263.             u(this.user).morse.code++;
  264.             },
  265.         "u": function() {
  266.             var user=u(this.user);
  267.             if(Date.now()-user.morse.clock>100) {
  268.                 user.morse.id=0;
  269.                 }
  270.             if(user.morse.id) {
  271.                 ej.mor(user);
  272.                 user.morse.id=0;
  273.                 }
  274.             else if(user.morse.code>=2) {
  275.                 user.morse.id=user.morse.code;
  276.                 }
  277.             user.morse.code=0;
  278.             user.morse.clock=Date.now();
  279.             },
  280.         "<": function() {
  281.             if(auth) {
  282.                 if(slave&&this.directed===user) {
  283.                     bot.eval(this.msg,this);
  284.                     }
  285.                 }
  286.             },
  287.         "speech": function() {
  288.             if(this.type==="contact") {
  289.                 log("The roles are... "+this.data.join(", "));
  290.                 }
  291.             },
  292.         "m": function() {
  293.             if(this.say) {
  294.                 meet=this.meet;
  295.                 for(var i=0;i<this.members.length;i++) {
  296.                     u(this.members[i]).meet=this.meet;
  297.                     }
  298.                 }
  299.             switch(this.meet) {
  300.                 case "gun":
  301.                     $("#meetbox_village").before($("#meetbox_gun"));
  302.                     break;
  303.                 case "mafia":
  304.                     if(auth&&cmds.gamestate===1&&!this.hasdisguise&&!ranked) {
  305.                         socket.chat(me.role);
  306.                         }
  307.                 case "thief":
  308.                     me.mafia=true;
  309.                     var userlist=users.list;
  310.                     for(var i=0,name;i<userlist.length;i++) {
  311.                         name=userlist[i];
  312.                         if(!this.choosedata[name]) {
  313.                             u(name).mafia=true;
  314.                             log(name+" is your partner!");
  315.                             if(!cmds.current_users[name].revealed) {
  316.                                 cmds.select_role(name, "mafia");
  317.                                 }
  318.                             }
  319.                         }
  320.                     break;
  321.                 }
  322.             if(afk) {
  323.                 socket.vote(this.basket[0],this.meet);
  324.                 }
  325.             },
  326.         "end_meet": function() {
  327.             if(this.say) {
  328.                 var meet=this.meet;
  329.                 users.find(function() {
  330.                     return this.meet===meet;
  331.                     }).forEach(function(name) {
  332.                         u(name).meet=null;
  333.                         });
  334.                 }
  335.             },
  336.         "point": function() {
  337.             u(this.user).voted=!this.unpoint;
  338.             },
  339.         "reveal": function() {
  340.             u(this.user).role=this.data;
  341.             if(!u(this.user).dead) {
  342.                 if(this.user===user) {
  343.                     log("Your role is now "+this.data);
  344.                     }
  345.                 else {
  346.                     log(this.user+" is a "+this.data);
  347.                     }
  348.                 }
  349.             },
  350.         "countdown": function() {
  351.             if(afk||autokick) {
  352.                 clearTimeout(kicktimer);
  353.                 kicktimer=setTimeout(function() {
  354.                     socket.cmd("kick");
  355.                     },this.totaltime);
  356.                 }
  357.             }
  358.         }
  359.    
  360.     //townie input
  361.     var typebox=$("#typebox");
  362.     typebox.on("keydown",function(event) {
  363.         if(event.which===13&&this.value.charAt(0)==="/") {
  364.             commands.eval(this.value.substring(1));
  365.             this.value="";
  366.             }
  367.         });
  368.     $(window).on("keydown",function(event) {
  369.         if(event.ctrlKey) {
  370.             switch(event.which) {
  371.                 case 66:
  372.                     socket.cmd("option",{
  373.                         field:"fastgame"
  374.                         });
  375.                     socket.cmd("option",{
  376.                         field:"nospectate"
  377.                         });
  378.                     break;
  379.                 case 81:
  380.                     socket.cmd("<",{
  381.                         meet:meet,
  382.                         msg:typebox.val(),
  383.                         quote:true,
  384.                         target:user
  385.                         });
  386.                     typebox.val("");
  387.                     break;
  388.                 }
  389.             }
  390.         else if(event.target.value===undefined) {
  391.             typebox.focus();
  392.             }
  393.         });
  394.  
  395.     //u(name);
  396.     function u(name) {
  397.         return users[name]||new User({
  398.             id:null,
  399.             name:name
  400.             });
  401.         }
  402.  
  403.     //userperson
  404.     function User(data) {
  405.         this.id=data.id;
  406.         this.name=data.username;
  407.         this.role=null;
  408.         this.meet=meet;
  409.         this.mafia=false;
  410.         this.dead=false;
  411.         this.voted=false;
  412.         this.morse={
  413.             id:0,
  414.             code:0,
  415.             clock:0
  416.             }
  417.         if(this.id!==null) {
  418.             users[this.name]=this;
  419.             }
  420.         }
  421.  
  422.     //userpeople
  423.     Object.defineProperties(users,{
  424.         all:{
  425.             get:function() {
  426.                 return this.find(function() {
  427.                     return true;
  428.                     },true);
  429.                 }
  430.             },
  431.         list:{
  432.             get:function() {
  433.                 return this.find(function() {
  434.                     return !this.dead&&this.name!==user;
  435.                     },true);
  436.                 }
  437.             },
  438.         find:{
  439.             value:function(callback,list) {
  440.                 var output=new Array();
  441.                 for(var who in this) {
  442.                     if(callback.call(this[who])) {
  443.                         output.push(list?this[who].name:this[who]);
  444.                         }
  445.                     }
  446.                 return output;
  447.                 }
  448.             }
  449.         });
  450.  
  451.     //cmdlist class
  452.     function Cmdlist(data) {
  453.         return Object.create(data,Cmdlist.methods);
  454.         }
  455.     Cmdlist.methods={  
  456.         eval:{
  457.             value:function(input,scope) {
  458.                 var match=null;
  459.                 if(scope===undefined) {
  460.                     scope=this;
  461.                     }
  462.                 for(var i=0;i<this.length;i++) {
  463.                     match=this[i].regex.exec(input);
  464.                     if(match!==null&&input===match[0]) {
  465.                         match.shift();
  466.                         this[i].callback.apply(scope,match);
  467.                         break;
  468.                         }
  469.                     }
  470.                 }
  471.             }
  472.         }
  473.  
  474.     //this is a sin
  475.     var bot=null,
  476.         slave=false;
  477.     bot=Cmdlist([
  478.         {
  479.             name: "Scriptcheck",
  480.             regex: /scriptcheck/,
  481.             callback: function() {
  482.                 socket.chat(ej.fname,this.user);
  483.                 }
  484.             },
  485.         {
  486.             name: "Echo",
  487.             regex: /(?:echo|say) (.+)/,
  488.             callback: function(what) {
  489.                 //socket.chat(what);
  490.                 }
  491.             },
  492.         {
  493.             name: "Say hi",
  494.             regex: /hi+|hey+|hel+o+/,
  495.             callback: function() {
  496.                 socket.chat([
  497.                     "Hi",
  498.                     "Hi...",
  499.                     "Hello"
  500.                     ].random(),this.user);
  501.                 }
  502.             },
  503.         {
  504.             name: "Say hi 2",
  505.             regex: /(?:wh?at'?)s ?up+/,
  506.             callback: function() {
  507.                 socket.chat([
  508.                     "Hi",
  509.                     "Hi...",
  510.                     "Hello",
  511.                     "Nothing",
  512.                     "Something",
  513.                     "Everything"
  514.                     ].random(),this.user);
  515.                 }
  516.             },
  517.         {
  518.             name: "Roulette",
  519.             regex: /roulette/,
  520.             callback: function() {
  521.                 if(roulette===null||rouletteOn!==null) return;
  522.                 var user=this.user,
  523.                     message=[
  524.                         "Wet your pants",
  525.                         "Gulp",
  526.                         "Get ready",
  527.                         "Say your prayers"
  528.                         ].random()
  529.                         +", "+[
  530.                         "pull it",
  531.                         "pull the trigger",
  532.                         "let it rip"
  533.                         ].random();
  534.                 socket.chat(roulette+" bullets left. You put the gun to your head...",user);
  535.                 rouletteOn=setTimeout(function() {
  536.                     rouletteOn=null;
  537.                     if(Math.random()*roulette>1) {
  538.                         roulette--;
  539.                         socket.chat(message+", and nothing happens.",user);
  540.                         }
  541.                     else {
  542.                         roulette=0
  543.                         socket.chat(message+", and die instantly.",user);
  544.                         socket.vote(user,"gun");
  545.                         }
  546.                     if(roulette===0) {
  547.                         roulette=null;
  548.                         }
  549.                     },3000);
  550.                 }
  551.             },
  552.         {
  553.             name: "Become bitch",
  554.             regex: /be my bitch/,
  555.             callback: function() {
  556.                 if(master===null) {
  557.                     master=this.user;
  558.                     socket.chat("Yes master "+master+"...");
  559.                     }
  560.                 else {
  561.                     socket.chat("I belong to "+master);
  562.                     }
  563.                 }
  564.             },
  565.         {
  566.             name: "Roll over",
  567.             regex: /roll over/,
  568.             callback: function() {
  569.                 socket.chat(this.user!==master?
  570.                     "You're not the boss of me":
  571.                     "/me rolls over for senpai..."
  572.                     );
  573.                 }
  574.             },
  575.         {
  576.             name: "Bow",
  577.             regex: /bow ?(down)?/,
  578.             callback: function() {
  579.                 socket.chat(this.user!==master?
  580.                     "You're not the boss of me":
  581.                     "/me bows for her master..."
  582.                     );
  583.                 }
  584.             },
  585.         {
  586.             name: "Beg",
  587.             regex: /beg(?: for (.+))?/,
  588.             callback: function(what) {
  589.                 if(this.user!==master) return;
  590.                 var output=[
  591.                     "gets on her knees",
  592.                     "gets on all fours"
  593.                     ].random();
  594.                 output="/me "+output+" and begs";
  595.                 if(what) {
  596.                     output+=" for "+what;
  597.                     }
  598.                 socket.chat(output);
  599.                 }
  600.             },
  601.         {
  602.             name: "Vote",
  603.             regex: /vote (\w+)/,
  604.             callback: function(who) {
  605.                 if(this.user!==master) return;
  606.                 socket.vote(who,this.meet);
  607.                 }
  608.             },
  609.         {
  610.             name: "Shoot",
  611.             regex: /shoot (\w+)/,
  612.             callback: function(who) {
  613.                 if(this.user!==master) return;
  614.                 socket.vote(who,"gun");
  615.                 }
  616.             }
  617.         ]);
  618.  
  619.     //get acquainted
  620.     var commands=null;
  621.     commands=Cmdlist([
  622.         {
  623.             name: "Scriptcheck",
  624.             regex: /scriptcheck/,
  625.             callback: function() {
  626.                 log(ej.fname);
  627.                 }
  628.             },
  629.         {
  630.             name: "About",
  631.             regex: /info|help|about/,
  632.             callback: function() {
  633.                 log("Running "+ej.fname,"bold");
  634.                 log("Type /cmdlist for a list of commands");
  635.                 }
  636.             },
  637.         {
  638.             name: "Evaluate",
  639.             regex: /eval (.+)/,
  640.             callback: function(input) {
  641.                 log(JSON.stringify(eval(input)));
  642.                 }
  643.             },
  644.         {
  645.             name: "Clear chat/logs",
  646.             regex: /clear( logs)?/,
  647.             callback: function(_logs) {
  648.                 if(_logs) {
  649.                     $("#window .emjack").remove();
  650.                     }
  651.                 else {
  652.                     $("#window").empty();
  653.                     }
  654.                 }
  655.             },
  656.         {
  657.             name: "Request scriptcheck",
  658.             regex: /botnet/,
  659.             callback: function() {
  660.                 if(!morse) {
  661.                     setTimeout(function() {
  662.                         morse=false;
  663.                         },1000);
  664.                     }
  665.                 morse=true;
  666.                 ej.mor.send(2,0,"botnet");
  667.                 }
  668.             },
  669.         {
  670.             name: "Get metadata",
  671.             regex: /meta(?:data)?/,
  672.             callback: function() {
  673.                 for(var param in ej.meta) {
  674.                     log("@"+param+": "+ej.meta[param]);
  675.                     }
  676.                 }
  677.             },
  678.         {
  679.             name: "Get command list",
  680.             regex: /cmdlist ?(bot)?/,
  681.             callback: function(_bot) {
  682.                 var data=(_bot?bot:commands);
  683.                 for(var i=0;i<data.length;i++) {
  684.                     log(data[i].name+" ~ "+data[i].regex);
  685.                     }
  686.                 }
  687.             },
  688.         {
  689.             name: "Get random user/number",
  690.             regex: /random ?(\\d+)?/,
  691.             callback: function(digit) {
  692.                 log(digit?Math.round(Math.random()*digit):users.list.random());
  693.                 }
  694.             },
  695.         {
  696.             name: "Toggle AFK",
  697.             regex: /afk/,
  698.             callback: function() {
  699.                 afk=!afk;
  700.                 log(afk?"Jeeves will handle your affairs...":"Jeeves has been dismissed.");
  701.                 }
  702.             },
  703.         {
  704.             name: "Toggle autowill",
  705.             regex: /agw|autowill|antiguise/,
  706.             callback: function() {
  707.                 autowill=!autowill;
  708.                 log(autowill?"Name & role will be written in will by default.":"Disabled autowill.");
  709.                 }
  710.             },
  711.         {
  712.             name: "Toggle I/O logging",
  713.             regex: /dev/,
  714.             callback: function() {
  715.                 development=!development;
  716.                 log(development?"Logging I/O.":"Logging disabled.");
  717.                 }
  718.             },
  719.         {
  720.             name: "Toggle slavemode",
  721.             regex: /slave/,
  722.             callback: function() {
  723.                 slave=!slave;
  724.                 log(slave?"You're a naughty girl.":"You've found Jesus.");
  725.                 }
  726.             },
  727.         {
  728.             name: "Toggle roulette",
  729.             regex: /roulette/,
  730.             callback: function() {
  731.                 roulette=roulette===null?6:null;
  732.                 if(roulette!==null) {
  733.                     socket.chat("Reloaded the revolver. Who's next?");
  734.                     }
  735.                 }
  736.             },
  737.         {
  738.             name: "Send ping",
  739.             regex: /ping ?(all)?/,
  740.             callback: function(all) {
  741.                 socket.chat((
  742.                     all===undefined?
  743.                         users.find(function() {
  744.                             return !this.dead&&!this.voted&&this.meet===meet;
  745.                             },true):
  746.                         users.list
  747.                         ).join(" ")
  748.                     );
  749.                 }
  750.             },
  751.         {
  752.             name: "Send kick",
  753.             regex: /kick (\w+)/,
  754.             callback: function(name) {
  755.                 socket.cmd("ban",{
  756.                     uid:u(name).id
  757.                     });
  758.                 }
  759.             },
  760.         {
  761.             name: "Send vote",
  762.             regex: /vote ?(no one)?(\w+)?/,
  763.             callback: function(none,name) {
  764.                 socket.vote(
  765.                     none!==undefined?
  766.                         "*":
  767.                         name!==undefined?
  768.                             name:
  769.                             users.list.random()
  770.                             );
  771.                 }
  772.             },
  773.         {
  774.             name: "Send vote (gun)",
  775.             regex: /shoot (\w+)/,
  776.             callback: function(name) {
  777.                 socket.vote(name,"gun");
  778.                 }
  779.             },
  780.         {
  781.             name: "Lobby join",
  782.             regex: /join/,
  783.             callback: function() {
  784.                 lobbyGames({
  785.                     status_id:0,
  786.                     target:12,
  787.                     password:false
  788.                     },function(table) {
  789.                         socket.dcfunc(function() {
  790.                             location.href="/game/"+table.id;
  791.                             });
  792.                         return true;
  793.                         }
  794.                     );
  795.                 }
  796.             },
  797.         {
  798.             name: "Lobby leave",
  799.             regex: /leave ?(\d+)?/,
  800.             callback: function(x) {
  801.                 if(x===undefined) {
  802.                     socket.cmd("leave");
  803.                     }
  804.                 else {
  805.                     antidc=true;
  806.                     socket.cmd("k");
  807.                     for(var i=0;i<x;i++) {
  808.                         socket.cmd("leave");
  809.                         }
  810.                     }
  811.                 }
  812.             },
  813.         {
  814.             name: "Lobby host",
  815.             regex: /host ?(.+)?/,
  816.             callback: function(title) {
  817.                 socket.dcfunc(function() {
  818.                     $.getJSON("/game/add/mafia",{
  819.                         setupid:setup_id,
  820.                         ranked:false,
  821.                         add_title:title===undefined?0:1,
  822.                         game_title:title
  823.                         },function(json) {
  824.                             location.href="/game/"+json.table
  825.                             });
  826.                     });
  827.                 }
  828.             },
  829.         {
  830.             name: "Lobby games",
  831.             regex: /games/,
  832.             callback: function() {
  833.                 var a,
  834.                     div,
  835.                     chat=document.getElementById("window");
  836.                 lobbyGames({
  837.                     status_id:0,
  838.                     password:false
  839.                     },function(table) {
  840.                     a=document.createElement("a");
  841.                     a.textContent="Table "+table.id;
  842.                     a.addEventListener("click",function(event) {
  843.                         socket.dcfunc(function() {
  844.                             location.href="/game/"+table.id;
  845.                             });
  846.                         });
  847.                     div=document.createElement("div");
  848.                     div.appendChild(a);
  849.                     div.appendChild(document.createTextNode(" - "+table.numplayers+" / "+table.target+" players"));
  850.                     chat.appendChild(div);
  851.                     chat.scrollTop=chat.scrollHeight;
  852.                     });
  853.                 }
  854.             },
  855.         {
  856.             name: "[Debug] Send morse",
  857.             regex: /morse (\d+) (\d+)/,
  858.             callback: function naughty_morse(type,code) {
  859.                 morse=true;
  860.                 ej.mor.send(type,code);
  861.                 }
  862.             }
  863.         ]);
  864.  
  865.     //miscellaneous
  866.     function lobbyGames(criteria,callback) {
  867.         $.ajax({url:"/game/find?page=1",method:"get"}).success(function(json) {
  868.             var criterion,
  869.                 openGames=[],
  870.                 games=JSON.parse(json[1]);
  871.             games.data.forEach(function(table) {
  872.                 for(criterion in criteria) {
  873.                     if(criteria[criterion]!==table[criterion]) {
  874.                         return;
  875.                         }
  876.                     }
  877.                 openGames.push(table);
  878.                 })
  879.             if(openGames.length===0) {
  880.                 log("No open games available!");
  881.                 }
  882.             else {
  883.                 openGames.some(function(table) {
  884.                     return callback(table);
  885.                     });
  886.                 }
  887.             });
  888.         }
  889.  
  890.     //preserve notes
  891.     var notes=new Object();
  892.     if(localStorage.notes) {
  893.         notes=JSON.parse(localStorage.notes);
  894.         }
  895.     $(".user_note").on("click",function(event) {
  896.         var user=cmds.selected_user;
  897.         console.log(user.details);
  898.         if(user.details.notes===undefined) {
  899.             user.details.notes=notes[user.username]||"";
  900.             $(".notes").val(user.details.notes);
  901.             }
  902.         });
  903.     $(".notes").on("keyup",function(event) {
  904.         notes[cmds.selected_user.username]=this.value;
  905.         });
  906.  
  907.     //load data
  908.     if(localStorage.emjack) {
  909.         var data=JSON.parse(localStorage.emjack);
  910.         slave=data.bot;
  911.         morse=data.mor;
  912.         autowill=data.agw;
  913.         development=data.dev;
  914.         delete data;
  915.         }
  916.  
  917.     //save data
  918.     $(window).on("beforeunload",function(event) {
  919.         localStorage.notes=JSON.stringify(notes);
  920.         localStorage.emjack=JSON.stringify({
  921.             bot: Boolean(slave),
  922.             mor: Boolean(morse),
  923.             agw: Boolean(autowill),
  924.             dev: Boolean(development)
  925.             });
  926.         });
  927.  
  928.     //important information
  929.     var last_error=null;
  930.     $(window).on("error",function(event) {
  931.         var message=event.originalEvent.message;
  932.         if(message!==last_error) {
  933.             log("You've got error!","bold");
  934.             log(message);
  935.             last_error=message;
  936.             }
  937.         });
  938.  
  939.     //MiniLib
  940.     Object.defineProperty(Array.prototype,"random",{
  941.         value:function() {
  942.             return this[Math.floor(Math.random()*this.length)];
  943.             }
  944.         });
  945.     }
  946.  
  947. //Updates
  948. var request=new XMLHttpRequest();
  949. request.onreadystatechange=function() {
  950.     if(request.readyState===4&&request.status===200) {
  951.         var meta=new Object(),
  952.             params=request.responseText.split(/\/\/ @([\w:]+)\s+(.+)/);
  953.         for(var i=1;i<params.length;i+=3) {
  954.             meta[params[i]]=params[i+1];
  955.             }
  956.         inject(String(function() {
  957.             ej.meta=$meta;
  958.             if(ej.version<ej.meta.version) {
  959.                 var a=document.createElement("a");
  960.                 a.textContent="Update "+ej.name+ej.meta.version;
  961.                 a.target="_blank";
  962.                 a.href="http://userscripts.org/scripts/show/162009";
  963.                 document.getElementById("window").appendChild(a);
  964.                 delete a;
  965.                 }
  966.             }).replace(/\$meta/,JSON.stringify(meta)));
  967.         }
  968.     }
  969. request.open("GET","http://userscripts.org/scripts/source/162009.meta.js",true);
  970. request.send();
  971.  
  972. //Syringe
  973. function inject(fn) {
  974.     var script=document.createElement("script");
  975.     script.type="text/javascript";
  976.     script.textContent="("+String(fn)+")()";
  977.     document.body.appendChild(script);
  978.     }
  979.  
  980. //Jack-in
  981. setTimeout(function() {
  982.     inject(EMjack);
  983.     document.body.addEventListener("contextmenu",function(event) {
  984.         event.stopPropagation();
  985.         },true);
  986.     });
Add Comment
Please, Sign In to add comment