Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. import socket
  2. import sys
  3.  
  4. M_IDLE = 0
  5. M_HOSTING_PLAYER = 1
  6. M_HOSTING_GAME = 2
  7. M_GAME_SETUP = 3
  8.  
  9. MSG_NOTIFY = 0
  10. MSG_NORMAL = 1
  11. MSG_PM = 2
  12. MSG_ERROR = 3
  13. MSG_POP = 4
  14.  
  15. hutlist = ["" for i in range(4*10)]
  16. receiving_huts = False
  17. players = 4
  18. myhut = 0
  19. mode = M_IDLE
  20. master = ""
  21. in_game = False
  22. connections = 0
  23. STATUS = "HostBot"
  24.  
  25. TCP_IP = '127.0.0.1'
  26. TCP_PORT = 7501
  27. BUFFER_SIZE = 1024
  28.  
  29. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  30. s.connect((TCP_IP, TCP_PORT))
  31.  
  32. def send(msg):
  33. print ">",msg
  34. s.send(msg+"\n")
  35.  
  36. def receive_message(t, message):
  37. if in_game:
  38. return
  39. try:
  40. if t == MSG_PM:
  41. sender, message = message.split("> ", 1)
  42. process_pm(sender, message)
  43. elif t == MSG_NOTIFY:
  44. process_notify(message)
  45. elif t == MSG_NORMAL:
  46. sender, message = message.split("> ", 1)
  47. process_msg(sender, message)
  48. else:
  49. # invalid message format
  50. pass
  51. except:
  52. pass
  53.  
  54. def process_pm(sender, message):
  55. global mode, master, players
  56. print sender, message
  57.  
  58. if sender == "IncaWarrior" and message == "quit":
  59. sys.exit(0)
  60.  
  61. if master == "" and message == "hostme":
  62. mode = M_HOSTING_PLAYER
  63. join_player_hut(sender)
  64. ##
  65. ## elif sender == master:
  66. ## if message.startswith("players "):
  67. ## players = int(message[-1])
  68. ## if players < 2: players = 2
  69. ## if players > 4: players = 4
  70. ## send('!set host players '+str(players))
  71. ## check_hut()
  72.  
  73.  
  74. def process_notify(message):
  75. global connections, master
  76. if message.startswith("$hut "):
  77. cmd, hut, pos, data = message.split()
  78. hutlist[ (int(hut)-1)*4 + int(pos) ] = data
  79. check_hut()
  80.  
  81. elif message.startswith("$pop "):
  82. message = message[5:]
  83. if message == "all ready":
  84. send('!startgame')
  85.  
  86. elif message.startswith("connect"):
  87. connections += 1
  88. elif message.startswith("disconnect"):
  89. connections -= 1
  90. if connections == 0:
  91. send('!closegame')
  92. master = ""
  93. send("!away "+STATUS)
  94.  
  95. def process_msg(sender, message):
  96. if message.startswith("$hut "):
  97. cmd, hut, pos = message.split()
  98. if int(hut) > 0:
  99. hutlist[ (int(hut)-1)*4 + int(pos) ] = sender
  100. else:
  101. # removed user from hut
  102. for i in range(len(hutlist)):
  103. if hutlist[i] == sender:
  104. hutlist[i] = "*"
  105. break
  106. if myhut == 0:
  107. check_hut()
  108. if mode == M_IDLE:
  109. join_empty_hut()
  110.  
  111. def set_host_params(hut):
  112. global players, mode, myhut
  113. send('!joinhut '+str(hut)+' 0')
  114. send('!set host watcher 0 1')
  115. send('!set host mappack 42')
  116. send('!set host level 10')
  117. send('!set host players 3')
  118. players = 3
  119. myhut = hut
  120. mode = M_HOSTING_GAME
  121.  
  122. def join_empty_hut():
  123. global players, mode, master, myhut
  124. hut = 1
  125. pos = 0
  126. for i in range(0, len(hutlist) / 4):
  127. j = i * 4
  128. if hutlist[j+0] == "*" and hutlist[j+1] == "*" and hutlist[j+2] == "*" and hutlist[j+3] == "*":
  129. break
  130. hut += 1
  131.  
  132. hostspot = hutlist[(hut-1)*4]
  133. set_host_params(hut)
  134.  
  135. # find the hut a player is in and join it as host
  136. def join_player_hut(nick):
  137. global players, mode, master, myhut
  138. hut = 1
  139. pos = 0
  140. for spot in hutlist:
  141. if spot == nick:
  142. break
  143. pos = (pos + 1) % 4
  144. if pos == 0: hut += 1
  145. else:
  146. # could not find user in hut
  147. send('!pvt: '+nick+'> You need to be in a hut')
  148. mode = M_IDLE
  149. return
  150.  
  151. hostspot = hutlist[(hut-1)*4]
  152. if pos == 0 or hostspot != "*":
  153. # user in host spot or host spot taken
  154. send('!pvt: '+nick+'> There is already a host in your hut')
  155. mode = M_IDLE
  156. return
  157.  
  158. # joining user's hut as host
  159. print "Hosting "+nick
  160. set_host_params(hut)
  161. master = nick
  162.  
  163. def check_hut():
  164. global myhut
  165. if myhut == 0: return
  166. players_in_hut = 0
  167. for i in range((myhut-1)*4, myhut*4):
  168. if hutlist[i] != "*" :
  169. players_in_hut += 1
  170. if players_in_hut == players:
  171. print "Launching:"
  172. for i in range((myhut-1)*4+1, myhut*4):
  173. print " - "+ hutlist[i]
  174. send('!launch')
  175. myhut = 0
  176. mode = M_GAME_SETUP
  177.  
  178.  
  179.  
  180. send("!away "+STATUS)
  181. send("!hutlist")
  182. while True:
  183. data = s.recv(BUFFER_SIZE)
  184. for line in data.split("\n"):
  185. if line:
  186. receive_message(int(line[0]), line[1:].strip())
  187.  
  188. s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement