diff options
author | Julien Dessaux | 2024-04-05 00:42:30 +0200 |
---|---|---|
committer | Julien Dessaux | 2024-04-07 23:01:52 +0200 |
commit | 234770b611df32178382b557df396db220070a7f (patch) | |
tree | 8c768846716eaf892ff75abe84d0f5c00e8519ff /nodejs/database | |
parent | [node] Fixed basic procurement trading loop (diff) | |
download | spacetraders-234770b611df32178382b557df396db220070a7f.tar.gz spacetraders-234770b611df32178382b557df396db220070a7f.tar.bz2 spacetraders-234770b611df32178382b557df396db220070a7f.zip |
[node] Big Ships lib refactoring
Diffstat (limited to '')
-rw-r--r-- | nodejs/database/agents.ts | 2 | ||||
-rw-r--r-- | nodejs/database/contracts.ts | 2 | ||||
-rw-r--r-- | nodejs/database/markets.ts | 2 | ||||
-rw-r--r-- | nodejs/database/ships.ts | 57 | ||||
-rw-r--r-- | nodejs/database/systems.ts | 2 |
5 files changed, 4 insertions, 61 deletions
diff --git a/nodejs/database/agents.ts b/nodejs/database/agents.ts index 3fc4182..5221dc7 100644 --- a/nodejs/database/agents.ts +++ b/nodejs/database/agents.ts @@ -1,4 +1,4 @@ -import { Agent } from '../model/agent.ts'; +import { Agent } from '../lib/types.ts'; import { DbData, db } from './db.ts'; const addAgentStatement = db.prepare(`INSERT INTO agents(data) VALUES (json(?));`); diff --git a/nodejs/database/contracts.ts b/nodejs/database/contracts.ts index 64d6c65..9adb4c8 100644 --- a/nodejs/database/contracts.ts +++ b/nodejs/database/contracts.ts @@ -1,4 +1,4 @@ -import { Contract } from '../model/contract.ts'; +import { Contract } from '../lib/types.ts'; import { DbData, db } from './db.ts'; const addContractStatement = db.prepare(`INSERT INTO contracts(data) VALUES (json(?));`); diff --git a/nodejs/database/markets.ts b/nodejs/database/markets.ts index 558d181..6e8865a 100644 --- a/nodejs/database/markets.ts +++ b/nodejs/database/markets.ts @@ -1,5 +1,5 @@ import { DbData, db } from './db.ts'; -import { Market } from '../model/market.ts'; +import { Market } from '../lib/types.ts'; import { systemFromWaypoint } from '../lib/utils.ts'; const addMarketStatement = db.prepare(`INSERT INTO markets(system, data) VALUES (?, json(?));`); diff --git a/nodejs/database/ships.ts b/nodejs/database/ships.ts deleted file mode 100644 index c827db8..0000000 --- a/nodejs/database/ships.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { DbData, db } from './db.ts'; -import { Cargo } from '../model/cargo.ts'; -import { Fuel, Nav, Ship } from '../model/ship.ts'; - -const addShipStatement = db.prepare(`INSERT INTO ships(data) VALUES (json(?));`); -const getShipStatement = db.prepare(`SELECT data FROM ships WHERE data->>'symbol' = ?;`); -const getShipsAtStatement = db.prepare(`SELECT data FROM ships WHERE data->>'$.nav.systemSymbol' = ?;`); -const getShipsStatement = db.prepare(`SELECT data FROM ships;`); -const setShipCargoStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.cargo', json(:cargo)) FROM ships WHERE data->>'symbol' = :symbol) WHERE data->>'symbol' = :symbol;`); -const setShipFuelStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.fuel', json(:fuel)) FROM ships WHERE data->>'symbol' = :symbol) WHERE data->>'symbol' = :symbol;`); -const setShipNavStatement = db.prepare(`UPDATE ships SET data = (SELECT json_set(data, '$.nav', json(:nav)) FROM ships WHERE data->>'symbol' = :symbol) WHERE data->>'symbol' = :symbol;`); -const updateShipStatement = db.prepare(`UPDATE ships SET data = json(:data) WHERE data->>'symbol' = :symbol;`); - -export function getShip(symbol: string): Ship { - const data = getShipStatement.get(symbol) as DbData|undefined; - if (!data) throw `invalid symbol ${symbol} in getShip database call`; - return JSON.parse(data.data); -} - -export function getShips(): Array<Ship> { - const data = getShipsStatement.all() as Array<DbData>; - return data.map(elt => JSON.parse(elt.data)); -} - -export function getShipsAt(symbol: string): Array<Ship> { - const data = getShipsAtStatement.all(symbol) as Array<DbData>; - return data.map(elt => JSON.parse(elt.data)); -} - -export function setShip(data: Ship): void { - const changes = updateShipStatement.run({ - data: JSON.stringify(data), - symbol: data.symbol, - }).changes; - if (changes === 0) addShipStatement.run(JSON.stringify(data)); -} - -export function setShipCargo(symbol: string, cargo: Cargo): void { - setShipCargoStatement.run({ - cargo: JSON.stringify(cargo), - symbol: symbol, - }); -} - -export function setShipFuel(symbol: string, fuel: Fuel): void { - setShipFuelStatement.run({ - fuel: JSON.stringify(fuel), - symbol: symbol, - }); -} - -export function setShipNav(symbol: string, nav: Nav): void { - setShipNavStatement.run({ - nav: JSON.stringify(nav), - symbol: symbol, - }); -} diff --git a/nodejs/database/systems.ts b/nodejs/database/systems.ts index d6d6cf1..646222e 100644 --- a/nodejs/database/systems.ts +++ b/nodejs/database/systems.ts @@ -1,5 +1,5 @@ import { DbData, db } from './db.ts'; -import { System, Waypoint } from '../model/system.ts'; +import { System, Waypoint } from '../lib/types.ts'; const addSystemStatement = db.prepare(`INSERT INTO systems(data) VALUES (json(?));`); const getSystemStatement = db.prepare(`SELECT data FROM systems WHERE data->>'symbol' = ?;`); |