Skip to content

Commit

Permalink
improve markets and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Ctri-The-Third committed Mar 2, 2024
1 parent 7b06b96 commit 3d8f903
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 40 deletions.
1 change: 1 addition & 0 deletions PostgresInit.SQL
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
refresh materialized view ship_mounts;
43 changes: 10 additions & 33 deletions PostgresSchema.SQL
Original file line number Diff line number Diff line change
Expand Up @@ -916,41 +916,17 @@ CREATE TABLE public.market_tradegoods (
trade_symbol text NOT NULL,
type text,
name text,
description text,
market_symbol text,
trade_symbol text,
"type " text
description text
);


ALTER TABLE public.market_tradegood OWNER TO spacetraders;
ALTER TABLE public.market_tradegoods OWNER TO spacetraders;

--
-- TOC entry 244 (class 1259 OID 16531)
-- Name: market_tradegoods; Type: VIEW; Schema: public; Owner: spacetraders
--

CREATE VIEW public.market_tradegoods AS
WITH buy_or_sell_vs(buy_or_sell, type) AS (
VALUES ('sell'::text,'EXPORT'::text), ('buy'::text,'IMPORT'::text), ('exchange'::text,'EXCHANGE'::text)
), market_tradegoods AS (
SELECT mt.market_waypoint AS market_symbol,
COALESCE(mt.trade_symbol, mt.symbol) AS trade_symbol,
COALESCE(bs.type, bs.type) AS type,
mt.name,
mt.description
FROM (public.market_tradegood mt
JOIN buy_or_sell_vs bs ON ((mt.buy_or_sell = bs.buy_or_sell)))
)
SELECT market_symbol,
trade_symbol,
type,
name,
description
FROM market_tradegoods;


ALTER VIEW public.market_tradegoods OWNER TO spacetraders;

--
-- TOC entry 245 (class 1259 OID 16536)
Expand Down Expand Up @@ -1658,10 +1634,10 @@ CREATE VIEW public.mkt_shpyrds_systems_to_visit_first AS
SELECT DISTINCT w.system_symbol,
mtl.last_updated,
(mtl.last_updated IS NOT NULL) AS visited
FROM ((public.market_tradegood mt
LEFT JOIN public.market_tradegood_listings mtl ON (((mt.market_waypoint = mtl.market_symbol) AND (mt.symbol = mtl.trade_symbol))))
JOIN public.waypoints w ON ((mt.market_waypoint = w.waypoint_symbol)))
WHERE (((mt.symbol = ANY (ARRAY['IRON'::text, 'IRON_ORE'::text, 'COPPER'::text, 'COPPER_ORE'::text, 'ALUMINUM'::text, 'ALUMINUM_ORE'::text, 'SILVER'::text, 'SILVER_ORE'::text, 'GOLD'::text, 'GOLD_ORE'::text, 'PLATINUM'::text, 'PLATINUM_ORE'::text, 'URANITE'::text, 'URANITE_ORE'::text, 'MERITIUM'::text, 'MERITIUM_ORE'::text])) AND (mtl.last_updated IS NULL)) OR (mtl.last_updated <= (timezone('utc'::text, now()) - '1 day'::interval)))
FROM ((public.market_tradegoods mt
LEFT JOIN public.market_tradegood_listings mtl ON (((mt.market_symbol = mtl.market_symbol) AND (mt.trade_symbol = mtl.trade_symbol))))
JOIN public.waypoints w ON ((mt.market_symbol = w.waypoint_symbol)))
WHERE (((mt.trade_symbol = ANY (ARRAY['IRON'::text, 'IRON_ORE'::text, 'COPPER'::text, 'COPPER_ORE'::text, 'ALUMINUM'::text, 'ALUMINUM_ORE'::text, 'SILVER'::text, 'SILVER_ORE'::text, 'GOLD'::text, 'GOLD_ORE'::text, 'PLATINUM'::text, 'PLATINUM_ORE'::text, 'URANITE'::text, 'URANITE_ORE'::text, 'MERITIUM'::text, 'MERITIUM_ORE'::text])) AND (mtl.last_updated IS NULL)) OR (mtl.last_updated <= (timezone('utc'::text, now()) - '1 day'::interval)))
ORDER BY (mtl.last_updated IS NOT NULL), mtl.last_updated
)
SELECT us.system_symbol
Expand Down Expand Up @@ -1718,7 +1694,7 @@ CREATE VIEW public.mkt_shpyrds_waypoints_scanned AS
JOIN public.waypoints w2 ON (((w1.system_symbol = w2.system_symbol) AND (w1.waypoint_symbol <> w2.waypoint_symbol))))
JOIN public.jump_gates jg ON ((jg.waypoint_symbol = w2.waypoint_symbol)))
LEFT JOIN public.shipyard_types st ON ((st.shipyard_symbol = w1.waypoint_symbol)))
LEFT JOIN public.market_tradegood mt ON ((mt.market_waypoint = w1.waypoint_symbol)))
LEFT JOIN public.market_tradegoods mt ON ((mt.market_symbol = w1.waypoint_symbol)))
WHERE (wt.trait_symbol = ANY (ARRAY['MARKETPLACE'::text, 'SHIPYARD'::text]))
GROUP BY wt.waypoint_symbol, wt.trait_symbol
)
Expand Down Expand Up @@ -2796,8 +2772,8 @@ ALTER TABLE ONLY public.market
-- Name: market_tradegood market_tradegood_pkey; Type: CONSTRAINT; Schema: public; Owner: spacetraders
--

