This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
454 lines (360 loc) · 14.9 KB
/
main.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# Greetings commander!
# If you're reading this, it means one of two things:
#
# - You're a user who just attempted to run CATS by double clicking main.py. I'd advise you to try reading the
# provided README file.
#
# - You're a developer who's about to find out just how shit this code is. Good luck attempting to even read this.
# The code quality can be attributed to the fact that this was intended to be a quick script and ended up not
# being that, so lots of things are hacky and held together with string and tape.
# It can also be attributed to the fact that I've only ever used Python for quick scripts so I've never been
# bothered to find out proper Python programming techniques. It's a scripting language in my mind, not
# object oriented. Sue me.
# Maybe I'll refactor and document it at some point. I wouldn't count on it. Do it yourself and open a PR
# if it bothers you.
# At least it used to be much worse. Take a look at some of the old commits if you like. We love thousand-line
# python files that could be much simpler, right?
import os
import time
import pydirectinput
import pyautogui
import random
import threading
import pyperclip
import datetime
import journalwatcher
from discordhandler import post_to_discord, post_with_fields, update_fields
from screenreader import time_until_jump
import pygetwindow as gw
# ----Options----
# How many up presses to reach tritium in carrier hold:
global tritium_slot
tritium_slot = 0
# Time to refill trit
hold_time = 10
global route_file
route_file = ""
window_name = "Elite - Dangerous (CLIENT)"
global webhook_url
webhook_url = ""
global journal_directory
journal_directory = ""
def load_settings():
global tritium_slot
global webhook_url
global journal_directory
global route_file
try:
settingsFile = open("settings.txt", "r")
a = settingsFile.read().split('\n')
try:
for line in a:
if line.startswith("webhook_url="):
print(line)
webhook_url = line.split("=")[1]
if line.startswith("journal_directory="):
print(line)
journal_directory = line.split("=")[1]
latest_journal()
if line.startswith("tritium_slot="):
print(line)
tritium_slot = int(line.split("=")[1])
if line.startswith("route_file="):
print(line)
route_file = line.split("=")[1]
except Exception as e:
print(e)
print("There seems to be a problem with your settings file. Make sure of the following:\n"
"- Your tritium slot is a valid integer. It should be the number of up presses it takes to reach "
"tritium in your carrier's cargo hold from the transfer menu.\n"
"- The journal directory is a valid directory for your operating system, and contains the Elite"
" Dangerous journal files.")
except:
settingsFile = open("settings.txt", "w+")
settingsFile.write("webhook_url=\n"
"journal_directory=\n"
"tritium_slot=\n")
print("Settings file created, please set up and run again")
def latest_journal():
global journal_directory
dir_name = journal_directory
# Get list of all files only in the given directory
list_of_files = filter(lambda x: os.path.isfile(os.path.join(dir_name, x)),
os.listdir(dir_name))
# Sort list of files based on last modification time in ascending order
list_of_files = sorted(list_of_files,
key=lambda x: os.path.getmtime(os.path.join(dir_name, x))
)
list_of_files.reverse()
journalName = ""
i = 0
while not journalName.startswith("Journal"):
journalName = list_of_files[i]
i += 1
return journal_directory + journalName.strip()
def slight_random_time(time):
return random.random() + time
def follow_button_sequence(sequence_name):
sequence = open("sequences/"+sequence_name, "r").read().split("\n")
for line in sequence:
if line.__contains__(":"):
pydirectinput.keyDown(line.split(":")[0])
time.sleep(slight_random_time(int(line.split(":")[1])))
pydirectinput.keyUp(line.split(":")[0])
else:
wait_time = 0.1
key = line
if line.__contains__("-"):
wait_time = int(line.split("-")[1])
key = line.split("-")[0]
pydirectinput.press(key)
time.sleep(slight_random_time(wait_time))
def restock_tritium():
# Navigate menu
follow_button_sequence("restock_nav_1.txt")
for i in range(tritium_slot):
pydirectinput.press('up')
time.sleep(slight_random_time(0.1))
follow_button_sequence("restock_nav_2.txt")
print("Tritium successfully refuelled.")
def jump_to_system(system_name):
follow_button_sequence("jump_nav_1.txt")
pyautogui.moveTo(921, 115)
time.sleep(slight_random_time(0.1))
pyautogui.moveTo(930, 115)
time.sleep(slight_random_time(0.1))
pydirectinput.press('space')
pyperclip.copy(system_name.lower())
time.sleep(slight_random_time(1.0))
pydirectinput.keyDown("ctrl")
time.sleep(slight_random_time(0.1))
pydirectinput.press("v")
time.sleep(slight_random_time(0.1))
pydirectinput.keyUp("ctrl")
time.sleep(slight_random_time(3.0))
pydirectinput.press('down')
time.sleep(slight_random_time(0.1))
pydirectinput.press('space')
time.sleep(slight_random_time(0.1))
pyautogui.moveTo(1496, 422)
time.sleep(slight_random_time(0.1))
pydirectinput.press('space')
time.sleep(6)
# Navigate carrier menu
pydirectinput.press('s')
time.sleep(slight_random_time(0.1))
pydirectinput.press('space')
if journalwatcher.last_carrier_request() != system_name:
print(journalwatcher.lastCarrierRequest)
print(system_name)
print("Jump appears to have failed.")
print("Re-attempting...")
follow_button_sequence("jump_fail.txt")
return 0
timeToJump = time_until_jump()
print(timeToJump.strip())
failCount = 0
while len(timeToJump.split(':')) == 1:
print("Trying again... (" + str(failCount) + ")")
timeToJump = time_until_jump()
print(timeToJump.strip())
failCount += 1
time.sleep(6)
pydirectinput.press('backspace')
time.sleep(slight_random_time(0.1))
pydirectinput.press('backspace')
return timeToJump.strip()
global lineNo
def main_loop():
global lineNo
global tritium_slot
global webhook_url
global journal_directory
global route_file
load_settings()
time.sleep(5)
latestJournal = latest_journal()
currentTime = datetime.datetime.now(datetime.timezone.utc)
arrivalTime = currentTime
th = threading.Thread(target=process_journal, args=(latestJournal,))
th.start()
win = gw.getWindowsWithTitle(window_name)[0]
win.activate()
lineNo = 0
saved = False
if os.path.exists("save.txt"):
print("Save file found. Setting up...")
lineNo = int((open("save.txt", "r")).read())
os.remove("save.txt")
saved = True
print("Beginning in 5...")
time.sleep(5)
# print("Stocking initial tritium...")
# restock_tritium()
routeFile = open(route_file, "r")
route = routeFile.read()
finalLine = route.split("\n")[len(route.split("\n")) - 1]
jumpsLeft = len(route.split("\n")) + 1
routeName = "Carrier Updates: Route to " + finalLine
a = route.split("\n")
delta = datetime.timedelta()
for i in a:
if a.index(i) < lineNo: continue
delta = delta + datetime.timedelta(seconds=1320)
arrivalTime = arrivalTime + delta
doneFirst = False
for i in range(len(a)):
jumpsLeft -= 1
if i < lineNo: continue
line = a[i]
win.activate()
time.sleep(3)
print("Next stop: " + line)
print("Beginning navigation.")
print("Please do not change windows until navigation is complete.")
print(arrivalTime.strftime("%d %b %Y %H:%M %Z"))
try:
timeToJump = jump_to_system(line)
while timeToJump == 0: timeToJump = jump_to_system(line)
print("Navigation complete. Jump occurs in " + timeToJump + ". Counting down...")
hours = int(timeToJump.split(':')[0])
minutes = int(timeToJump.split(':')[1])
seconds = int(timeToJump.split(':')[2])
totalTime = (hours * 3600) + (minutes * 60) + seconds - 12
if totalTime > 900:
arrivalTime = arrivalTime + datetime.timedelta(seconds=totalTime - 900)
print(arrivalTime.strftime("%d %b %Y %H:%M %Z"))
if doneFirst:
previous_system = a[i - 1]
post_with_fields("Carrier Jump", webhook_url,
"Jump to " + previous_system + " successful.\n"
"The carrier is now jumping to the " + line + " system.\n"
"Jumps remaining: " + str(
jumpsLeft) +
"\nEstimated time until next jump: " + timeToJump +
"\nEstimated time of route completion: " + arrivalTime.strftime("%d %b %Y %H:%M %Z") +
"\no7", routeName, "Wait...",
"Wait...")
time.sleep(2)
update_fields(0, 0)
else:
if not saved:
post_with_fields("Flight Begun", webhook_url,
"The Flight Computer has begun navigating the Carrier.\n"
"The Carrier's route is as follows:\n" +
route +
"\nEstimated time until first jump: " + timeToJump +
"\nEstimated time of route completion: " + arrivalTime.strftime(
"%d %b %Y %H:%M %Z") +
"\no7", routeName, "Wait...",
"Wait...")
time.sleep(2)
update_fields(0, 0)
else:
post_with_fields("Flight Resumed", webhook_url,
"The Flight Computer has resumed navigation.\n"
"Estimated time until first jump: " + timeToJump +
"\nEstimated time of route completion: " + arrivalTime.strftime(
"%d %b %Y %H:%M %Z") +
"\no7", routeName, "Wait...",
"Wait..."
)
time.sleep(2)
update_fields(0, 0)
except Exception as e:
print(e)
print("An error has occurred. Saving progress and aborting...")
post_to_discord("Critical Error", webhook_url,
"An error has occurred with the Flight Computer.\n"
"It's possible the game has crashed, or servers were taken down.\n"
"Please wait for the carrier to resume navigation.\n"
"o7", routeName)
print("Message sent...")
saveFile = open("save.txt", "w+")
saveFile.write(str(lineNo))
saveFile.close()
print("Progress saved...")
return False
while totalTime > 0:
print(totalTime)
time.sleep(1)
if totalTime == 600:
update_fields(1, 1)
elif totalTime == 200:
update_fields(2, 2)
elif totalTime == 190:
update_fields(2, 3)
elif totalTime == 144:
update_fields(2, 4)
elif totalTime == 103:
update_fields(2, 5)
elif totalTime == 90:
update_fields(2, 6)
elif totalTime == 75:
update_fields(2, 7)
elif totalTime == 60:
update_fields(3, 7)
elif totalTime == 30:
update_fields(4, 7)
totalTime -= 1
print("Jumping!")
update_fields(5, 7)
lineNo += 1
if not line == finalLine:
print("Counting down until next jump...")
totalTime = 362
while totalTime > 0:
print(totalTime)
if totalTime == 340:
update_fields(6, 7)
elif totalTime == 320:
update_fields(7, 7)
elif totalTime == 300:
update_fields(8, 7)
elif totalTime == 151:
update_fields(8, 8)
elif totalTime == 100:
update_fields(8, 9)
elif totalTime == 150:
print("Restocking tritium...")
win.activate()
time.sleep(2)
th = threading.Thread(target=restock_tritium)
th.start()
time.sleep(1)
totalTime -= 1
update_fields(9, 9)
else:
print("Counting down until jump finishes...")
update_fields(9, 9)
totalTime = 60
while totalTime > 0:
print(totalTime)
time.sleep(1)
totalTime -= 1
doneFirst = True
print("Route complete!")
post_to_discord("Carrier Arrived", webhook_url,
"The route is complete, and the carrier has arrived at " + finalLine + ".\n"
"o7", routeName)
return True
def process_journal(file_name):
while True:
c = journalwatcher.process_journal(file_name)
if not c:
print("An error has occurred. Saving progress and aborting...")
post_to_discord("Critical Error", webhook_url,
"An error has occurred with the Flight Computer.\n"
"It's possible the game has crashed, or servers were taken down.\n"
"Please wait for the carrier to resume navigation.\n"
"o7", "")
print("Message sent...")
saveFile = open("save.txt", "w+")
saveFile.write(str(lineNo))
saveFile.close()
print("Progress saved...")
raise SystemExit(0)
time.sleep(1)
if not main_loop():
print("Aborted.")
raise SystemExit(0)