Guest User

Untitled

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