Fix edge case of a commodity not actually being sold anywhere, but it still being able to be bought (Bexalite)

This commit is contained in:
Luke Robles 2023-06-15 12:10:18 -07:00
parent 56e4b3e45b
commit 0d982a94ad

View File

@ -3,6 +3,11 @@ import requests
import discord
import os
# prints for debug
import pprint
pp = pprint.PrettyPrinter(indent=4)
wiki_url = "https://starcitizen.tools/"
@ -206,7 +211,7 @@ async def calculate_trade_profies(commodity, scu=None):
scu_string = "%s scu" % scu
if not scu:
scu = 696
scu_string = "a C2 Hercules"
scu_string = "a C2 Hercules full"
commodity = commodity.capitalize()
@ -215,13 +220,20 @@ async def calculate_trade_profies(commodity, scu=None):
)
try:
response = requests.get(url, headers=headers).json()["data"]
except Exception:
print(commodity)
embed.set_author(
name=commodity,
name="❌ Couldnt find that commodity",
url="%s%s" % (wiki_url, commodity),
)
# Loop through every system/outpost and find the cheapest place selling the commodity
embed.set_author(
name=commodity,
url="%s%s" % (wiki_url, commodity),
)
# Loop through every system/outpost and find the cheapest place selling the commodity
try:
costs = {}
for systems, stations in response.items():
for station, trades in stations.items():
@ -230,7 +242,7 @@ async def calculate_trade_profies(commodity, scu=None):
break
embed.add_field(
name="**Cheapest place to fill %s**" % scu_string,
name="**Cheapest place to fill %s of %s**" % (scu_string, commodity),
value="%s: $%s"
% (
min(costs, key=costs.get),
@ -238,40 +250,35 @@ async def calculate_trade_profies(commodity, scu=None):
),
inline=True,
)
except Exception:
pass
# Same logic, but finding the place buying the commodity for the most
profits = {}
for systems, stations in response.items():
for station, trades in stations.items():
if "sell" in trades and commodity in trades["sell"]:
profits[station] = trades["sell"][commodity] * int(scu)
break
# Same logic, but finding the place buying the commodity for the most
profits = {}
for systems, stations in response.items():
for station, trades in stations.items():
if "sell" in trades and commodity in trades["sell"]:
profits[station] = trades["sell"][commodity] * int(scu)
break
embed.add_field(
name="Most profitable place to sell %s" % scu_string,
value="%s: $%s"
% (
max(profits, key=profits.get),
"{:20,.2f}".format(profits[max(profits, key=profits.get)]).strip(),
),
inline=True,
)
embed.add_field(
name="Most profitable place to sell %s of " % (scu_string, commodity),
value="%s: $%s"
% (
max(profits, key=profits.get),
"{:20,.2f}".format(profits[max(profits, key=profits.get)]).strip(),
),
inline=True,
)
if len(costs) and len(profits):
net_profit = float(profits[max(profits, key=profits.get)]) - float(
costs[min(costs, key=costs.get)]
)
embed.add_field(
name="**Net Profit**",
value="$%s" % "{:20,.2f}".format(net_profit).strip(),
inline=True,
)
return embed
except Exception:
print(commodity)
embed.set_author(
name="❌ Couldnt find that commodity",
url="%s%s" % (wiki_url, commodity),
)
return embed
return embed