Guest User

Untitled

a guest
Jun 3rd, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.03 KB | None | 0 0
  1. import struct
  2. import socket
  3. import time
  4. import hashlib
  5. import sys
  6. import os
  7. import platform
  8. from copy import deepcopy
  9.  
  10. try:
  11. import psyco
  12. psyco.full()
  13. print 'Psyco enabled.'
  14. except:
  15. print 'Psyco disabled.'
  16.  
  17.  
  18. '''
  19. class ConsoleColor:
  20. def __init__(self):
  21. self.platf = platform.system()
  22. if self.platf == 'Windows':
  23. import WConio
  24. else:
  25. from termcolor import colored
  26.  
  27. def __call__(self, col, txt):
  28. if col == 'mod':
  29. if self.platf == 'Windows':
  30. WConio.textcolor(WConio.LIGHTMAGENTA)
  31. print txt
  32. WConio.textcolor(WConio.LIGHTGREY)
  33. else:
  34. colored(txt, 'yellow')
  35.  
  36. if col == 'arb':
  37. if self.platf == 'Windows':
  38. WConio.textcolor(WConio.MAGENTA)
  39. print txt
  40. WConio.textcolor(WConio.LIGHTGREY)
  41. else:
  42. colored(txt, 'yellow')
  43.  
  44. if col == 'green':
  45. if self.platf == 'Windows':
  46. WConio.textcolor(WConio.LIGHTGREEN)
  47. print txt
  48. WConio.textcolor(WConio.LIGHTGREY)
  49. else:
  50. colored(txt, 'green')
  51.  
  52.  
  53. if col == 'blue':
  54. if self.platf == 'Windows':
  55. WConio.textcolor(WConio.LIGHTBLUE)
  56. print txt
  57. WConio.textcolor(WConio.LIGHTGREY)
  58. else:
  59. colored(txt, 'blue')
  60.  
  61.  
  62.  
  63. con_color = ConsoleColor()
  64. '''
  65.  
  66. try:
  67. import WConio as con
  68. except:
  69. class NullCon:
  70. def textcolor(self, col):
  71. pass
  72.  
  73. def __getattr__(self, v):
  74. return None
  75.  
  76. con = NullCon()
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. version = 151
  84.  
  85. def bin_to_str(b):
  86. return ' '.join([hex(ord(c))[2:].rjust(2,'0') for c in b])
  87.  
  88. import code
  89. def debugger(namespace):
  90. code.interact("<Bot debugging console>", local=namespace)
  91.  
  92. class Server:
  93. EN1 = ('91.121.28.82', 443)
  94. EN2 = ('95.142.173.20', 44444)
  95. FR = ('91.121.116.13', 443)
  96. RU = ('91.121.85.153', 443)
  97. KR = ('91.121.157.83', 44444)
  98. BR = ('92.243.6.199', 44444)
  99. BR2 = ('173.246.100.255', 44444)
  100. BR3 = ('173.246.103.161', 44444)
  101. TR = ('92.243.10.245', 44444)
  102. CN = ('95.142.170.114', 443)
  103. class Packet:
  104. int_size = {1:'b', 2:'h', 4:'l'}
  105.  
  106. def __init__(self, data=""):
  107. self.data = data
  108. self.protocol = 'new'
  109.  
  110. def setProtocol(self, t):
  111. self.protocol = t
  112.  
  113. def peek(self, b):
  114. return self.data[:b]
  115. def read(self, b):
  116. ret = self.data[:b]
  117. self.data = self.data[b:]
  118. return ret
  119. def readint(self, b):
  120. s = self.read(b)
  121. #return int(s.encode('hex'), 16)
  122. try:
  123. return struct.unpack('!'+self.int_size[b], s)[0]
  124. except:
  125. return 0
  126. def readstring(self):
  127. s_len = self.readint(2)
  128. return self.read(s_len)
  129.  
  130. def write(self, s):
  131. self.data += s
  132. def writeint(self, i, size):
  133. size_c = self.int_size[size]
  134. self.data += struct.pack('!'+size_c, i)
  135. def writebyte(self, b):
  136. self.data += chr(b)
  137. def writestring(self, s):
  138. self.writeint(len(s), 2)
  139. self.write(s)
  140.  
  141. def oldProtocolSplit(self):
  142. ret = self.data.split('\x01')
  143. ret = map(lambda s: s.replace('\x00',''), ret)
  144. return ret
  145.  
  146. def size(self):
  147. return len(self.data)
  148.  
  149. def __str__(self):
  150. if len(self.data) > 0: return bin_to_str(self.data)
  151. else: return 'Empty packet'
  152.  
  153.  
  154. class PrefixGen:
  155. def __init__(self, data):
  156. self.MDT = []
  157. self.data = data
  158. self.data = self.data.replace('\x00','')
  159. message = self.data.split('\x01')
  160. LCMDT = list(message[2])
  161. for c in map(int, LCMDT):
  162. if c == 0: self.MDT.append(chr(10))
  163. else: self.MDT.append(chr(c))
  164.  
  165. self.CMDTEC = int(message[3])
  166.  
  167. def __call__(self):
  168. final = ""
  169. loc_2 = map(int, list(str(self.CMDTEC%9000 + 1000)))
  170. final = ''.join([self.MDT[x] for x in loc_2])
  171. self.CMDTEC += 1
  172. return final
  173.  
  174.  
  175. class Mouse:
  176. mice_ref = []
  177.  
  178. def __init__(self, mousedata):
  179. data = mousedata.split('#')
  180. #print 'MouseData:',data
  181. self.name = "Unknown"
  182. self.code = "0"
  183. self.score = 0
  184. self.codeforum = "0"
  185. self.dead = False
  186.  
  187. if len(data) == 9:
  188. self.name = data[0]
  189. self.code = data[1]
  190. self.score = int(data[3])
  191. self.codeforum = data[8]
  192. self.dead = data[2] == "1"
  193. self.x = 0
  194. self.y = 0
  195. self.speedx = 0
  196. self.speedy = 0
  197. self.jump = False
  198. self.left = False
  199. self.right = False
  200. self.lastdir = 0
  201.  
  202.  
  203.  
  204. class MouseList:
  205. def __init__(self):
  206. self.mice = []
  207.  
  208. def clear(self):
  209. self.mice = []
  210.  
  211. def add(self, m):
  212. self.mice.append(m)
  213.  
  214. def remove(self, m):
  215. self.mice.remove(m)
  216.  
  217. def getCodes(self):
  218. return [m.code for m in self.mice]
  219.  
  220. def findBy(self, lamb, con):
  221. f = filter(lamb, con)
  222. if len(f) > 0:
  223. return f[0]
  224. else: return None
  225.  
  226. def findByCode(self, code):
  227. return self.findBy(lambda m: m.code == code, self.mice)
  228.  
  229. def findByForumCode(self, fcode):
  230. return self.findBy(lambda m: m.forumcode == fcode, self.mice)
  231.  
  232. def findByName(self, name):
  233. return self.findBy(lambda m: m.name == name, self.mice)
  234.  
  235.  
  236. class Magic:
  237. ARROW = 0
  238. SMALLBOX = 1
  239. LARGEBOX = 2
  240. SMALLBOARD = 3
  241. LARGEBOARD = 4
  242. BALL = 6
  243. TRAMPOLINE = 7
  244. ANVIL = 10
  245. CANNONDOWN = 18
  246. CANNONRIGHT = 19
  247. CANNONLEFT = 20
  248. SPIRIT = 24
  249. RUNE = 32
  250.  
  251. class ForbiddenMagic:
  252. REDPOINT = 11
  253. REDLEFT = 12
  254. REDRIGHT = 13
  255. BLUEPOINT = 14
  256. BLUERIGHT = 15
  257. BLUELEFT = 16
  258. YELLOWPOINT = 22
  259. CANNONUP = 17
  260. BOMB = 23
  261. CHEESE = 25
  262. BLUEPORTAL = 26
  263. ORANGEPORTAL = 27
  264. BALLOON = 28
  265. REDBALLOON = 29
  266. GREENBALLOON = 30
  267. YELLOWBALLOON = 31
  268. SNOW = 34
  269. #HEAVYBALL = 5
  270. #SMALLROUGHPLANK = 8
  271. #LARGEROUGHPLANK = 9
  272. #STICKYBALL = 21
  273.  
  274.  
  275. class Bot:
  276. def __init__(self, roomname, username, password=""):
  277. self.sock = None
  278. self.GeneratePrefix = None
  279. self.r_data = ""
  280. self.packet_queue = []
  281. self.o_username = username
  282. self.username = username
  283. self.password = password
  284. self.roomname = roomname
  285. self.loginstate = "not ready"
  286. self.tzat_last = time.time()
  287. self.move_code = "0"
  288. self.mice = MouseList()
  289. self.disconnected = True
  290. self.shaman = False
  291. self.sync = False
  292. self.running = True
  293. self.stayalive = True
  294. self.casting = False
  295. self.object_count = 0
  296. self.map_number = 0
  297. self.dead = False
  298. self.last_packet = None
  299.  
  300. def reset_data(self):
  301. self.disconnected = True
  302. self.sync = False
  303. self.casting = False
  304. self.r_data = ""
  305. self.packet_queue = []
  306. self.tzat_last = time.time()
  307. self.object_count = 0
  308. self.loginstate = "not ready"
  309.  
  310. def TZAT(self):
  311. self.tzat_last = time.time()
  312. p = Packet("\x1A\x02111000")
  313. self.send(p)
  314.  
  315. def send(self, packet, prefix=True):
  316. p_size = packet.size()+8
  317. if packet.protocol=='old': p_size += 4
  318.  
  319. data = Packet()
  320. data.writeint(p_size, 4)
  321. pre = '\x00\x00\x00\x00' if not self.GeneratePrefix else self.GeneratePrefix()
  322. if prefix == False: pre = '\x00\x00\x00\x00'
  323. data.write(pre)
  324. if packet.protocol=='old':
  325. data.write('\x01\x01')
  326. data.writeint(packet.size(), 2)
  327. data.write(packet.data)
  328. #print 'Sending:',str(data)
  329. try:
  330. self.last_packet = deepcopy(data)
  331. self.sock.send(data.data)
  332. except:
  333. print 'Connection problem.'
  334. print 'crash packet',str(data)
  335. #os.system("pause")
  336. self.reset_data()
  337.  
  338. def delay_packet(self, delay, packet, msg="", exe=""):
  339. self.packet_queue.append([time.time()+delay, packet, msg, exe])
  340.  
  341. def sendVersion(self):
  342. packet = Packet()
  343. packet.writebyte(0x1C)
  344. packet.writebyte(0x01)
  345. packet.writeint(version, 2)
  346. self.send(packet, prefix=False)
  347.  
  348. def login(self):
  349. passwordhash = hashlib.sha256(self.password).hexdigest()
  350. if self.password == "":
  351. passwordhash = ""
  352. print 'password:',self.password.encode('base64'),'passhash',passwordhash
  353. p = Packet()
  354. p.setProtocol('old')
  355. p.write('\x1A\x04\x01')
  356. p.write(self.o_username)
  357. p.write('\x01')
  358. p.write(passwordhash)
  359. p.write('\x01')
  360. p.write(self.roomname)
  361. self.send(p)
  362. self.loginstate = "logged in"
  363. if self.password == "":
  364. self.username = '*'+self.username
  365.  
  366. def connect(self, server):
  367. self.reset_data()
  368. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  369. try:
  370. self.sock.connect(server)
  371. except Exception as ex:
  372. print ex
  373. return False
  374. self.sendVersion()
  375. time.sleep(1)
  376. self.sock.setblocking(False)
  377. self.disconnected = False
  378. return True
  379.  
  380. def parsePacket(self, packet):
  381. packet_type = packet.peek(2)
  382.  
  383. # Mice in server & prefix gen data
  384. if packet_type == '\x1A\x1B':
  385. msg = packet.oldProtocolSplit()
  386. print 'Mice in server:',int(msg[1])
  387. self.GeneratePrefix = PrefixGen(packet.data)
  388. self.loginstate = "ready to login"
  389.  
  390. # Name data
  391. if packet_type == '\x1A\x08':
  392. msg = packet.oldProtocolSplit()
  393. self.username = msg[1]
  394. self.event_login()
  395.  
  396. # Ping
  397. if packet_type == '\x1A\x1A':
  398. #print ' * PING *'
  399. p = Packet()
  400. p.setProtocol('old')
  401. p.write('\x1A\x1A')
  402. self.delay_packet(11, p) #, ' * PONG *')
  403.  
  404. # New Map
  405. if packet_type == '\x05\x05':
  406. self.dead = False
  407. msg = packet.oldProtocolSplit()
  408. self.map_number = int(msg[1])
  409. self.move_code = msg[3]
  410. self.object_count = 0
  411. if self.stayalive:
  412. keepalive = Packet("\x04\x0A")
  413. keepalive.setProtocol('old')
  414. self.send(keepalive)
  415. self.event_newmap()
  416.  
  417. # New room
  418. if packet_type == '\x05\x15':
  419. msg = packet.oldProtocolSplit()
  420. self.roomname = msg[1]
  421. self.event_newroom(self.roomname)
  422.  
  423. # Chat
  424. if packet_type == '\x06\x06':
  425. packet.read(2)
  426. number = packet.readint(4)
  427. who = packet.readstring()
  428. said = packet.readstring()
  429. self.event_chat(who, said)
  430.  
  431. # Whisper
  432. if packet_type == '\x06\x07':
  433. #print 'WHISPER,',str(packet)
  434. packet.read(2)
  435. sent_by_me = not bool(packet.readint(1))
  436. #print 'sent_by_me',sent_by_me
  437. who = packet.readstring()
  438. try:
  439. whispered = packet.readstring()
  440. except:
  441. whispered = ""
  442.  
  443. if not sent_by_me:
  444. self.event_whisper(who, whispered)
  445.  
  446. # Mod message
  447. if packet_type == '\x06\x0A':
  448. packet.read(2)
  449. arb = not(packet.read(1)=='\x03')
  450. who = packet.readstring()
  451. said = packet.readstring()
  452. self.event_modmessage(who, said, arb=arb)
  453. #print 'Mod message ['+who+'] '+said
  454.  
  455. # Server message
  456. if packet_type == '\x06\x14':
  457. msg = packet.oldProtocolSplit()
  458. self.event_servermessage(msg[1])
  459. if packet_type == '\x1A\x05':
  460. msg = packet.oldProtocolSplit()
  461. self.event_servermessage(msg[2])
  462. if packet_type == '\x1A\x06':
  463. msg = packet.oldProtocolSplit()
  464. self.event_servermessage(msg[2], arb=True)
  465.  
  466. # Shaman info
  467. if packet_type == '\x08\x14':
  468. msg = packet.oldProtocolSplit()
  469. #print 'Shaman info',msg
  470. if len(msg) > 1:
  471. self.shaman = False
  472. sha_ids = msg[1:]
  473. for sha_id in sha_ids:
  474. tm = self.mice.findByCode(sha_id)
  475. if tm:
  476. #print "Shaman:",repr(tm.name)
  477. #print repr(tm.name),"vs.",repr(self.username)
  478. if tm.name == self.username:
  479. #print 'I am shaman'
  480. self.shaman = True
  481.  
  482. self.event_shamaninfo(sha_ids)
  483.  
  484. # Mice in room
  485. if packet_type == '\x08\x09':
  486. msg = packet.oldProtocolSplit()
  487. newlist = MouseList()
  488. for mouseinfo in msg[1:]:
  489. m_object = Mouse(mouseinfo)
  490. newlist.add(m_object)
  491.  
  492. #old_codes = self.mice.getCodes()
  493. #for new_mouse in newlist.getCodes():
  494. # if not new_mouse in old_codes:
  495. # new_m = newlist.findByCode(new_mouse)
  496. #print 'New mouse',newlist.findByCode(new_mouse).name
  497.  
  498. del self.mice
  499. self.mice = newlist
  500.  
  501. # Mouse joined room
  502. if packet_type == '\x08\x08':
  503. msg = packet.oldProtocolSplit()
  504. print 'Mouse joined room ',msg
  505. m = Mouse(msg[1])
  506. self.mice.add(m)
  507. self.event_mousejoined(m.name)
  508.  
  509. # Mouse left room
  510. if packet_type == '\x08\x07':
  511. msg = packet.oldProtocolSplit()
  512. print 'Mouse left room',msg
  513. tm = self.mice.findByCode(msg[2])
  514. if tm: self.mice.remove(tm)
  515. self.event_mouseleft(msg[2])
  516.  
  517. # Join tribe request
  518. if packet_type == '\x10\x0E':
  519. msg = packet.oldProtocolSplit()
  520. self.event_jointribe(msg[2], msg[3])
  521.  
  522. # Mouse movement
  523. if packet_type == '\x04\x04':
  524. packet.read(2)
  525. code = packet.read(4)
  526. data = packet.peek(13)
  527. goingright = packet.readint(1)
  528. goingleft = packet.readint(1)
  529. posx = packet.readint(2)
  530. posy = packet.readint(2)
  531. movx = packet.readint(2)
  532. movy = packet.readint(2)
  533. jump = bool(packet.readint(1))
  534. jumpimage = packet.readint(1)
  535. unk = packet.readint(1)
  536. mouse_code = packet.readint(4)
  537.  
  538. tm = self.mice.findByCode(str(mouse_code))
  539. if tm:
  540. tm.right = goingright; tm.left = goingleft
  541. tm.x = posx; tm.y = posy
  542. tm.jump = jump
  543. tm.speedx = movx; tm.speedy = movy
  544. if tm.right: tm.lastdir = 1
  545. if tm.left: tm.lastdir = -1
  546.  
  547. self.event_mousemove(mouse_code, data)
  548.  
  549. # Got cheese
  550. if packet_type == '\x05\x13':
  551. msg = packet.oldProtocolSplit()
  552. self.event_gotcheese(msg[1])
  553.  
  554. # Cheese to hole
  555. if packet_type == '\x08\x06':
  556. msg = packet.oldProtocolSplit()
  557. self.event_hole(msg[1])
  558.  
  559. # Mouse died
  560. if packet_type == '\x08\x05':
  561. msg = packet.oldProtocolSplit()
  562. self.event_mousedied(msg[1])
  563. tm = self.mice.findByCode(msg[1])
  564. if tm: tm.dead = True
  565.  
  566. # Mouse ballooned
  567. if packet_type == '\x08\x10':
  568. msg = packet.oldProtocolSplit()
  569. self.event_mouseballoon(msg[1])
  570.  
  571. # 20 seconds remaining
  572. if packet_type == '\x06\x11':
  573. self.event_20seconds()
  574.  
  575. # Ducking
  576. if packet_type == '\x04\x09':
  577. msg = packet.oldProtocolSplit()
  578. ducking = (len(msg) == 3)
  579. self.event_mouseduck(msg[1], ducking)
  580.  
  581. # Emote
  582. if packet_type == '\x08\x16':
  583. msg = packet.oldProtocolSplit()
  584. #print 'emote packet',msg
  585. mouse_id = msg[1]
  586. emote = ['dance','laugh','cry','kiss'][int(msg[2])-1]
  587. self.event_emote(mouse_id, emote)
  588.  
  589. # Sync status
  590. if packet_type == '\x08\x15':
  591. msg = packet.oldProtocolSplit()
  592. #print 'Sync status',msg
  593. self.event_syncstatus(msg[1])
  594. self.sync = False
  595. tm = self.mice.findByCode(msg[1])
  596. if tm and tm.name == self.username:
  597. self.sync = True
  598.  
  599. # Tribe stuff
  600. if packet_type == '\x06\x08':
  601. packet.read(2)
  602. msg = packet.readstring()
  603. who = packet.readstring()
  604. self.event_tribechat(who, msg)
  605.  
  606. def change_gravity(self, x_dir, y_dir, delay=0.0):
  607. p = Packet('\x05\x22\x01')
  608. p.write(str(x_dir)+'\x01')
  609. p.write(str(y_dir))
  610. p.setProtocol('old')
  611. self.delay_packet(delay, p)
  612.  
  613. def move_cheese(self, nx, ny, delay=0.0):
  614. p = Packet('\x05\x10\x01')
  615. p.write(str(int(nx))+'\x01')
  616. p.write(str(int(ny)))
  617. p.protocol = 'old'
  618. self.delay_packet(delay, p)
  619.  
  620. def conjuration(self, x, y, delay=0.0):
  621. print 'Making conjuration at',(int(x),int(y))
  622. p = Packet('\x04\x0E\x01')
  623. p.write(str(int(x)))
  624. p.write(str(int(y)))
  625. p.setProtocol('old')
  626. self.delay_packet(delay, p)
  627.  
  628. # [type, id, X, Y, speedX, speedY, rot, rotspeed, dur(?), sleep]
  629. def update_objects(self, obj_list, delay=0.0):
  630. p = Packet('\x04\x03')
  631. p.writeint(int(self.move_code), 4)
  632.  
  633. for obj in obj_list:
  634. p.writeint(obj[0], 2)
  635. #p.writeint(obj[1], 2)
  636. p.writeint(obj[2], 2)
  637. p.writeint(obj[3], 2)
  638. p.writeint(obj[4], 2)
  639. p.writeint(obj[5], 2)
  640. p.writeint(obj[6], 2)
  641. p.writeint(obj[7], 2)
  642. p.writebyte(obj[8])
  643. p.writebyte(obj[9])
  644.  
  645. print 'Sending',str(p)
  646. self.delay_packet(delay, p)
  647.  
  648.  
  649. def remove_object(self, code, delay=0.0):
  650. p = Packet('\x05\x18')
  651. p.writeint(code, 2)
  652. self.delay_packet(delay, p)
  653.  
  654. def explosion(self, x, y, power=1, radius=1,unk=True, delay=0.0):
  655. p = Packet('\x05\x11\x01')
  656. p.write(str(int(x))+'\x01')
  657. p.write(str(int(y))+'\x01')
  658. p.write(str(power)+'\x01')
  659. p.write(str(radius)+'\x01')
  660. p.write(str(int(unk))+'\x01')
  661. p.write('1')
  662. p.setProtocol('old')
  663. self.delay_packet(delay, p)
  664.  
  665. def create_object(self, code, x, y, speedx=0, speedy=0, rot=0, normal=True, delay=0.0):
  666. #p = Packet('\x05\x15\x01')
  667. #p.write(str(code)+'\x01')
  668. #p.write(str(int(x))+'\x01')
  669. #p.write(str(int(y))+'\x01')
  670. #p.write(str(rot)+'\x01') # 1
  671. #p.write(str(speedx)+'\x01')
  672. #p.write(str(speedy)+'\x01')
  673. #p.write(str(int(normal))+'\x01') # 1
  674. #p.setProtocol('old')
  675.  
  676. p = Packet('\x05\x14')
  677. p.writeint(code, 2)
  678. p.writeint(int(x), 2)
  679. p.writeint(int(y), 2)
  680. p.writeint(int(rot), 2)
  681. p.writeint(int(speedx), 1)
  682. p.writeint(int(speedy), 1)
  683. #p.write('\xFF\x00')
  684. p.writebyte(int(normal))
  685. self.delay_packet(delay, p)
  686.  
  687. self.object_count += 1
  688. return self.object_count-1
  689.  
  690. def magic_begin(self, code, x, y, delay=0.0, rot=1):
  691. p = Packet('\x05\x08\x01')
  692. p.write(str(code)+'\x01')
  693. p.write(str(x)+'\x01')
  694. p.write(str(y)+'\x01')
  695. p.write(str(int(rot)))
  696. p.setProtocol('old')
  697. if delay==0.0: self.send(p)
  698. else: self.delay_packet(delay, p, exe="self.casting = True")
  699.  
  700. def magic_cast(self, code, x, y, delay=0.0, speedx=0, speedy=0, rot=0, normal=True):
  701. p = Packet('\x05\x14')
  702. p.writeint(code, 2)
  703. p.writeint(int(x), 2)
  704. p.writeint(int(y), 2)
  705. p.writeint(int(rot), 2)
  706. p.writebyte(int(speedx))
  707. p.writebyte(int(speedy))
  708. p.writebyte(int(normal))
  709. if delay==0.0: self.send(p)
  710. else: self.delay_packet(delay, p)
  711.  
  712. def magic_stop(self, delay=0.0):
  713. p = Packet('\x05\x09')
  714. p.setProtocol('old')
  715. self.delay_packet(delay, p, exe="self.casting = False")
  716.  
  717. def magic(self, code, x, y, delay=0.0, cast_time=1.0, speedx=0, speedy=0, rot=1, normal=True, attach=None):
  718. if self.casting: return
  719.  
  720. if not self.shaman: return
  721. self.magic_begin(code,x,y, delay, rot)
  722. self.magic_cast(code,x,y,delay+cast_time, speedx, speedy, rot, normal)
  723. if attach:
  724. self.attach_balloon(attach, delay+cast_time+0.2)
  725. #else:
  726. # p = Packet('\x05\x07\x0111,0,0,0,0,-2,5.4,6.2,0')
  727. # p.setProtocol('old')
  728. # self.delay_packet(delay+cast_time+0.2, p)
  729. self.magic_stop(delay+cast_time+0.2)
  730.  
  731. self.object_count += 1
  732. return self.object_count-1
  733.  
  734. def create_anchor(self, anchor, obj=0, x=0, y=0, angle=0, x2=0, y2=0, delay=0.0):
  735. p = Packet('\x05\x07\x01')
  736. p.write(str(anchor)+',')
  737. p.write(str(obj)+',')
  738. p.write(str(x)+',')
  739. p.write(str(y)+',')
  740. p.write(str(angle)+',')
  741. p.write('-2,')
  742. p.write(str(x2)+',')
  743. p.write(str(y2)+',')
  744. p.setProtocol('old')
  745. self.delay_packet(delay, p)
  746.  
  747. def attach_balloon(self, mouse_code, delay=0.0):
  748. p = Packet('\x08\x10\x01')
  749. p.write(str(mouse_code))
  750. p.setProtocol('old')
  751. self.delay_packet(delay, p)
  752.  
  753. def command(self, txt, delay=0.0):
  754. p = Packet('\x06\x1A\x01')
  755. p.setProtocol('old')
  756. p.write(txt)
  757. self.delay_packet(delay, p)
  758.  
  759. def modmessage(self, txt, delay=0.0):
  760. p = Packet('\x06\x0A\x00')
  761. p.writestring(txt)
  762. self.delay_packet(delay, p)
  763.  
  764. def modchat(self, txt):
  765. p = Packet('\x06\x0A')
  766. p.write('\x03')
  767. p.writestring(txt)
  768. self.send(p)
  769.  
  770. def arbchat(self, txt):
  771. p = Packet('\x06\x0A')
  772. p.write('\x02')
  773. p.writestring(txt)
  774. self.send(p)
  775.  
  776. def chat(self, txt, delay=0.0):
  777. p = Packet('\x06\x06')
  778. p.writestring(txt)
  779. self.delay_packet(delay, p)
  780.  
  781. def tribechat(self, txt, delay=0.0):
  782. p = Packet('\x06\x08')
  783. p.writestring(txt)
  784. self.delay_packet(delay, p)
  785.  
  786. def whisper(self, who, txt):
  787. p = Packet('\x06\x07')
  788. p.writestring(who)
  789. p.writestring(txt)
  790. self.send(p)
  791.  
  792. def duck(self, state, delay=0.0):
  793. if state == True: p = Packet('\x04\x09\x01\x31')
  794. else: p = Packet('\x04\x09\x01\x30')
  795. p.setProtocol('old')
  796. self.delay_packet(delay, p)
  797.  
  798. def get_cheese(self, delay=0.0):
  799. p = Packet('\x05\x13\x01'+str(self.move_code))
  800. p.setProtocol('old')
  801. self.delay_packet(delay, p)
  802.  
  803. def hole(self, delay=0.0):
  804. p = Packet('\x05\x12\x010\x01'+str(self.move_code))
  805. p.setProtocol('old')
  806. self.delay_packet(delay, p)
  807.  
  808. def die(self, delay=0.0):
  809. self.dead = True
  810. p = Packet('\x04\x05')
  811. p.writeint(int(self.move_code), 4)
  812. self.delay_packet(delay, p)
  813.  
  814. def change_room(self, new_room):
  815. self.command("room "+new_room)
  816.  
  817. def event_20seconds(self):
  818. pass
  819.  
  820. def event_mousemove(self, mouse_code, data):
  821. pass
  822.  
  823. def event_mouseduck(self, mouse_code, state):
  824. pass
  825.  
  826. def event_mousedied(self, mouse_code):
  827. pass
  828.  
  829. def event_mousejoined(self, mouse_name):
  830. pass
  831.  
  832. def event_mouseleft(self, mouse_name):
  833. pass
  834.  
  835. def event_mouseballoon(self, mouse_code):
  836. pass
  837.  
  838. def event_gotcheese(self, mouse_code):
  839. pass
  840.  
  841. def event_hole(self, mouse_code):
  842. pass
  843.  
  844. def event_emote(self, mouse_id, emote):
  845. pass
  846.  
  847. def event_shamaninfo(self, sha_ids):
  848. pass
  849.  
  850. def event_jointribe(self, who, tribename):
  851. pass
  852.  
  853. def event_newmap(self):
  854. pass
  855.  
  856. def event_chat(self, who, said):
  857. pass
  858.  
  859. def event_tribechat(self, who, said):
  860. pass
  861.  
  862. def event_whisper(self, who, whispered):
  863. pass
  864.  
  865. def event_syncstatus(self, mouse_code):
  866. pass
  867.  
  868. def event_modmessage(self, who, said, arb=False):
  869. pass
  870.  
  871. def event_servermessage(self, msg, arb=False):
  872. pass
  873.  
  874. def event_newroom(self, name):
  875. pass
  876.  
  877. def event_login(self):
  878. pass
  879.  
  880. def mainLoop(self):
  881. # Send delayed packets
  882. for packet_data in self.packet_queue:
  883. if time.time() >= packet_data[0]:
  884. self.send(packet_data[1])
  885. if packet_data[2] != "":
  886. print packet_data[2]
  887. if packet_data[3] != "":
  888. exec(packet_data[3])
  889. try:
  890. self.packet_queue.remove(packet_data)
  891. except: pass
  892.  
  893. # TZAT packet
  894. if self.loginstate == "logged in" and time.time()-self.tzat_last >= 11.2:
  895. self.TZAT()
  896.  
  897. # Receive data from the server
  898. data = ''
  899. try:
  900. data = self.sock.recv(2*1024)
  901. if data == '':
  902. print str(self.last_packet)
  903. print ' -- Connection problem --'
  904. self.reset_data()
  905. except socket.error:
  906. pass
  907.  
  908. #if data == '':
  909. # return
  910.  
  911. #print 'Got:',str(Packet(data))
  912.  
  913. self.r_data += data
  914.  
  915. # Parse data
  916. if self.r_data != "":
  917. try:
  918. packet_size = struct.unpack('!L', self.r_data[:4])[0]
  919. except:
  920. self.r_data = ""
  921. return
  922.  
  923. #print 'Packet should be ',packet_size,', packet is ',len(self.r_data)
  924. if packet_size <= len(self.r_data):
  925. p_data = self.r_data[:packet_size]
  926. self.r_data = self.r_data[packet_size:]
  927.  
  928. p = Packet(p_data)
  929. p.read(4)
  930. if p.peek(2) == '\x01\x01':
  931. p.read(4)
  932. p.setProtocol('old')
  933.  
  934. if not (p.peek(2) in ['\x04\x03']):
  935. pass
  936.  
  937. self.parsePacket(p)
  938.  
  939.  
  940.  
  941.  
  942. class Testbot(Bot):
  943. def __init__(self, *args, **kwargs):
  944. Bot.__init__(self, *args, **kwargs)
  945. self.lastcode = 0
  946. self.target = 'Maqqara'
  947.  
  948. self.maprot = ['1','2','3','4','5']
  949.  
  950. self.timer = time.time()
  951.  
  952. def event_chat(self, who, said):
  953. print '['+who+'] '+said
  954.  
  955. def event_newmap(self):
  956. print ' *** New map *** :',time.ctime()
  957.  
  958. mp = self.maprot.pop(0)
  959. self.command('npp '+mp)
  960. self.maprot.append(mp)
  961.  
  962. def event_syncstatus(self, mouse_code):
  963. tm = self.mice.findByCode(mouse_code)
  964. if tm:
  965. print 'Sync:',tm.name
  966.  
  967. def event_whisper(self, who, w):
  968. if who == 'Maqqara':
  969. cmd = w.split(' ')
  970. if cmd[0] == 'die': self.die()
  971. if cmd[0] == 'shutdown':
  972. self.sock.close()
  973. self.running = False
  974.  
  975. if cmd[0] == 'ms':
  976. self.modmessage(' '.join(cmd[1:]))
  977.  
  978. if cmd[0] == 'm':
  979. self.modchat(' '.join(cmd[1:]))
  980.  
  981. if cmd[0] == 'cmd':
  982. self.command(' '.join(cmd[1:]))
  983.  
  984. def event_modmessage(self, who, msg, arb=False):
  985. if who != "":
  986. con.textcolor(con.LIGHTMAGENTA)
  987. if arb: con.textcolor(con.MAGENTA)
  988. arb_s = '' if not arb else '[A]'
  989. message = arb_s+'['+who+'] '+msg
  990. print message
  991. #if self.whisperchat and self.modfilter(message): self.whisper('Maqqara', message)
  992. con.textcolor(con.LIGHTGREY)
  993.  
  994. def event_servermessage(self, msg, arb=False):
  995. con.textcolor(con.LIGHTGREEN)
  996. if arb: con.textcolor(con.GREEN)
  997. arb_s = '' if not arb else '[A]'
  998. message = arb_s+'[Server] '+msg
  999. print message
  1000. #if self.whisperchat and self.modfilter(message): self.whisper('Maqqara', message)
  1001. con.textcolor(con.LIGHTGREY)
  1002.  
  1003.  
  1004. '''
  1005. def event_mousemove(self, mouse_code, data):
  1006. tm = self.mice.findByCode(str(mouse_code))
  1007. if tm and tm.name == self.target:
  1008. self.update_objects([[Magic.ANVIL, self.lastcode, tm.x, tm.y, tm.speedx, tm.speedy, 0, 0, False, False]])
  1009.  
  1010.  
  1011. def event_mouseduck(self, mouse_code, ducking):
  1012. tm = self.mice.findByCode(str(mouse_code))
  1013. if tm and tm.name != self.username:
  1014. if ducking and tm.name == self.target:
  1015. print 'spawn',(tm.x,tm.y)
  1016.  
  1017. #self.move_cheese(tm.x/3.3, tm.y/3.5)
  1018.  
  1019. self.lastcode = self.create_object(Magic.ANVIL, tm.x/3.3, tm.y/3.5)
  1020. #self.create_anchor(11, self.lastcode, x=0, y=0, x2=tm.x/100.3, y2=tm.y/100.5)
  1021.  
  1022. #self.lastcode = self.create_object(Magic.ANVIL, tm.x/3.3, tm.y/3.5)
  1023. #self.explosion(tm.x/3.3, tm.y/3.5, power=20, radius=120, delay=3.0)
  1024. #self.remove_object(self.lastcode, delay=3.0)
  1025.  
  1026. d = 0.0
  1027. for mouse in self.mice.mice:
  1028. print (mouse.x, mouse.y)
  1029. if (mouse.x, mouse.y) != (0,0):
  1030. self.magic_begin(Magic.SPIRIT, int(mouse.x/3.3), int(mouse.y/3.5), delay=d)
  1031. self.magic_stop(delay=d)
  1032. d += 0.1
  1033. '''
  1034.  
  1035. #def mainLoop(self):
  1036.  
  1037. #if time.time()-self.timer > 2 and self.loginstate == "logged in":
  1038. # for mouse in self.mice.mice:
  1039. # self.magic_begin(Magic.SPIRIT, mouse.x/3.3, mouse.y/3.5)
  1040. # self.magic_stop(delay=0.1)
  1041.  
  1042. # Bot.mainLoop(self)
  1043.  
  1044.  
  1045. if __name__ == '__main__':
  1046. server = Server.EN2
  1047. bot = Testbot("maq", "Idibot", "xxxxx")
  1048.  
  1049. success = bot.connect(server)
  1050. if not success:
  1051. print 'Connecting to the server failed.'
  1052. sys.exit(1)
  1053.  
  1054. while True:
  1055. bot.mainLoop()
  1056.  
  1057. if bot.loginstate == "ready to login":
  1058. print 'Logging in...'
  1059. bot.login()
  1060.  
  1061. if bot.disconnected:
  1062. print 'Attempting to re-connect...'
  1063. time.sleep(5)
  1064. success = bot.connect(server)
  1065. if not success:
  1066. print 'Re-connecting failed.'
Add Comment
Please, Sign In to add comment