Skip to content

Commit

Permalink
fix: end mining
Browse files Browse the repository at this point in the history
  • Loading branch information
mindrunner committed Jul 14, 2024
1 parent 007890c commit 6c86241
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 64 deletions.
8 changes: 8 additions & 0 deletions src/main/basedbot/basedbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ export const start = async (): Promise<void> => {
'Tortoise Fleet',
createMiningStrategy(mineTitaniumOre(map), player),
],
[
'Cotton Rat Fleet',
createMiningStrategy(mineIronOre(map), player),
],
[
'Greenland Shark Fleet',
createMiningStrategy(mineHydrogen(map), player),
],
])
: new Map([
[
Expand Down
12 changes: 6 additions & 6 deletions src/main/basedbot/fsm/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const transition = async (fleetInfo: FleetInfo): Promise<void> => {
}
case 'StarbaseLoadingBay':
logger.info(
`Fleet: ${fleetInfo.fleetName} is in the loading bay at ${getName(fleetInfo.fleetState.data.starbase)}`,
`${fleetInfo.fleetName} is in the loading bay at ${getName(fleetInfo.fleetState.data.starbase)}`,
)
break
case 'MoveWarp': {
Expand All @@ -31,7 +31,7 @@ const transition = async (fleetInfo: FleetInfo): Promise<void> => {

if (warpFinish.isBefore(now())) {
logger.info(
`Fleet: ${fleetInfo.fleetName} has arrived at ${fleetInfo.fleetState.data.toSector}`,
`${fleetInfo.fleetName} has arrived at ${fleetInfo.fleetState.data.toSector}`,
)
} else {
logger.info(
Expand All @@ -46,7 +46,7 @@ const transition = async (fleetInfo: FleetInfo): Promise<void> => {

if (arrivalTime.isBefore(now())) {
logger.info(
`Fleet: ${fleetInfo.fleetName} has arrived at ${fleetInfo.fleetState.data.toSector}`,
`${fleetInfo.fleetName} has arrived at ${fleetInfo.fleetState.data.toSector}`,
)
} else {
logger.info(
Expand All @@ -61,20 +61,20 @@ const transition = async (fleetInfo: FleetInfo): Promise<void> => {

if (end.isBefore(now())) {
logger.info(
`Fleet: ${fleetInfo.fleetName} has finished mining ${getName(mineItem)} for ${amountMined}`,
`${fleetInfo.fleetName} has finished mining ${getName(mineItem)} for ${amountMined}`,
)
} else {
const log = endReason === 'FULL' ? logger.info : logger.warn

log(
`Fleet: ${fleetInfo.fleetName} mining ${getName(mineItem)} for ${amountMined}. Time remaining: ${dayjs.duration(end.diff(now())).humanize(false)} until ${endReason}`,
`${fleetInfo.fleetName} mining ${getName(mineItem)} for ${amountMined}. Time remaining: ${dayjs.duration(end.diff(now())).humanize(false)} until ${endReason}`,
)
}
break
}
default:
logger.info(
`Fleet: ${fleetInfo.fleetName} is ${fleetInfo.fleetState.type}`,
`${fleetInfo.fleetName} is ${fleetInfo.fleetState.type}`,
)
}

Expand Down
48 changes: 23 additions & 25 deletions src/main/basedbot/fsm/mine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,63 +62,61 @@ const transition = async (

if (!currentStarbase && cargoLevelFuel < 1) {
logger.warn(
`Fleet: ${fleetName} is out of fuel and not at a starbase, need self destruction`,
`${fleetName} is out of fuel and not at a starbase, need self destruction`,
)

return Promise.resolve()
}
if (isAtHomeBase) {
logger.info(`Fleet: ${fleetName} is at home base`)
logger.info(`${fleetName} is at home base`)
if (hasCargo) {
logger.info(
`Fleet: ${fleetName} has ${cargoLoad} ${resourceName}, docking to unload`,
`${fleetName} has ${cargoLoad} ${resourceName}, docking to unload`,
)

return dock(fleetInfo, location, player)
}
if (!hasEnoughFood || !hasEnoughFuel || !hasEnoughAmmo) {
logger.info(
`Fleet: ${fleetName} doesn't have enough resources, docking to resupply`,
`${fleetName} doesn't have enough resources, docking to resupply`,
)

return dock(fleetInfo, location, player)
}
if (isSameBase) {
logger.info(
`Fleet: ${fleetName} is at home/target base, mining`,
)
logger.info(`${fleetName} is at home/target base, mining`)

return mine(fleetInfo, player, resource)
}
logger.info(
`Fleet: ${fleetName} is at home base, moving to target base`,
`${fleetName} is at home base, moving to target base`,
)

return move(fleetInfo, targetBase, player, warpMode)
}

if (isAtTargetBase && !isSameBase) {
logger.info(`Fleet: ${fleetName} is at target base`)
logger.info(`${fleetName} is at target base`)
if (hasCargo) {
logger.info(
`Fleet: ${fleetName} has ${cargoLoad} ${resourceName}, returning home`,
`${fleetName} has ${cargoLoad} ${resourceName}, returning home`,
)

return move(fleetInfo, homeBase, player, warpMode)
}
if (hasEnoughFood) {
logger.info(`Fleet: ${fleetName} has enough food, mining`)
logger.info(`${fleetName} has enough food, mining`)

return mine(fleetInfo, player, resource)
}
logger.info(
`Fleet: ${fleetName} doesn't have enough food, returning home`,
`${fleetName} doesn't have enough food, returning home`,
)

return move(fleetInfo, homeBase, player, warpMode)
}

logger.info(`Fleet: ${fleetName} is at ${location}`)
logger.info(`${fleetName} is at ${location}`)

return move(
fleetInfo,
Expand All @@ -129,12 +127,12 @@ const transition = async (
}
case 'StarbaseLoadingBay': {
logger.info(
`Fleet: ${fleetInfo.fleetName} is in the loading bay at ${getName(fleetInfo.fleetState.data.starbase)}`,
`${fleetInfo.fleetName} is in the loading bay at ${getName(fleetInfo.fleetState.data.starbase)}`,
)

if (hasCargo) {
logger.info(
`Fleet: ${fleetInfo.fleetName} has ${cargoLoad} cargo, unloading`,
`${fleetInfo.fleetName} has ${cargoLoad} cargo, unloading`,
)

return unloadAllCargo(
Expand All @@ -146,7 +144,7 @@ const transition = async (
}

if (!hasEnoughFuel) {
logger.info(`Fleet: ${fleetInfo.fleetName} is refueling`)
logger.info(`${fleetInfo.fleetName} is refueling`)

return loadCargo(
fleetInfo,
Expand All @@ -158,7 +156,7 @@ const transition = async (
}

if (!hasEnoughAmmo) {
logger.info(`Fleet: ${fleetInfo.fleetName} is rearming`)
logger.info(`${fleetInfo.fleetName} is rearming`)

return loadCargo(
fleetInfo,
Expand All @@ -171,7 +169,7 @@ const transition = async (

if (!hasEnoughFood) {
logger.info(
`Fleet: ${fleetInfo.fleetName} is loading ${desiredFood - cargoLevelFood} food`,
`${fleetInfo.fleetName} is loading ${desiredFood - cargoLevelFood} food`,
)

return loadCargo(
Expand All @@ -191,7 +189,7 @@ const transition = async (

if (warpFinish.isBefore(now())) {
logger.info(
`Fleet: ${fleetInfo.fleetName} has arrived at ${fleetInfo.fleetState.data.toSector}`,
`${fleetInfo.fleetName} has arrived at ${fleetInfo.fleetState.data.toSector}`,
)
} else {
logger.info(
Expand All @@ -206,7 +204,7 @@ const transition = async (

if (arrivalTime.isBefore(now())) {
logger.info(
`Fleet: ${fleetInfo.fleetName} has arrived at ${fleetInfo.fleetState.data.toSector}`,
`${fleetInfo.fleetName} has arrived at ${fleetInfo.fleetState.data.toSector}`,
)
} else {
logger.info(
Expand All @@ -221,7 +219,7 @@ const transition = async (

if (end.isBefore(now())) {
logger.info(
`Fleet: ${fleetInfo.fleetName} has finished mining ${getName(mineItem)} for ${amountMined}`,
`${fleetInfo.fleetName} has finished mining ${getName(mineItem)} for ${amountMined}`,
)

return endMine(fleetInfo, player, config.resource)
Expand All @@ -230,7 +228,7 @@ const transition = async (
const log = endReason === 'FULL' ? logger.info : logger.warn

log(
`Fleet: ${fleetInfo.fleetName} mining ${getName(mineItem)} for ${amountMined}. Time remaining: ${dayjs.duration(end.diff(now())).humanize(false)} until ${endReason}`,
`${fleetInfo.fleetName} mining ${getName(mineItem)} for ${amountMined}. Time remaining: ${dayjs.duration(end.diff(now())).humanize(false)} until ${endReason}`,
)

break
Expand All @@ -239,17 +237,17 @@ const transition = async (
const { destructionTime, ETA } = fleetInfo.fleetState.data

if (ETA.isBefore(now())) {
logger.info(`Fleet: ${fleetInfo.fleetName} has respawned`)
logger.info(`${fleetInfo.fleetName} has respawned`)
} else {
logger.info(
`Fleet: ${fleetInfo.fleetName} respawning at ${fleetInfo.fleetState.data.sector}. ETA: ${dayjs.duration(ETA.diff(now())).humanize(false)}. Destruction time: ${destructionTime}`,
`${fleetInfo.fleetName} respawning at ${fleetInfo.fleetState.data.sector}. ETA: ${dayjs.duration(ETA.diff(now())).humanize(false)}. Destruction time: ${destructionTime}`,
)
}
break
}
default:
logger.info(
`Fleet: ${fleetInfo.fleetName} is ${fleetInfo.fleetState.type}`,
`${fleetInfo.fleetName} is ${fleetInfo.fleetState.type}`,
)

return Promise.resolve()
Expand Down
29 changes: 10 additions & 19 deletions src/main/basedbot/lib/sage/act/end-mine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
} from '@staratlas/data-source'

import { logger } from '../../../../../logger'
import { sleep } from '../../../../../service/sleep'
import { sendAndConfirmInstructions } from '../../../../../service/sol/send-and-confirm-tx'
import { programs } from '../../programs'
import { miningHandlerIx } from '../ix/fleet-state-handler'
Expand All @@ -26,40 +25,41 @@ export const endMine = async (
return
}

const ix = []
const ix1 = []
const ix2 = []

const foodTokenFromResult = createAssociatedTokenAccountIdempotent(
player.game.data.mints.food,
fleetInfo.fleet.data.cargoHold,
true,
)

ix.push(foodTokenFromResult.instructions)
ix1.push(foodTokenFromResult.instructions)

const ammoTokenFromResult = createAssociatedTokenAccountIdempotent(
player.game.data.mints.ammo,
fleetInfo.fleet.data.ammoBank,
true,
)

ix.push(ammoTokenFromResult.instructions)
ix1.push(ammoTokenFromResult.instructions)

const resourceTokenFromResult = createAssociatedTokenAccountIdempotent(
mineable.mineItem.data.mint,
mineable.resource.data.mineItem,
true,
)

ix.push(resourceTokenFromResult.instructions)
ix1.push(resourceTokenFromResult.instructions)
const resourceTokenToResult = createAssociatedTokenAccountIdempotent(
mineable.mineItem.data.mint,
fleetInfo.fleet.data.cargoHold,
true,
)

ix.push(resourceTokenToResult.instructions)
ix1.push(resourceTokenToResult.instructions)

ix.push(
ix1.push(
miningHandlerIx(
fleetInfo,
player,
Expand All @@ -71,17 +71,8 @@ export const endMine = async (
programs,
),
)
ix2.push(stopMiningIx(fleetInfo, player, mineable, programs))

const instructions = await ixReturnsToIxs(ix, player.signer)

await sendAndConfirmInstructions(instructions)

await sleep(2000)

await sendAndConfirmInstructions(
await ixReturnsToIxs(
stopMiningIx(fleetInfo, player, mineable, programs),
player.signer,
),
)
await sendAndConfirmInstructions(await ixReturnsToIxs(ix1, player.signer))
await sendAndConfirmInstructions(await ixReturnsToIxs(ix2, player.signer))
}
4 changes: 2 additions & 2 deletions src/main/basedbot/lib/sage/act/mine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ export const mine = async (

if (fleet.state.StarbaseLoadingBay) {
logger.info(
`Fleet: ${fleetInfo.fleetName} is in the loading bay at ${fleet.state.StarbaseLoadingBay.starbase}, undocking...`,
`${fleetInfo.fleetName} is in the loading bay at ${fleet.state.StarbaseLoadingBay.starbase}, undocking...`,
)

await undock(fleet, fleetInfo.location, player)
}

if (fleet.state.MoveSubwarp || fleet.state.MoveWarp) {
logger.info(`Fleet: ${fleetInfo.fleetName} is moving, cannot mine`)
logger.info(`${fleetInfo.fleetName} is moving, cannot mine`)

return
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/basedbot/lib/sage/act/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ export const move = async (

if (fleet.state.StarbaseLoadingBay) {
logger.info(
`Fleet: ${fleetInfo.fleetName} is in the loading bay at ${fleet.state.StarbaseLoadingBay.starbase}, undocking...`,
`${fleetInfo.fleetName} is in the loading bay at ${fleet.state.StarbaseLoadingBay.starbase}, undocking...`,
)

await undock(fleet, fleetInfo.location, player)
}

if (fleet.state.MineAsteroid) {
logger.info(
`Fleet: ${fleetInfo.fleetName} is mining an asteroid, cannot move`,
)
logger.info(`${fleetInfo.fleetName} is mining an asteroid, cannot move`)

return
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/basedbot/lib/sage/state/settle-fleet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const settleFleet = async (
}
default:
logger.info(
`Fleet: ${fleetInfo.fleetName} is ${fleetInfo.fleetState.type}`,
`${fleetInfo.fleetName} is ${fleetInfo.fleetState.type}`,
)
}
}
2 changes: 1 addition & 1 deletion src/main/basedbot/lib/sage/state/show-fleet-cargo-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FleetInfo } from './user-fleets'
export const showFleetCargoInfo = (fleetInfo: FleetInfo): void => {
const { ammo, cargo, food, fuel, toolkit } = fleetInfo.cargoLevels

logger.info(`Fleet: ${fleetInfo.fleetName} cargo levels:`)
logger.info(`${fleetInfo.fleetName} cargo levels:`)
logger.info(`Ammo: ${ammo}`)
logger.info(`Food: ${food}`)
logger.info(`Fuel: ${fuel}`)
Expand Down
Loading

0 comments on commit 6c86241

Please sign in to comment.