Fooksie

EMjack

Sep 16th, 2013
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name            EMjack
  3. // @version         2.0
  4. // @description     Some crap you may find useful.
  5. // @match           http://www.epicmafia.com/game/*
  6. // ==/UserScript==
  7.  
  8. //Welcome back
  9. EMjack=String(function() {
  10.  
  11.     //Variables
  12.     users={}
  13.     meet=null
  14.     master=null
  15.     afk=false
  16.     autokick=true
  17.     autobomb=false
  18.     development=false
  19.     kicktimer=0
  20.     roulette=6
  21.     rouletteTurn=false
  22.  
  23.     //Locate the socket
  24.     socket=io.sockets["http://www.epicmafia.com:80"].namespaces[""]
  25.  
  26.     //Breaking into that $scope
  27.     socket.log=function(message,classes) {
  28.         this.$events.message[0](JSON.stringify([["msg",{
  29.             msg:message,
  30.             type:classes
  31.             }]]))
  32.         }
  33.  
  34.     //Extend the socket
  35.     socket.cmd=function(cmd,data) {
  36.         this.send(JSON.stringify([cmd,data]))
  37.         }
  38.  
  39.     //Extend it some more
  40.     socket.chat=function(message) {
  41.         this.cmd("<",{
  42.             meet:meet,
  43.             msg:message
  44.             })
  45.         }
  46.  
  47.     //Extend it until it bursts
  48.     socket.vote=function(vote,meeting) {
  49.         this.cmd("point",{
  50.             meet:meeting||meet,
  51.             target:vote
  52.             })
  53.         }
  54.  
  55.     //Going too far
  56.     socket.dcfunc=function(callback) {
  57.         this.socket.onDisconnect=callback
  58.         this.cmd("leave")
  59.         }
  60.  
  61.     //Plug in
  62.     socket.on("message",function(packet) {
  63.         var i,
  64.             cmd,
  65.             data
  66.         packet=JSON.parse(packet)
  67.         for(i=0;i<packet.length;i++) {
  68.             cmd=packet[i][0],
  69.             data=packet[i][1]
  70.             switch(cmd) {
  71.                 case "auth":
  72.                     break
  73.                 case "round":
  74.                     for(var x in users) {
  75.                         users[x].voted=false
  76.                         }
  77.                     break
  78.                 case "users":
  79.                     for(var x in data.users) {
  80.                         new User(data.users[x])
  81.                         }
  82.                     me=users[user]
  83.                     break
  84.                 case "join":
  85.                     new User(data.data)
  86.                     socket.log(data.user+" has joined")
  87.                     break
  88.                 case "leave":
  89.                     delete users[data.user]
  90.                     socket.log(data.user+" has left")
  91.                     break
  92.                 case "kick":
  93.                     users[data.user].dead=true
  94.                     break
  95.                 case "kill":
  96.                     users[data.target].dead=true
  97.                     break
  98.                 case "<":
  99.                     if(i!==packet.length-1) break
  100.                     var target=data.msg.match(/^@(\w+)/)
  101.                     if(target!==null&&target[1]===user) {
  102.                         bot.eval(data.msg.substring(target[1].length+2),data)
  103.                         }
  104.                     break
  105.                 case "speech":
  106.                     if(data.type==="contact") {
  107.                         socket.log("The roles are... "+data.data.join(", "))
  108.                         }
  109.                     break
  110.                 case "meet":
  111.                     if(data.say) {
  112.                         meet=data.meet
  113.                         data.members.forEach(function(who) {
  114.                             if(users[who]) {
  115.                                 users[who].meet=data.meet
  116.                                 }
  117.                             })
  118.                         }
  119.                     if(data.meet==="mafia"||data.meet==="thief") {
  120.                         me.mafia=true
  121.                         users.list.forEach(function(who) {
  122.                             if(!data.choosedata[who]) {
  123.                                 users[who].mafia=true
  124.                                 socket.log(who+" is your partner!")
  125.                                 }
  126.                             })
  127.                         }
  128.                     if(afk) {
  129.                         socket.vote(data.basket[0],data.meet)
  130.                         }
  131.                     break
  132.                 case "end_meet":
  133.                     if(data.say) {
  134.                         users.find({meet:data.meet}).forEach(function(who) {
  135.                             if(users[who]) {
  136.                                 users[who].meet=null
  137.                                 }
  138.                             })
  139.                         }
  140.                     break
  141.                 case "point":
  142.                     if(users[data.user]===undefined) break
  143.                     users[data.user].voted=!data.unpoint
  144.                     break
  145.                 case "countdown":
  146.                     if(!afk&&!autokick) break
  147.                     clearTimeout(kicktimer)
  148.                     kicktimer=setTimeout(function() {
  149.                         socket.cmd("kick")
  150.                         },data.totaltime)
  151.                     break
  152.                 case "start_input":
  153.                     if(autobomb) {
  154.                         socket.cmd("input",{
  155.                             id:data.id,
  156.                             input:{
  157.                                 player:autobomb.constructor===String?autobomb:users.list.random()
  158.                                 }
  159.                             })
  160.                         }
  161.                     else if(afk) {
  162.                         setTimeout(function() {
  163.                             socket.cmd("input",{
  164.                                 id:data.id,
  165.                                 input:{
  166.                                     player:users.list.random()
  167.                                     }
  168.                                 })
  169.                             },5000)
  170.                         }
  171.                     break
  172.                 }
  173.             }
  174.         })
  175.    
  176.     //Hottie keys
  177.     typebox=$("#typebox")
  178.     $(window).on("keydown",function(event) {
  179.         if(event.ctrlKey) {
  180.             switch(event.which) {
  181.                 case 66:
  182.                     socket.cmd("option",{
  183.                         field:"fastgame"
  184.                         })
  185.                     socket.cmd("option",{
  186.                         field:"nospectate"
  187.                         })
  188.                     break
  189.                 case 81:
  190.                     socket.cmd("<",{
  191.                         meet:meet,
  192.                         msg:typebox.val(),
  193.                         quote:true,
  194.                         target:user
  195.                         })
  196.                     typebox.val("")
  197.                     break
  198.                 }
  199.             }
  200.         else if(!event.target.type) {
  201.             typebox.focus()
  202.             }
  203.         })
  204.  
  205.     //Important information
  206.     $(window).on("error",function(event) {
  207.         socket.log("You've got error!")
  208.         socket.log(event.originalEvent.message)
  209.         })
  210.  
  211.     //Townie input
  212.     typebox.on("keydown",function(event) {
  213.         if(event.which!==13||this.value.charAt(0)!=="/") return
  214.         commands.eval(this.value.substring(1))
  215.         this.value=""
  216.         })
  217.  
  218.     //Userperson
  219.     function User(data) {
  220.         this.id=data.id
  221.         this.name=data.username
  222.         this.me=this.name===user
  223.         this.meet=meet
  224.         this.mafia=false
  225.         this.dead=false
  226.         this.voted=false
  227.         users[this.name]=this
  228.         }
  229.  
  230.     //Userpeople
  231.     Object.defineProperties(users,{
  232.         list:{
  233.             get:function() {
  234.                 return this.find({
  235.                     me:false,
  236.                     dead:false
  237.                     },true)
  238.                 }
  239.             },
  240.         find:{
  241.             value:function(criteria,names) {
  242.                 var who,
  243.                     valid,
  244.                     criterion,
  245.                     output=[]
  246.                 for(who in this) {
  247.                     valid=true
  248.                     for(criterion in criteria) {
  249.                         if(this[who][criterion]!==criteria[criterion]) {
  250.                             valid=false
  251.                             break
  252.                             }
  253.                         }
  254.                     if(valid===true) {
  255.                         output.push(names===true?this[who].name:this[who])
  256.                         }
  257.                     }
  258.                 return output
  259.                 }
  260.             }
  261.         })
  262.  
  263.     //Command list
  264.     function Cmdlist(cmds) {
  265.         this.cmds=[]
  266.         for(var x in cmds) {
  267.             this.cmds.push({
  268.                 regex:RegExp(x),
  269.                 callback:cmds[x]
  270.                 })
  271.             }
  272.         }
  273.     Cmdlist.prototype.eval=function(input,scope) {
  274.         var match
  275.         if(scope===undefined) scope=this
  276.         this.cmds.some(function(command) {
  277.             match=command.regex.exec(input)
  278.             if(match!==null&&input===match[0]) {
  279.                 match.shift()
  280.                 command.callback.apply(scope,match)
  281.                 return true
  282.                 }
  283.             })
  284.         }
  285.  
  286.     //This is a sin
  287.     bot=new Cmdlist({
  288.         "scriptcheck":function() {
  289.             socket.chat("I have it")
  290.             },
  291.         "roulette":function() {
  292.             if(rouletteTurn) return
  293.             var prefix="@"+this.user+" "
  294.             socket.chat(prefix+roulette+" bullets left. You put the gun to your head...")
  295.             var message=prefix+["Wet your pants","Gulp","Get ready","Say your prayers"].random()
  296.             message+=", "+["pull it","pull the trigger","let it rip"].random()
  297.             rouletteTurn=true
  298.             setTimeout(function() {
  299.                 rouletteTurn=false
  300.                 if(Math.random()*roulette>1) {
  301.                     roulette--
  302.                     socket.chat(message+", and nothing happens.")
  303.                     }
  304.                 else {
  305.                     roulette=0
  306.                     socket.chat(message+", and die instantly.")
  307.                     }
  308.                 if(roulette===0) {
  309.                     roulette=6
  310.                     socket.chat("Reloaded the revolver. Who's next?")
  311.                     }
  312.                 },3000)
  313.             },
  314.         "be my bitch":function() {
  315.             if(master===null) {
  316.                 master=this.user
  317.                 socket.chat("Yes master "+master+"...")
  318.                 }
  319.             else {
  320.                 socket.chat("I belong to "+master)
  321.                 }
  322.             },
  323.         "roll over":function() {
  324.             if(this.user===master) {
  325.                 socket.chat("/me rolls over for senpai...")
  326.                 }
  327.             else {
  328.                 socket.chat("You're not the boss of me")
  329.                 }
  330.             },
  331.         "beg(?: for (.+))?":function(what) {
  332.             if(this.user!==master) return
  333.             var output=[
  334.                 "gets on her knees",
  335.                 "gets on all fours"
  336.                 ].random()
  337.             output="/me "+output+" and begs"
  338.             if(what) {
  339.                 output+=" for "+what
  340.                 }
  341.             socket.chat(output)
  342.             },
  343.         "vote (\\w+)":function(who) {
  344.             if(this.user!==master) return
  345.             socket.vote(who,this.meet)
  346.             }
  347.         })
  348.  
  349.     //Get acquainted
  350.     commands=new Cmdlist({
  351.         "dev":function() {
  352.             development=!development
  353.             socket.log(development?"Logging I/O.":"Logging disabled.")
  354.             },
  355.         "afk":function() {
  356.             afk=!afk
  357.             socket.log(afk?"Jeeves will handle your affairs":"Jeeves has been dismissed")
  358.             },
  359.         "random":function() {
  360.             socket.log(users.list.random())
  361.             },
  362.         "eval (.+)":function(input) {
  363.             socket.log(JSON.stringify(eval(input)))
  364.             },
  365.         "kick (\\w+)":function(who) {
  366.             if(users[who]!==undefined) {
  367.                 socket.cmd("ban",{
  368.                     uid:users[who].id
  369.                     })
  370.                 }
  371.             },
  372.         "vote(?: (no one)|(\\w+))?":function(none,who) {
  373.             socket.vote(none!==undefined?"*":who!==undefined?who:users.list.random())
  374.             },
  375.         "will (.+)":function(will) {
  376.             socket.log("You revised your will.","lastwill")
  377.             socket.cmd("will",{
  378.                 msg:will
  379.                 })
  380.             },
  381.         "(?:fq|quote|fakequote) (\\w+) (.+)":function(who,message) {
  382.             socket.cmd("<",{
  383.                 meet:meet,
  384.                 msg:message,
  385.                 quote:true,
  386.                 target:who
  387.                 })
  388.             },
  389.         "(?:ab|autobomb)(?: (\\w+))?":function(who) {
  390.             if(users[who]!==undefined) {
  391.                 autobomb=who
  392.                 socket.log("Passing the bomb to "+who)
  393.                 }
  394.             else {
  395.                 autobomb=!autobomb
  396.                 socket.log(autobomb?"You're now an anarchist!":"You're now a tree.")
  397.                 }
  398.             },
  399.         "ping( all)?":function(all) {
  400.             socket.chat((all===undefined?users.find({
  401.                 dead:false,
  402.                 voted:false,
  403.                 meet:meet
  404.                 },true):users.list).join(" "))
  405.             },
  406.         "host(?: (.+))?":function(title) {
  407.             socket.dcfunc(function() {
  408.                 $.getJSON("/game/add/mafia",{
  409.                     setupid:setup_id,
  410.                     ranked:false,
  411.                     add_title:title===undefined?0:1,
  412.                     game_title:title
  413.                     },function(json) {
  414.                         location.href="/game/"+json.table
  415.                         })
  416.                 })
  417.             },
  418.         "join":function() {
  419.             lobbyGames({
  420.                 status_id:0,
  421.                 target:12,
  422.                 password:false
  423.                 },function(table) {
  424.                 socket.dcfunc(function() {
  425.                     location.href="/game/"+table.id
  426.                     })
  427.                 return true
  428.                 })
  429.             },
  430.         "games":function() {
  431.             var a,
  432.                 div,
  433.                 chat=document.getElementById("window")
  434.             lobbyGames({
  435.                 status_id:0,
  436.                 password:false
  437.                 },function(table) {
  438.                 a=document.createElement("a")
  439.                 a.textContent="Table "+table.id
  440.                 a.addEventListener("click",function(event) {
  441.                     socket.dcfunc(function() {
  442.                         location.href="/game/"+table.id
  443.                         })
  444.                     })
  445.                 div=document.createElement("div")
  446.                 div.appendChild(a)
  447.                 div.appendChild(document.createTextNode(" - "+table.numplayers+" / "+table.target+" players"))
  448.                 chat.appendChild(div)
  449.                 chat.scrollTop=chat.scrollHeight
  450.                 })
  451.             }
  452.         })
  453.    
  454.     //Misc.
  455.     function lobbyGames(criteria,callback) {
  456.         $.ajax({url:"/game/find?page=1",method:"get"}).success(function(json) {
  457.             var criterion,
  458.                 openGames=[]
  459.             json.data.forEach(function(table) {
  460.                 for(criterion in criteria) {
  461.                     if(criteria[criterion]!==table[criterion]) {
  462.                         return
  463.                         }
  464.                     }
  465.                 openGames.push(table)
  466.                 })
  467.             if(openGames.length===0) {
  468.                 socket.log("No open games available!")
  469.                 }
  470.             else {
  471.                 openGames.some(function(table) {
  472.                     return callback(table)
  473.                     })
  474.                 }
  475.             })
  476.         }
  477.  
  478.     //MiniLib
  479.     Array.prototype.random=function() {
  480.         return this[Math.floor(Math.random()*this.length)]
  481.         }
  482.  
  483.     })
  484.  
  485. //Jack-in
  486. setTimeout(function() {
  487.     var script=document.createElement("script")
  488.     script.type="text/javascript"
  489.     script.textContent="("+EMjack+")()"
  490.     document.body.appendChild(script)
  491.     document.body.oncontextmenu=null
  492.     })
Add Comment
Please, Sign In to add comment