Guest User

Untitled

a guest
Apr 30th, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 26.09 KB | None | 0 0
  1. import struct
  2. import socket
  3. import time
  4. import hashlib
  5. import sys
  6. from random import choice
  7.  
  8. # To do
  9. # - fix mouse list
  10.  
  11. version = 167
  12.  
  13. def bin_to_str(b):
  14.     return ' '.join([hex(ord(c))[2:].rjust(2,'0') for c in b])
  15.  
  16. class Server:
  17.     EN1 = ('91.121.28.82', 443)
  18.     EN2 = ('95.142.173.20', 44444)
  19.     KR = ('91.121.157.83', 44444)
  20.     BR = ('187.22.25.190', 44444)
  21.     BR2 = ('173.246.100.255', 44444)
  22.     BR3 = ('173.246.103.161', 44444)
  23. class Packet:
  24.     int_size = {1:'b', 2:'h', 4:'l'}
  25.    
  26.     def __init__(self, data=""):
  27.         self.data = data
  28.         self.protocol = 'new'
  29.     def setProtocol(self, t):
  30.         self.protocol = t
  31.        
  32.     def peek(self, b):
  33.         return self.data[:b]
  34.     def read(self, b):
  35.         ret = self.data[:b]
  36.         self.data = self.data[b:]
  37.         return ret
  38.     def readint(self, b):
  39.         s = self.read(b)
  40.         #return int(s.encode('hex'), 16)
  41.         return struct.unpack('!'+self.int_size[b], s)[0]
  42.     def readstring(self):
  43.         s_len = self.readint(2)
  44.         return self.read(s_len)
  45.    
  46.     def write(self, s):
  47.         self.data += s
  48.     def writeint(self, i, size):
  49.         size_c = self.int_size[size]
  50.         self.data += struct.pack('!'+size_c, i)
  51.     def writebyte(self, b):
  52.         self.data += chr(b)
  53.     def writestring(self, s):
  54.         self.writeint(len(s), 2)
  55.         self.write(s)
  56.  
  57.     def oldProtocolSplit(self):
  58.         ret = self.data.split('\x01')
  59.         ret = map(lambda s: s.replace('\x00',''), ret)
  60.         return ret
  61.  
  62.     def size(self):
  63.         return len(self.data)
  64.  
  65.     def __str__(self):
  66.         if len(self.data) > 0: return bin_to_str(self.data)
  67.         else: return 'Empty packet'
  68.  
  69.  
  70. class PrefixGen:
  71.     def __init__(self, data):
  72.         self.MDT = []
  73.         self.data = data
  74.         self.data = self.data.replace('\x00','')
  75.         message = self.data.split('\x01')
  76.         LCMDT = list(message[2])
  77.         for c in map(int, LCMDT):
  78.             if c == 0: self.MDT.append(chr(10))
  79.             else: self.MDT.append(chr(c))
  80.  
  81.         self.CMDTEC = int(message[3])
  82.  
  83.     def __call__(self):
  84.         final = ""
  85.         loc_2 = map(int, list(str(self.CMDTEC%9000 + 1000)))
  86.         final = ''.join([self.MDT[x] for x in loc_2])
  87.         self.CMDTEC += 1
  88.         return final
  89.  
  90.  
  91. class Mouse:
  92.     mice_ref = []
  93.    
  94.     def __init__(self, mousedata):
  95.         data = mousedata.split('#')
  96.         #print 'MouseData:',data
  97.         self.name = "Unknown"
  98.         self.code = "0"
  99.         self.score = 0
  100.         self.codeforum = "0"
  101.         self.dead = True
  102.  
  103.         if len(data) == 9:
  104.             self.name = data[0]
  105.             self.code = data[1]
  106.             self.score = int(data[3])
  107.             self.codeforum = data[8]
  108.             self.dead = data[2] == "1"
  109.         self.x = 0
  110.         self.y = 0
  111.         self.speedx = 0
  112.         self.speedy = 0
  113.         self.jump = False
  114.         self.left = False
  115.         self.right = False
  116.         self.lastdir = 0
  117.  
  118.  
  119.  
  120. class MouseList:
  121.     def __init__(self):
  122.         self.mice = []
  123.  
  124.     def clear(self):
  125.         self.mice = []
  126.  
  127.     def add(self, m):
  128.         self.mice.append(m)
  129.  
  130.     def getCodes(self):
  131.         return [m.code for m in self.mice]
  132.  
  133.     def findBy(self, lamb, con):
  134.         f = filter(lamb, con)
  135.         if len(f) > 0:
  136.             return f[0]
  137.         else: return None
  138.  
  139.     def findByCode(self, code):
  140.         return self.findBy(lambda m: m.code == code, self.mice)
  141.  
  142.     def findByForumCode(self, fcode):
  143.         return self.findBy(lambda m: m.forumcode == fcode, self.mice)
  144.  
  145.     def findByName(self, name):
  146.         return self.findBy(lambda m: m.name == name, self.mice)
  147.  
  148.  
  149. class Magic:
  150.     ARROW = 0
  151.     SMALLBOX = 1
  152.     LARGEBOX = 2
  153.     SMALLBOARD = 3
  154.     LARGEBOARD = 4
  155.     BALL = 6
  156.     TRAMPOLINE = 7
  157.     ANVIL = 10
  158.     CANNONDOWN = 18
  159.     CANNONRIGHT = 19
  160.     CANNONLEFT = 20
  161.     SPIRIT = 24
  162.     RUNE = 32
  163.  
  164. class ForbiddenMagic:
  165.     REDPOINT = 11
  166.     REDLEFT = 12
  167.     REDRIGHT = 13
  168.     BLUEPOINT = 14
  169.     BLUERIGHT = 15
  170.     BLUELEFT = 16
  171.     YELLOWPOINT = 22
  172.     CANNONUP = 17
  173.     BOMB = 23
  174.     CHEESE = 25
  175.     BLUEPORTAL = 26
  176.     ORANGEPORTAL = 27
  177.     BALLOON = 28
  178.     REDBALLOON = 29
  179.     GREENBALLOON = 30
  180.     YELLOWBALLOON = 31
  181.     SNOW = 34
  182.     #HEAVYBALL = 5
  183.     #SMALLROUGHPLANK = 8
  184.     #LARGEROUGHPLANK = 9
  185.     #STICKYBALL = 21
  186.  
  187.  
  188. class Bot:
  189.     def __init__(self, roomname, username, password=""):
  190.         self.sock = None
  191.         self.GeneratePrefix = None
  192.         self.r_data = ""
  193.         self.packet_queue = []
  194.         self.o_username = username
  195.         self.username = username
  196.         self.password = password
  197.         self.roomname = roomname
  198.         self.loginstate = "not ready"
  199.         self.tzat_last = time.time()
  200.         self.move_code = "0"
  201.         self.mice = MouseList()
  202.         self.disconnected = False
  203.         self.shaman = False
  204.         self.sync = False
  205.         self.running = True
  206.         self.stayalive = True
  207.         self.casting = False
  208.         self.object_count = 0
  209.         self.micerz = []
  210.     def TZAT(self):
  211.         self.tzat_last = time.time()
  212.         p = Packet("\x1A\x02111000")
  213.         self.send(p)
  214.  
  215.     def send(self, packet, prefix=True):
  216.         p_size = packet.size()+8
  217.         if packet.protocol=='old': p_size += 4
  218.  
  219.         data = Packet()
  220.         data.writeint(p_size, 4)
  221.         pre = '\x00\x00\x00\x00' if not self.GeneratePrefix else self.GeneratePrefix()
  222.         if prefix == False: pre = '\x00\x00\x00\x00'
  223.         data.write(pre)
  224.         if packet.protocol=='old':
  225.             data.write('\x01\x01')
  226.             data.writeint(packet.size(), 2)
  227.         data.write(packet.data)
  228.         #print 'Sending:',str(data)
  229.         try:
  230.             self.sock.send(data.data)
  231.         except:
  232.             print 'Connection problem.'
  233.             self.disconnected = True
  234.  
  235.     def delay_packet(self, delay, packet, msg="", exe=""):
  236.         self.packet_queue.append([time.time()+delay, packet, msg, exe])
  237.  
  238.     def sendVersion(self):
  239.         packet = Packet()
  240.         packet.writebyte(0x1C)
  241.         packet.writebyte(0x01)
  242.         packet.writeint(version, 2)
  243.         self.send(packet, prefix=False)
  244.  
  245.     def login(self):
  246.         passwordhash = hashlib.sha256(self.password).hexdigest()
  247.         if self.password == "":
  248.             passwordhash = ""
  249.         print 'password:',self.password.encode('base64'),'passhash',passwordhash
  250.         p = Packet()
  251.         p.setProtocol('old')
  252.         p.write('\x1A\x04\x01')
  253.         p.write(self.o_username)
  254.         p.write('\x01')
  255.         p.write(passwordhash)
  256.         p.write('\x01')
  257.         p.write(self.roomname)
  258.         self.send(p)
  259.         self.loginstate = "logged in"
  260.         if self.password == "":
  261.             self.username = '*'+self.username
  262.  
  263.     def connect(self, server):
  264.         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  265.         try:
  266.             self.sock.connect(server)
  267.         except:
  268.             return False
  269.         self.sendVersion()
  270.         time.sleep(1)
  271.         self.sock.setblocking(False)
  272.         self.disconnected = False
  273.         return True
  274.  
  275.     def parsePacket(self, packet):
  276.         packet_type = packet.peek(2)
  277.         try:
  278.             # Mice in server & prefix gen data
  279.             if packet_type == '\x1A\x1B':
  280.                 msg = packet.oldProtocolSplit()
  281.                 print 'Mice in server:',int(msg[1])
  282.                 self.GeneratePrefix = PrefixGen(packet.data)
  283.                 self.loginstate = "ready to login"
  284.  
  285.             # Name data
  286.             if packet_type == '\x1A\x08':
  287.                 msg = packet.oldProtocolSplit()
  288.                 self.username = msg[1]
  289.  
  290.             # Ping
  291.             if packet_type == '\x1A\x1A':
  292.                 #print ' * PING *'
  293.                 p = Packet()
  294.                 p.setProtocol('old')
  295.                 p.write('\x1A\x1A')
  296.                 self.delay_packet(11, p) #, ' * PONG *')
  297.  
  298.             # New Map
  299.             if packet_type == '\x05\x05':
  300.                 msg = packet.oldProtocolSplit()
  301.                 self.move_code = msg[3]
  302.                 self.object_count = 0
  303.                 if self.stayalive:
  304.                     keepalive = Packet("\x04\x0A")
  305.                     keepalive.setProtocol('old')
  306.                     self.send(keepalive)
  307.                 self.event_newmap()
  308.  
  309.             # Chat
  310.             if packet_type == '\x06\x06':
  311.                 packet.read(2)
  312.                 number = packet.readint(4)
  313.                 who = packet.readstring()
  314.                 said = packet.readstring()
  315.                 self.event_chat(who, said)
  316.  
  317.             # Whisper
  318.             if packet_type == '\x06\x07':
  319.                 #print 'WHISPER,',str(packet)
  320.                 packet.read(2)
  321.                 sent_by_me = not bool(packet.readint(1))
  322.                 #print 'sent_by_me',sent_by_me
  323.                 who = packet.readstring()
  324.                 try:
  325.                     whispered = packet.readstring()
  326.                 except:
  327.                     whispered = ""
  328.  
  329.                 if not sent_by_me:
  330.                     self.event_whisper(who, whispered)
  331.             # Profile
  332.             if packet_type == '\x08\x0A':
  333.                 packet.read(1)
  334.                 said = packet.readstring()
  335.                 self.event_profile(said)
  336.             # Serveur
  337.             if packet_type == '\x06\x14':
  338.                 #'\x06\x14'
  339.                 packet.read(1)
  340.                 said = packet.readstring()
  341.                 #WConio.textattr(#WConio.GREEN)
  342.                 #WConio.cputs("[Serveur] ")
  343.                 #WConio.textattr(#WConio.CYAN)
  344.                 #WConio.cputs("%s\r\n" % (said))
  345.                 self.event_serveur(said)
  346.                 #\x06\x14
  347.                 #\x14
  348.                 #\x01
  349.                 #Split = \x1A
  350.                 #0A*
  351.             # Mod message
  352.             if packet_type == '\x06\x0A':
  353.                 packet.read(2)
  354.                 unknown = packet.read(1)
  355.                 who = packet.readstring()
  356.                 said = packet.readstring()
  357.                 self.event_mod(who, said)
  358.                 print '[ModArb Channel][%s] %s' % (who, said)
  359.             # Shaman info
  360.             if packet_type == '\x08\x14':
  361.                 msg = packet.oldProtocolSplit()
  362.                 #print 'Shaman info',msg
  363.                 if len(msg) > 1:
  364.                     self.shaman = False
  365.                     sha_ids = msg[1:]
  366.                     for sha_id in sha_ids:
  367.                         tm = self.mice.findByCode(sha_id)
  368.                         if tm:
  369.                             print "Shaman:",repr(tm.name)
  370.                             #print repr(tm.name),"vs.",repr(self.username)
  371.                             if tm.name == self.username:
  372.                                 #print 'I am shaman'
  373.                                 self.shaman = True
  374.                  
  375.                     self.event_shamaninfo(sha_ids)
  376.  
  377.             # Mice in room
  378.             if packet_type == '\x08\x09':
  379.                 msg = packet.oldProtocolSplit()
  380.                 newlist = MouseList()
  381.                 for mouseinfo in msg[1:]:
  382.                     m_object = Mouse(mouseinfo)
  383.                     newlist.add(m_object)
  384.  
  385.                 old_codes = self.mice.getCodes()
  386.                 for new_mouse in newlist.getCodes():
  387.                     if not new_mouse in old_codes:
  388.                         new_m = newlist.findByCode(new_mouse)
  389.                         print 'New mouse',newlist.findByCode(new_mouse).name
  390.                         name = newlist.findByCode(new_mouse).name
  391.                         if name == "Artebot":
  392.                             pass
  393.                         else:
  394.                             self.micerz.append(newlist.findByCode(new_mouse).name)
  395.  
  396.                 del self.mice
  397.                 self.mice = newlist
  398.  
  399.             # Mouse joined room
  400.             if packet_type == '\x08\x08':
  401.                 msg = packet.oldProtocolSplit()
  402.                 m = Mouse(msg[1])
  403.                 self.mice.add(m)
  404.                 who = m.name
  405.                 self.micerz.append(who)
  406.                 self.event_mousejoined(m.name)
  407.                 self.event_micejoined(who)
  408.  
  409.             # Mouse left room
  410.             if packet_type == '\x08\x07':
  411.                 msg = packet.oldProtocolSplit()
  412.                 print 'Mouse left room',msg
  413.                 self.event_mouseleft(msg[2])
  414.                 self.micerz.remove(msg[2])
  415.  
  416.             # Join tribe request
  417.             if packet_type == '\x10\x0E':
  418.                 msg = packet.oldProtocolSplit()
  419.                 self.event_jointribe(msg[2], msg[3])
  420.            
  421.             # Mouse movement
  422.             if packet_type == '\x04\x04':
  423.                 packet.read(2)
  424.                 code = packet.read(4)
  425.                 data = packet.peek(13)
  426.                 goingright = packet.readint(1)
  427.                 goingleft = packet.readint(1)
  428.                 posx = packet.readint(2)
  429.                 posy = packet.readint(2)
  430.                 movx = packet.readint(2)
  431.                 movy = packet.readint(2)
  432.                 jump = bool(packet.readint(1))
  433.                 jumpimage = packet.readint(1)
  434.                 unk = packet.readint(1)
  435.                 mouse_code = packet.readint(4)
  436.  
  437.                 tm = self.mice.findByCode(str(mouse_code))
  438.                 if tm:
  439.                     tm.right = goingright; tm.left = goingleft
  440.                     tm.x = posx; tm.y = posy
  441.                     tm.jump = jump
  442.                     tm.speedx = movx; tm.speedy = movy
  443.                     if tm.right: tm.lastdir = 1
  444.                     if tm.left: tm.lastdir = -1
  445.  
  446.                 self.event_mousemove(mouse_code, data)
  447.  
  448.             # Got cheese
  449.             if packet_type == '\x05\x13':
  450.                 msg = packet.oldProtocolSplit()
  451.                 self.event_gotcheese(msg[1])
  452.             # Cheese to hole
  453.             if packet_type == '\x08\x06':
  454.                 msg = packet.oldProtocolSplit()
  455.                 self.event_hole(msg[1])
  456.  
  457.             # Mouse died
  458.             if packet_type == '\x08\x05':
  459.                 msg = packet.oldProtocolSplit()
  460.                 self.event_mousedied(msg[1])
  461.  
  462.             # Mouse ballooned
  463.             if packet_type == '\x08\x10':
  464.                 msg = packet.oldProtocolSplit()
  465.                 self.event_mouseballoon(msg[1])
  466.  
  467.             # 20 seconds remaining
  468.             if packet_type == '\x06\x11':
  469.                 self.event_20seconds()
  470.  
  471.             # Ducking
  472.             if packet_type == '\x04\x09':
  473.                 msg = packet.oldProtocolSplit()
  474.                 ducking = (len(msg) == 3)
  475.                 self.event_mouseduck(msg[1], ducking)
  476.  
  477.             # Emote
  478.             if packet_type == '\x08\x16':
  479.                 msg = packet.oldProtocolSplit()
  480.                 #print 'emote packet',msg
  481.                 mouse_id = msg[1]
  482.                 emote = ['dance','laugh','cry','kiss'][int(msg[2])-1]
  483.                 self.event_emote(mouse_id, emote)
  484.  
  485.             # Sync status
  486.             if packet_type == '\x08\x15':
  487.                 msg = packet.oldProtocolSplit()
  488.                 #print 'Sync status',msg
  489.                 self.event_syncstatus(msg[1])
  490.                 self.sync = False
  491.                 tm = self.mice.findByCode(msg[1])
  492.                 if tm and tm.name == self.username:
  493.                     #self.whisper("Randompeople", "OH NO, IM SYNC D: - changing sync...")
  494.                     if self.micerz == []:
  495.                         #self.whisper("Randompeople", "No mice in room, not changing sync.")
  496.                         pass
  497.                     else:
  498.                         newsync = choice(self.micerz)
  499.                         #self.whisper("Randompeople", "New sync: %s" % (newsync))
  500.                         self.command("sy "+newsync)
  501.                     #self.sync = True
  502.         except:
  503.             pass
  504.     def change_gravity(self, x_dir, y_dir, delay=0.0):
  505.         p = Packet('\x05\x22\x01')
  506.         p.write(str(x_dir)+'\x01')
  507.         p.write(str(y_dir))
  508.         p.setProtocol('old')
  509.         self.delay_packet(delay, p)
  510.     def move_cheese(self, nx, ny, delay=0.0):
  511.         p = Packet('\x05\x10\x01')
  512.         p.write(str(int(nx))+'\x01')
  513.         p.write(str(int(ny)))
  514.         p.protocol = 'old'
  515.         self.delay_packet(delay, p)
  516.     def conjuration(self, x, y, delay=0.0):
  517.         print 'Making conjuration at',(int(x),int(y))
  518.         p = Packet('\x04\x0E\x01')
  519.         p.write(str(int(x)))
  520.         p.write(str(int(y)))
  521.         p.setProtocol('old')
  522.         self.delay_packet(delay, p)
  523.  
  524.     # [type, id, X, Y, speedX, speedY, rot, rotspeed, dur(?), sleep]
  525.            
  526.     def remove_object(self, code, delay=0.0):
  527.         p = Packet('\x05\x18')
  528.         p.writeint(code, 2)
  529.         self.delay_packet(delay, p)
  530.     def explosion(self, x, y, power=1, radius=1,unk=True, delay=0.0):
  531.         p = Packet('\x05\x11\x01')
  532.         p.write(str(int(x))+'\x01')
  533.         p.write(str(int(y))+'\x01')
  534.         p.write(str(power)+'\x01')
  535.         p.write(str(radius)+'\x01')
  536.         p.write(str(int(unk))+'\x01')
  537.         p.write('1')
  538.         p.setProtocol('old')
  539.         self.delay_packet(delay, p)
  540.     def create_object(self, code, x, y, speedx=0, speedy=0, rot=0, normal=True):
  541.         #p = Packet('\x05\x15\x01')
  542.         #p.write(str(code)+'\x01')
  543.         #p.write(str(int(x))+'\x01')
  544.         #p.write(str(int(y))+'\x01')
  545.         #p.write(str(rot)+'\x01')  # 1
  546.         #p.write(str(speedx)+'\x01')
  547.         #p.write(str(speedy)+'\x01')
  548.         #p.write(str(int(normal))+'\x01') # 1
  549.         #p.setProtocol('old')
  550.  
  551.         p = Packet('\x05\x14')
  552.         p.writeint(code, 2)
  553.         p.write('\xFC\x16')
  554.         p.writeint(int(x), 2)
  555.         p.writeint(int(y), 2)
  556.         p.writeint(int(rot), 2)
  557.         p.writeint(int(speedx), 1)
  558.         p.writeint(int(speedy), 1)
  559.         p.writebyte(int(normal))
  560.         self.send(p)
  561.  
  562.         self.object_count += 1
  563.         return self.object_count-1
  564.  
  565.     def magic_begin(self, code, x, y, delay=0.0, rot=1):
  566.         p = Packet('\x05\x08\x01')
  567.         p.write(str(code)+'\x01')
  568.         p.write(str(x)+'\x01')
  569.         p.write(str(y)+'\x01')
  570.         p.write(str(int(rot)))
  571.         p.setProtocol('old')
  572.         self.delay_packet(delay, p, exe="self.casting = True")
  573.  
  574.     def magic_cast(self, code, x, y, delay=0.0, speedx=0, speedy=0, rot=0, normal=True):
  575.         p = Packet('\x05\x14')
  576.         p.writeint(code, 2)
  577.         p.write('\xFF\xFF')
  578.         p.writeint(int(x), 2)
  579.         p.writeint(int(y), 2)
  580.         p.writeint(int(rot), 2)
  581.         p.writebyte(int(speedx))
  582.         p.writebyte(int(speedy))
  583.         p.writebyte(int(normal))
  584.         self.delay_packet(delay, p)
  585.  
  586.     def magic_stop(self, delay=0.0):
  587.         p = Packet('\x05\x09')
  588.         p.setProtocol('old')
  589.         self.delay_packet(delay, p, exe="self.casting = False")
  590.  
  591.     def magic(self, code, x, y, delay=0.0, cast_time=1.0, speedx=0, speedy=0, rot=1, normal=True, attach=None):
  592.         if self.casting: return
  593.  
  594.         if not self.shaman: return
  595.         self.magic_begin(code,x,y, delay, rot)
  596.         self.magic_cast(code,x,y,delay+cast_time, speedx, speedy, rot, normal)
  597.         if attach:
  598.             self.attach_balloon(attach, delay+cast_time+0.2)
  599.         #else:
  600.         #    p = Packet('\x05\x07\x0111,0,0,0,0,-2,5.4,6.2,0')
  601.         #    p.setProtocol('old')
  602.         #    self.delay_packet(delay+cast_time+0.2, p)
  603.         self.magic_stop(delay+cast_time+0.2)
  604.  
  605.     def attach_balloon(self, mouse_code, delay=0.0):
  606.         p = Packet('\x08\x10\x01')
  607.         p.write(str(mouse_code))
  608.         p.setProtocol('old')
  609.         self.delay_packet(delay, p)
  610.  
  611.     def command(self, txt, delay=0.0):
  612.         p = Packet('\x06\x1A\x01')
  613.         p.setProtocol('old')
  614.         p.write(txt)
  615.         self.delay_packet(delay, p)
  616.     def tribe(self, txt):
  617.         p = Packet('\x06\x08')
  618.         p.writestring(txt)
  619.         self.send(p)
  620.     def modmessage(self, txt):
  621.         p = Packet('\x06\x0A\x00')
  622.         p.writestring(txt)
  623.         self.send(p)
  624.  
  625.     def chat(self, txt, delay=0.0):
  626.         p = Packet('\x06\x06')
  627.         p.writestring(txt)
  628.         self.delay_packet(delay, p)
  629.     def deathpacket(self):
  630.         p = Packet('\x0E\x03')
  631.         self.send(p)
  632.     def whisper(self, who, txt):
  633.         p = Packet('\x06\x07')
  634.         p.writestring(who)
  635.         p.writestring(txt)
  636.         self.send(p)
  637.  
  638.     def duck(self, state, delay=0.0):
  639.         if state == True: p = Packet('\x04\x09\x01\x31')
  640.         else: p = Packet('\x04\x09\x01\x30')
  641.         p.setProtocol('old')
  642.         self.delay_packet(delay, p)
  643.  
  644.     def get_cheese(self, delay=0.0):
  645.         p = Packet('\x05\x13\x01'+str(self.move_code))
  646.         p.setProtocol('old')
  647.         self.delay_packet(delay, p)
  648.     def modmessagez(self, txt):
  649.         p = Packet('\x06\x0A\x03')
  650.         p.writestring(txt)
  651.         self.send(p)
  652.     def hole(self, delay=0.0):
  653.         p = Packet('\x05\x12\x010\x01'+str(self.move_code))
  654.         p.setProtocol('old')
  655.         self.delay_packet(delay, p)
  656.  
  657.     def die(self, delay=0.0):
  658.         self.dead = True
  659.         p = Packet('\x04\x05')
  660.         p.writeint(int(self.move_code), 4)
  661.         self.delay_packet(delay, p)
  662.  
  663.     def change_room(self, new_room):
  664.         self.command("room "+new_room)
  665.  
  666.     def event_20seconds(self):
  667.         pass
  668.  
  669.     def event_mousemove(self, mouse_code, data):
  670.         pass
  671.  
  672.     def event_mouseduck(self, mouse_code, state):
  673.         pass
  674.  
  675.     def event_mousedied(self, mouse_code):
  676.         pass
  677.  
  678.     def event_mousejoined(self, mouse_name):
  679.         pass
  680.  
  681.     def event_mouseleft(self, mouse_name):
  682.         pass
  683.  
  684.     def event_mouseballoon(self, mouse_code):
  685.         pass
  686.  
  687.     def event_gotcheese(self, mouse_code):
  688.         pass
  689.  
  690.     def event_hole(self, mouse_code):
  691.         pass
  692.  
  693.     def event_emote(self, mouse_id, emote):
  694.         pass
  695.  
  696.     def event_shamaninfo(self, sha_ids):
  697.         pass
  698.  
  699.     def event_jointribe(self, who, tribename):
  700.         pass
  701.  
  702.     def event_newmap(self):
  703.         pass
  704.  
  705.     def event_chat(self, who, said):
  706.         pass
  707.  
  708.     def event_whisper(self, who, whispered):
  709.         pass
  710.  
  711.     def event_syncstatus(self, mouse_code):
  712.         pass
  713.  
  714.     def mainLoop(self):
  715.         # Send delayed packets
  716.         for packet_data in self.packet_queue:
  717.             if time.time() >= packet_data[0]:
  718.                 self.send(packet_data[1])
  719.                 if packet_data[2] != "":
  720.                     print packet_data[2]
  721.                 if packet_data[3] != "":
  722.                     exec(packet_data[3])
  723.                 try:
  724.                     self.packet_queue.remove(packet_data)
  725.                 except: pass
  726.  
  727.         # TZAT packet
  728.         if self.loginstate == "logged in" and time.time()-self.tzat_last >= 11.2:
  729.             self.TZAT()
  730.        
  731.         # Receive data from the server
  732.         data = ''
  733.         try:
  734.             data = self.sock.recv(2*1024)
  735.             if data == '':
  736.                 #print str(self.last_packet)
  737.                 print ' -- Connection problem --'
  738.                 #self.reset_data()
  739.         except socket.error:
  740.             pass
  741.  
  742.         #if data == '':
  743.         #    return
  744.  
  745.         #print 'Got:',str(Packet(data))
  746.        
  747.         self.r_data += data
  748.  
  749.         # Parse data
  750.         if self.r_data != "":
  751.             try:
  752.                 packet_size = struct.unpack('!L', self.r_data[:4])[0]
  753.             except:
  754.                 self.r_data = ""
  755.                 return
  756.  
  757.             #print 'Packet should be ',packet_size,', packet is ',len(self.r_data)
  758.             if packet_size <= len(self.r_data):
  759.                 p_data = self.r_data[:packet_size]
  760.                 self.r_data = self.r_data[packet_size:]
  761.  
  762.                 p = Packet(p_data)
  763.                 p.read(4)
  764.                 if p.peek(2) == '\x01\x01':
  765.                     p.read(4)
  766.                     p.setProtocol('old')
  767.  
  768.                 if not (p.peek(2) in ['\x04\x03']):
  769.                     pass
  770.                            
  771.                 self.parsePacket(p)
  772.  
  773.  
  774. class Testbot(Bot):
  775.     target = "Maqqara"
  776.     def __init__(self, *args, **kwargs):
  777.         Bot.__init__(self, *args, **kwargs)
  778.         self.timer = 0
  779.         self.last_code = 0
  780.  
  781.     def event_newmap(self):
  782.         self.command("sy Idibot")
  783.         self.command("npp @332541")
  784.  
  785.     def event_chat(self, who, said):
  786.         print '('+who+') said',said
  787.  
  788.         if said.lower().strip('!?.,') == "idibot die":
  789.             self.die()
  790.  
  791.     def event_syncstatus(self, mouse_code):
  792.         tm = self.mice.findByCode(mouse_code)
  793.         if tm:
  794.             print '** Sync:',tm.name,'**'
  795.  
  796.     def event_mousemove(self, mouse_code, data):
  797.         tm = self.mice.findByCode(str(mouse_code))
  798.         if tm and tm.name == self.target:
  799.             pass
  800.     def event_mouseduck(self, mouse_code, ducking):
  801.         tm = self.mice.findByCode(str(mouse_code))
  802.         if tm and tm.name == self.target:
  803.             if ducking:
  804.                 self.last_code = self.create_object(ForbiddenMagic.CANNONUP, 540, 128)
  805.    
  806.     def event_whisper(self, who, said):
  807.         print '(',who,') whispered',said
  808.  
  809.         if who == "Maqqara":
  810.             cmd = said.split()
  811.             if cmd[0] == 'spawn':
  812.                 obj = None
  813.                 try: obj = eval("Magic."+cmd[1].upper())
  814.                 except: pass
  815.  
  816.                 if obj:
  817.                     print 'Spawning',cmd[1],'...'
  818.                     self.last_code = self.create_object(obj, 540, 128)
  819.  
  820.             if cmd[0] == 'room':
  821.                 r = (' '.join(cmd[1:])).replace('<','<')
  822.                 self.command('room '+r)
  823.  
  824.  
  825. if __name__ == '__main__':
  826.     server = Server.EN1
  827.     bot = Testbot("bottest6", "Idibot", "xxxxxx")
  828.    
  829.     success = bot.connect(server)
  830.     if not success:
  831.         print 'Connecting to the server failed.'
  832.         sys.exit(1)
  833.  
  834.     while True:
  835.         bot.mainLoop()
  836.  
  837.         if bot.loginstate == "ready to login":
  838.             print 'Logging in...'
  839.             bot.login()
  840.  
  841.         if bot.disconnected:
  842.             print 'Attempting to re-connect...'
  843.             time.sleep(5)
  844.             success = bot.connect(server)
  845.             if not success:
  846.                 print 'Re-connecting failed.'
Add Comment
Please, Sign In to add comment