-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface_socketIO.py
368 lines (305 loc) · 15.9 KB
/
interface_socketIO.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import sys
sys.path.append('C:\Python27\Lib\site-packages')
import logging
import time
import math
from math import atan2,degrees
import socketIO_client
import random
import pygame
from pygame.locals import *
from pygame.color import THECOLORS
from functools import partial
import datetime
#logging.basicConfig(level=logging.DEBUG) #ENABLE THIS AT YOUR OWN RISK!!! FLOODS THE CONSOLE WITH ALL TRANSMITTED/RECIEVED PACKETS!
reconnect = True #Set to true to enable automatic reconnection after a disconnect
attack = True #stores wether the bot is enabled or not. This will set the default state when it first logs on
updates = False #if this is true, then we say statistics ingame
firstConnect = False #This is true if this is the first time connecting to the server
projectiles = {} #dict storing all data about projectiles
playerDat = {'d':0} #dict storing all data about players
pnbData = {} #dict storing planet data
chatLog = [] #list that stores a log of all the chat events that have happened.
chatIdx = 0 #index pointer to the current location in the chat log
numMines = 0 #number of mines on the current playing field
deadMines = 0 #number of mines we have killed
ourID = 'd' #stores our UUID. Set to 'd' so it does not cause an index error the first time it is loaded
ANGLES = [] #I more or less don't use this anymore, it stores the last five or so angles.
dist = 0 #Distance from the current targer
angC = 0 #the angle sent to the server, after any math is completed.
nearPlan = 0 #the x,y position of the nearest planet to the bot.
nearPlanDist = 0 #the distance the bot is away from the nearest planet
ang = 0 #the angle that the ship needs to be at to fly straight at the target
shipAng = 0 #the current angle of the ship, as reported by the server
myMaster = None #Stores the UUID of the person with OP permissions for commands
velocity = {'x':0,
'y':0,
'd':0,
't':0}
#messages printed to console on certain events.
eventMsgs = {'join':'JOINED!!!',
'pnbcollision':'Tried to run over a planet. The planet won.',
'disconnect':'LEFT!!!',
'collision':'Person was run over!',
'projectile':'Person was shot by a photon torpedo!'}
targetPlayer = False
playerToTarget = 'MakerBlock'
silentStart = True
class ninjaClient:
"""
Contains all the stuff needed for socketIO and a few random other things
"""
class EventHandler(socketIO_client.BaseNamespace):
"""
Handles events from socketIO
"""
def on_connect( self):
print "connected." #When we connect to the server. Simply print a debug message to console
def on_disconnect( self ):
global myMaster
print "DISCONNECTED!" #When we get forcefully disconnected.
if reconnect: #if we want to reconnect, try it
client.Connect()
def on_pos(self,data): #wheb we recieve new information about player positions
global playerDat #make sure the variable is global
for k in data.keys(): #iterate through all the keys in the rcieved data
if playerDat.has_key(k): playerDat[k]['pos'].update(data[k]) #if the player is already in the system, only overwrite the changes
else: playerDat[k]['pos'] = data[k] #otherwise, overwrite it all!
def on_chat(self,data): # when new data arrives from the chat system
chatLog.append(data) #add that data to the que
def on_shipstat(self,data): #recieved info on ships
global playerDat #globalize all the things!
for k in data.keys(): #iterate through keys in recv'd data
if playerDat.has_key(k): playerDat[k].update(data[k]) #if the player is already in the system, only overwrite the changes
else: playerDat[k] = data[k] #otherwise, overwrite it all!
if data[k]['status'] == 'destroy': #If the player needs tobe removed from memory
playerDat.pop(k)
def on_projstat(self,data): #updates on projectiles status
for k in data.keys():
if data[k]['status'] == 'create': #if the projectile is being created
if projectiles.has_key(k): projectiles[k].update(data[k]) #if the projectile is already in the system, only overwrite the changes
else: projectiles[k] = data[k] #otherwise, overwrite it all!
else:
projectiles.pop(k) #If we're not creating it, destroy it!
def on_projpos(self,data): #on position update of projectiles
for k in data.keys(): #write new data to the dict
projectiles[k].update(data[k])
def on_pnbitsstat(self,data): #This is only called once, on login, it gives data on PNBITS
global pnbData
pnbData = data #just copy the data into a global variable
def getClosest(self,coord,projectiles):
"""
returns closest coordinate to a coordinate (coord) from the list of coordinates (projectiles)
"""
dist=lambda s,d: (s[0]-d[0])**2+(s[1]-d[1])**2 #a little function which calculates the distance between two coordinates
pos = [] #clear the local list of positions
for k in projectiles.keys():
if projectiles[k].has_key('cssClass'): #if this is a planet
pos.append((200-int(projectiles[k]['pos']['x']/50),200-int(projectiles[k]['pos']['y']/50))) #add the coordinates in a (0,0) fashion
elif projectiles[k]['weaponID'] == 1: #or if this is a mine
pos.append((200-int(projectiles[k]['pos']['x']/50),200-int(projectiles[k]['pos']['y']/50))) #add the coordinates in a (0,0) fashion
try:
return min(pos, key=partial(dist, coord))
except ValueError:
return coord
def GetName(self,key):
try:
return playerDat[key]['name']
except:
return ''
def GetKey(self,name):
try:
for k in data.keys():
if playerDat[key]['name'] == name:
return k
except:
return ''
def __init__(self):
self.sio = socketIO_client.SocketIO('ninjanode.tn42.com',80, self.EventHandler)
self.sio.timeout_in_seconds = 0.001
self.ShipInfo = {'status':"create",
'name':"Now With Velocity calculations!.py",
'style':"f"}
def Connect(self):
global firstConnect
self.sio.emit('shipstat',self.ShipInfo)
self.sio.wait(0.001)
firstConnect = True
def MoveForward(self,state):
self.sio.emit('key',{'s':int(state), 'c': "u"})
self.sio.wait(0.001)
def MoveBackward(self,state):
self.sio.emit('key',{'s':int(state), 'c': "d"})
self.sio.wait(0.001)
def MoveLeft(self,state):
self.sio.emit('key',{'s':int(state), 'c': "l"})
self.sio.wait(0.001)
def MoveRight(self,state):
self.sio.emit('key',{'s':int(state), 'c': "r"})
self.sio.wait(0.001)
def DropMine(self):
self.sio.emit('key',{'s':1, 'c': "s"})
self.sio.wait(0.001)
self.sio.emit('key',{'s':0, 'c': "s"})
self.sio.wait(0.001)
def Fire(self):
self.sio.emit('key',{'s':1, 'c': "f"})
self.sio.wait(0.001)
self.sio.emit('key',{'s':0, 'c': "f"})
self.sio.wait(0.001)
def MoveDegrees(self,deg,state):
self.sio.emit('key',{'c':'m',
's':state,
'd': deg})
self.sio.wait(0.001)
def ChatSend(self,msg):
self.sio.emit('chat', {'msg': str(msg)})
self.sio.wait(0.001)
def GetAngle(p1, p2):
xDiff = p2[0]-p1[0]
yDiff= p2[1]-p1[1]
return degrees(atan2(yDiff,xDiff))
def GetNextPos(angle, posX,posY,velX,velY,sec=1):
X = posX+velX
Y = posY+velY
return (X,Y)
client = ninjaClient()
client.Connect()
if not silentStart:
client.ChatSend('I am a bot written by Roger (theSteamRoller). My one goal is to obliterate the mines placed by the oppressors. ')
pygame.init()
window = pygame.display.set_mode((800,500))
screen = pygame.surface.Surface((400,400))
font = pygame.font.SysFont('console',18)
clock = pygame.time.Clock()
while True:
client.sio.wait(0.001)
clock.tick(0)
if len(chatLog) > chatIdx:
cht = chatLog[chatIdx]
if cht['type'] == 'system':
print client.GetName(cht['id']),'|',eventMsgs[cht['action']]
if firstConnect:
playerDat.pop(ourID)
ourID = cht['id']
firstConnect = False
elif cht['type'] == 'chat':
print client.GetName(cht['id']),'SAYS:',cht['msg']
if '!info' in cht['msg'].lower():
client.ChatSend('I am a bot written by Roger (theSteamRoller). My one goal is to obliterate the mines placed by the oppressors.')
elif '!setcontroltome' in cht['msg'].lower():
if myMaster == None:
client.ChatSend('Control set to {0} ({1})! We shall forever be in your service!'.format(client.GetName(cht['id']),cht['id']))
myMaster = cht['id']
elif '!enable' in cht['msg'].lower()and myMaster == cht['id']:
attack = True
client.ChatSend('Phasers set to Kill! Mines, watch out!')
elif '!disable' in cht['msg'].lower()and myMaster == cht['id']:
attack = False
client.ChatSend('Phasers set to Stun! Consider yourself lucky, mines!')
#attack = True
#client.ChatSend('Theres no disabling me!')
elif '!toggle' in cht['msg'].lower() and myMaster == cht['id']:
attack = not attack
if attack:
client.ChatSend('Phasers set to Kill! Mines, watch out!')
else:
client.ChatSend('Phasers set to Stun! Consider yourself lucky, mines!')
elif '!updates' in cht['msg'].lower() :
updates = not updates
if updates:
client.ChatSend('I shall now spam this clean chat log with useless messages!')
else:
client.ChatSend('You are now free of my spam!')
elif '!stats' in cht['msg'].lower():
client.ChatSend('Disarmed {0} mines out of {1} remaining mines. \
Current TPS is {2}. My UUID is: {3}. \
My current owner is: {4} ({5})' .format(deadMines,
numMines,
clock.get_fps(),
ourID,
myMaster,
client.GetName(myMaster)
))
else:
print cht
chatIdx += 1
event = pygame.event.poll()
if event.type == KEYDOWN:
if event.key == 107:
attack = not attack
if attack:
client.ChatSend('Phasers set to Kill! Mines, watch out!')
else:
client.ChatSend('Phasers set to Stun! Consider yourself lucky, mines!')
else:
print event.key
screen.fill((30,30,30))
try:
for k in playerDat.keys():
if not playerDat[k]['status'] == 'boom':
pos = (200-int(playerDat[k]['pos']['x']/50),200-int(playerDat[k]['pos']['y']/50))
pygame.draw.circle(screen,THECOLORS[playerDat[k]['shieldStyle']],pos,4)
velocity = playerDat[ourID]['pos']['vel']
newPos = GetNextPos(int(velocity['t']),
int(playerDat[k]['pos']['x']),
int(playerDat[k]['pos']['y']),
int(velocity['x']),
int(velocity['y']))
newPos = (200-int(newPos[0]/50),200-int(newPos[1]/50))
pygame.draw.line(screen,THECOLORS['red'],pos,newPos)
if k == ourID:
if targetPlayer:
tID = client.GetKey(playerToTarget)
closePos = (200-int(playerDat[tID]['pos']['x']/50),200-int(playerDat[tID]['pos']['y']/50))
else:
closePos = client.getClosest(pos,projectiles)
shipAng = playerDat[k]['pos']['d']
ang = int(GetAngle(pos,closePos)) -90
if ang < 0: ang += 360
ANGLES.append(ang)
if len(ANGLES) > 5:
ANGLES.pop(0)
angC = ANGLES[-1]
nearPlan = client.getClosest(pos,pnbData)
nearPlanDist = int(math.hypot(pos[0]-nearPlan[0], pos[1]-nearPlan[1]))
if attack:
pygame.draw.line(screen,THECOLORS['grey'],pos,closePos)
dist = int(math.hypot(pos[0]-closePos[0], pos[1]-closePos[1]))
if attack:
client.MoveDegrees(angC,0)
client.MoveDegrees(angC,1)
if dist < 10:
client.Fire()
except BaseException as e:
print e
for k in pnbData.keys():
pos = (200-int(pnbData[k]['pos']['x']/50),200-int(pnbData[k]['pos']['y']/50))
pygame.draw.circle(screen,THECOLORS['orange'],pos,pnbData[k]['radius']/50)
o_numMines = numMines
numMines = 0
for k in projectiles.keys():
if projectiles[k]['weaponID'] == 1:
numMines += 1
pos = (200-int(projectiles[k]['pos']['x']/50),200-int(projectiles[k]['pos']['y']/50))
pygame.draw.circle(screen,THECOLORS[projectiles[k]['style']],pos,4, 1)
if not o_numMines == numMines and o_numMines-1 == numMines:
deadMines += 1
#client.ChatSend(str(datetime.datetime.now())+' | Killed: '+str(deadMines))
#client.ChatSend(str(numMines)+' Left!')
if updates:
client.ChatSend('1 mine down! {0} left to go. This makes a total of {1} mines disarmed! {2}'.format(numMines,deadMines,datetime.datetime.now()))
window.fill(THECOLORS['white'])
window.blit(screen, (5,5))
window.blit(font.render('# of mines disarmed:'+str(deadMines),1,THECOLORS['black']),(420,5))
window.blit(font.render('# of mines left: '+str(numMines),1,THECOLORS['black']),(420,25))
window.blit(font.render('Distance to target:'+str(dist),1,THECOLORS['black']),(420,85))
window.blit(font.render('target Angle:'+str(ang),1,THECOLORS['black']),(420,105))
window.blit(font.render('Dist to nearest planet:'+str(nearPlanDist),1,THECOLORS['black']),(420,125))
window.blit(font.render('Dif of cur ang and target ang:'+str(ang-shipAng),1,THECOLORS['black']),(420,145))
window.blit(font.render('Velocity:'+str(int(velocity['t'])),1,THECOLORS['black']),(420,165))
window.blit(font.render('Velocity X:'+str(int(velocity['x'])),1,THECOLORS['black']),(420,185))
window.blit(font.render('Velocity Y:'+str(int(velocity['y'])),1,THECOLORS['black']),(420,205))
window.blit(font.render('# of players:'+str(len(playerDat)),1,THECOLORS['black']),(420,305))
window.blit(font.render('TPS:'+str(int(clock.get_fps())),1,THECOLORS['black']),(420,325))
pygame.display.update()