-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcraftbot.py
357 lines (321 loc) · 11 KB
/
craftbot.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
import socket, threading, select
from Queue import Queue, Empty
from world import World
from math import floor
import requests
import json
CHUNK_SIZE = 32
CREDENTIALS = {'USERNAME':'USER', 'IDENTITY_TOKEN':'TOKEN'}
try:
with open('settings.json') as fp:
CREDENTIALS = json.load(fp)
except Exception, e:
print ('Loading settings.json failed: %s' % e)
MATERIALS = { 0: 'EMPTY',
1: 'GRASS',
2: 'SAND',
3: 'STONE',
4: 'BRICK',
5: 'WOOD',
6: 'CEMENT',
7: 'DIRT',
8: 'PLANK',
9: 'SNOW',
10: 'GLASS',
11: 'COBBLE',
12: 'LIGHT_STONE',
13: 'DARK_STONE',
14: 'CHEST',
15: 'LEAVES',
16: 'CLOUD',
17: 'TALL_GRASS',
18: 'YELLOW_FLOWER',
19: 'RED_FLOWER',
20: 'PURPLE_FLOWER',
21: 'SUN_FLOWER',
22: 'WHITE_FLOWER',
23: 'BLUE_FLOWER',
32: 'COLOR_00',
33: 'COLOR_01',
34: 'COLOR_02',
35: 'COLOR_03',
36: 'COLOR_04',
37: 'COLOR_05',
38: 'COLOR_06',
39: 'COLOR_07',
40: 'COLOR_08',
41: 'COLOR_09',
42: 'COLOR_10',
43: 'COLOR_11',
44: 'COLOR_12',
45: 'COLOR_13',
46: 'COLOR_14',
47: 'COLOR_15',
48: 'COLOR_16',
49: 'COLOR_17',
50: 'COLOR_18',
51: 'COLOR_19',
52: 'COLOR_20',
53: 'COLOR_21',
54: 'COLOR_22',
55: 'COLOR_23',
56: 'COLOR_24',
57: 'COLOR_25',
58: 'COLOR_26',
59: 'COLOR_27',
60: 'COLOR_28',
61: 'COLOR_29',
62: 'COLOR_30',
63: 'COLOR_31'}
PLANTS = dict([(x,MATERIALS[x]) for x in range(17,24)])
class TwoWayDict(dict):
def __init__(self, d={}):
super(TwoWayDict, self).__init__()
for key, value in d.items():
self[key] = value
def __setitem__(self, key, value):
if key in self:
del self[key]
if value in self:
del self[value]
dict.__setitem__(self, key, value)
dict.__setitem__(self, value, key)
def __delitem__(self, key):
dict.__delitem__(self, self[key])
dict.__delitem__(self, key)
def __len__(self):
return int(dict.__len__(self) / 2)
MATERIALS = TwoWayDict(MATERIALS)
PLANTS = TwoWayDict(PLANTS)
def material(x):
return x if isinstance(x,int) else MATERIALS[x]
def thread(f):
def run(*k, **kw):
t = threading.Thread(target=f, args=k, kwargs=kw)
t.setDaemon(True)
t.start()
return t
return run
def chunked(x):
return int(floor(round(x) / CHUNK_SIZE))
class Player(object):
def __init__(self, id=None, nick=None, position=None):
self.position = position or [0.0]*5
self.id = id
self.nick = nick
def __repr__(self):
return '<Player id=%s "%s">' % (self.id, self.nick)
def __str__(self):
return repr(self)
class CraftBot(object):
def __init__(self, host='localhost', port=4080):
self.lock = threading.RLock()
self.host = host
self.port = port
self.input_buffer = ''
self.handlers = {
'T' : self.handle_talk,
'K' : self.handle_key,
'B' : self.handle_block,
'C' : self.handle_chunk,
'U' : self.handle_you,
'P' : self.handle_position,
'N' : self.handle_nick,
'D' : self.handle_disconnect,
'S' : self.handle_sign,
'R' : self.handle_redraw
}
self.players = {}
self.world = World()
self.id = None
self.queue = Queue()
self.handler_queue = Queue()
self.chunk_keys = {}
# Commands the player can do
def talk(self, text):
self.queue.put('T,%s\n' % text)
def add_block(self, x,y,z, type, check=True):
existing_block = self.get_block(x,y,z)
if not check or not existing_block:
self.queue.put('B,%d,%d,%d,%d\n' % (x,y,z,type))
def remove_block(self, x,y,z):
self.queue.put('B,%d,%d,%d,%d\n' % (x,y,z,0))
def get_block(self,x,y,z):
p,q = chunked(x),chunked(z)
if (p,q) not in self.world.cache:
self.request_chunk(p,q)
'''
self.request_chunk(p-1,q)
self.request_chunk(p+1,q)
self.request_chunk(p,q-1)
self.request_chunk(p,q+1)
self.request_chunk(p+1,q-1)
self.request_chunk(p+1,q+1)
self.request_chunk(p-1,q+1)
self.request_chunk(p-1,q-1)
'''
while (p,q) not in self.chunk_keys:
pass
return self.world.get_chunk(p,q).get((x,y,z),0)
def add_sign(self, x,y,z, face, text):
self.queue.put('S,%d,%d,%d,%d,%s\n' % (x,y,z,face,text))
def remove_sign(self, x,y,z, face):
self.add_sign(x,y,z,face,'')
def move_player(self, x=None,y=None,z=None, rx=None, ry=None):
x = x or self.player.position[0]
y = y or self.player.position[1]
z = z or self.player.position[2]
rx = rx or self.player.position[3]
ry = ry or self.player.position[4]
self.queue.put('P,%d,%d,%d,%d,%d\n' % (x,y,z,rx,ry))
def get_player(self, nick):
for player in self.players.items():
if player.id == nick or player.nick == nick:
return player
return self.players[nickname]
def request_chunk(self, p,q):
print "requesting chunk"
key = self.chunk_keys.get((p,q),0)
self.queue.put('C,%d,%d,%d\n' % (p,q,key))
@property
def ready(self):
return self.player != None
def authenticate(self, username, identity_token):
url = 'https://craft.michaelfogleman.com/api/1/identity'
payload = {
'username': username,
'identity_token': identity_token,
}
response = requests.post(url, data=payload)
if response.status_code == 200 and response.text.isalnum():
access_token = response.text
self.queue.put('A,%s,%s\n' % (username, access_token))
else:
raise Exception('Failed to authenticate.')
@property
def player(self):
return self.players.get(self.id, None)
def connect(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((self.host, self.port))
self.socket.setblocking(0)
def handle_command(self, command):
args = command.strip().split(',')
if args:
cmd = args[0]
handler = self.handlers.get(cmd, self.handle_unhandled)
callback, args = handler(*args)
self.handler_queue.put((callback,args))
def handle_talk(self, *args):
return self.on_talk, (args[1],)
def handle_position(self, *args):
id = int(args[1])
x,y,z,rx,ry = map(float, args[2:])
with self.lock:
player = self.players.get(id, Player(id=id))
player.position = (x,y,z,rx,ry)
self.players[id] = player
return self.on_position, (player,)
def handle_nick(self, *args):
id = int(args[1])
nick = str(args[2])
with self.lock:
player = self.players.get(id, Player(id=id))
player.nick = nick
self.players[id] = player
return self.on_nick, (player,)
def handle_you(self, *args):
id = int(args[1])
position = map(float, args[2:])
with self.lock:
self.id = id
player = self.players.get(id, Player(id=id))
player.position = position
return self.on_you, (id,position)
def handle_block(self, *args):
p,q,x,y,z,type = map(int, args[1:])
chunk = self.world.get_chunk(p,q)
chunk[x,y,z] = type
return self.on_block, (x,y,z,type)
def handle_sign(self, *args):
p,q,x,y,z,face = map(int, args[1:7])
text = args[7]
return self.on_sign, (x,y,z,face,text)
def handle_key(self, *args):
p,q,key = map(int, args[1:])
self.chunk_keys[(p,q)] = key
return self.on_key, (p,q,key)
def handle_chunk(self, *args):
p,q = map(int, args[1:])
self.chunk_keys[(p,q)] = 0
return self.on_chunk, (p,q)
def handle_disconnect(self, *args):
return self.on_disconnect, (int(args[1]),)
def handle_unhandled(self, *args):
print "Unhandled command: %s" % (str(args),)
return self.on_unhandled, []
def handle_redraw(self, *args):
p,q = map(int, args[1:])
return self.on_redraw, (p,q)
def on_talk(self, text):
pass
def on_position(self, p):
pass
def on_nick(self, player):
pass
def on_you(self, id, position):
pass
def on_disconnect(self, id):
pass
def on_block(self, x,y,z,type):
pass
def on_sign(self, x,y,z,face,text):
pass
def on_key(self, p,q,key):
pass
def on_unhandled(self):
pass
def on_chunk(self, p, q):
pass
def on_redraw(self, p, q):
pass
@thread
def handler_loop(self):
while True:
try:
handler, args = self.handler_queue.get(False)
handler(*args)
except Empty:
pass
except Exception, e:
print "Exception in player handler: ", e
def run(self):
self.connect()
self.authenticate(CREDENTIALS['USERNAME'], CREDENTIALS['IDENTITY_TOKEN'])
self.handler_loop()
while True:
readers, writers, errorers = select.select([self.socket], [self.socket], [self.socket], 60)
# Deal with errors first
for socket in errorers:
# Deal with total existence failure
pass
# Inbound: Data coming from server
for socket in readers:
data = self.input_buffer + socket.recv(4096)
lines = data.split('\n')
if not lines[-1].endswith('\n'):
self.input_buffer = lines.pop(-1)
for line in lines:
#print '<- %s' % line
self.handle_command(line)
# Outbound: Commands going to server
for socket in writers:
try:
command = self.queue.get(False)
#print '-> %s' % command,
socket.send(command)
self.queue.task_done()
except Empty:
pass
except Exception, e:
print e