diff options
author | Julien Dessaux | 2024-03-30 14:22:59 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-03-30 14:22:59 +0100 |
commit | 8819cf9c67e33c76cbac65a9ca2b6ff86786d251 (patch) | |
tree | b9f8c26922909b2b59eaed2c1b2b397835db7c45 /nodejs/database | |
parent | [node] fixed mining and selling loop issues (diff) | |
download | spacetraders-8819cf9c67e33c76cbac65a9ca2b6ff86786d251.tar.gz spacetraders-8819cf9c67e33c76cbac65a9ca2b6ff86786d251.tar.bz2 spacetraders-8819cf9c67e33c76cbac65a9ca2b6ff86786d251.zip |
[node] fixed contracting and implemented renegotiation
Diffstat (limited to 'nodejs/database')
-rw-r--r-- | nodejs/database/contracts.ts | 13 | ||||
-rw-r--r-- | nodejs/database/markets.ts | 2 | ||||
-rw-r--r-- | nodejs/database/ships.ts | 13 |
3 files changed, 11 insertions, 17 deletions
diff --git a/nodejs/database/contracts.ts b/nodejs/database/contracts.ts index 8442622..64d6c65 100644 --- a/nodejs/database/contracts.ts +++ b/nodejs/database/contracts.ts @@ -18,12 +18,9 @@ export function getContracts(): Array<Contract> { } export function setContract(data: Contract): void { - if (getContract(data.id) === null) { - addContractStatement.run(JSON.stringify(data)); - } else { - updateContractStatement.run({ - data: JSON.stringify(data), - id: data.id, - }); - } + const changes = updateContractStatement.run({ + data: JSON.stringify(data), + id: data.id, + }).changes; + if (changes === 0) addContractStatement.run(JSON.stringify(data)); } diff --git a/nodejs/database/markets.ts b/nodejs/database/markets.ts index 7ae5ca2..558d181 100644 --- a/nodejs/database/markets.ts +++ b/nodejs/database/markets.ts @@ -8,7 +8,7 @@ const getMarketAtWaypointStatement = db.prepare(`SELECT data FROM markets WHERE const updateMarketStatement = db.prepare(`UPDATE markets SET data = json(:data) WHERE data->>'symbol' = :symbol;`); export function getMarketAtWaypoint(symbol: string): Market|null { - const data = getMarketAtWaypointStatement.get(symbol) as DbData; + const data = getMarketAtWaypointStatement.get(symbol) as DbData|undefined; if (!data) return null; return JSON.parse(data.data); } diff --git a/nodejs/database/ships.ts b/nodejs/database/ships.ts index f918099..5ea2ffa 100644 --- a/nodejs/database/ships.ts +++ b/nodejs/database/ships.ts @@ -23,14 +23,11 @@ export function getShipsAt(symbol: string): Array<Ship> { export function setShip(data: Ship): void { - if (getShip(data.symbol) === null) { - addShipStatement.run(JSON.stringify(data)); - } else { - updateShipStatement.run({ - data: JSON.stringify(data), - symbol: data.symbol, - }); - } + 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 { |