ALTER TABLE ONLY public.market_tradegood
ADD CONSTRAINT market_tradegood_pkey PRIMARY KEY (market_waypoint, symbol);
ALTER TABLE ONLY public.market_tradegoods
ADD CONSTRAINT market_tradegood_pkey PRIMARY KEY (market_symbol, trade_symbol);


--
Expand Down Expand Up @@ -3044,3 +3020,4 @@ GRANT ALL ON SCHEMA public TO PUBLIC;
-- PostgreSQL database dump complete
--


6 changes: 4 additions & 2 deletions postgres.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ ENV ST_DB_NAME = spacetraders
run mkdir -p /docker-entrypoint-initdb.d


copy ./PostgresConfig.SQL /docker-entrypoint-initdb.d/postgresconfig.sql
copy ./PostgresConfig.SQL /docker-entrypoint-initdb.d/a_postgresconfig.sql
# copy ./postgresql.conf /var/lib/postgresql/data/postgresql.conf
copy ./PostgresSchema.SQL /docker-entrypoint-initdb.d/postgresschema.sql
copy ./PostgresSchema.SQL /docker-entrypoint-initdb.d/b_postgresschema.sql

copy ./PostgresInit.SQL /docker-entrypoint-initdb.d/c_postgresinit.sql
#cp /usr/share/postgresql/postgresql.conf /var/lib/postgresql/data/postgresql.conf

# run pg_restore -U spacetraders -d spacetraders /docker-entrypoint-initdb.d/postgresschema.sql
Expand Down
1 change: 1 addition & 0 deletions straders_sdk/client_mediator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,7 @@ def ship_purchase_cargo(
if resp:
self.update(resp.data)
ship.update(resp.data)
self.update(ship)
return resp

def ship_survey(self, ship: "Ship") -> list[Survey] or SpaceTradersResponse:
Expand Down
3 changes: 1 addition & 2 deletions straders_sdk/client_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,6 @@ def system_market(self, wp: Waypoint) -> Market or SpaceTradersResponse:
"""/game/systems/{symbol}/marketplace"""
try:
sql = """SELECT mt.trade_symbol, mt.name, mt.description, mt.type FROM market_tradegoods mt where mt.market_symbol = %s"""

rows = try_execute_select(sql, (wp.symbol,), self.connection)
if not rows:
return LocalSpaceTradersRespose(
Expand All @@ -613,7 +612,7 @@ def system_market(self, wp: Waypoint) -> Market or SpaceTradersResponse:
where market_symbol = %s"""
rows = try_execute_select(listings_sql, (wp.symbol,), self.connection)
listings = [MarketTradeGoodListing(*row) for row in rows]
return Market(wp.symbol, imports, exports, exchanges, listings)
return Market(wp.symbol, exports, imports, exchanges, listings)
except Exception as err:
return LocalSpaceTradersRespose(
f"Could not find market data for that waypoint - {err}", 0, 0, sql
Expand Down
6 changes: 3 additions & 3 deletions straders_sdk/pg_pieces/upsert_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ def _upsert_market(market: Market, connection):
resp = try_execute_upsert(sql, (market.symbol, system_symbol), connection)
if not resp:
return resp
sql = """INSERT INTO public.market_tradegood(
market_waypoint, symbol, buy_or_sell, name, description)
sql = """INSERT INTO public.market_tradegoods(
market_symbol, trade_symbol, type, name, description)
VALUES (%s, %s, %s, %s, %s)
ON CONFLICT (market_waypoint, symbol) DO NOTHING"""
ON CONFLICT (market_symbol, trade_symbol) DO NOTHING"""
if not resp:
return resp
for trade_good in market.exports:
Expand Down

0 comments on commit 3d8f903

Please sign in to comment.