forked from Gadgetoid/python-airmash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom-bot.py
More file actions
executable file
·107 lines (89 loc) · 2.7 KB
/
Copy pathrandom-bot.py
File metadata and controls
executable file
·107 lines (89 loc) · 2.7 KB
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
#! /usr/bin/python3
from airmash import packets
from airmash.player import Player
from airmash.mob import Mob
from airmash.country import COUNTRY_CODES
from airmash import games
import random
import websocket
import threading
import time
import names
UP = 'UP'
DOWN = 'DOWN'
LEFT = 'LEFT'
RIGHT = 'RIGHT'
FIRE = 'FIRE'
SPECIAL = 'SPECIAL'
def rare():
return random.randrange(0, 10) == 0
class StoppableThread(threading.Thread):
def __init__(self, *args, **kwargs):
super(StoppableThread, self).__init__(*args, **kwargs)
self._event = threading.Event()
def stop(self):
self._event.set()
def wait(self, timeout=1):
return self._event.wait(timeout=timeout)
class ClientUpdate(StoppableThread):
def __init__(self, *args, **kwargs):
StoppableThread.__init__(self, *args, **kwargs)
self.keyProbs = {};
keys = [None, None, UP, UP, UP, UP, DOWN, LEFT, RIGHT, FIRE, FIRE, FIRE, SPECIAL]
for i in set(keys):
self.keyProbs[i] = []
for j in keys:
for k in range(random.randrange(0, 3)):
self.keyProbs[i].append(j)
print(self.keyProbs)
def send_keydown(self, key):
client.key(key=key, state=True)
def send_keyup(self, key):
client.key(key=key, state=False)
def run(self):
while not self.wait():
if client.connected:
break
packet = packets.build_player_command('COMMAND', com='respawn', data=str(random.randrange(1,6)))
client.send(packet)
if False: #rare():
packet = packets.build_player_command('CHAT', text = "All hail the robot overlords!")
client.send(packet)
self.wait(2)
lastKey = None
pressedKeys = []
while not self.wait(random.randrange(1, 8)/4.):
key = random.choice(self.keyProbs[lastKey])
#print("sending ", key)
lastKey = key
if key is None:
continue
if key in pressedKeys:
self.send_keyup(key);
pressedKeys.remove(key)
else:
self.send_keydown(key)
pressedKeys.append(key)
my_status = players[me].status
#print("Status: {}".format(my_status))
#print("Position: {}:{}".format(me.posX, me.posY))
client = Client()
@client.on('LOGIN')
def on_login(client, message):
print("Client has logged in!")
_t_update = ClientUpdate()
_t_update.start()
name = names.get_full_name()
if rare():
name = "Robot " + name
name = name[:20]
print("Name is ", name)
client.connect(
name=name,
flag='US',
region='us',
room='ffa1',
enable_trace=True
)
_t_update.stop()
_t_update.join()