Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import sqlite3
from sqlite3 import Error

from log import log


class Database:
def __init__(self, db_file):
self.db_file = db_file
self.conn = None
self.setup()

def setup(self):
create_systems_table_sql = """CREATE TABLE IF NOT EXISTS systems (
cmdr TEXT,
origin TEXT,
destination TEXT,
travelled FLOAT
);"""
log("Starting systems database setup...")
conn = self.__connect_database()
if conn is not None:
self.conn = conn
self.__create_table(create_systems_table_sql)
log("Systems database setup complete")

def update(self, cmdr, origin, destination):
log("Updating systems database...")
if self.conn is not None:
systems_table = self.__select_systems_records()
records_amount = len(systems_table)
records_checked = 0
for record in systems_table:
if cmdr in record:
self.__update_systems_record((origin, destination, cmdr))
else:
records_checked += 1
if records_checked >= records_amount:
self.__insert_systems_record((cmdr, origin, destination))
log("Systems database updated")

def get_systems(self, cmdr):
log("Retrieving CMDR systems from database...")
success = False
origin = ""
destination = ""
if self.conn is not None:
systems_table = self.__select_systems_records()
records_amount = len(systems_table)
records_checked = 0
for record in systems_table:
if cmdr in record:
log("Retrieving systems...")
success = True
origin = record[1]
destination = record[2]
log("Systems retrieval complete")
else:
records_checked += 1
if records_checked >= records_amount:
log("Failed to retrieve systems, using values stored in registry instead")
success = False
return success, origin, destination

def __connect_database(self):
log("Attempting to connect to systems database...")
conn = None
try:
conn = sqlite3.connect(self.db_file)
log("Database connected")
except Error as e:
log(e)
return conn

def __create_table(self, sql):
log("Creating systems table...")
try:
c = self.conn.cursor()
c.execute(sql)
log("Systems table created")
except Error as e:
print(e)

def __insert_systems_record(self, data):
sql = """INSERT INTO systems(cmdr,origin,destination)
VALUES(?,?,?)"""
log("Inserting new systems record...")
cur = self.conn.cursor()
cur.execute(sql, data)
self.conn.commit()
return cur.lastrowid

def __update_systems_record(self, data):
sql = """UPDATE systems
SET origin = ? ,
destination = ?
WHERE cmdr = ?"""
log("Updating existing systems record...")
cur = self.conn.cursor()
cur.execute(sql, data)
self.conn.commit()
return cur.lastrowid

def __select_systems_records(self):
log("Getting systems table")
cur = self.conn.cursor()
cur.execute("SELECT * FROM systems")
rows = cur.fetchall()
return rows
4 changes: 3 additions & 1 deletion edsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def get_coords_from_edsm(system_name):
system_name_url = quote(system_name)
edsm_url = "https://www.edsm.net/api-v1/system?systemName={SYSTEM}&showCoordinates=1".format(SYSTEM=system_name_url)
success = False
name = "MissingNo"
x = 0
y = 0
z = 0
Expand All @@ -22,11 +23,12 @@ def get_coords_from_edsm(system_name):
edsm_json = json.loads(response)
if "name" and "coords" in edsm_json:
success = True
name = edsm_json["name"]
x = edsm_json["coords"]["x"]
y = edsm_json["coords"]["y"]
z = edsm_json["coords"]["z"]
else:
pass
except IOError:
pass
return success, x, y, z
return success, name, x, y, z
38 changes: 25 additions & 13 deletions load.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# EDMC plugin for Exploration Progress
import sys
import os

import myNotebook as nb
from ttkHyperlinkLabel import HyperlinkLabel
Expand All @@ -9,6 +10,7 @@
from system import System
from log import log
import calculate
from database import Database

try:
# Python 2
Expand All @@ -26,16 +28,20 @@
current = System()

version = "1.0.2-indev"
database_file = "systems.db"

def plugin_start():

