-
Notifications
You must be signed in to change notification settings - Fork 7
/
executeYou.py
executable file
·359 lines (331 loc) · 15.4 KB
/
executeYou.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
#!/usr/bin/env python3
"""
HOST = "ofmiceandmechs.com" #input("enter server address") # The server's hostname or IP address
PORT = 23 # The port used by the server
"""
import gzip
import json
import socket
import time
import tcod
HOST = "127.0.0.1" #input("enter server address") # The server's hostname or IP address
PORT = 65475 # The port used by the server
screen_width = 200
screen_height = 51
tileset = tcod.tileset.load_tilesheet(
"Acorntileset2.png", 16, 16, tcod.tileset.CHARMAP_CP437
)
context = tcod.context.new_terminal(
screen_width,
screen_height,
tileset=tileset,
title="OfMiceAndMechs",
vsync=True,
)
root_console = tcod.Console(screen_width, screen_height, order="F")
def draw(raw):
root_console.clear()
y=0
for line in raw:
x = 0
for char in line:
if char is None:
continue
if char == "\n":
1/0
if char == "":
x += 1
continue
if isinstance(char,str):
if "\n" in char:
1/0
root_console.print(x=x,y=y,string=char,fg=(255,255,255),bg=(0,0,0))
x += len(char)
else:
root_console.print(x=x,y=y,string=char[1],fg=tuple(char[0][0]),bg=tuple(char[0][1]))
x += len(char[1])
y += 1
context.present(root_console)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.setblocking(False)
dataString = b""
def getData():
global dataString
while True:
try:
part = s.recv(1024)
except:
part = None
if not part:
break
dataString += part
if b"\n-*_*-\n" not in dataString:
return None
valuepart = dataString.split(b"\n-*_*-\n")[0]
dataString = dataString.split(b"\n-*_*-\n")[-1]
valuepart = gzip.decompress(valuepart)
return valuepart.decode("utf-8")
commands = ["~"]
def sendKeystrokes():
getTcodEvents()
global commands
if not commands:
return
info = {"commands":commands}
info = json.dumps(info)
data = info.encode("utf-8")
s.sendall(data+b"\n")
commands = []
lastcheck = 0
def getTcodEvents():
global lastcheck
global commands
events = tcod.event.get()
if lastcheck < time.time()-0.05:
for event in events:
if isinstance(event, tcod.event.Quit):
raise SystemExit()
if isinstance(event, tcod.event.WindowEvent):
if event.type == "WINDOWCLOSE":
raise SystemExit()
if event.type == "WINDOWEXPOSED":
commands.append("~")
if isinstance(event,tcod.event.MouseButtonDown):# or isinstance(event,tcod.event.MouseButtonUp):
pass
"""
tcodContext.convert_event(event)
clickPos = (event.tile.x,event.tile.y)
if src.gamestate.gamestate.clickMap:
if not clickPos in src.gamestate.gamestate.clickMap:
continue
value = src.gamestate.gamestate.clickMap[clickPos]
if isinstance(value,str) or isinstance(value,list):
src.gamestate.gamestate.mainChar.runCommandString(value,nativeKey=True)
elif isinstance(value,dict):
if not "params" in value:
value["params"] = {}
value["params"]["event"] = event
src.saveing.Saveable.callIndirect(None,value)
else:
value()
if isinstance(event,tcod.event.MouseButtonUp):
src.gamestate.gamestate.dragState = None
"""
if isinstance(event,tcod.event.KeyDown):
key = event.sym
translatedKey = None
if key == tcod.event.KeySym.LSHIFT:
continue
if key == tcod.event.KeySym.RETURN:
translatedKey = "enter"
if key == tcod.event.KeySym.BACKSPACE:
translatedKey = "backspace"
if key == tcod.event.KeySym.SPACE:
translatedKey = " "
if key == tcod.event.KeySym.PERIOD:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = ":"
else:
translatedKey = "."
if key == tcod.event.KeySym.HASH:
translatedKey = "#"
if key == tcod.event.KeySym.ESCAPE:
if event.mod in (tcod.event.Modifier.RSHIFT,):
translatedKey = "rESC"
elif event.mod in (tcod.event.Modifier.LSHIFT,):
translatedKey = "lESC"
elif event.mod in (tcod.event.Modifier.SHIFT,):
translatedKey = "ESC"
else:
translatedKey = "esc"
if key == tcod.event.KeySym.N1:
translatedKey = "1"
if key == tcod.event.KeySym.N2:
translatedKey = "2"
if key == tcod.event.KeySym.N3:
translatedKey = "3"
if key == tcod.event.KeySym.N4:
translatedKey = "4"
if key == tcod.event.KeySym.N5:
translatedKey = "5"
if key == tcod.event.KeySym.N6:
translatedKey = "6"
if key == tcod.event.KeySym.N7:
translatedKey = "7"
if key == tcod.event.KeySym.N8:
translatedKey = "8"
if key == tcod.event.KeySym.N9:
translatedKey = "9"
if key == tcod.event.KeySym.N0:
translatedKey = "0"
if key == tcod.event.KeySym.COMMA:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = ";"
else:
translatedKey = ","
if key == tcod.event.KeySym.MINUS:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "_"
else:
translatedKey = "-"
if key == tcod.event.KeySym.PLUS or key == tcod.event.KeySym.KP_PLUS:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "*"
else:
translatedKey = "+"
if key == tcod.event.KeySym.a:
if event.mod in (tcod.event.Modifier.LCTRL,tcod.event.Modifier.RCTRL,):
translatedKey = "ctrl a"
elif event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "A"
else:
translatedKey = "a"
if key == tcod.event.KeySym.b:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "B"
else:
translatedKey = "b"
if key == tcod.event.KeySym.c:
if event.mod in (tcod.event.Modifier.LCTRL,tcod.event.Modifier.RCTRL,):
translatedKey = "ctrl c"
elif event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "C"
else:
translatedKey = "c"
if key == tcod.event.KeySym.d:
if event.mod in (tcod.event.Modifier.LCTRL,tcod.event.Modifier.RCTRL,):
translatedKey = "ctrl d"
elif event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "D"
else:
translatedKey = "d"
if key == tcod.event.KeySym.e:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "E"
else:
translatedKey = "e"
if key == tcod.event.KeySym.f:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "F"
else:
translatedKey = "f"
if key == tcod.event.KeySym.g:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "G"
else:
translatedKey = "g"
if key == tcod.event.KeySym.h:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "H"
else:
translatedKey = "h"
if key == tcod.event.KeySym.i:
if event.mod in (tcod.event.Modifier.LCTRL,tcod.event.Modifier.RCTRL,):
translatedKey = "ctrl i"
elif event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "I"
else:
translatedKey = "i"
if key == tcod.event.KeySym.j:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "J"
else:
translatedKey = "j"
if key == tcod.event.KeySym.k:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "K"
else:
translatedKey = "k"
if key == tcod.event.KeySym.l:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "L"
else:
translatedKey = "l"
if key == tcod.event.KeySym.m:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "M"
else:
translatedKey = "m"
if key == tcod.event.KeySym.n:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "N"
else:
translatedKey = "n"
if key == tcod.event.KeySym.o:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "O"
else:
translatedKey = "o"
if key == tcod.event.KeySym.p:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "P"
else:
translatedKey = "p"
if key == tcod.event.KeySym.q:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "Q"
else:
translatedKey = "q"
if key == tcod.event.KeySym.r:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "R"
else:
translatedKey = "r"
if key == tcod.event.KeySym.s:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "S"
else:
translatedKey = "s"
if key == tcod.event.KeySym.t:
if event.mod in (tcod.event.Modifier.LCTRL,tcod.event.Modifier.RCTRL,):
translatedKey = "ctrl t"
elif event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "T"
else:
translatedKey = "t"
if key == tcod.event.KeySym.u:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "U"
else:
translatedKey = "u"
if key == tcod.event.KeySym.v:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "V"
else:
translatedKey = "v"
if key == tcod.event.KeySym.w:
if event.mod in (tcod.event.Modifier.LCTRL,tcod.event.Modifier.RCTRL,):
translatedKey = "ctrl w"
elif event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "W"
else:
translatedKey = "w"
if key == tcod.event.KeySym.x:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "X"
else:
translatedKey = "x"
if key == tcod.event.KeySym.y:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "Y"
else:
translatedKey = "y"
if key == tcod.event.KeySym.z:
if event.mod in (tcod.event.Modifier.SHIFT,tcod.event.Modifier.RSHIFT,tcod.event.Modifier.LSHIFT,):
translatedKey = "Z"
else:
translatedKey = "z"
if translatedKey is None:
print(event)
continue
commands.append(translatedKey)
lastcheck = time.time()
while 1:
data = getData()
if data not in [None,""]:
raw = json.loads(data)
if "pseudoDisplay" in raw:
draw(raw["pseudoDisplay"])
sendKeystrokes()
time.sleep(0.01)