def market_file_handler(self, filepath, spend_limit): ''' This function takes in a market.json file and updates the players current location and the cargo values. ''' # Get the json into a dict while 1: try: with open(filepath, 'r') as f: js = json.load(f) star_system = js['StarSystem'] station_name = js['StationName'] break except: # Try again until the file actually exists pass # Create a DBHandler instance to get the location of the star system db_conn = DBHandler() out = db_conn.request("SELECT x, y, z FROM systems WHERE name = ?", (star_system,)) #print('Market db request returned {}'.format(out)) if self.location != list(out[0]): self.location = list(out[0]) self.dump_info() print('[Market Check] New location, rebuilding distance table. . .') ldb.build_distances(self.location) print('[Market Check] Done.') # Setup the database thread to run in the background future = pool.submit(ldb.planets_in_range, self.ship_range, self.location, self.cargo_size, station_name, spend_limit) # Set up the call back for the future. future.add_done_callback(con.next_location_to_buy) print('[Market Check] Looking for goods to trade. . .')
def cargo_file_handler(self, filepath, spend_limit, last_cargo): ''' This function takes in a filepath, spending limit, and a cargo and determines if the limit needs to increase or the cargo needs to be updated. ''' # For now, cut this out. We're not using it return spend_limit, last_cargo # From the cargo file, determine what will be sold and next_cargo = self.grab_current_cargo() # First case: No cargo -> Cargo # spend_limit remains the same, last_cargo updates. print('[Cargo Check] Last cargo is {}'.format(last_cargo)) print('[Cargo Check] Next cargo is {}'.format(next_cargo)) # If they are the same, nothing has changed. Return. if next_cargo == last_cargo: return spend_limit, last_cargo if last_cargo == None or last_cargo == []: return spend_limit, next_cargo # Second case: Cargo -> No Cargo # spend_limit will increase depending on what the value of the cargo # was at the location that it gets sold, last_cargo becomes next_cargo. if next_cargo == None or next_cargo == []: # Grab the station name that we are at db_conn = DBHandler() station_name = None while 1: try: print('[Cargo Check] Opening cargo.json. . .') with open(filepath, 'r') as f: js = json.load(f) station_name = js['StationName'] break except: # Try again until the file actually exists continue print('[Cargo Check] Station Name is {}'.format(station_name)) # Get the station ID station_id = db_conn.request("SELECT id FROM stations WHERE name = ?", (station_name,)) # Get the commodity id for the current cargo. cargo_ids = [] for item in last_cargo: cargo_ids.append(db_conn.request("SELECT id FROM commodities WHERE name = ?", (item,))) # Grab the sell value at for the cargo id's at the station id for item in cargo_ids: profit = db_conn.request( "SELECT sell_price FROM listings WHERE station_id = ? AND commodity_id = ?", (station_id, item,) ) spend_limit += profit # Return! return spend_limit, next_cargo