-
Notifications
You must be signed in to change notification settings - Fork 4
/
load.py
368 lines (293 loc) · 14 KB
/
load.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
"""Artemis Scanner Tracker v0.3.0 by Balvald."""
import json
import logging
import os
import tkinter as tk
from typing import Optional
import myNotebook as nb # type: ignore
from config import appname # type: ignore
import eventhandling
import ui
import organicinfo as orgi
from AST import ArtemisScannerTracker
frame: Optional[tk.Frame] = None
# Shows debug fields in preferences when True
logger = logging.getLogger(f"{appname}.{os.path.basename(os.path.dirname(__file__))}")
PLUGIN_NAME = "AST"
AST_VERSION = "v0.3.0"
AST_REPO = "Balvald/ArtemisScannerTracker"
firstdashboard = True
firstsystemevent = True
not_yet_sold_data = {}
sold_exobiology = {}
currententrytowrite = {}
cmdrstates = {}
plugin = None
# Gonna need the files directory to store data for full
# tracking of all the biological things that the CMDR scans.
directory, filename = os.path.split(os.path.realpath(__file__))
filenames = ["/soldbiodata.json", "/notsoldbiodata.json", "/cmdrstates.json"]
for file in filenames:
if not os.path.exists(directory + file):
f = open(directory + file, "w", encoding="utf8")
f.write(r"{}")
f.close()
elif file == "/soldbiodata.json" or file == "/notsoldbiodata.json":
# (not)soldbiodata file already exists
with open(directory + file, "r+", encoding="utf8") as f:
test = json.load(f)
if type([]) == type(test): # noqa E721
# we have an old version of the (not)soldbiodata.json
# clear it, have the user do the journal crawling again.
logger.warning(f"Found old {file} format")
logger.warning("Clearing file...")
f.seek(0)
f.write(r"{}")
f.truncate()
# load notyetsolddata and cmdrstates
with open(directory + "/notsoldbiodata.json", "r+", encoding="utf8") as f:
not_yet_sold_data = json.load(f)
with open(directory + "/cmdrstates.json", "r+", encoding="utf8") as f:
cmdrstates = json.load(f)
# region eventhandling
def dashboard_entry(cmdr: str, is_beta, entry) -> None:
"""
React to changes in the CMDRs status (Movement for CCR feature).
:param cmdr: The current ED Commander
:param is_beta: Is the game currently in beta
:param entry: full excerpt from status.json
"""
# TODO: Move most of this into AST.py class
global plugin, firstdashboard
if plugin.AST_debug.get():
logger.debug("Called Dashboard ...")
if plugin.AST_in_Legacy is True:
# We're in legacy we don't update anything through dashboard entries
return
# flag determines if we have to rebuild the ui at the end.
flag = plugin.handle_possible_cmdr_change(cmdr)
if firstdashboard:
firstdashboard = False
plugin.on_preferences_closed(cmdr, is_beta)
if plugin.AST_debug.get():
logger.debug("Prepared for First Dashboard")
if "PlanetRadius" in entry.keys():
currentbody = plugin.AST_current_body.get()
# flag = True
# We found a PlanetRadius again, this means we are near a planet.
if plugin.AST_debug.get():
logger.debug("On planet")
if currentbody != entry["BodyName"]:
# Body we are currently at isn't the one we currently assume
currentbody = entry["BodyName"]
plugin.AST_current_body.set(entry["BodyName"])
flag = True
if not plugin.AST_near_planet:
# We just came into range of a planet again.
if plugin.AST_debug.get():
logger.debug(f"just realized we approach a planet, nearplanet: {plugin.AST_near_planet}")
flag = True
if currentbody not in ["", None, "None"]:
if plugin.AST_debug.get():
logger.debug(plugin.AST_bios_on_planet)
key = currentbody.replace(plugin.AST_current_system.get(), '')[1:]
try:
plugin.AST_num_bios_on_planet = plugin.AST_bios_on_planet[key]
except (KeyError, TypeError):
# Nothing found at currently closest planet
if plugin.AST_debug.get():
logger.warning(f"No amount of bio signals found for body; {currentbody} with key: {key}")
plugin.AST_near_planet = True
plugin.AST_current_radius = entry["PlanetRadius"]
plugin.AST_current_pos_vector[0] = entry["Latitude"]
plugin.AST_current_pos_vector[1] = entry["Longitude"]
plugin.AST_current_pos_vector[2] = entry["Heading"]
if plugin.AST_current_pos_vector[2] < 0:
# Frontier gives us different value intervals for headings in the status.json
# Within a vehicle (srv, ship) its (0, 360) but on foot it is (-180, 180) ffs!
# With this change we force every bearing to be in the (0, 360) interval
plugin.AST_current_pos_vector[2] += 360
text = "lat: " + str(round(plugin.AST_current_pos_vector[0], 2)) + \
", long: " + str(round(plugin.AST_current_pos_vector[1], 2)) + ", B:" + \
str(plugin.AST_current_pos_vector[2]) # + ", " + str(plugin.AST_current_radius)
plugin.AST_current_pos.set(text)
if plugin.AST_current_scan_progress.get() in ["1/3", "2/3"] and plugin.AST_scan_1_pos_vector[0] is not None:
distance1 = orgi.computedistance(plugin.AST_current_pos_vector[0],
plugin.AST_current_pos_vector[1],
plugin.AST_scan_1_pos_vector[0],
plugin.AST_scan_1_pos_vector[1],
plugin.AST_current_radius)
plugin.AST_scan_1_pos_dist.set(str(round(distance1))
+ " m / " + str(plugin.AST_CCR.get()) + " m, B:" +
str(round(orgi.bearing(plugin.AST_current_pos_vector[0],
plugin.AST_current_pos_vector[1],
plugin.AST_scan_1_pos_vector[0],
plugin.AST_scan_1_pos_vector[1]), 2)))
olddist1check = plugin.AST_scan_1_dist_green
plugin.AST_scan_1_dist_green = False
if plugin.AST_CCR.get() < distance1:
plugin.AST_scan_1_dist_green = True
if olddist1check != plugin.AST_scan_1_dist_green:
flag = True
if plugin.AST_current_scan_progress.get() in ["1/3", "2/3"] and plugin.AST_scan_2_pos_vector[0] is not None:
distance2 = orgi.computedistance(plugin.AST_current_pos_vector[0],
plugin.AST_current_pos_vector[1],
plugin.AST_scan_2_pos_vector[0],
plugin.AST_scan_2_pos_vector[1],
plugin.AST_current_radius)
plugin.AST_scan_2_pos_dist.set(str(round(distance2, 2))
+ " m / " + str(plugin.AST_CCR.get()) + " m, B:" +
str(round(orgi.bearing(plugin.AST_current_pos_vector[0],
plugin.AST_current_pos_vector[1],
plugin.AST_scan_2_pos_vector[0],
plugin.AST_scan_2_pos_vector[1]), 2)))
olddist2check = plugin.AST_scan_2_dist_green
plugin.AST_scan_2_dist_green = False
if plugin.AST_CCR.get() < distance2:
plugin.AST_scan_2_dist_green = True
if olddist2check != plugin.AST_scan_2_dist_green:
flag = True
else:
if plugin.AST_near_planet:
# Switch happened, we went too far from the planet to get any reference from it.
flag = True
if plugin.AST_debug.get():
logger.debug(f"just realized we approach a planet, nearplanet: {plugin.AST_near_planet}")
plugin.AST_num_bios_on_planet = 0
plugin.AST_near_planet = False
plugin.AST_current_radius = None
plugin.AST_current_pos_vector[0] = None
plugin.AST_current_pos_vector[1] = None
plugin.AST_current_pos_vector[2] = None
plugin.AST_current_pos.set("No reference point")
if plugin.AST_debug.get():
logger.debug(f"nearplanet is now: {plugin.AST_near_planet}, will rebuild UI: {flag}")
if (((plugin.AST_last_scan_body.get() == "")
or (plugin.AST_last_scan_body.get() == "None"))):
plugin.AST_last_scan_body.set(currentbody)
if flag:
if not plugin.AST_near_planet:
plugin.AST_current_body.set("")
plugin.on_preferences_closed(cmdr, is_beta)
# ui.rebuild_ui(plugin, cmdr) is already done in on_preferences_closed
def journal_entry(cmdr: str, is_beta: bool, system: str, station: str, entry, state) -> None:
"""
React accordingly to events in the journal.
Scan Organic events tell us what we have scanned.
Scan Type of these events can tell us the progress of the scan.
Add the value of a finished Scan to the tally
:param cmdr: The current ED Commander
:param is_beta: Is the game currently in beta
:param system: Current system, if known
:param station: Current station, if any
:param entry: the current Journal entry
:param state: More info about the commander, their ship, and their cargo
"""
global plugin, firstsystemevent
if plugin.AST_debug.get():
logger.debug(entry)
logger.debug(f"Current event is {entry['event']}")
logger.debug(f"Current state Gameversion {state['GameVersion']}")
logger.debug(f"State of AST_in_Legacy variable: {plugin.AST_in_Legacy}")
if ((int(state["GameVersion"][0]) < 4) and (plugin.AST_in_Legacy is False)):
# We're in Legacy, we'll not change the state of anything through journal entries.
plugin.AST_in_Legacy = True
return
else:
plugin.AST_in_Legacy = False
if plugin.AST_debug.get():
logger.debug("Got past Version check")
# flag determines if we have to rebuild the ui at the end.
flag = plugin.handle_possible_cmdr_change(cmdr)
if plugin.AST_debug.get():
logger.debug("Handled possible CMDR change")
if ((plugin.AST_current_system.get() != system
or plugin.AST_current_system.get() == ""
or plugin.AST_current_system.get() == "None")
or (plugin.AST_current_system.get() == system
and firstsystemevent)):
if plugin.AST_debug.get():
logger.debug("Asking Canonn for system (first system related event run of plugin or system was unknown)")
firstsystemevent = False
if system not in ["None", "", None]:
plugin.AST_current_system.set(system)
try:
logger.debug("Asking Canonn about the SAAsignals in current system")
plugin.AST_bios_on_planet = plugin.ask_canonn_nicely(system)
except Exception as e:
logger.warning(e)
logger.warning(e.__doc__)
logger.warning("Couldn't get info about the SAAsignals in current system")
flag = True
if plugin.AST_debug.get():
logger.debug("Got past check to ask Canonn")
# TODO: Check if upon death in 4.0 Horizons do we lose Exobiodata.
# Probably?
# Dying while in Frontline solutions CZ does not have a Resurrect event.
# Also doesn't lose the Exobiology data.
if entry["event"] == "Resurrect":
# Reset - player was unable to sell before death
flag = True
plugin.notyetsolddata[cmdr] = eventhandling.resurrection_event(plugin)
if entry["event"] == "ScanOrganic":
logger.debug("Encountered Scan Organic Event")
flag = True
if plugin.AST_debug.get():
logger.debug("Calling eventhandler")
eventhandling.bioscan_event(cmdr, is_beta, entry, plugin, currententrytowrite)
if plugin.AST_debug.get():
logger.debug("Finished handling ScanOrganic Event")
if entry["event"] in ["Location", "Embark", "Disembark", "Touchdown", "Liftoff", "FSDJump"]:
flag = True
eventhandling.system_body_change_event(cmdr, entry, plugin)
if entry["event"] == "SellOrganicData":
flag = True
eventhandling.biosell_event(cmdr, entry, plugin)
if entry["event"] == "SAASignalsFound":
eventhandling.SAASignalsFound_event(entry, plugin)
flag = True
if flag:
# save most recent relevant state so in case of crash of the system
# we still have a proper record as long as it finishes saving below.
plugin.on_preferences_closed(cmdr, is_beta)
# endregion
plugin = ArtemisScannerTracker(AST_VERSION, AST_REPO, PLUGIN_NAME,
directory, cmdrstates,
not_yet_sold_data, sold_exobiology)
def plugin_start3(plugin_dir: str) -> str:
"""
Handle start up of the plugin.
See PLUGINS.md#startup
"""
pluginname = plugin.on_load()
return pluginname
def plugin_stop() -> None:
"""
Handle shutdown of the plugin.
See PLUGINS.md#shutdown
"""
plugin.on_unload()
return
def plugin_prefs(parent: nb.Notebook, cmdr: str, is_beta: bool) -> Optional[tk.Frame]:
"""
Handle preferences tab for the plugin.
See PLUGINS.md#configuration
"""
preferenceframe = plugin.setup_preferences(parent, cmdr, is_beta)
return preferenceframe
def prefs_changed(cmdr: str, is_beta: bool) -> None:
"""
Handle any changed preferences for the plugin.
See PLUGINS.md#configuration
"""
ui.rebuild_ui(plugin, cmdr)
plugin.on_preferences_closed(cmdr, is_beta)
return
def plugin_app(parent: tk.Frame) -> tk.Frame:
"""
Set up the UI of the plugin.
See PLUGINS.md#display
"""
global frame
frame = plugin.setup_main_ui(parent)
return frame