summaryrefslogtreecommitdiff
path: root/nodejs/database/ships.ts
blob: f918099c96a84b9de1f7cf67a29bddc2e975c327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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 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 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 {
	if (getShip(data.symbol) === null) {
		addShipStatement.run(JSON.stringify(data));
	} else {
		updateShipStatement.run({
			data: JSON.stringify(data),
			symbol: data.symbol,
		});
	}
}

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,
	});
}