forked from SumZer0-git/EDAPGui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEDWayPoint.py
388 lines (285 loc) · 12.8 KB
/
EDWayPoint.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
from time import sleep
from EDlogger import logger
import json
from pyautogui import typewrite, keyUp, keyDown
from MousePt import MousePoint
from pathlib import Path
"""
File: EDWayPoint.py
Description:
Class will load file called waypoints.json which contains a list of System name to jump to.
Provides methods to select a waypoint pass into it.
Author: sumzer0@yahoo.com
"""
class EDWayPoint:
def __init__(self, is_odyssey=True):
self.is_odyssey = is_odyssey
self.filename = './waypoints.json'
self.waypoints = {}
# { "Ninabin": {"DockWithTarget": false, "TradeSeq": None, "Completed": false} }
# for i, key in enumerate(self.waypoints):
# self.waypoints[target]['DockWithTarget'] == True ... then go into SC Assist
# self.waypoints[target]['Completed'] == True
# if docked and self.waypoints[target]['Completed'] == False
# execute_seq(self.waypoints[target]['TradeSeq'])
ss = self.read_waypoints()
# if we read it then point to it, otherwise use the default table above
if ss is not None:
self.waypoints = ss
logger.debug("EDWayPoint: read json:"+str(ss))
self.num_waypoints = len(self.waypoints)
#print("waypoints: "+str(self.waypoints))
self.step = 0
self.mouse = MousePoint()
def load_waypoint_file(self, filename=None):
if filename == None:
return
ss = self.read_waypoints(filename)
if ss is not None:
self.waypoints = ss
self.filename = filename
logger.debug("EDWayPoint: read json:"+str(ss))
def read_waypoints(self, fileName='./waypoints/waypoints.json'):
s = None
try:
with open(fileName,"r") as fp:
s = json.load(fp)
except Exception as e:
logger.warning("EDWayPoint.py read_waypoints error :" + str(e))
return s
def write_waypoints(self, data, fileName='./waypoints/waypoints.json'):
if data is None:
data = self.waypoints
try:
with open(fileName,"w") as fp:
json.dump(data,fp, indent=4)
except Exception as e:
logger.warning("EDWayPoint.py write_waypoints error:" + str(e))
def mark_waypoint_complete(self, key):
self.waypoints[key]['Completed'] = True
self.write_waypoints(data=None, fileName='./waypoints/' + Path(self.filename).name)
def waypoint_next(self, ap, target_select_cb=None) -> str:
dest_key = "REPEAT"
# loop back to beginning if last record is "REPEAT"
while dest_key == "REPEAT":
for i, key in enumerate(self.waypoints):
# skip records we already processed
if i < self.step:
continue
# if this step is marked to skip.. i.e. completed, go to next step
if self.waypoints[key]['Completed'] == True:
continue
# if this entry is REPEAT, loop through all and mark them all as Completed = False
if key == "REPEAT":
self.mark_all_waypoints_not_complete()
else:
# Call sequence to select route
if self.set_waypoint_target(ap, key, target_select_cb) == False:
# Error setting target
logger.warning("Error setting waypoint, breaking")
self.step = i
dest_key = key
break
else:
dest_key = "" # End of list, return empty string
print("test: " + dest_key)
return dest_key
def mark_all_waypoints_not_complete(self):
for j, tkey in enumerate(self.waypoints):
self.waypoints[tkey]['Completed'] = False
self.step = 0
self.write_waypoints(data=None, fileName='./waypoints/' + Path(self.filename).name)
def is_station_targeted(self, dest) -> bool:
return self.waypoints[dest]['DockWithStation']
def set_station_target(self, ap, dest):
(x, y) = self.waypoints[dest]['StationCoord']
# check if StationBookmark exists to get the transition compatibility with old waypoint lists
if "StationBookmark" in self.waypoints[dest]:
bookmark = self.waypoints[dest]['StationBookmark']
else:
bookmark = -1
ap.keys.send('SystemMapOpen')
sleep(3.5)
if self.is_odyssey and bookmark != -1:
ap.keys.send('UI_Left')
sleep(1)
ap.keys.send('UI_Select')
sleep(.5)
ap.keys.send('UI_Down', repeat=2)
sleep(.5)
ap.keys.send('UI_Right')
sleep(.5)
ap.keys.send('UI_Down', repeat=bookmark)
sleep(.5)
ap.keys.send('UI_Select', hold=4.0)
else:
self.mouse.do_click(x, y)
self.mouse.do_click(x, y, 1.25)
# for horizons we need to select it
if self.is_odyssey == False:
ap.keys.send('UI_Select')
ap.keys.send('SystemMapOpen')
sleep(0.5)
# Call either the Odyssey or Horizons version of the Galatic Map sequence
def set_waypoint_target(self, ap, target_name, target_select_cb=None) -> bool:
# No waypoints defined, then return False
if self.waypoints == None:
return False
if self.is_odyssey != True:
return self.set_waypoint_target_horizons(ap, target_name, target_select_cb)
else:
return self.set_waypoint_target_odyssey(ap, target_name, target_select_cb)
#
# This sequence for the Horizons
#
def set_waypoint_target_horizons(self, ap, target_name, target_select_cb=None) -> bool:
ap.keys.send('GalaxyMapOpen')
sleep(2)
ap.keys.send('CycleNextPanel')
sleep(1)
ap.keys.send('UI_Select')
sleep(2)
typewrite(target_name, interval=0.25)
sleep(1)
# send enter key
ap.keys.send_key('Down', 28)
sleep(0.05)
ap.keys.send_key('Up', 28)
sleep(7)
ap.keys.send('UI_Right')
sleep(1)
ap.keys.send('UI_Select')
# if got passed through the ship() object, lets call it to see if a target has been
# selected yet.. otherwise we wait. If long route, it may take a few seconds
if target_select_cb != None:
while not target_select_cb()['target']:
sleep(1)
ap.keys.send('GalaxyMapOpen')
sleep(2)
return True
#
# This sequence for the Odyssey
def set_waypoint_target_odyssey(self, ap, target_name, target_select_cb=None) -> bool:
x = ap.scr.screen_width / 2
y = ap.scr.screen_height / 2
ap.keys.send('GalaxyMapOpen')
sleep(2)
ap.keys.send('UI_Up')
sleep(.5)
ap.keys.send('UI_Select')
sleep(.5)
#print("Target:"+target_name)
# type in the System name
typewrite(target_name, interval=0.25)
sleep(1)
# send enter key
ap.keys.send_key('Down', 28)
sleep(0.15)
ap.keys.send_key('Up', 28)
sleep(1)
self.mouse.do_click(x, y)
sleep(0.1)
ap.keys.send('UI_Right', repeat=4)
sleep(0.1)
# go down 6x's to plot to target
for i in range(7): # ED 4.0 update, since have new menu item
ap.keys.send('UI_Down')
sleep(0.05)
sleep(0.1)
# select Plot course
ap.keys.send('UI_Select')
# if got passed through the ship() object, lets call it to see if a target has been
# selected yet.. otherwise we wait. If long route, it may take a few seconds
if target_select_cb != None:
while not target_select_cb()['target']:
sleep(1)
sleep(1)
ap.keys.send('GalaxyMapOpen')
sleep(1)
return True
def execute_trade(self, ap, dest):
sell_down = self.waypoints[dest]['SellNumDown']
buy_down = self.waypoints[dest]['BuyNumDown']
if sell_down == -1 and buy_down == -1:
return
# We start off on the Main Menu in the Station
ap.keys.send('UI_Up', repeat=3) # make sure at the top
ap.keys.send('UI_Down')
ap.keys.send('UI_Select') # Select StarPort Services
sleep(8) # wait for new menu to finish rendering
ap.keys.send('UI_Down')
ap.keys.send('UI_Select') # Select Commodities
sleep(2.5)
# --------- SELL ----------
if sell_down != -1:
ap.keys.send('UI_Down')
ap.keys.send('UI_Select') # Select Sell
sleep(1.5) # give time to bring up, if needed
ap.keys.send('UI_Right') # Go to top of commodities list
ap.keys.send('UI_Up', repeat=10) # go up 10x in case were not on top of list
ap.keys.send('UI_Down', repeat=sell_down) # go down # of times user specified
ap.keys.send('UI_Select') # Select that commodity
sleep(3) # give time for popup
ap.keys.send('UI_Up', repeat=3) # make sure at top
ap.keys.send('UI_Down') # Down to the Sell button (already assume sell all)
ap.keys.send('UI_Select') # Select to Sell all
# TODO: Note, if the waypoint plan has sell_down != -1, then we are assuming we have
# cargo to sell, if not we are in limbo here as the Sell button not selectable
# Could look at the ship_status['MarketSel'] == True (to be added), to see that we sold
# and if not, go down 1 and select cancel
# --------- BUY ----------
if buy_down != -1:
sleep(3) # give time to popdown
ap.keys.send('UI_Left') # back to left menu
sleep(0.5)
ap.keys.send('UI_Up', repeat=2) # go up to Buy
ap.keys.send('UI_Select') # Select Buy
sleep(1.5) # give time to bring up list
ap.keys.send('UI_Right') # Go to top of commodities list
ap.keys.send('UI_Up', repeat=sell_down+5) # go up sell_down times in case were not on top of list (+5 for pad)
ap.keys.send('UI_Down', repeat=buy_down) # go down # of times user specified
ap.keys.send('UI_Select') # Select that commodity
sleep(2) # give time to popup
ap.keys.send('UI_Up', repeat=3) # go up to quantity to buy (may not default to this)
ap.keys.send('UI_Right', hold=4.0) # Hold down Right key to buy will fill cargo
ap.keys.send('UI_Down')
ap.keys.send('UI_Select') # Select Buy
sleep(1.5) # give time to popdown
ap.keys.send('UI_Left') # back to left menu
ap.keys.send('UI_Down', repeat=8) # go down 4x to highlight Exit
ap.keys.send('UI_Select') # Select Exit, back to StartPort Menu
sleep(1) # give time to get back to menu
if self.is_odyssey == True:
ap.keys.send('UI_Down', repeat=4) # go down 4x to highlight Exit
ap.keys.send('UI_Select') # Select Exit, back to top menu
sleep(2) # give time to popdown menu
# this import the temp class needed for unit testing
"""
from EDKeys import *
class temp:
def __init__(self):
self.keys = EDKeys()
"""
def main():
#keys = temp()
wp = EDWayPoint(True) # False = Horizons
wp.step = 0 #start at first waypoint
sleep(3)
#dest = 'Enayex'
#print(dest)
#print("In waypoint_assist, at:"+str(dest))
# already in doc config, test the trade
#wp.execute_trade(keys, dest)
# Set the Route for the waypoint^#
dest = wp.waypoint_next(ap=None)
while dest != "":
# print("Doing: "+str(dest))
# print(wp.waypoints[dest])
# print("Dock w/station: "+ str(wp.is_station_targeted(dest)))
#wp.set_station_target(None, dest)
# Mark this waypoint as complated
#wp.mark_waypoint_complete(dest)
# set target to next waypoint and loop)::@
dest = wp.waypoint_next(ap=None)
if __name__ == "__main__":
main()