-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sketch out a trade_in behavior
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import 'package:cli/behavior/job.dart'; | ||
import 'package:cli/caches.dart'; | ||
import 'package:cli/central_command.dart'; | ||
import 'package:cli/nav/exploring.dart'; | ||
import 'package:cli/net/actions.dart'; | ||
import 'package:cli/net/queries.dart'; | ||
import 'package:cli/plan/ships.dart'; | ||
import 'package:db/db.dart'; | ||
import 'package:types/types.dart'; | ||
|
||
/// Upcycle the ship. | ||
Future<JobResult> doTradeInJob( | ||
BehaviorState state, | ||
Api api, | ||
Database db, | ||
CentralCommand centralCommand, | ||
Caches caches, | ||
Ship ship, { | ||
DateTime Function() getNow = defaultGetNow, | ||
}) async { | ||
// Will also dock the ship. | ||
await visitLocalShipyard( | ||
db, | ||
api, | ||
caches.waypoints, | ||
caches.static, | ||
caches.agent, | ||
ship, | ||
); | ||
// Get the purchase price of a new ship of this type. | ||
final shipType = assertNotNull( | ||
guessShipType(caches.static.shipyardShips, ship), | ||
'No ship type found.', | ||
const Duration(minutes: 5), | ||
); | ||
|
||
final price = assertNotNull( | ||
await db.shipyardPriceAt(ship.waypointSymbol, shipType), | ||
'No price found.', | ||
const Duration(minutes: 5), | ||
); | ||
|
||
final scrapTransaction = assertNotNull( | ||
await getScrapValue(api, ship.symbol), | ||
'No scrap value found', | ||
const Duration(minutes: 5), | ||
); | ||
final scrapValue = scrapTransaction.totalPrice; | ||
jobAssert( | ||
scrapValue > price.purchasePrice, | ||
'Scrap value is too low.', | ||
const Duration(minutes: 5), | ||
); | ||
// New ships are cheaper than the scrap value, trade in! | ||
await purchaseShip(db, api, caches.agent, ship.waypointSymbol, shipType); | ||
await scrapShipAndLog(api, db, caches.agent, ship); | ||
return JobResult.complete(); | ||
} | ||
|
||
/// Advance the trade in. | ||
final advanceTradeIn = const MultiJob('Trade In', [ | ||
doTradeInJob, | ||
]).run; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters