2019-09-03 14:56:30 +02:00
|
|
|
|
#!/usr/bin/env python3
|
2019-09-03 21:00:51 +02:00
|
|
|
|
import argparse
|
2019-09-03 14:56:30 +02:00
|
|
|
|
import math
|
|
|
|
|
import requests
|
|
|
|
|
import sys
|
|
|
|
|
import tkinter as tk
|
|
|
|
|
|
2019-11-24 22:58:16 +01:00
|
|
|
|
from pyEDSM.edsm.exception import CommanderNotFoundError, ServerError, SystemNotFoundError
|
|
|
|
|
from pyEDSM.edsm.models import Commander, System
|
2019-09-18 13:36:25 +02:00
|
|
|
|
|
2019-09-18 13:29:26 +02:00
|
|
|
|
# =================================================================================
|
2019-09-03 14:56:30 +02:00
|
|
|
|
|
|
|
|
|
def distance (coords1, coords2):
|
2019-09-18 13:29:26 +02:00
|
|
|
|
return math.sqrt( (coords1['x']-coords2['x'])**2
|
2019-09-03 21:00:51 +02:00
|
|
|
|
+ (coords1['y']-coords2['y'])**2
|
|
|
|
|
+ (coords1['z']-coords2['z'])**2 )
|
2019-09-03 14:56:30 +02:00
|
|
|
|
|
|
|
|
|
def getDistances (system, cmdrs):
|
2019-09-03 21:00:51 +02:00
|
|
|
|
distances = {}
|
|
|
|
|
for cmdr in cmdrs:
|
2019-11-24 22:58:16 +01:00
|
|
|
|
distances[cmdr] = round(distance(cmdr.currentPosition, system.coords))
|
2019-09-03 21:00:51 +02:00
|
|
|
|
return distances
|
2019-09-03 14:56:30 +02:00
|
|
|
|
|
2019-09-03 21:00:51 +02:00
|
|
|
|
# =================================================================================
|
2019-09-03 14:56:30 +02:00
|
|
|
|
|
2019-09-03 21:00:51 +02:00
|
|
|
|
def outputGui():
|
|
|
|
|
def runsearch(event=None):
|
|
|
|
|
for child in frame.winfo_children():
|
|
|
|
|
child.grid_remove()
|
|
|
|
|
child.destroy()
|
2019-09-03 14:56:30 +02:00
|
|
|
|
try:
|
2019-11-24 22:58:16 +01:00
|
|
|
|
system = System(systemField.get())
|
2019-10-15 19:38:12 +02:00
|
|
|
|
distances = getDistances(system, cmdrs)
|
2019-09-03 21:00:51 +02:00
|
|
|
|
nearestCmdr = min(distances,key=distances.get)
|
|
|
|
|
lbl = tk.Label(
|
2019-11-24 22:58:16 +01:00
|
|
|
|
frame, text='nearest CMDR: {} ({} ly from {})'.format(nearestCmdr.name,
|
|
|
|
|
distances[nearestCmdr], system.name))
|
2019-09-03 21:00:51 +02:00
|
|
|
|
lbl.grid(row=0, columnspan=2)
|
|
|
|
|
row = 1
|
|
|
|
|
for cmdr in distances:
|
|
|
|
|
row += 1
|
2019-11-24 22:58:16 +01:00
|
|
|
|
lbl = tk.Label(frame, text='{}:'.format(cmdr.name))
|
2019-09-03 21:00:51 +02:00
|
|
|
|
lbl.grid(row=row, column=0)
|
|
|
|
|
lbl = tk.Label(frame, text='{} ly'.format(distances[cmdr]))
|
|
|
|
|
lbl.grid(row=row, column=1)
|
2019-10-14 17:27:24 +02:00
|
|
|
|
except (ServerError, SystemNotFoundError) as e:
|
2019-09-03 21:00:51 +02:00
|
|
|
|
lbl = tk.Label(frame, text=e)
|
|
|
|
|
lbl.grid(row=0, columnspan=2)
|
2019-09-18 13:40:35 +02:00
|
|
|
|
except EdsmApiException as e:
|
|
|
|
|
lbl = tk.Label(frame, text=e)
|
|
|
|
|
lbl.grid(row=0, columnspan=2)
|
2019-09-03 21:00:51 +02:00
|
|
|
|
window = tk.Tk()
|
|
|
|
|
window.title('EDSM nearest CMDR')
|
|
|
|
|
lbl = tk.Label(window, text='system:')
|
|
|
|
|
lbl.grid(row=0, column=0)
|
|
|
|
|
systemField = tk.Entry(window, width=50)
|
|
|
|
|
systemField.grid(row=0, column=1)
|
2019-11-24 22:58:16 +01:00
|
|
|
|
systemField.insert(tk.END, system.name)
|
2019-09-03 21:00:51 +02:00
|
|
|
|
systemField.focus()
|
|
|
|
|
frame = tk.Frame(window)
|
|
|
|
|
frame.grid(row=1, columnspan=3)
|
|
|
|
|
btn = tk.Button(window, text='get distances', command=runsearch)
|
|
|
|
|
btn.grid(row=0, column=2)
|
|
|
|
|
window.bind('<Return>', runsearch)
|
|
|
|
|
runsearch()
|
|
|
|
|
window.mainloop()
|
|
|
|
|
|
|
|
|
|
# =================================================================================
|
|
|
|
|
|
|
|
|
|
def outputText():
|
|
|
|
|
try:
|
|
|
|
|
distances = getDistances(system, cmdrs)
|
2019-11-24 22:58:16 +01:00
|
|
|
|
except CommanderNotFoundError as e:
|
|
|
|
|
print(e)
|
|
|
|
|
sys.exit(1)
|
2019-10-14 10:14:23 +02:00
|
|
|
|
except ServerError as e:
|
2019-09-03 21:00:51 +02:00
|
|
|
|
print(e)
|
2019-10-14 17:27:24 +02:00
|
|
|
|
sys.exit(1)
|
2019-10-14 10:14:23 +02:00
|
|
|
|
except SystemNotFoundError as e:
|
|
|
|
|
print(e)
|
2019-10-14 17:27:24 +02:00
|
|
|
|
sys.exit(2)
|
2019-09-18 13:40:35 +02:00
|
|
|
|
except EdsmApiException as e:
|
|
|
|
|
print(e)
|
2019-10-14 17:27:24 +02:00
|
|
|
|
sys.exit(1)
|
2019-09-03 21:00:51 +02:00
|
|
|
|
nearestCmdr = min(distances,key=distances.get)
|
|
|
|
|
if shortOutput:
|
2019-11-24 22:58:16 +01:00
|
|
|
|
print('nearest commander: {} ({} ly).'.format(nearestCmdr.name,
|
2019-09-03 21:00:51 +02:00
|
|
|
|
distances[nearestCmdr]))
|
|
|
|
|
else:
|
2019-11-24 22:58:16 +01:00
|
|
|
|
print('nearest CMDR: {} ({} ly from {}).'.format(nearestCmdr.name,
|
|
|
|
|
distances[nearestCmdr], system.name))
|
2019-09-03 14:56:30 +02:00
|
|
|
|
print()
|
|
|
|
|
for cmdr in distances:
|
2019-11-24 22:58:16 +01:00
|
|
|
|
print('{}: {} ly'.format(cmdr.name, distances[cmdr]))
|
2019-10-14 17:27:24 +02:00
|
|
|
|
sys.exit(0)
|
2019-09-03 21:00:51 +02:00
|
|
|
|
|
|
|
|
|
# =================================================================================
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Locate your CMDRs using EDSM and '
|
|
|
|
|
+ 'find their distance to a given system.')
|
|
|
|
|
parser.add_argument('cmdrs', metavar='CMDR', nargs='+', help='a list of CMDR names '
|
|
|
|
|
+ '(must have their location public on EDSM!)')
|
|
|
|
|
parser.add_argument('--system', nargs=1, help='the target system (must be in '
|
|
|
|
|
+ 'EDDN!)', required=True)
|
|
|
|
|
parser.add_argument('--short', action='store_true', help='short output (only '
|
|
|
|
|
+ 'makes sense with `--text`)')
|
|
|
|
|
group = parser.add_mutually_exclusive_group()
|
|
|
|
|
group.add_argument('--gui', action='store_true', help='explicitly run the GUI')
|
|
|
|
|
group.add_argument('--text', action='store_true', help='explicitly give text output')
|
|
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
2019-11-24 22:58:16 +01:00
|
|
|
|
system = System(args.system[0].strip().replace(' ', '').replace('', ''))
|
|
|
|
|
cmdrs = []
|
|
|
|
|
for name in args.cmdrs:
|
|
|
|
|
cmdrs += [Commander(name)]
|
2019-09-03 21:00:51 +02:00
|
|
|
|
shortOutput = args.short
|
2019-09-03 14:56:30 +02:00
|
|
|
|
|
2019-09-03 21:00:51 +02:00
|
|
|
|
# =================================================================================
|
2019-09-03 14:56:30 +02:00
|
|
|
|
|
2019-09-03 21:00:51 +02:00
|
|
|
|
if args.text:
|
|
|
|
|
outputText()
|
|
|
|
|
elif args.gui:
|
|
|
|
|
outputGui()
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
outputGui()
|
|
|
|
|
except tk.TclError:
|
|
|
|
|
outputText()
|