Skip to content

Instantly share code, notes, and snippets.

@cowboy
Last active August 16, 2024 11:30
Show Gist options
  • Save cowboy/52e066fe615f538a275793280fcc5a84 to your computer and use it in GitHub Desktop.
Save cowboy/52e066fe615f538a275793280fcc5a84 to your computer and use it in GitHub Desktop.
Elite: Dangerous, Trade Dangerous wrapper script
#!/usr/bin/env bash
userprofile_dir=$(wslpath "$(wslvar USERPROFILE)")
journal_dir="$userprofile_dir/Saved Games/Frontier Developments/Elite Dangerous"
status_file="$journal_dir/Status.json"
journal_credits=$(cat "$status_file" | jq -r .Balance)
[[ "$journal_credits" == null ]] && journal_credits=999999999
# journal_system_id=$(cat "$status_file" | jq -r .Destination.System)
# journal_system_name=$(curl -s https://spansh.co.uk/api/system/$journal_system_id | jq -r .record.name)
market_file="$journal_dir/Market.json"
journal_system_name=$(cat "$market_file" | jq -r .StarSystem)
journal_market_name=$(cat "$market_file" | jq -r .StationName)
# journal_market_name=$(curl -s https://spansh.co.uk/api/station/$journal_market_id | jq -r .record.name)
# echo "<journal_credits:$journal_credits>"
# echo "<journal_system_name:$journal_system_name>"
# echo "<journal_market_name:$journal_market_name>"
# exit
mode=${1:-local}
destination=$2
if [[ "$mode" == local || "$mode" == l ]]; then
current_location="$journal_system_name/$journal_market_name"
global_route_search=
elif [[ "$mode" == global || "$mode" == g ]]; then
current_location="$journal_system_name/$journal_market_name"
global_route_search=1
elif [[ "$mode" ]]; then
current_location="$mode"
global_route_search=
fi
# echo "<current_location:$current_location>"
# echo "<global_route_search:$global_route_search>"
# exit
capacity=272
insurance=4m
full_ly=16.38
empty_ly=24.36
data_age=1
num_routes=8
if [[ "$TMP" ]]; then
tmp="$TMP"
else
tmp=$(mktemp)
fi
trade_run_opts=(
-vv
--show-jumps
--no-planet
--fleet-carrier N
--ls-max 3000
--max-gain-per-ton 200k
--ly-per $full_ly
--empty-ly $empty_ly
--capacity $capacity
--credits $journal_credits
--insurance $insurance
--age $data_age
)
if [[ "$global_route_search" ]]; then
clear
trade_run_opts=("${trade_run_opts[@]}" --routes $num_routes)
else
trade_run_opts=("${trade_run_opts[@]}" --routes 1 --from "$current_location")
fi
if [[ "$destination" ]]; then
trade_run_opts=("${trade_run_opts[@]}" --to "$destination")
else
trade_run_opts=("${trade_run_opts[@]}" --loop)
fi
echo "Detected current location: $current_location"
LC_ALL=en_US.UTF-8 printf "Detected credits: %'.f\n" $journal_credits
echo "Trade run options:" "${trade_run_opts[@]}"
if [[ ! "$TMP" ]]; then
trade run "${trade_run_opts[@]}" > "$tmp"
code=$?
echo "Wrote raw trade run output to $tmp"
if [[ "$code" != 0 ]]; then
cat "$tmp"
exit $code
fi
fi
mapfile -t lines < "$tmp"
last_system=
function last_system_details() {
if [[ "$last_system" ]]; then
avg=$(($run_total_profit/$run_total_quantity))
LC_ALL=en_US.UTF-8 printf "[ LOOP ]$(printf %${pad}s |tr " " "=")[ AVG PROFIT/TON %'9.f cr ]========[ TOTAL PROFIT %'12.f cr ]\n" $avg $run_total_profit
echo
if [[ "$global_route_search" && ! "$TMP" ]]; then
trade nav --ly-per $empty_ly -v "$current_location" "$last_system"
echo
fi
fi
}
for line in "${lines[@]}"; do
line=$(echo "$line" | sed -E 's#^(Finish)# \1#')
system=$(echo "$line" | sed -nE 's#^(\w[^/]+)/.*#\1#p')
if [[ "$system" ]]; then
last_system_details
if [[ "$last_system" ]]; then
echo; echo; echo
fi
last_system="$system"
echo
echo "##### $system #####"
run_total_profit=0
run_total_quantity=0
else
if echo "$line" | grep "cr vs" > /dev/null; then
pad=$(($(echo "$line" | sed 's#cr vs .*##' | awk '{print length}')-18))
parts=($line)
quantity=${parts[0]}
for i in "${!parts[@]}"; do
num=$(echo ${parts[i]} | sed -nE 's#([0-9,]+)cr,?$#\1#p')
if [[ "$num" ]]; then
parts[i]=$(echo $num | tr -d ,)
else
unset parts[i]
fi
done
parts=("${parts[@]}")
buy=${parts[0]}
sell=${parts[1]}
profit=$(($sell-$buy))
profit_acc=$(($profit*$quantity))
profit_total=$(($profit_total+$profit_acc))
quantity_total=$(($quantity_total+$quantity))
buy_orders[$profit]="$(LC_ALL=en_US.UTF-8 printf "$(echo "$line" | sed -E "s#[0-9, ]{8}cr vs.*, #%'9.f cr - %'9.f cr = %'9.f cr x %4d = %'12.f cr (#"))\n" $sell $buy $profit $quantity $profit_acc)"
continue
fi
if echo "$line" | grep "^ Jump" > /dev/null; then
for i in "${!buy_orders[@]}"; do
echo "${buy_orders[i]}"
done | tac
run_total_profit=$(($run_total_profit+$profit_total))
run_total_quantity=$(($run_total_quantity+$quantity_total))
avg=$(($profit_total/$quantity_total))
LC_ALL=en_US.UTF-8 printf "\n[ JUMP ]$(printf %${pad}s |tr " " "-")[ AVG PROFIT/TON %'9.f cr ]--------[ TOTAL PROFIT %'12.f cr ]\n" $avg $profit_total
fi
if echo "$line" | grep -E "^ (Unload|Finish|---)" > /dev/null; then
continue
fi
echo "$line" | sed -E '
s#^ ## # remove leading spaces
s#=>.*## # remove totals from "Unload" line
s#\((.*)ls, [^)]*\)#(\1 ls)# # remove stuff in parens after ls
s# *\(score:[^)]*\)## # remove score in parens
s#, ([0-9.]+)ly -># (\1 ly)-> #g # rewrite jump ly amounts
s#^(Jump|Load)#\n\1# # add newlines for readability
'
if echo "$line" | grep "^ Load" > /dev/null; then
profit_total=0
quantity_total=0
buy_orders=()
fi
fi
done
last_system_details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment