generated from Ctri-The-Third/PythonTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Ctri-The-Third/nodeV
Add SDK changes and simple "trade best in system" behaviour for commanders
- Loading branch information
Showing
8 changed files
with
191 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# takes a tradegood. Then trades that to ensure that the market is LIMITED | ||
# if the export activity hits RESTRICTED, it switches to finding profitable import goods until that clears. | ||
|
||
# script is happy to work to 0 profit, but will not work at a loss. | ||
|
||
|
||
import time | ||
import sys | ||
|
||
|
||
sys.path.append(".") | ||
from behaviours.generic_behaviour import Behaviour | ||
import logging | ||
from straders_sdk.ship import Ship | ||
from straders_sdk.models import Market, Waypoint | ||
from straders_sdk.utils import waypoint_slicer, set_logging, try_execute_select | ||
from straders_sdk.constants import SUPPLY_LEVELS | ||
from behaviours.generic_behaviour import Behaviour | ||
import random | ||
|
||
BEHAVIOUR_NAME = "TRADE_BEST_INTRASOLAR" | ||
SAFETY_PADDING = 180 | ||
|
||
|
||
class TradeBestInSystem(Behaviour): | ||
def __init__( | ||
self, | ||
agent_name, | ||
ship_name, | ||
behaviour_params: dict = ..., | ||
config_file_name="user.json", | ||
session=None, | ||
) -> None: | ||
super().__init__( | ||
agent_name, | ||
ship_name, | ||
behaviour_params, | ||
config_file_name, | ||
session, | ||
) | ||
self.agent = self.st.view_my_self() | ||
self.logger = logging.getLogger(BEHAVIOUR_NAME) | ||
|
||
def run(self): | ||
self.ship = self.st.ships_view_one(self.ship_name) | ||
self.sleep_until_ready() | ||
self.st.logging_client.log_beginning( | ||
BEHAVIOUR_NAME, | ||
self.ship.name, | ||
self.agent.credits, | ||
behaviour_params=self.behaviour_params, | ||
) | ||
self._run() | ||
self.end() | ||
|
||
def _run(self): | ||
st = self.st | ||
ship = self.ship # = st.ships_view_one(self.ship_name, True) | ||
ship: Ship | ||
agent = self.agent | ||
self.sleep_until_arrived() | ||
best_trade = self.get_best_trade() | ||
if not best_trade: | ||
self.logger.info("No trade found") | ||
self.st.sleep(SAFETY_PADDING) | ||
return | ||
|
||
( | ||
trade_symbol, | ||
system_symbol, | ||
export_market, | ||
market_depth, | ||
sell_price, | ||
import_market, | ||
) = best_trade | ||
|
||
export_market = st.waypoints_view_one(export_market) | ||
import_market = st.waypoints_view_one(import_market) | ||
if trade_symbol not in [c.symbol for c in ship.cargo_inventory]: | ||
have_cargo = self.go_and_buy( | ||
trade_symbol, export_market, burn_allowed=True, max_to_buy=market_depth | ||
) | ||
else: | ||
have_cargo = True | ||
|
||
if have_cargo: | ||
self.go_and_sell_or_fulfill(trade_symbol, import_market, burn_allowed=True) | ||
else: | ||
self.logger.info("No cargo to sell") | ||
self.st.sleep(SAFETY_PADDING) | ||
# your code goes here | ||
|
||
def get_best_trade(self): | ||
sql = """SELECT trade_symbol, system_symbol, export_market, market_depth, sell_price, import_market | ||
FROM public.trade_routes_intrasystem | ||
where system_symbol = %s;""" | ||
results = try_execute_select( | ||
sql, (self.ship.nav.system_symbol,), self.st.connection | ||
) | ||
if not results: | ||
return None | ||
best_trade = results[0] | ||
return ( | ||
best_trade[0], | ||
best_trade[1], | ||
best_trade[2], | ||
best_trade[3], | ||
best_trade[4], | ||
best_trade[5], | ||
) | ||
|
||
|
||
# | ||
# to execute from commandline, run the script with the agent and ship_symbol as arguments, or edit the values below | ||
# | ||
if __name__ == "__main__": | ||
from dispatcherWK16 import lock_ship | ||
|
||
set_logging(level=logging.DEBUG) | ||
agent = sys.argv[1] if len(sys.argv) > 2 else "CTRI-U-" | ||
ship_number = sys.argv[2] if len(sys.argv) > 2 else "1" | ||
ship = f"{agent}-{ship_number}" | ||
behaviour_params = { | ||
"priority": 3, | ||
} | ||
|
||
bhvr = TradeBestInSystem(agent, ship, behaviour_params or {}) | ||
|
||
lock_ship(ship, "MANUAL", 60 * 24) | ||
|
||
bhvr.run() | ||
lock_ship(ship, "MANUAL", 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# week 28 | ||
|
||
Database instability continues to be our woe - and we're now back to "idle in transaction" issues causing the database to be locked. | ||
|
||
The first transaction to lock itself was one on node U - ship CTRI--4 | ||
`select total_seconds, expiration from ship_cooldown where ship_symbol = 'CTRI-U--4'` | ||
|
||
Intestingly, the thread appears to have moved beyond the database (perhaps because it got a bad response from a timeout) and is now stuck waiting for the request consumer, which has jammed. Attempts to pause it and inspect are failing, which is fascinating. | ||
|
||
## ramp up improvements - "Trade best" | ||
|
||
Chain trading is pretty good for efficiency, but during the ramp up we really want one ship trading the best possible trade - the commander. | ||
|
||
I've created a simple "trade best" behaviour that will look at the best trade available and execute it. It can be optimised by factoring in the distance to the starting location. The commander is set to use this behaviour when it's trading. | ||
|
||
: | ||
|
||
## Command and control | ||
|
||
We're at the point where we want to be able to make instructions via the UI. | ||
|
||
* Must haves | ||
* Log task for ship | ||
* Set behaviour for ship | ||
* direct move, buy, sell instructions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule spacetraders_sdk
updated
3 files
+3 −1 | PostgresConfig.SQL | |
+5 −5 | straders_sdk/pg_pieces/upsert_shipyard.py | |
+1 −1 | straders_sdk/request_consumer.py |