feat(GUI): Add Download functionality,

update Rust code, update Python code

- Add Download functionality to GUI
- Update rust code for use with GUI
- Add callbacks to rust code
- Add python modules for routing and download
This commit is contained in:
Daniel S. 2019-07-22 01:55:38 +02:00
parent a647d26337
commit ec3972b06c
16 changed files with 775 additions and 1687 deletions

7
.gitignore vendored
View file

@ -5,6 +5,11 @@
*.tmp
*.idx
.vscode/**
*.pyd
build.bat
test.bat
__pycache__
__pycache__
DL
*.egg-info
pip-wheel-metadata
rust/target/*

View file

@ -14,8 +14,6 @@ rs_gui_test
conda create -n ed_lrr_gui_env python=3
conda activate ed_lrr_gui_env
python build_gui.py
python build_gui.py
python build_gui.py
pip install setuptools_rust
pip install .
python setup.py build
@ -39,5 +37,5 @@ cd ..
```
# TODO
- refactor ed_lrr to use callbacks
- integrate callbacks into the GUI
- integrate callbacks into the GUI: WIP
- QTimer pulls from queue updates UI (every 100ms)

View file

@ -1,4 +1,7 @@
import sys
import os
import requests as RQ
from datetime import datetime, timedelta
from PyQt5.QtCore import QThread, pyqtSignal, Qt
from PyQt5.QtWidgets import (
QMainWindow,
@ -7,23 +10,71 @@ from PyQt5.QtWidgets import (
QProgressDialog,
QTreeWidgetItem,
)
from urllib.request import Request, urlopen
import gzip
from PyQt5.QtGui import QPalette, QColor
import ed_lrr_gui
import ed_lrr_gui.config as cfg
from ed_lrr_gui.gui.ed_lrr import Ui_ED_LRR
import os
from datetime import datetime
# print(ed_lrr_gui.test({"a": 1, "b": 2}))
# exit(1)
import multiprocessing as MP
class Progressdialog(QProgressDialog):
def sizeof_fmt(num, suffix="B"):
for unit in ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]:
if abs(num) < 1024.0:
return "{:.02f}{}{}".format(num, unit, suffix)
num /= 1024.0
return "{:.02f}{}{}".format(num, "Yi", suffix)
def t_round(dt):
return dt - dt % timedelta(seconds=1)
class ProgressDialog(QProgressDialog):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowModality(Qt.WindowModal)
class DownloadThread(QThread):
progress = pyqtSignal("PyQt_PyObject")
def __init__(self, systems_url, systems_file, bodies_url, bodies_file):
super().__init__()
self.systems_url = systems_url
self.systems_file = systems_file
self.bodies_url = bodies_url
self.bodies_file = bodies_file
self.running = True
def __del__(self):
self.wait()
def stop(self):
self.running = False
def run(self):
jobs = [
(self.systems_url, self.systems_file),
(self.bodies_url, self.bodies_file),
]
for url, dest in jobs:
outfile = url.split("/")[-1]
size = RQ.head(url, headers={"Accept-Encoding": "None"})
size.raise_for_status()
size = int(size.headers.get("Content-Length", 0))
with open(dest, "wb") as of:
resp = RQ.get(url, stream=True)
for chunk in resp.iter_content(1024 * 1024):
of.write(chunk)
self.progress.emit(
{"done": of.tell(), "size": size, "outfile": outfile}
)
if not self.running:
return
class App(QApplication):
def __init__(self):
super().__init__(sys.argv)
@ -60,7 +111,9 @@ class App(QApplication):
class ED_LRR(Ui_ED_LRR):
pbar_thread = None
dl_thread = None
diag_prog = None
dl_started = None
def __init__(self):
super().__init__()
@ -118,27 +171,29 @@ class ED_LRR(Ui_ED_LRR):
if self.rd_comp.isChecked():
comp_mode = "Compute Route"
self.btn_add.setText("Search+Add")
self.lst_sys.setEnabled(True)
self.btn_rm.setEnabled(True)
self.cmb_mode.setEnabled(True)
mode = self.cmb_mode.currentText()
self.set_route_mode(mode)
if self.rd_precomp.isChecked():
comp_mode = "Precompute Graph"
self.btn_add.setText("Select")
self.lst_sys.setEnabled(False)
self.btn_rm.setEnabled(False)
self.lbl_greedyness.setEnabled(False)
self.sld_greedyness.setEnabled(False)
self.cmb_mode.setEnabled(False)
self.log("COMP_MODE", comp_mode)
self.lst_sys.setEnabled(self.rd_comp.isChecked())
self.btn_rm.setEnabled(self.rd_comp.isChecked())
self.cmb_mode.setEnabled(self.rd_comp.isChecked())
self.btn_permute.setEnabled(self.rd_comp.isChecked())
self.lbl_keep.setEnabled(self.rd_comp.isChecked())
self.lbl_mode.setEnabled(self.rd_comp.isChecked())
self.chk_permute_keep_first.setEnabled(self.rd_comp.isChecked())
self.chk_permute_keep_last.setEnabled(self.rd_comp.isChecked())
self.set_route_mode(self.rd_precomp.isChecked() or None)
def set_route_mode(self, mode):
def set_route_mode(self, mode=None):
if mode == None:
mode = self.cmb_mode.currentText()
self.lbl_greedyness.setEnabled(mode == "A*-Search")
self.sld_greedyness.setEnabled(mode == "A*-Search")
self.log("ROUTE_MODE", mode)
def set_greedyness(self, value):
val = value / 100
self.lbl_greedyness.setText("Greedyness Factor ({:.0%})".format(val))
self.lbl_greedyness.setText("Greedyness Factor ({:.0%})".format(value / 100))
def sys_to_dict(self, n):
header = [
@ -153,21 +208,30 @@ class ED_LRR(Ui_ED_LRR):
def run(self):
settings = {}
settings["permute"] = [None, False, True][self.chk_permute_keep.checkState()]
settings["permute"] = (
self.chk_permute_keep_first.checkState(),
self.chk_permute_keep_last.checkState(),
)
settings["range"] = self.sb_range.value()
settings["systems"] = [
self.sys_to_dict(i) for i in range(self.lst_sys.topLevelItemCount())
]
print(settings)
# progress = Progressdialog("TEST\nBLAH", "Cancel", 0, 0, self.main_window)
# progress.setWindowTitle("Computing Route")
# progress.setFixedSize(400, 100)
# progress.setRange(0, 0)
# progress.show()
progress = ProgressDialog(
"(Not actually computing)", "Cancel", 0, 0, self.main_window
)
progress.setWindowTitle("Computing Route")
progress.setFixedSize(400, 100)
progress.setRange(0, 0)
progress.show()
def add_system(self):
n = self.lst_sys.topLevelItemCount() + 1
item = QTreeWidgetItem(self.lst_sys, ["A" + str(n), "B" + str(n)])
name = self.inp_sys.text()
item = QTreeWidgetItem(
self.lst_sys, [str(n) + ". Name: " + name, "Type: " + name[::-1]]
)
item.sys_id = "SYS_ID_HERE"
item.setFlags(item.flags() & ~Qt.ItemIsDropEnabled)
def remove_system(self):
@ -175,7 +239,66 @@ class ED_LRR(Ui_ED_LRR):
for item in self.lst_sys.selectedItems():
root.removeChild(item)
def dl_canceled(self):
if self.dl_thread:
print("Cancel!")
try:
self.dl_thread.progress.disconnect()
except TypeError:
pass
self.dl_thread.stop()
self.dl_thread.wait()
self.diag_prog.close()
self.dl_thread = None
self.diag_prog = None
self.dl_started = None
def handle_progress(self, args):
filename = os.path.split(args["outfile"])[-1]
if self.diag_prog is None:
self.diag_prog = ProgressDialog("", "Cancel", 0, 1000, self.main_window)
if self.dl_thread:
self.diag_prog.canceled.connect(self.dl_canceled)
self.diag_prog.show()
t_elapsed = datetime.today() - self.dl_started
rate = args["done"] / t_elapsed.total_seconds()
remaining = (args["size"] - args["done"]) / rate
rate = round(rate, 2)
# print(rate, remaining)
try:
t_rem = timedelta(seconds=remaining)
except OverflowError:
t_rem = "-"
msg = "Downloading {} [{}/{}] ({}/s)\n[{}/{}]".format(
filename,
sizeof_fmt(args["done"]),
sizeof_fmt(args["size"]),
sizeof_fmt(rate),
t_round(t_elapsed),
t_round(t_rem),
)
self.diag_prog.setLabelText(msg)
self.diag_prog.setWindowTitle("Downloading EDSM Dumps")
self.diag_prog.setValue((args["done"] * 1000) // args["size"])
def run_download(self):
if self.dl_thread:
return
self.dl_started = datetime.today()
self.dl_thread = DownloadThread(
self.inp_systems_dl.currentText(),
self.inp_systems_dest_dl.currentText(),
self.inp_bodies_dl.currentText(),
self.inp_bodies_dest_dl.currentText(),
)
self.dl_thread.progress.connect(self.handle_progress)
self.dl_thread.start()
print(".")
def setup_signals(self):
self.btn_download.clicked.connect(self.run_download)
self.inp_systems_dest_dl.setCurrentText(r"D:\devel\rust\ed_lrr_gui\DL\s.json")
self.inp_bodies_dest_dl.setCurrentText(r"D:\devel\rust\ed_lrr_gui\DL\b.json")
self.set_greedyness(self.sld_greedyness.value())
self.cmb_mode.currentTextChanged.connect(self.set_route_mode)
self.rd_comp.toggled.connect(self.set_comp_mode)
@ -203,7 +326,6 @@ class ED_LRR(Ui_ED_LRR):
self.btn_systems_dest_browse_dl.clicked.connect(
lambda: self.get_save_file("JSON File (*.json)", self.set_systems_file)
)
self.menu_act_quit.triggered.connect(self.app.quit)
def handle_close(self):
cfg.write(self.config)
@ -216,11 +338,10 @@ class ED_LRR(Ui_ED_LRR):
self.app = app
self.setup_signals()
self.lst_sys.setHeaderLabels(["Name", "Type"])
self.set_route_mode()
def main():
import sys
app = App()
MainWindow = QMainWindow()
ui = ED_LRR()
@ -228,8 +349,9 @@ def main():
MainWindow.show()
ret = app.exec_()
ui.handle_close()
sys.exit(ret)
exit(ret)
if __name__ == "__main__":
MP.freeze_support()
main()

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'D:\devel\rust\py_test\ed_lrr_gui\gui\ed_lrr.ui'
# Form implementation generated from reading ui file 'D:\devel\rust\ed_lrr_gui\ed_lrr_gui\gui\ed_lrr.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
@ -22,6 +22,7 @@ class Ui_ED_LRR(object):
ED_LRR.setSizePolicy(sizePolicy)
ED_LRR.setMinimumSize(QtCore.QSize(577, 500))
ED_LRR.setMaximumSize(QtCore.QSize(577, 500))
ED_LRR.setStyleSheet("")
ED_LRR.setDocumentMode(False)
ED_LRR.setTabShape(QtWidgets.QTabWidget.Rounded)
self.centralwidget = QtWidgets.QWidget(ED_LRR)
@ -30,7 +31,10 @@ class Ui_ED_LRR(object):
self.verticalLayout.setObjectName("verticalLayout")
self.tabs = QtWidgets.QTabWidget(self.centralwidget)
self.tabs.setEnabled(True)
self.tabs.setAutoFillBackground(False)
self.tabs.setTabPosition(QtWidgets.QTabWidget.North)
self.tabs.setTabShape(QtWidgets.QTabWidget.Rounded)
self.tabs.setElideMode(QtCore.Qt.ElideNone)
self.tabs.setTabsClosable(False)
self.tabs.setTabBarAutoHide(False)
self.tabs.setObjectName("tabs")
@ -46,10 +50,12 @@ class Ui_ED_LRR(object):
self.formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.lbl_systems_dl)
self.inp_bodies_dl = QtWidgets.QComboBox(self.tab_download)
self.inp_bodies_dl.setEditable(True)
self.inp_bodies_dl.setInsertPolicy(QtWidgets.QComboBox.InsertAtTop)
self.inp_bodies_dl.setObjectName("inp_bodies_dl")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.inp_bodies_dl)
self.inp_systems_dl = QtWidgets.QComboBox(self.tab_download)
self.inp_systems_dl.setEditable(True)
self.inp_systems_dl.setInsertPolicy(QtWidgets.QComboBox.InsertAtTop)
self.inp_systems_dl.setObjectName("inp_systems_dl")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.inp_systems_dl)
self.gridLayout = QtWidgets.QGridLayout()
@ -61,6 +67,7 @@ class Ui_ED_LRR(object):
sizePolicy.setHeightForWidth(self.inp_bodies_dest_dl.sizePolicy().hasHeightForWidth())
self.inp_bodies_dest_dl.setSizePolicy(sizePolicy)
self.inp_bodies_dest_dl.setEditable(True)
self.inp_bodies_dest_dl.setInsertPolicy(QtWidgets.QComboBox.InsertAtTop)
self.inp_bodies_dest_dl.setObjectName("inp_bodies_dest_dl")
self.gridLayout.addWidget(self.inp_bodies_dest_dl, 0, 0, 1, 1)
self.btn_bodies_dest_browse_dl = QtWidgets.QPushButton(self.tab_download)
@ -79,6 +86,7 @@ class Ui_ED_LRR(object):
sizePolicy.setHeightForWidth(self.inp_systems_dest_dl.sizePolicy().hasHeightForWidth())
self.inp_systems_dest_dl.setSizePolicy(sizePolicy)
self.inp_systems_dest_dl.setEditable(True)
self.inp_systems_dest_dl.setInsertPolicy(QtWidgets.QComboBox.InsertAtTop)
self.inp_systems_dest_dl.setObjectName("inp_systems_dest_dl")
self.gridLayout_2.addWidget(self.inp_systems_dest_dl, 0, 0, 1, 1)
self.formLayout.setLayout(4, QtWidgets.QFormLayout.FieldRole, self.gridLayout_2)
@ -111,6 +119,7 @@ class Ui_ED_LRR(object):
sizePolicy.setHeightForWidth(self.inp_bodies_pp.sizePolicy().hasHeightForWidth())
self.inp_bodies_pp.setSizePolicy(sizePolicy)
self.inp_bodies_pp.setEditable(True)
self.inp_bodies_pp.setInsertPolicy(QtWidgets.QComboBox.InsertAtTop)
self.inp_bodies_pp.setObjectName("inp_bodies_pp")
self.gr_bodies_pp.addWidget(self.inp_bodies_pp, 0, 0, 1, 1)
self.formLayout_3.setLayout(0, QtWidgets.QFormLayout.FieldRole, self.gr_bodies_pp)
@ -129,6 +138,7 @@ class Ui_ED_LRR(object):
sizePolicy.setHeightForWidth(self.inp_systems_pp.sizePolicy().hasHeightForWidth())
self.inp_systems_pp.setSizePolicy(sizePolicy)
self.inp_systems_pp.setEditable(True)
self.inp_systems_pp.setInsertPolicy(QtWidgets.QComboBox.InsertAtTop)
self.inp_systems_pp.setObjectName("inp_systems_pp")
self.gr_systems_pp.addWidget(self.inp_systems_pp, 0, 0, 1, 1)
self.formLayout_3.setLayout(1, QtWidgets.QFormLayout.FieldRole, self.gr_systems_pp)
@ -152,6 +162,7 @@ class Ui_ED_LRR(object):
sizePolicy.setHeightForWidth(self.inp_out_pp.sizePolicy().hasHeightForWidth())
self.inp_out_pp.setSizePolicy(sizePolicy)
self.inp_out_pp.setEditable(True)
self.inp_out_pp.setInsertPolicy(QtWidgets.QComboBox.InsertAtTop)
self.inp_out_pp.setObjectName("inp_out_pp")
self.gr_out_grid_pp.addWidget(self.inp_out_pp, 0, 0, 1, 1)
self.formLayout_3.setLayout(2, QtWidgets.QFormLayout.FieldRole, self.gr_out_grid_pp)
@ -178,6 +189,9 @@ class Ui_ED_LRR(object):
sizePolicy.setHeightForWidth(self.inp_sys_lst.sizePolicy().hasHeightForWidth())
self.inp_sys_lst.setSizePolicy(sizePolicy)
self.inp_sys_lst.setEditable(True)
self.inp_sys_lst.setInsertPolicy(QtWidgets.QComboBox.InsertAtTop)
self.inp_sys_lst.setFrame(True)
self.inp_sys_lst.setModelColumn(0)
self.inp_sys_lst.setObjectName("inp_sys_lst")
self.gr_sys.addWidget(self.inp_sys_lst, 0, 0, 1, 1)
self.formLayout_2.setLayout(0, QtWidgets.QFormLayout.FieldRole, self.gr_sys)
@ -203,15 +217,6 @@ class Ui_ED_LRR(object):
self.btn_permute = QtWidgets.QPushButton(self.tab_route)
self.btn_permute.setObjectName("btn_permute")
self.formLayout_2.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.btn_permute)
self.chk_permute_keep = QtWidgets.QCheckBox(self.tab_route)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.chk_permute_keep.sizePolicy().hasHeightForWidth())
self.chk_permute_keep.setSizePolicy(sizePolicy)
self.chk_permute_keep.setTristate(True)
self.chk_permute_keep.setObjectName("chk_permute_keep")
self.formLayout_2.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.chk_permute_keep)
self.lst_sys = QtWidgets.QTreeWidget(self.tab_route)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
@ -226,15 +231,16 @@ class Ui_ED_LRR(object):
self.lst_sys.setAlternatingRowColors(True)
self.lst_sys.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
self.lst_sys.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.lst_sys.setHeaderHidden(True)
self.lst_sys.setObjectName("lst_sys")
self.lst_sys.headerItem().setText(0, "1")
self.formLayout_2.setWidget(5, QtWidgets.QFormLayout.SpanningRole, self.lst_sys)
self.formLayout_2.setWidget(7, QtWidgets.QFormLayout.SpanningRole, self.lst_sys)
self.sb_range = QtWidgets.QDoubleSpinBox(self.tab_route)
self.sb_range.setObjectName("sb_range")
self.formLayout_2.setWidget(6, QtWidgets.QFormLayout.FieldRole, self.sb_range)
self.formLayout_2.setWidget(8, QtWidgets.QFormLayout.FieldRole, self.sb_range)
self.lbl_range = QtWidgets.QLabel(self.tab_route)
self.lbl_range.setObjectName("lbl_range")
self.formLayout_2.setWidget(6, QtWidgets.QFormLayout.LabelRole, self.lbl_range)
self.formLayout_2.setWidget(8, QtWidgets.QFormLayout.LabelRole, self.lbl_range)
self.gr_opts = QtWidgets.QGridLayout()
self.gr_opts.setObjectName("gr_opts")
self.cmb_mode = QtWidgets.QComboBox(self.tab_route)
@ -262,11 +268,34 @@ class Ui_ED_LRR(object):
self.lbl_mode = QtWidgets.QLabel(self.tab_route)
self.lbl_mode.setObjectName("lbl_mode")
self.gr_opts.addWidget(self.lbl_mode, 0, 1, 1, 1)
self.formLayout_2.setLayout(7, QtWidgets.QFormLayout.SpanningRole, self.gr_opts)
self.formLayout_2.setLayout(9, QtWidgets.QFormLayout.SpanningRole, self.gr_opts)
self.btn_go = QtWidgets.QPushButton(self.tab_route)
self.btn_go.setFlat(False)
self.btn_go.setObjectName("btn_go")
self.formLayout_2.setWidget(8, QtWidgets.QFormLayout.LabelRole, self.btn_go)
self.formLayout_2.setWidget(10, QtWidgets.QFormLayout.LabelRole, self.btn_go)
self.gridLayout_4 = QtWidgets.QGridLayout()
self.gridLayout_4.setObjectName("gridLayout_4")
self.chk_permute_keep_last = QtWidgets.QCheckBox(self.tab_route)
self.chk_permute_keep_last.setObjectName("chk_permute_keep_last")
self.gridLayout_4.addWidget(self.chk_permute_keep_last, 0, 2, 1, 1)
self.chk_permute_keep_first = QtWidgets.QCheckBox(self.tab_route)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.chk_permute_keep_first.sizePolicy().hasHeightForWidth())
self.chk_permute_keep_first.setSizePolicy(sizePolicy)
self.chk_permute_keep_first.setTristate(False)
self.chk_permute_keep_first.setObjectName("chk_permute_keep_first")
self.gridLayout_4.addWidget(self.chk_permute_keep_first, 0, 1, 1, 1)
self.lbl_keep = QtWidgets.QLabel(self.tab_route)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lbl_keep.sizePolicy().hasHeightForWidth())
self.lbl_keep.setSizePolicy(sizePolicy)
self.lbl_keep.setObjectName("lbl_keep")
self.gridLayout_4.addWidget(self.lbl_keep, 0, 0, 1, 1)
self.formLayout_2.setLayout(4, QtWidgets.QFormLayout.FieldRole, self.gridLayout_4)
self.tabs.addTab(self.tab_route, "")
self.tab_log = QtWidgets.QWidget()
self.tab_log.setObjectName("tab_log")
@ -299,6 +328,7 @@ class Ui_ED_LRR(object):
self.retranslateUi(ED_LRR)
self.tabs.setCurrentIndex(2)
self.menu_act_quit.triggered.connect(ED_LRR.close)
QtCore.QMetaObject.connectSlotsByName(ED_LRR)
ED_LRR.setTabOrder(self.rd_comp, self.cmb_mode)
ED_LRR.setTabOrder(self.cmb_mode, self.chk_primary)
@ -329,11 +359,11 @@ class Ui_ED_LRR(object):
self.lbl_sys_lst.setText(_translate("ED_LRR", "System List"))
self.btn_sys_lst_browse.setText(_translate("ED_LRR", "..."))
self.btn_add.setText(_translate("ED_LRR", "Search+Add"))
self.inp_sys.setPlaceholderText(_translate("ED_LRR", "System Name"))
self.btn_rm.setText(_translate("ED_LRR", "Remove"))
self.rd_comp.setText(_translate("ED_LRR", "Compute Route"))
self.rd_precomp.setText(_translate("ED_LRR", "Precompute Graph"))
self.btn_permute.setText(_translate("ED_LRR", "Permute"))
self.chk_permute_keep.setText(_translate("ED_LRR", "Keep endpoints (No, first only, yes)"))
self.lbl_range.setText(_translate("ED_LRR", "Jump Range (Ly)"))
self.cmb_mode.setCurrentText(_translate("ED_LRR", "Breadth-First Search"))
self.cmb_mode.setItemText(0, _translate("ED_LRR", "Breadth-First Search"))
@ -343,6 +373,9 @@ class Ui_ED_LRR(object):
self.chk_primary.setText(_translate("ED_LRR", "Primary Stars Only"))
self.lbl_mode.setText(_translate("ED_LRR", "Mode"))
self.btn_go.setText(_translate("ED_LRR", "GO!"))
self.chk_permute_keep_last.setText(_translate("ED_LRR", "Last"))
self.chk_permute_keep_first.setText(_translate("ED_LRR", "First"))
self.lbl_keep.setText(_translate("ED_LRR", "Keep Endpoints:"))
self.tabs.setTabText(self.tabs.indexOf(self.tab_route), _translate("ED_LRR", "Route"))
self.tabs.setTabText(self.tabs.indexOf(self.tab_log), _translate("ED_LRR", "Log"))
self.menu_file.setTitle(_translate("ED_LRR", "File"))

View file

@ -34,6 +34,9 @@
<property name="windowTitle">
<string>Elite: Dangerous Long Range Route Plotter</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="documentMode">
<bool>false</bool>
</property>
@ -47,12 +50,21 @@
<property name="enabled">
<bool>true</bool>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="tabPosition">
<enum>QTabWidget::North</enum>
</property>
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
</property>
<property name="currentIndex">
<number>2</number>
</property>
<property name="elideMode">
<enum>Qt::ElideNone</enum>
</property>
<property name="tabsClosable">
<bool>false</bool>
</property>
@ -86,6 +98,9 @@
<property name="currentText">
<string>https://www.edsm.net/dump/bodies.json</string>
</property>
<property name="insertPolicy">
<enum>QComboBox::InsertAtTop</enum>
</property>
</widget>
</item>
<item row="3" column="1">
@ -96,6 +111,9 @@
<property name="currentText">
<string>https://www.edsm.net/dump/systemsWithCoordinates.json</string>
</property>
<property name="insertPolicy">
<enum>QComboBox::InsertAtTop</enum>
</property>
</widget>
</item>
<item row="2" column="1">
@ -111,6 +129,9 @@
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::InsertAtTop</enum>
</property>
</widget>
</item>
<item row="0" column="1">
@ -142,6 +163,9 @@
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::InsertAtTop</enum>
</property>
</widget>
</item>
</layout>
@ -201,6 +225,9 @@
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::InsertAtTop</enum>
</property>
</widget>
</item>
</layout>
@ -232,6 +259,9 @@
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::InsertAtTop</enum>
</property>
</widget>
</item>
</layout>
@ -269,6 +299,9 @@
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::InsertAtTop</enum>
</property>
</widget>
</item>
</layout>
@ -314,6 +347,15 @@
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::InsertAtTop</enum>
</property>
<property name="frame">
<bool>true</bool>
</property>
<property name="modelColumn">
<number>0</number>
</property>
</widget>
</item>
</layout>
@ -326,7 +368,11 @@
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="inp_sys"/>
<widget class="QLineEdit" name="inp_sys">
<property name="placeholderText">
<string>System Name</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QPushButton" name="btn_rm">
@ -363,23 +409,7 @@
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QCheckBox" name="chk_permute_keep">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Keep endpoints (No, first only, yes)</string>
</property>
<property name="tristate">
<bool>true</bool>
</property>
</widget>
</item>
<item row="5" column="0" colspan="2">
<item row="7" column="0" colspan="2">
<widget class="QTreeWidget" name="lst_sys">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
@ -414,6 +444,9 @@
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="headerHidden">
<bool>true</bool>
</property>
<column>
<property name="text">
<string notr="true">1</string>
@ -421,17 +454,17 @@
</column>
</widget>
</item>
<item row="6" column="1">
<item row="8" column="1">
<widget class="QDoubleSpinBox" name="sb_range"/>
</item>
<item row="6" column="0">
<item row="8" column="0">
<widget class="QLabel" name="lbl_range">
<property name="text">
<string>Jump Range (Ly)</string>
</property>
</widget>
</item>
<item row="7" column="0" colspan="2">
<item row="9" column="0" colspan="2">
<layout class="QGridLayout" name="gr_opts">
<item row="0" column="2">
<widget class="QComboBox" name="cmb_mode">
@ -503,7 +536,7 @@
</item>
</layout>
</item>
<item row="8" column="0">
<item row="10" column="0">
<widget class="QPushButton" name="btn_go">
<property name="text">
<string>GO!</string>
@ -513,6 +546,46 @@
</property>
</widget>
</item>
<item row="4" column="1">
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="2">
<widget class="QCheckBox" name="chk_permute_keep_last">
<property name="text">
<string>Last</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QCheckBox" name="chk_permute_keep_first">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>First</string>
</property>
<property name="tristate">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="lbl_keep">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Keep Endpoints:</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_log">
@ -580,5 +653,22 @@
<tabstop>rd_precomp</tabstop>
</tabstops>
<resources/>
<connections/>
<connections>
<connection>
<sender>menu_act_quit</sender>
<signal>triggered()</signal>
<receiver>ED_LRR</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>288</x>
<y>249</y>
</hint>
</hints>
</connection>
</connections>
</ui>

45
ed_lrr_gui/preprocess.py Normal file
View file

@ -0,0 +1,45 @@
from multiprocessing import Process, Queue, freeze_support
import queue
from datetime import datetime, timedelta
import _ed_lrr
from collections import namedtuple
class Preprocessor(Process):
def __init__(self, *args, **kwargs):
super().__init__()
self.state = {}
self.queue = Queue()
self.daemon = True
self.args = args
self.kwargs = kwargs
self.kwargs["callback"] = self.callback
self.start()
def __iter__(self):
while self.is_alive():
try:
self.state.update(self.queue.get(True, 0.5))
yield self.state
except queue.Empty:
pass
while not self.queue.empty():
self.state.update(self.queue.get(True, 0.5))
yield self.state
def callback(self, state):
self.queue.put({"status": state})
def run(self):
_ed_lrr.preprocess(*self.args, **self.kwargs)
if __name__ == "__main__":
freeze_support()
r = Preprocessor(
r"D:\devel\rust\ED_LRR\dumps\systemsWithCoordinates.json",
r"D:\devel\rust\ED_LRR\dumps\bodies.json",
r"D:\devel\rust\ED_LRR\stars.csv",
)
for i, e in enumerate(r):
print(e)

51
ed_lrr_gui/router.py Normal file
View file

@ -0,0 +1,51 @@
from multiprocessing import Process, Queue, freeze_support
import queue
from datetime import datetime, timedelta
import _ed_lrr
from collections import namedtuple
class Router(Process):
def __init__(self, *args, **kwargs):
super().__init__()
self.state = {}
self.queue = Queue()
self.daemon = True
self.args = args
self.kwargs = kwargs
self.kwargs["callback"] = self.callback
self.start()
def __iter__(self):
while self.is_alive():
try:
self.state.update(self.queue.get(True, 0.5))
yield self.state
except queue.Empty:
pass
while not self.queue.empty():
self.state.update(self.queue.get(True, 0.5))
yield self.state
def callback(self, state):
self.queue.put({"status": state})
def run(self):
route = _ed_lrr.route(*self.args, **self.kwargs)
self.queue.put({"return": route})
if __name__ == "__main__":
freeze_support()
r = Router(
["Ix", "Beagle Point"],
48,
"BFS",
False,
False,
None,
None,
r"D:\devel\rust\ED_LRR\stars.csv",
)
for e in r:
print(e)

1233
rust/Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -11,15 +11,13 @@ license = "WTFPL"
crate-type = ["cdylib"]
[profile.release]
# debug=true
debug=true
[dependencies]
csv = "1.1.1"
serde = "1.0.94"
serde_derive = "1.0.94"
rstar = {version="0.4.0"}
serde = { version = "1.0", features = ["derive"] }
rstar = "0.4.0"
humantime = "1.2.0"
structopt = "0.2.18"
permutohedron = "0.2.4"
serde_json = "1.0.40"
indicatif = "0.11.0"
@ -27,7 +25,6 @@ fnv = "1.0.6"
bincode = "1.1.4"
sha3 = "0.8.2"
byteorder = "1.3.2"
reqwest = "0.9.18"
[dependencies.pyo3]
version = "0.7.0"

View file

@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemSerde {
pub id: u32,

View file

@ -1,63 +0,0 @@
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use std::fs::File;
use std::io;
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(Debug, StructOpt, Clone)]
pub struct DownloadOpts {
#[structopt(
long = "bodies",
default_value = "https://www.edsm.net/dump/bodies.json"
)]
/// Url to bodies.json
bodies_url: String,
#[structopt(
long = "systems",
default_value = "https://www.edsm.net/dump/systemsWithCoordinates.json"
)]
/// Url to systemsWithCoordinates.json
systems_url: String,
}
fn fetch_url(url: &str, prog_bar: &ProgressBar) -> std::io::Result<()> {
let outfile = url.split('/').last().unwrap();
let template = format!("{} {}", "[{elapsed_precise}] {binary_bytes}", outfile);
prog_bar.set_style(ProgressStyle::default_bar().template(&template));
let client = reqwest::Client::builder().gzip(true).build().unwrap();
let resp = client.get(url).send().unwrap();
let target_path = PathBuf::from(format!("dumps/{}", outfile));
if target_path.exists() {
eprintln!("Error: Target {} exists!", outfile);
return Ok(());
}
let mut target = File::create(target_path)?;
io::copy(&mut prog_bar.wrap_read(resp), &mut target).unwrap();
prog_bar.finish();
Ok(())
}
pub fn download(opts: DownloadOpts) -> std::io::Result<()> {
let mut threads = Vec::new();
let m = MultiProgress::new();
{
let opts = opts.clone();
let pb = m.add(ProgressBar::new(0));
threads.push(std::thread::spawn(move || {
fetch_url(&opts.bodies_url, &pb).unwrap();
}));
};
{
let opts = opts.clone();
let pb = m.add(ProgressBar::new(0));
threads.push(std::thread::spawn(move || {
fetch_url(&opts.systems_url, &pb).unwrap();
}));
}
m.join_and_clear().unwrap();
for th in threads {
th.join().unwrap();
}
Ok(())
}

View file

@ -1,56 +1,120 @@
mod common;
mod download;
mod preprocess;
mod route;
use std::collections::HashMap;
use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyo3::types::{PyDict,PyList};
use pyo3::exceptions::*;
use std::path::PathBuf;
#[pymodule]
pub fn _ed_lrr(py: Python, m: &PyModule) -> PyResult<()> {
/// Test
#[pyfn(m,"test")]
fn test(py:Python,o:PyObject) -> PyResult<()> {
println!("OBJ: {:?}",o);
return Ok(());
}
pub fn _ed_lrr(_py: Python, m: &PyModule) -> PyResult<()> {
/// download(systems_url,systems_file, bodies_url, bodies_file, callback)
/// --
///
/// Download bodies.json and systemsWithCoordinates.json
#[pyfn(m, "download")]
fn download(py: Python, systems_url: String, systems_file: String, bodies_url:String,bodies_file:String,callback: PyObject) -> PyResult<PyObject> {
let state = PyDict::new(py);
state.set_item("systems_url", systems_url)?;
state.set_item("systems_file", systems_file)?;
state.set_item("bodies_url", bodies_url)?;
state.set_item("bodies_file", bodies_file)?;
state.set_item("callback", callback)?;
return Ok(state.to_object(py));
}
/// preprocess(infile_systems, infile_bodies, outfile, callback)
/// --
/// --
///
/// Preprocess bodies.json and systemsWithCoordinates.json into stars.csv
#[pyfn(m, "preprocess")]
fn preprocess(py: Python, infile_systems: String, infile_bodies: String, outfile:String,callback: PyObject) -> PyResult<PyObject> {
fn ed_lrr_preprocess(
py: Python<'static>,
infile_systems: String,
infile_bodies: String,
outfile: String,
callback: PyObject,
) -> PyResult<PyObject> {
use preprocess::*;
let state = PyDict::new(py);
state.set_item("infile_systems", infile_systems)?;
state.set_item("infile_bodies", infile_bodies)?;
state.set_item("outfile", outfile)?;
state.set_item("callback", callback)?;
let state_dict = PyDict::new(py);
callback.call(py,(state_dict,),None).unwrap();
let callback_wrapped = move |state: &PreprocessState| {
// println!("SEND: {:?}",state);
state_dict.set_item("file",state.file.clone())?;
state_dict.set_item("total",state.total)?;
state_dict.set_item("done",state.done)?;
callback.call(py,(state_dict,),None)
};
preprocess_files(&PathBuf::from(infile_bodies), &PathBuf::from(infile_systems), &PathBuf::from(outfile), Box::new(callback_wrapped)).unwrap();
return Ok(state.to_object(py));
}
/// route(infile, source, dest, range, mode, greedyness, precomp, callback)
/// route(infile, hops, range, mode,primary, greedyness, precomp, callback)
/// --
///
/// Compute a Route using the suplied parameters
#[pyfn(m, "route")]
fn route(py: Python, infile: String, source: String, dest:String, range: f32, mode: String,greedyness: Option<f32>, precomp: Option<String>,callback: PyObject) -> PyResult<PyObject> {
fn route(
py: Python<'static>,
hops: Vec<String>,
range: f32,
mode: String,
primary: bool,
permute: bool,
greedyness: Option<f32>,
precomp: Option<String>,
path: String,
callback: PyObject,
) -> PyResult<PyObject> {
use route::*;
// TODO: Verify Parameters
let mode = match Mode::parse(&mode) {
Ok(val) => val,
Err(e) => {
return Err(PyErr::new::<ValueError,_>(e));
}
};
let state_dict = PyDict::new(py);
callback.call(py,(state_dict,),None).unwrap();
let callback_wrapped = move |state: &SearchState| {
println!("SEND: {:?}",state);
state_dict.set_item("mode",state.mode.clone())?;
state_dict.set_item("system",state.system.clone())?;
state_dict.set_item("body",state.body.clone())?;
state_dict.set_item("depth",state.depth)?;
state_dict.set_item("queue_size",state.queue_size)?;
state_dict.set_item("d_rem",state.d_rem)?;
state_dict.set_item("d_total",state.d_total)?;
state_dict.set_item("prc_done",state.prc_done)?;
state_dict.set_item("n_seen",state.n_seen)?;
state_dict.set_item("prc_seen",state.prc_seen)?;
callback.call(py,(state_dict,),None)
};
let opts=RouteOpts{
systems:hops,
range:Some(range),
file_path: PathBuf::from(path),
precomp_file: precomp.map(PathBuf::from),
callback: Box::new(callback_wrapped),
mode,
factor: greedyness,
precompute: false,
permute: permute,
keep_first: true,
keep_last: true,
primary
};
let none=().to_object(py);
match route(opts) {
Ok(Some(route)) => {
let hops=route.iter().map(|hop| {
let pos=PyList::new(py,hop.pos.iter());
let elem=PyDict::new(py);
elem.set_item("star_type",hop.star_type.clone()).unwrap();
elem.set_item("system",hop.system.clone()).unwrap();
elem.set_item("body",hop.body.clone()).unwrap();
elem.set_item("distance",hop.distance).unwrap();
elem.set_item("pos",pos).unwrap();
return elem;
});
let lst=PyList::new(py,hops);
return Ok(lst.to_object(py));
}
Ok(None) => {
return Ok(none);
},
Err(e) => {
return Err(PyErr::new::<ValueError,_>(e));
}
};
/*
let state = PyDict::new(py);
state.set_item("infile", infile)?;
state.set_item("source", source)?;
@ -60,7 +124,8 @@ pub fn _ed_lrr(py: Python, m: &PyModule) -> PyResult<()> {
state.set_item("greedyness", greedyness)?;
state.set_item("precomp", precomp)?;
state.set_item("callback", callback)?;
return Ok(state.to_object(py));
return callback.call(py,(state.to_object(py),),None);
*/
}
Ok(())
}
}

