Adding a /trade function to help calculate most profitable trade routes

This commit is contained in:
Luke Robles 2023-06-13 10:08:25 -07:00
parent 23fe549ef3
commit d639b1f767
3 changed files with 102 additions and 12 deletions

View File

@ -33,11 +33,31 @@ class StarCitizen(commands.Cog):
description="Ship you want info on, must be the exact name of the ship, eg Aegs Avenger",
required=True,
)
async def star_citizen(self, ctx: commands.Context, ship):
async def get_ship(self, ctx: commands.Context, ship):
await ctx.defer()
embed = await star_citizen.get_ship(ship_name=ship)
await ctx.send_followup(embed=embed)
@commands.slash_command(
guild_ids=None,
name="trade",
description="Calculates the most profitable route for a given commodity",
)
@option(
name="commodity",
description="The commodity you want to look up",
required=True,
)
@option(
name="scu",
description="Optinal how much SCU to fill",
required=False,
)
async def calculate_trade_profies(self, ctx: commands.Context, commodity, scu):
await ctx.defer()
embed = await star_citizen.calculate_trade_profies(commodity=commodity, scu=scu)
await ctx.send_followup(embed=embed)
@tasks.loop(seconds=5)
async def poll_for_patch_notes(self):
# Wait until the bot is ready before we actually start executing code

View File

@ -1,18 +1,17 @@
from bs4 import BeautifulSoup
import requests
import discord
import os
wiki_url = "https://starcitizen.tools/"
async def get_ship(ship_name):
base_url = "https://robertspaceindustries.com"
wiki_url = "https://starcitizen.tools/"
try:
wiki_ship_name = "_".join(elem for elem in ship_name.split())
# print(wiki_ship_name)
# print("%s%s" % (wiki_url, wiki_ship_name))
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/112.0"
}
@ -23,10 +22,7 @@ async def get_ship(ship_name):
description="-------", color=discord.Color.blue(), type="rich"
)
ship_image = (
(soup.find("a", {"class": "mw-file-description"})).img["src"]
# .replace("400px", "1920px")
)
ship_image = (soup.find("a", {"class": "mw-file-description"})).img["src"]
ship_name_on_page = soup.find("span", {"class": "mw-page-title-main"}).text
try:
@ -113,8 +109,6 @@ async def get_ship(ship_name):
inline=True,
)
# embed.add_field(name="-------", value="", inline=False)
# embed.add_field(name="**Description**", value=ship["description"], inline=False)
embed.add_field(name="-------", value="", inline=False)
try:
cargo_capacity = (
@ -202,3 +196,79 @@ async def get_ship(ship_name):
return embed
return embed
async def calculate_trade_profies(commodity, scu=None):
url = "https://portal.uexcorp.space/api/all_prices/pretty_mode/1/"
headers = {"api_key": os.getenv("uexcorp_key")}
scu_string = "%s scu" % scu
if not scu:
scu = 696
scu_string = "a C2 Hercules"
commodity = commodity.capitalize()
embed = discord.Embed(
description="-------", color=discord.Color.blue(), type="rich"
)
try:
response = requests.get(url, headers=headers).json()["data"]
embed.set_author(
name=commodity,
url="%s%s" % (wiki_url, commodity),
)
costs = {}
for systems, stations in response.items():
for station, trades in stations.items():
if "buy" in trades and commodity in trades["buy"]:
cost_to_fill = trades["buy"][commodity] * int(scu)
costs[station] = cost_to_fill
embed.add_field(
name="**Cheapest place to fill %s**" % scu_string,
value="%s: $%s"
% (
min(costs, key=costs.get),
"{:20,.2f}".format(costs[min(costs, key=costs.get)]).strip(),
),
inline=True,
)
profits = {}
for systems, stations in response.items():
for station, trades in stations.items():
if "sell" in trades and commodity in trades["sell"]:
net_profit_in_a_c2 = trades["sell"][commodity] * int(scu)
profits[station] = net_profit_in_a_c2
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,
)
net_gain = float(profits[max(profits, key=profits.get)]) - float(
costs[min(costs, key=costs.get)]
)
embed.add_field(
name="**Net Gain**",
value="$%s" % "{:20,.2f}".format(net_gain).strip(),
inline=True,
)
return embed
except ValueError:
print(commodity)
embed.set_author(
name="❌ Couldnt find that commodity",
url="%s%s" % (wiki_url, commodity),
)
return embed

View File

@ -87,6 +87,6 @@ secrets:
gitlab_token: gitlab_token
OPENAI_API_KEY: OPENAI_API_KEY
token: discord_token
tracker_network_token: tracker_network_token
twitch_token: twitch_token
uexcorp_key: uexcorp_key
wolfram_token: wolfram_token