refactoring using Commander model
Also refactored everything to use the actual models instead of passing strings around. Functionally the same, but … nicer, I guess.
This commit is contained in:
parent
24ce59786c
commit
487168c6fb
1 changed files with 19 additions and 30 deletions
|
@ -5,36 +5,20 @@ import requests
|
||||||
import sys
|
import sys
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
|
|
||||||
from pyEDSM.edsm.exception import ServerError, SystemNotFoundError
|
from pyEDSM.edsm.exception import CommanderNotFoundError, ServerError, SystemNotFoundError
|
||||||
from pyEDSM.edsm.models import System, Commander
|
from pyEDSM.edsm.models import Commander, System
|
||||||
|
|
||||||
class EdsmApiException(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
# =================================================================================
|
# =================================================================================
|
||||||
|
|
||||||
def getCmdrCoords (cmdr):
|
|
||||||
resp = requests.get('https://www.edsm.net/api-logs-v1/get-position?commanderName={}&showCoordinates=1'.format(cmdr))
|
|
||||||
if resp.status_code != 200:
|
|
||||||
raise EdsmApiException('GET /get-position/ {}'.format(resp.status_code))
|
|
||||||
try:
|
|
||||||
ret = resp.json()['coordinates']
|
|
||||||
except KeyError:
|
|
||||||
raise EdsmApiException('Coordinates for CMDR {} not found!'.format(cmdr))
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def distance (coords1, coords2):
|
def distance (coords1, coords2):
|
||||||
return math.sqrt( (coords1['x']-coords2['x'])**2
|
return math.sqrt( (coords1['x']-coords2['x'])**2
|
||||||
+ (coords1['y']-coords2['y'])**2
|
+ (coords1['y']-coords2['y'])**2
|
||||||
+ (coords1['z']-coords2['z'])**2 )
|
+ (coords1['z']-coords2['z'])**2 )
|
||||||
|
|
||||||
def getDistances (system, cmdrs):
|
def getDistances (system, cmdrs):
|
||||||
systemcoords = System(system).coords
|
|
||||||
distances = {}
|
distances = {}
|
||||||
for cmdr in cmdrs:
|
for cmdr in cmdrs:
|
||||||
cmdrcoords = getCmdrCoords(cmdr)
|
distances[cmdr] = round(distance(cmdr.currentPosition, system.coords))
|
||||||
distances[cmdr] = round(distance(cmdrcoords, systemcoords))
|
|
||||||
return distances
|
return distances
|
||||||
|
|
||||||
# =================================================================================
|
# =================================================================================
|
||||||
|
@ -45,17 +29,17 @@ def outputGui():
|
||||||
child.grid_remove()
|
child.grid_remove()
|
||||||
child.destroy()
|
child.destroy()
|
||||||
try:
|
try:
|
||||||
system = systemField.get()
|
system = System(systemField.get())
|
||||||
distances = getDistances(system, cmdrs)
|
distances = getDistances(system, cmdrs)
|
||||||
nearestCmdr = min(distances,key=distances.get)
|
nearestCmdr = min(distances,key=distances.get)
|
||||||
lbl = tk.Label(
|
lbl = tk.Label(
|
||||||
frame, text='nearest CMDR: {} ({} ly from {})'.format(nearestCmdr,
|
frame, text='nearest CMDR: {} ({} ly from {})'.format(nearestCmdr.name,
|
||||||
distances[nearestCmdr], system))
|
distances[nearestCmdr], system.name))
|
||||||
lbl.grid(row=0, columnspan=2)
|
lbl.grid(row=0, columnspan=2)
|
||||||
row = 1
|
row = 1
|
||||||
for cmdr in distances:
|
for cmdr in distances:
|
||||||
row += 1
|
row += 1
|
||||||
lbl = tk.Label(frame, text='{}:'.format(cmdr))
|
lbl = tk.Label(frame, text='{}:'.format(cmdr.name))
|
||||||
lbl.grid(row=row, column=0)
|
lbl.grid(row=row, column=0)
|
||||||
lbl = tk.Label(frame, text='{} ly'.format(distances[cmdr]))
|
lbl = tk.Label(frame, text='{} ly'.format(distances[cmdr]))
|
||||||
lbl.grid(row=row, column=1)
|
lbl.grid(row=row, column=1)
|
||||||
|
@ -71,7 +55,7 @@ def outputGui():
|
||||||
lbl.grid(row=0, column=0)
|
lbl.grid(row=0, column=0)
|
||||||
systemField = tk.Entry(window, width=50)
|
systemField = tk.Entry(window, width=50)
|
||||||
systemField.grid(row=0, column=1)
|
systemField.grid(row=0, column=1)
|
||||||
systemField.insert(tk.END, system)
|
systemField.insert(tk.END, system.name)
|
||||||
systemField.focus()
|
systemField.focus()
|
||||||
frame = tk.Frame(window)
|
frame = tk.Frame(window)
|
||||||
frame.grid(row=1, columnspan=3)
|
frame.grid(row=1, columnspan=3)
|
||||||
|
@ -86,6 +70,9 @@ def outputGui():
|
||||||
def outputText():
|
def outputText():
|
||||||
try:
|
try:
|
||||||
distances = getDistances(system, cmdrs)
|
distances = getDistances(system, cmdrs)
|
||||||
|
except CommanderNotFoundError as e:
|
||||||
|
print(e)
|
||||||
|
sys.exit(1)
|
||||||
except ServerError as e:
|
except ServerError as e:
|
||||||
print(e)
|
print(e)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
@ -97,14 +84,14 @@ def outputText():
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
nearestCmdr = min(distances,key=distances.get)
|
nearestCmdr = min(distances,key=distances.get)
|
||||||
if shortOutput:
|
if shortOutput:
|
||||||
print('nearest commander: {} ({} ly).'.format(nearestCmdr,
|
print('nearest commander: {} ({} ly).'.format(nearestCmdr.name,
|
||||||
distances[nearestCmdr]))
|
distances[nearestCmdr]))
|
||||||
else:
|
else:
|
||||||
print('nearest CMDR: {} ({} ly from {}).'.format(nearestCmdr,
|
print('nearest CMDR: {} ({} ly from {}).'.format(nearestCmdr.name,
|
||||||
distances[nearestCmdr], system))
|
distances[nearestCmdr], system.name))
|
||||||
print()
|
print()
|
||||||
for cmdr in distances:
|
for cmdr in distances:
|
||||||
print('{}: {} ly'.format(cmdr, distances[cmdr]))
|
print('{}: {} ly'.format(cmdr.name, distances[cmdr]))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# =================================================================================
|
# =================================================================================
|
||||||
|
@ -123,8 +110,10 @@ group.add_argument('--text', action='store_true', help='explicitly give text out
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
system = args.system[0].strip().replace(' ', '').replace('', '')
|
system = System(args.system[0].strip().replace(' ', '').replace('', ''))
|
||||||
cmdrs = args.cmdrs
|
cmdrs = []
|
||||||
|
for name in args.cmdrs:
|
||||||
|
cmdrs += [Commander(name)]
|
||||||
shortOutput = args.short
|
shortOutput = args.short
|
||||||
|
|
||||||
# =================================================================================
|
# =================================================================================
|
||||||
|
|
Loading…
Reference in a new issue