View file

@ -1,33 +1,14 @@
use crate::common::SystemSerde;
use fnv::FnvHashMap;
use humantime::format_duration;
use indicatif::{ProgressBar, ProgressStyle};
use serde::Deserialize;
use serde_json::Result;
use std::fs::File;
use std::io::Seek;
use std::io::{BufRead, BufReader, BufWriter, SeekFrom};
use std::path::PathBuf;
use std::str;
use std::time::Instant;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
pub struct PreprocessOpts {
#[structopt(short, long = "bodies", default_value = "dumps/bodies.json")]
/// Path to bodies.json
pub bodies: PathBuf,
#[structopt(
short,
long = "systems",
default_value = "dumps/systemsWithCoordinates.json"
)]
/// Path to systemsWithCoordinates.json
pub systems: PathBuf,
#[structopt(default_value = "stars")]
/// outfile prefix
pub prefix: String,
}
use std::str;
use pyo3::prelude::*;
#[derive(Debug, Deserialize)]
#[allow(non_snake_case)]
@ -58,25 +39,15 @@ struct System {
date: String,
}
#[derive(Debug, StructOpt)]
#[structopt(
name = "ed_lrr_pp",
about = "Preprocessor for Elite: Dangerous Long-Range Router",
rename_all = "snake_case"
)]
/// Preprocess data for ed_lrr
struct Opt {
#[structopt(short, long = "bodies")]
/// Path to bodies.json
bodies: PathBuf,
#[structopt(short, long = "systems")]
/// Path to systemsWithCoordinates.json
systems: PathBuf,
#[structopt(default_value = "stars")]
/// outfile prefix
prefix: String,
#[derive(Debug)]
pub struct PreprocessState {
pub file: String,
pub total: u64,
pub done: u64,
pub count: usize,
}
fn get_mult(star_type: &str) -> f32 {
if star_type.contains("White Dwarf") {
return 1.5;
@ -87,22 +58,18 @@ fn get_mult(star_type: &str) -> f32 {
1.0
}
fn process(path: &PathBuf, func: &mut dyn for<'r> FnMut(&'r str) -> ()) -> std::io::Result<()> {
let mut cnt = 0;
fn process(path: &PathBuf, func: &mut dyn for<'r> FnMut(&'r str) -> (),callback: &Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>>) -> std::io::Result<()> {
let mut buffer = String::new();
let t_start = Instant::now();
let fh = File::open(path)?;
let prog_bar = ProgressBar::new(fh.metadata()?.len());
prog_bar.set_style(
ProgressStyle::default_bar()
.template(
"[{elapsed_precise}/{eta_precise}]{spinner} [{wide_bar}] {binary_bytes}/{binary_total_bytes} ({percent}%)",
)
.progress_chars("#9876543210 ")
.tick_chars("/-\\|"),
);
prog_bar.set_draw_delta(1024 * 1024);
let total_size= fh.metadata()?.len();
let mut t_last=Instant::now();
let mut reader = BufReader::new(fh);
let mut state=PreprocessState {
file: path.to_str().unwrap().to_owned(),
total: total_size,
done: 0,
count: 0
};
println!("Loading {} ...", path.to_str().unwrap());
while let Ok(n) = reader.read_line(&mut buffer) {
if n == 0 {
@ -112,20 +79,19 @@ fn process(path: &PathBuf, func: &mut dyn for<'r> FnMut(&'r str) -> ()) -> std::
if !buffer.is_empty() {
func(&buffer);
}
prog_bar.set_position(reader.seek(SeekFrom::Current(0)).unwrap());
cnt += 1;
let pos=reader.seek(SeekFrom::Current(0)).unwrap();
state.done=pos;
state.count += 1;
if (t_last.elapsed().as_millis()>100) {
callback(&state)?;
t_last=Instant::now();
}
buffer.clear();
}
prog_bar.finish_and_clear();
println!(
"Processed {} lines in {} ...",
cnt,
format_duration(t_start.elapsed())
);
Ok(())
}
fn process_systems(path: &PathBuf) -> FnvHashMap<i32, System> {
fn process_systems(path: &PathBuf,callback: &Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>> ) -> FnvHashMap<i32, System> {
let mut ret = FnvHashMap::default();
process(path, &mut |line| {
let sys_res: Result<System> = serde_json::from_str(&line);
@ -134,7 +100,7 @@ fn process_systems(path: &PathBuf) -> FnvHashMap<i32, System> {
} else {
eprintln!("\nError parsing: {}\n\t{:?}\n", line, sys_res.unwrap_err());
}
})
},callback)
.unwrap();
ret
}
@ -155,10 +121,10 @@ fn build_index(path: &PathBuf) -> std::io::Result<()> {
fn process_bodies(
path: &PathBuf,
out_prefix: &str,
out_path: &PathBuf,
systems: &mut FnvHashMap<i32, System>,
callback: &Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>>,
) -> std::io::Result<()> {
let out_path = PathBuf::from(format!("{}.csv", out_prefix));
println!(
"Processing {} into {} ...",
path.to_str().unwrap(),
@ -196,18 +162,17 @@ fn process_bodies(
} else {
eprintln!("\nError parsing: {}\n\t{:?}\n", line, body_res.unwrap_err());
}
})
},callback)
.unwrap();
println!("Total Systems: {}", n);
systems.clear();
Ok(())
}
pub fn preprocess_files(opts: PreprocessOpts) -> std::io::Result<()> {
let out_path = PathBuf::from(format!("{}.csv", &opts.prefix));
pub fn preprocess_files(bodies: &PathBuf,systems:&PathBuf,out_path:&PathBuf,callback: Box<dyn Fn(&PreprocessState) -> PyResult<PyObject>>) -> std::io::Result<()> {
if !out_path.exists() {
let mut systems = process_systems(&opts.systems);
process_bodies(&opts.bodies, &opts.prefix, &mut systems)?;
let mut systems = process_systems(systems,&callback);
process_bodies(bodies, out_path, &mut systems,&callback)?;
} else {
println!(
"File '{}' exists, not overwriting it",

View file

@ -11,25 +11,25 @@ use std::hash::{Hash, Hasher};
use std::io::Seek;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Instant;
use structopt::StructOpt;
use pyo3::prelude::*;
use crate::common::{System, SystemSerde};
#[derive(Debug)]
pub struct SearchState {
pub mode: String,
pub system: String,
pub body: String,
pub depth: usize,
pub queue_size: usize,
pub d_rem: f64,
pub prc_done: f64,
pub d_rem: f32,
pub d_total: f32,
pub prc_done: f32,
pub n_seen: usize,
pub prc_seen: f64
pub prc_seen: f32
}
#[derive(Debug)]
pub struct RouteOpts {
pub range: Option<f32>,
pub file_path: PathBuf,
@ -37,10 +37,12 @@ pub struct RouteOpts {
pub precompute: bool,
pub permute: bool,
pub primary: bool,
pub full_permute: bool,
pub keep_first: bool,
pub keep_last: bool,
pub factor: Option<f32>,
pub mode: Mode,
pub systems: Vec<String>,
pub callback: Box<dyn Fn(&SearchState) -> PyResult<PyObject>>
}
#[derive(Debug)]
@ -50,10 +52,10 @@ pub enum Mode {
AStar,
}
impl FromStr for Mode {
type Err = String;
fn from_str(s: &str) -> Result<Mode, String> {
match s {
impl Mode {
pub fn parse(s: &str) -> Result<Mode, String> {
let s=s.to_lowercase();
match s.as_ref() {
"bfs" => Ok(Mode::BFS),
"greedy" => Ok(Mode::Greedy),
"astar" => Ok(Mode::AStar),
@ -138,8 +140,6 @@ struct LineCache {
impl LineCache {
pub fn new(path: &PathBuf) -> Option<Self> {
let idx_path = path.with_extension("idx");
let t_load = Instant::now();
println!("Loading Index from {}", idx_path.to_str().unwrap());
let cache =
bincode::deserialize_from(&mut BufReader::new(File::open(idx_path).ok()?)).ok()?;
let mut reader = BufReader::new(File::open(path).ok()?);
@ -149,7 +149,6 @@ impl LineCache {
cache,
header,
};
println!("Done in {}!", format_duration(t_load.elapsed()));
Some(ret)
}
fn read_record(reader: &mut BufReader<File>) -> Option<StringRecord> {
@ -176,19 +175,18 @@ pub struct Router {
range: f32,
primary_only: bool,
path: PathBuf,
callback: Box<Fn(SearchState) -> ()>,
callback: Box<dyn Fn(&SearchState) -> PyResult<PyObject>>
}
impl Router {
pub fn new(path: &PathBuf, range: f32, primary_only: bool,callback: Box<Fn(SearchState) -> ()>) -> Self {
pub fn new(path: &PathBuf, range: f32, primary_only: bool,callback: Box<dyn Fn(&SearchState) -> PyResult<PyObject>>) -> Result<Self,String> {
let mut scoopable = FnvHashSet::default();
let mut reader = csv::ReaderBuilder::new()
.from_path(path)
.unwrap_or_else(|e| {
println!("Error opening {}: {}", path.to_str().unwrap(), e);
std::process::exit(1);
});
let t_load = Instant::now();
let mut reader = match csv::ReaderBuilder::new().from_path(path) {
Ok(rdr) => rdr,
Err(e) => {
return Err(format!("Error opening {}: {}", path.to_str().unwrap(), e).to_string());
}
};
println!("Loading {}...", path.to_str().unwrap());
let systems: Vec<System> = reader
.deserialize::<SystemSerde>()
@ -214,7 +212,6 @@ impl Router {
sys.build()
})
.collect();
println!("Building RTree...");
let ret = Self {
tree: RTree::bulk_load(systems),
scoopable,
@ -225,18 +222,11 @@ impl Router {
path: path.clone(),
callback: callback,
};
println!(
"{} Systems loaded in {}",
ret.tree.size(),
format_duration(t_load.elapsed())
);
ret
Ok(ret)
}
pub fn from_file(filename: &PathBuf) -> (PathBuf, Self) {
let t_load = Instant::now();
pub fn from_file(filename: &PathBuf,callback: Box<dyn Fn(&SearchState) -> PyResult<PyObject>>) -> Result<(PathBuf, Self),String> {
let mut reader = BufReader::new(File::open(&filename).unwrap());
println!("Loading {}", filename.to_str().unwrap());
let (primary, range, file_hash, path, route_tree): (
bool,
f32,
@ -245,12 +235,11 @@ impl Router {
FnvHashMap<u32, u32>,
) = bincode::deserialize_from(&mut reader).unwrap();
let path = PathBuf::from(path);
println!("Done in {}!", format_duration(t_load.elapsed()));
if hash_file(&path) != file_hash {
panic!("File hash mismatch!")
return Err("File hash mismatch!".to_string());
}
let cache = LineCache::new(&path);
(
Ok((
path.clone(),
Self {
tree: RTree::default(),
@ -260,9 +249,9 @@ impl Router {
cache,
primary_only: primary,
path,
callback: Box::new(|s| {}),
callback
},
)
))
}
fn closest(&self, point: &[f32; 3]) -> &System {
@ -284,34 +273,34 @@ impl Router {
&self,
waypoints: &[String],
range: f32,
full: bool,
keep: (bool,bool),
mode: Mode,
factor: f32,
) -> Vec<System> {
) -> Result<Vec<System>,String> {
let mut best_score: f32 = std::f32::MAX;
let mut waypoints = waypoints.to_owned();
let mut best_permutation_waypoints = waypoints.to_owned();
let first = waypoints.first().cloned();
let last = waypoints.last().cloned();
let t_start = Instant::now();
println!("Finding best permutation of hops...");
while waypoints.prev_permutation() {}
loop {
let c_first = waypoints.first().cloned();
let c_last = waypoints.last().cloned();
if full || ((c_first == first) && (c_last == last)) {
let valid = (keep.0 && (c_first == first)) && (keep.1 && (c_last==last));
if valid {
let mut total_d = 0.0;
for pair in waypoints.windows(2) {
match pair {
[src, dst] => {
let (mut src, dst) =
(self.name_to_systems(&src), self.name_to_systems(&dst));
(self.name_to_systems(&src)?, self.name_to_systems(&dst)?);
src.sort_by_key(|&p| (p.mult * 10.0) as u8);
let src = src.last().unwrap();
let dst = dst.last().unwrap();
total_d += src.distp2(dst);
}
_ => panic!("Invalid routing parameters!"),
_ => return Err("Invalid routing parameters!".to_string()),
}
}
if total_d < best_score {
@ -323,8 +312,6 @@ impl Router {
break;
}
}
println!("Done in {}!", format_duration(t_start.elapsed()));
println!("Best permutation: {:?}", best_permutation_waypoints);
self.name_multiroute(&best_permutation_waypoints, range, mode, factor)
}
@ -335,10 +322,10 @@ impl Router {
range: f32,
mode: Mode,
factor: f32,
) -> Vec<System> {
) -> Result<Vec<System>,String> {
let mut coords = Vec::new();
for p_name in waypoints {
let mut p_l = self.name_to_systems(p_name);
let mut p_l = self.name_to_systems(p_name)?;
p_l.sort_by_key(|&p| (p.mult * 10.0) as u8);
let p = p_l.last().unwrap();
coords.push((p_name, p.pos));
@ -351,15 +338,15 @@ impl Router {
range: f32,
mode: Mode,
factor: f32,
) -> Vec<System> {
) -> Result<Vec<System>,String> {
let mut route: Vec<System> = Vec::new();
for pair in waypoints.windows(2) {
match *pair {
[src, dst] => {
let block = match mode {
Mode::BFS => self.route_bfs(&src, &dst, range),
Mode::Greedy => self.route_greedy(&src, &dst, range),
Mode::AStar => self.route_astar(&src, &dst, range, factor),
Mode::BFS => self.route_bfs(&src, &dst, range)?,
Mode::Greedy => self.route_greedy(&src, &dst, range)?,
Mode::AStar => self.route_astar(&src, &dst, range, factor)?,
};
if route.is_empty() {
for sys in block.iter() {
@ -371,20 +358,19 @@ impl Router {
}
}
}
_ => panic!("Invalid routing parameters!"),
_ => return Err("Invalid routing parameters!".to_string()),
}
}
route
Ok(route)
}
fn name_to_systems(&self, name: &str) -> Vec<&System> {
fn name_to_systems(&self, name: &str) -> Result<Vec<&System>,String> {
for sys in &self.tree {
if sys.system == name {
return self.neighbours(&sys, 0.0).collect();
return Ok(self.neighbours(&sys, 0.0).collect());
}
}
eprintln!("System not found: \"{}\"", name);
std::process::exit(1);
Err(format!("System not found: \"{}\"", name))
}
pub fn route_astar(
@ -393,29 +379,33 @@ impl Router {
dst: &(&String, [f32; 3]),
range: f32,
factor: f32,
) -> Vec<System> {
) -> Result<Vec<System>,String> {
if factor == 0.0 {
return self.route_bfs(src, dst, range);
}
println!("Running A-Star with greedy factor of {}", factor);
let (src_name, src) = src;
let (dst_name, dst) = dst;
let start_sys = self.closest(src);
let goal_sys = self.closest(dst);
{
let d = dist(src, dst);
println!("Plotting route from {} to {}...", src_name, dst_name);
println!(
"Jump Range: {} Ly, Distance: {} Ly, Theoretical Jumps: {}",
range,
d,
d / range
);
}
let d_total = dist(&start_sys.pos,&goal_sys.pos);
let mut d_rem=d_total;
let mut state=SearchState {
mode:"A-Star".into(),
depth:0,
queue_size:0,
d_rem: d_total,
d_total: d_total,
prc_done: 0.0,
n_seen:0,
prc_seen: 0.0,
body: start_sys.body.clone(),
system: start_sys.system.clone()
};
let total = self.tree.size() as f32;
let t_start = Instant::now();
let mut prev = FnvHashMap::default();
let mut seen = FnvHashSet::default();
let t_start = Instant::now();
let mut found = false;
let mut maxd = 0;
let mut queue: Vec<(usize, usize, &System)> = Vec::new();
@ -425,11 +415,24 @@ impl Router {
&start_sys,
));
seen.insert(start_sys.id);
while !(queue.is_empty() || found) {
while let Some((depth, _, sys)) = queue.pop() {
if depth > maxd {
maxd = depth;
if (depth+1) > maxd {
maxd = depth+1;
state.depth=depth;
state.queue_size=queue.len();
state.prc_done=((d_total-d_rem)*100f32) / d_total;
state.d_rem=d_rem;
state.n_seen=seen.len();
state.prc_seen=((seen.len()*100) as f32) / total;
state.body=sys.body.clone();
state.system=sys.system.clone();
match (self.callback)(&state) {
Ok(_) => (),
Err(e) => {
return Err(format!("{:?}",e).to_string());
}
};
print!(
"[{}] Depth: {}, Queue: {}, Seen: {} ({:.02}%) \r",
format_duration(t_start.elapsed()),
@ -451,6 +454,10 @@ impl Router {
.map(|nb| {
prev.insert(nb.id, sys);
let d_g = (nb.distp(goal_sys) / range) as usize;
let dist = dist(&nb.pos, &goal_sys.pos);
if dist < d_rem {
d_rem = dist;
}
(depth + 1, d_g, nb)
}),
);
@ -468,8 +475,7 @@ impl Router {
println!();
if !found {
eprintln!("No route from {} to {} found!", src_name, dst_name);
return Vec::new();
return Err(format!("No route from {} to {} found!", src_name, dst_name).to_string());
}
let mut v: Vec<System> = Vec::new();
let mut curr_sys = goal_sys;
@ -483,7 +489,7 @@ impl Router {
}
}
v.reverse();
v
Ok(v)
}
pub fn route_greedy(
@ -491,26 +497,28 @@ impl Router {
src: &(&String, [f32; 3]),
dst: &(&String, [f32; 3]),
range: f32,
) -> Vec<System> {
println!("Running Greedy-Search");
) -> Result<Vec<System>,String> {
let (src_name, src) = src;
let (dst_name, dst) = dst;
let start_sys = self.closest(src);
let goal_sys = self.closest(dst);
{
let d = dist(src, dst);
println!("Plotting route from {} to {}...", src_name, dst_name);
println!(
"Jump Range: {} Ly, Distance: {} Ly, Theoretical Jumps: {}",
range,
d,
d / range
);
}
let d_total = dist(&start_sys.pos, &goal_sys.pos);
let mut d_rem=d_total;
let mut state=SearchState {
mode:"Greedy".into(),
depth:0,
queue_size:0,
d_rem: d_total,
d_total: d_total,
prc_done: 0.0,
n_seen:0,
prc_seen: 0.0,
body:start_sys.body.clone(),
system:start_sys.system.clone(),
};
let total = self.tree.size() as f32;
let mut prev = FnvHashMap::default();
let mut seen = FnvHashSet::default();
let t_start = Instant::now();
let mut found = false;
let mut maxd = 0;
let mut queue: Vec<(f32, f32, usize, &System)> = Vec::new();
@ -518,17 +526,22 @@ impl Router {
seen.insert(start_sys.id);
while !(queue.is_empty() || found) {
while let Some((_, _, depth, sys)) = queue.pop() {
if depth > maxd {
maxd = depth;
print!(
"[{}] Depth: {}, Queue: {}, Seen: {} ({:.02}%) \r",
format_duration(t_start.elapsed()),
depth,
queue.len(),
seen.len(),
((seen.len() * 100) as f32) / total
);
std::io::stdout().flush().unwrap();
if (depth+1) > maxd {
state.depth=depth;
state.queue_size=queue.len();
state.prc_done=((d_total-d_rem)*100f32) / d_total;
state.d_rem=d_rem;
state.n_seen=seen.len();
state.prc_seen=((seen.len()*100) as f32) / total;
state.body=sys.body.clone();
state.system=sys.system.clone();
match (self.callback)(&state) {
Ok(_) => (),
Err(e) => {
return Err(format!("{:?}",e).to_string());
}
};
maxd = depth+1;
}
if sys.id == goal_sys.id {
found = true;
@ -540,6 +553,10 @@ impl Router {
.filter(|&nb| seen.insert(nb.id))
.map(|nb| {
prev.insert(nb.id, sys);
let dist = dist(&nb.pos, &goal_sys.pos);
if dist < d_rem {
d_rem = dist;
}
(-nb.mult, nb.distp2(goal_sys), depth + 1, nb)
}),
);
@ -547,11 +564,8 @@ impl Router {
queue.reverse();
}
}
println!();
println!();
if !found {
eprintln!("No route from {} to {} found!", src_name, dst_name);
return Vec::new();
return Err(format!("No route from {} to {} found!", src_name, dst_name).to_string());
}
let mut v: Vec<System> = Vec::new();
let mut curr_sys = goal_sys;
@ -565,18 +579,17 @@ impl Router {
}
}
v.reverse();
v
Ok(v)
}
pub fn precompute(&mut self, src: &str) {
let mut sys_l = self.name_to_systems(src);
pub fn precompute(&mut self, src: &str) -> Result<(),String> {
let mut sys_l = self.name_to_systems(src)?;
sys_l.sort_by_key(|&sys| (sys.mult * 10.0) as u8);
let sys = sys_l.last().unwrap();
println!("Precomputing routes starting at {} ...", sys.system);
let total = self.tree.size() as f32;
let t_start = Instant::now();
let mut prev = FnvHashMap::default();
let mut seen = FnvHashSet::default();
let t_start = Instant::now();
let mut depth = 0;
let mut queue: VecDeque<(usize, &System)> = VecDeque::new();
let mut queue_next: VecDeque<(usize, &System)> = VecDeque::new();
@ -613,9 +626,7 @@ impl Router {
self.range,
if self.primary_only { "_primary" } else { "" }
);
println!("\nSaving to {}", ofn);
let mut out_fh = BufWriter::new(File::create(&ofn).unwrap());
// (range, path, route_tree)
let data = (
self.primary_only,
self.range,
@ -623,11 +634,13 @@ impl Router {
String::from(self.path.to_str().unwrap()),
self.route_tree.as_ref().unwrap(),
);
bincode::serialize_into(&mut out_fh, &data).unwrap();
return match bincode::serialize_into(&mut out_fh, &data) {
Ok(_) => Ok(()),
Err(e) => Err(format!("Error: {}",e).to_string())
};
}
fn get_systems_by_ids(&mut self, path: &PathBuf, ids: &[u32]) -> FnvHashMap<u32, System> {
println!("Processing {}", path.to_str().unwrap());
fn get_systems_by_ids(&mut self, path: &PathBuf, ids: &[u32]) -> Result<FnvHashMap<u32, System>,String> {
let mut ret = FnvHashMap::default();
if let Some(c) = &mut self.cache.as_mut() {
let mut missing = false;
@ -637,22 +650,22 @@ impl Router {
ret.insert(*id, sys);
}
None => {
println!("ID {} not found in cache", id);
missing = true;
break;
}
}
}
if !missing {
return ret;
return Ok(ret);
}
}
let mut reader = csv::ReaderBuilder::new()
.from_path(path)
.unwrap_or_else(|e| {
println!("Error opening {}: {}", path.to_str().unwrap(), e);
std::process::exit(1);
});
let mut reader = match csv::ReaderBuilder::new()
.from_path(path) {
Ok(reader) => reader,
Err(e) => {
return Err(format!("Error opening {}: {}", path.to_str().unwrap(), e));
}
};
reader
.deserialize::<SystemSerde>()
.map(|res| res.unwrap())
@ -660,15 +673,12 @@ impl Router {
.map(|sys| {
ret.insert(sys.id, sys.build());
})
.last()
.unwrap_or_else(|| {
eprintln!("Error: No systems matching {:?} found!", ids);
std::process::exit(1);
});
ret
.count();
Ok(ret)
}
fn get_system_by_name(path: &PathBuf, name: &str) -> System {
fn get_system_by_name(path: &PathBuf, name: &str) -> Result<System,String> {
// TODO: Fuzzy search (https://github.com/andylokandy/simsearch-rs)
let mut reader = csv::ReaderBuilder::new()
.from_path(path)
.unwrap_or_else(|e| {
@ -678,20 +688,18 @@ impl Router {
let sys = reader
.deserialize::<SystemSerde>()
.map(|res| res.unwrap())
.find(|sys| sys.system == name)
.unwrap_or_else(|| {
eprintln!("Error: System '{}' not found!", name);
std::process::exit(1);
});
sys.build()
.find(|sys| sys.system == name);
match sys {
Some(system) => Ok(system.build()),
None => Err(format!("System {} not found!",name).to_string())
}
}
pub fn route_to(&mut self, dst: &str, systems_path: &PathBuf) -> Vec<System> {
pub fn route_to(&mut self, dst: &str, systems_path: &PathBuf) -> Result<Vec<System>,String> {
let prev = self.route_tree.as_ref().unwrap();
let dst = Self::get_system_by_name(&systems_path, dst);
let dst = Self::get_system_by_name(&systems_path, dst)?;
if !prev.contains_key(&dst.id) {
eprintln!("System-ID {} not found", dst.id);
std::process::exit(1);
return Err(format!("System-ID {} not found", dst.id).to_string());
};
let mut v_ids: Vec<u32> = Vec::new();
let mut v: Vec<System> = Vec::new();
@ -706,12 +714,17 @@ impl Router {
}
}
v_ids.reverse();
let id_map = self.get_systems_by_ids(&systems_path, &v_ids);
let id_map = self.get_systems_by_ids(&systems_path, &v_ids)?;
for sys_id in v_ids {
let sys = id_map.get(&sys_id).unwrap();
let sys = match id_map.get(&sys_id) {
Some(sys) => sys,
None => {
return Err(format!("System-ID {} not found!",sys_id));
}
};
v.push(sys.clone())
}
v
Ok(v)
}
pub fn route_bfs(
@ -719,13 +732,25 @@ impl Router {
src: &(&String, [f32; 3]),
dst: &(&String, [f32; 3]),
range: f32,
) -> Vec<System> {
) -> Result<Vec<System>,String> {
println!("Running BFS");
let (src_name, src) = src;
let (dst_name, dst) = dst;
let start_sys = self.closest(src);
let goal_sys = self.closest(dst);
let d_total = dist(src, dst);
let d_total = dist(&start_sys.pos, &goal_sys.pos);
let mut state=SearchState {
mode:"BFS".into(),
depth:0,
queue_size:0,
d_rem: d_total,
d_total: d_total,
prc_done: 0.0,
n_seen:0,
prc_seen: 0.0,
system: start_sys.system.clone(),
body: start_sys.body.clone()
};
{
println!("Plotting route from {} to {}...", src_name, dst_name);
println!(
@ -738,26 +763,31 @@ impl Router {
let total = self.tree.size() as f32;
let mut prev = FnvHashMap::default();
let mut seen = FnvHashSet::default();
let t_start = Instant::now();
let mut depth = 0;
let mut found = false;
let mut queue: VecDeque<(usize, &System)> = VecDeque::new();
let mut queue_next: VecDeque<(usize, &System)> = VecDeque::new();
let mut d_rem = dist2(&start_sys.pos, &goal_sys.pos);
let mut d_rem = dist(&start_sys.pos, &goal_sys.pos);
queue.push_front((0, &start_sys));
seen.insert(start_sys.id);
while !(queue.is_empty() || found) {
print!(
"[{}] {:.02}% | Depth: {}, Queue: {}, Seen: {} ({:.02}%), Remaining Distance: {} \r",
format_duration(t_start.elapsed()),
(((d_total-d_rem.sqrt())*100f32)/d_total),
depth,
queue.len(),
seen.len(),
((seen.len() * 100) as f32) / total,
d_rem.sqrt()
);
std::io::stdout().flush().unwrap();
state.depth=depth;
state.queue_size=queue.len();
state.prc_done=((d_total-d_rem)*100f32) / d_total;
state.d_rem=d_rem;
state.n_seen=seen.len();
state.prc_seen=((seen.len()*100) as f32) / total;
{
let s=queue.get(0).unwrap().1;
state.system=s.system.clone();
state.body=s.body.clone();
}
match (self.callback)(&state) {
Ok(_) => (),
Err(e) => {
return Err(format!("{:?}",e).to_string());
}
};
while let Some((d, sys)) = queue.pop_front() {
if sys.id == goal_sys.id {
found = true;
@ -769,7 +799,7 @@ impl Router {
.filter(|&nb| seen.insert(nb.id))
.map(|nb| {
prev.insert(nb.id, sys);
let dist = dist2(&nb.pos, &goal_sys.pos);
let dist = dist(&nb.pos, &goal_sys.pos);
if dist < d_rem {
d_rem = dist;
}
@ -783,8 +813,7 @@ impl Router {
println!();
println!();
if !found {
eprintln!("No route from {} to {} found!", src_name, dst_name);
return Vec::new();
return Err(format!("No route from {} to {} found!", src_name, dst_name).to_string());
}
let mut v: Vec<System> = Vec::new();
let mut curr_sys = goal_sys;
@ -798,82 +827,57 @@ impl Router {
}
}
v.reverse();
v
Ok(v)
}
}
pub fn route(opts: RouteOpts) -> std::io::Result<()> {
pub fn route(opts: RouteOpts) -> Result<Option<Vec<System>>,String> {
if opts.systems.is_empty() {
if opts.precomp_file.is_some() {
eprintln!("Error: Please specify exatly one system");
return if opts.precomp_file.is_some() {
Err("Error: Please specify exatly one system".to_string())
} else if opts.precompute {
eprintln!("Error: Please specify at least one system");
Err("Error: Please specify at least one system".to_string())
} else {
eprintln!("Error: Please specify at least two systems");
Err("Error: Please specify at least two systems".to_string())
}
std::process::exit(1);
}
let mut path = opts.file_path;
let mut router: Router = if opts.precomp_file.is_some() {
let ret = Router::from_file(&opts.precomp_file.clone().unwrap());
path = ret.0;
ret.1
let (path_,ret) = Router::from_file(&opts.precomp_file.clone().unwrap(),Box::new(opts.callback))?;
path=path_;
ret
} else if opts.range.is_some() {
Router::new(&path, opts.range.unwrap(), opts.primary, Box::new(opts.callback))?
} else {
Router::new(&path, opts.range.unwrap(), opts.primary, Box::new(|state| {
println!("State: {:?}",state);
}))
return Err("Please specify a jump range!".to_string());
};
if opts.precompute {
for sys in opts.systems {
router.route_tree = None;
router.precompute(&sys);
router.precompute(&sys)?;
}
std::process::exit(0);
return Ok(None)
}
let t_route = Instant::now();
let route = if router.route_tree.is_some() {
router.route_to(opts.systems.first().unwrap(), &path)
} else if opts.permute || opts.full_permute {
router.route_to(opts.systems.first().unwrap(), &path)?
} else if opts.permute {
router.best_name_multiroute(
&opts.systems,
opts.range.unwrap(),
opts.full_permute,
(opts.keep_first,opts.keep_last),
opts.mode,
opts.factor.unwrap_or(1.0),
)
)?
} else {
router.name_multiroute(
&opts.systems,
opts.range.unwrap(),
opts.mode,
opts.factor.unwrap_or(1.0),
)
)?
};
println!("Route computed in {}\n", format_duration(t_route.elapsed()));
if route.is_empty() {
eprintln!("No route found!");
return Ok(());
return Err("No route found!".to_string())
}
let mut total: f32 = 0.0;
for (sys1, sys2) in route.iter().zip(route.iter().skip(1)) {
let dist = sys1.distp(sys2);
total += dist;
println!(
"{} ({}) [{}] ({},{},{}) [{} Ls]: {:.2} Ly",
sys1.body,
sys1.system,
sys1.star_type,
sys1.pos[0],
sys1.pos[1],
sys1.pos[2],
sys1.distance,
dist
);
}
let sys = route.iter().last().unwrap();
println!(
"{} ({}) [{}] ({},{},{}) [{} Ls]: {:.2} Ly",
sys.body, sys.system, sys.star_type, sys.pos[0], sys.pos[1], sys.pos[2], sys.distance, 0.0
);
println!("Total: {:.2} Ly ({} Jumps)", total, route.len());
Ok(())
return Ok(Some(route));
}

View file

@ -18,6 +18,7 @@ setup(
],
packages=["ed_lrr_gui"],
entry_points={"console_scripts": ["ed_lrr_gui=ed_lrr_gui.__main__:main"]},
install_requires=["PyQt5", "appdirs", "PyYAML"],
install_requires=["PyQt5", "appdirs", "PyYAML", "requests", "python-dateutil"],
include_package_data=True,
zip_safe=False,
)

1
test_gui.bat Normal file
View file

@ -0,0 +1 @@
python build_gui.py && ed_lrr_gui