def plugin_start(plugin_dir):
# Load plugin into EDMC
database_file_path = os.path.join(plugin_dir, database_file)
this.systems_database = Database(database_file_path)
log("Exploration Progress has been loaded")
return "Exploration Progress"


def plugin_start3(plugin_dir):
# Python 3 mode
return plugin_start()
return plugin_start(plugin_dir)


def plugin_stop():
Expand All @@ -45,9 +51,13 @@ def plugin_stop():

def plugin_prefs(parent, cmdr, is_beta):
# Plugin settings GUI in EDMC Settings dialog
success, origin_name, destination_name = this.systems_database.get_systems(cmdr)
if not success:
origin_name = config.get("ExProg_OriginSystem")
destination_name = config.get("ExProg_DestinationSystem")
frame = nb.Frame(parent)
this.origin_system = tk.StringVar(value=config.get("ExProg_OriginSystem"))
this.destination_system = tk.StringVar(value=config.get("ExProg_DestinationSystem"))
this.origin_system = tk.StringVar(value=origin_name)
this.destination_system = tk.StringVar(value=destination_name)
nb.Label(frame, text="Origin System").grid()
nb.Entry(frame, textvariable=this.origin_system).grid()
nb.Label(frame, text="Destination System").grid()
Expand All @@ -62,7 +72,8 @@ def prefs_changed(cmdr, is_beta):
# Save settings
config.set("ExProg_OriginSystem", this.origin_system.get())
config.set("ExProg_DestinationSystem", this.destination_system.get())
update_systems()
this.systems_database.update(cmdr, this.origin_system.get(), this.destination_system.get())
update_systems(cmdr, ui_update=True)


def plugin_app(parent):
Expand All @@ -76,7 +87,6 @@ def plugin_app(parent):
this.bar.grid()
this.status = tk.Label(this.frame, text="")
this.status.grid()
update_systems()
return this.frame


Expand All @@ -89,21 +99,22 @@ def journal_entry(cmdr, is_beta, system, station, entry, state):
coords = tuple(entry["StarPos"])
current.setCoords(coords[0], coords[1], coords[2])
log("Updated current system")
update_status()
update_progress()
update_systems(cmdr, ui_update=True)
if current.getName() == destination.getName():
log("Destination reached")
# Reached destination
config.set("ExProg_OriginSystem", "")
config.set("ExProg_DestinationSystem", "")
update_systems(ui_update=False)
update_systems(cmdr, ui_update=False)
update_status("destination_reached")


def update_systems(ui_update=False):
def update_systems(cmdr, ui_update=False):
log("Updating origin and destination systems...")
origin_name = config.get("ExProg_OriginSystem")
destination_name = config.get("ExProg_DestinationSystem")
success, origin_name, destination_name = this.systems_database.get_systems(cmdr)
if not success:
origin_name = config.get("ExProg_OriginSystem")
destination_name = config.get("ExProg_DestinationSystem")
log("Origin system: " + str(origin_name) + ", Destination system: " + str(destination_name))
origin.setName(name=origin_name, verify=True, populate=True)
destination.setName(name=destination_name, verify=True, populate=True)
Expand Down Expand Up @@ -180,7 +191,8 @@ def update_status(external_message=None):
status_colour = "yellow"
else:
log("All systems go!")
status_message = ""
status_message = "Origin: " + origin.getName() + "\n" \
"Destination: " + destination.getName()
status_colour = "green"

this.status["text"] = status_message
Expand Down
8 changes: 5 additions & 3 deletions system.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ def setCoords(self, x, y, z):

# Function to populate coordinates from EDSM
def populateCoords(self):
success, x, y, z = get_coords_from_edsm(self.name)
success, name, x, y, z = get_coords_from_edsm(self.name)
if success:
self.setCoords(x, y, z)

# Function to verify system existence on EDSM
def verifySystem(self):
success = get_coords_from_edsm(self.name)
return success[0]
success, name, x, y, z = get_coords_from_edsm(self.name)
if success:
self.setName(name=name, verify=False, populate=False)
return success