[node] factorize sortByPrice
This commit is contained in:
parent
09537408a5
commit
3306159369
3 changed files with 21 additions and 9 deletions
|
@ -7,7 +7,7 @@ import {
|
||||||
} from '../lib/utils.ts';
|
} from '../lib/utils.ts';
|
||||||
import { Ship } from '../lib/ships.ts';
|
import { Ship } from '../lib/ships.ts';
|
||||||
|
|
||||||
// example ctx { ship: {XXX}, keep: 'SILVER_ORE' }
|
// example ctx { ship: {XXX}, good: 'SILVER_ORE' }
|
||||||
export async function sell(ship: Ship, good: string): Promise<Ship> {
|
export async function sell(ship: Ship, good: string): Promise<Ship> {
|
||||||
outer: while(true) {
|
outer: while(true) {
|
||||||
const waypoint = await libSystems.waypoint(ship.nav.waypointSymbol);
|
const waypoint = await libSystems.waypoint(ship.nav.waypointSymbol);
|
||||||
|
|
|
@ -24,6 +24,7 @@ import {
|
||||||
import {
|
import {
|
||||||
is_there_a_ship_at_this_waypoint,
|
is_there_a_ship_at_this_waypoint,
|
||||||
shortestPath,
|
shortestPath,
|
||||||
|
sortByPrice,
|
||||||
} from './utils.ts';
|
} from './utils.ts';
|
||||||
|
|
||||||
export class Ship {
|
export class Ship {
|
||||||
|
@ -105,6 +106,9 @@ export class Ship {
|
||||||
// }
|
// }
|
||||||
// this.nav = response.data;
|
// this.nav = response.data;
|
||||||
//}
|
//}
|
||||||
|
isEmpty(): boolean {
|
||||||
|
return this.cargo.inventory.some(i => i.symbol !== 'ANTIMATTER');
|
||||||
|
}
|
||||||
isFull(): boolean {
|
isFull(): boolean {
|
||||||
return this.cargo.units >= this.cargo.capacity * 0.9;
|
return this.cargo.units >= this.cargo.capacity * 0.9;
|
||||||
}
|
}
|
||||||
|
@ -273,14 +277,7 @@ export async function purchaseShip(shipType: string): Promise<Ship> {
|
||||||
candidates = backupCandidates;
|
candidates = backupCandidates;
|
||||||
needsNavigate = true;
|
needsNavigate = true;
|
||||||
}
|
}
|
||||||
candidates.sort(function(a, b) {
|
sortByPrice(candidates);
|
||||||
if (a.price < b.price) {
|
|
||||||
return -1;
|
|
||||||
} else if (a.price > b.price) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
});
|
|
||||||
if (needsNavigate) {
|
if (needsNavigate) {
|
||||||
// we did not have a probe in orbit of a shipyard selling ${shipType}
|
// we did not have a probe in orbit of a shipyard selling ${shipType}
|
||||||
// yet, must be early game buying our second probe so let's move the
|
// yet, must be early game buying our second probe so let's move the
|
||||||
|
|
|
@ -21,6 +21,10 @@ type Point = {
|
||||||
y: number;
|
y: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type Price = {
|
||||||
|
price: number;
|
||||||
|
};
|
||||||
|
|
||||||
// cargo is a ship.cargo object, want is an optional symbol
|
// cargo is a ship.cargo object, want is an optional symbol
|
||||||
export function categorizeCargo(cargo: Cargo, want?: string): CategorizedCargo {
|
export function categorizeCargo(cargo: Cargo, want?: string): CategorizedCargo {
|
||||||
const wanted = cargo.inventory.filter(i => i.symbol === want || i.symbol === 'ANTIMATTER');
|
const wanted = cargo.inventory.filter(i => i.symbol === want || i.symbol === 'ANTIMATTER');
|
||||||
|
@ -60,6 +64,17 @@ export function sortByDistanceFrom<T extends Point>(a: Point, points: Array<T>):
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function sortByPrice<T extends Price>(data: Array<T>): void {
|
||||||
|
data.sort(function(a, b) {
|
||||||
|
if (a.price < b.price) {
|
||||||
|
return -1;
|
||||||
|
} else if (a.price > b.price) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
type Step = {waypoint: Waypoint, prev: string, fuel: number, total: number};
|
type Step = {waypoint: Waypoint, prev: string, fuel: number, total: number};
|
||||||
type ShortestPath = Array<{symbol: string, fuel: number}>;
|
type ShortestPath = Array<{symbol: string, fuel: number}>;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue