Skip to content

Commit

Permalink
fix: force self destruct
Browse files Browse the repository at this point in the history
  • Loading branch information
mindrunner committed Sep 18, 2024
1 parent f67d1bf commit 34e2e49
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 20 deletions.
21 changes: 21 additions & 0 deletions src/main/basedbot/fleet-strategies/destruct-all-strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Game } from '@staratlas/sage'

import { createDestructStrategy, destructConfig } from '../fsm/destruct'
import { Player } from '../lib/sage/state/user-account'
import { WorldMap } from '../lib/sage/state/world-map'

import { nameMapMatcher } from './name-map-matcher'
import { makeStrategyMap, StrategyConfig } from './strategy-config'

export const destructAllStrategy = (
worldMap: WorldMap,
player: Player,
game: Game,
): StrategyConfig => {
return {
match: nameMapMatcher(
createDestructStrategy(destructConfig({ worldMap }), player, game),
),
map: makeStrategyMap(),
}
}
9 changes: 5 additions & 4 deletions src/main/basedbot/fleet-strategies/get-fleet-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Player } from '../lib/sage/state/user-account'
import { WorldMap } from '../lib/sage/state/world-map'

import { atlasnetLuStrategy } from './atlasnet-lu-strategy'

Check warning on line 6 in src/main/basedbot/fleet-strategies/get-fleet-strategy.ts

View workflow job for this annotation

GitHub Actions / lint

Dependency cycle via ../fsm/mine:15=>../lib/sage/act/load-cargo:8=>../../../basedbot:12

Check warning on line 6 in src/main/basedbot/fleet-strategies/get-fleet-strategy.ts

View workflow job for this annotation

GitHub Actions / lint

Dependency cycle via ../fsm/mine:15=>../lib/sage/act/load-cargo:8=>../../../basedbot:12
import { destructAllStrategy } from './destruct-all-strategy'
import { disbandAllStrategy } from './disband-all-strategy'

Check failure on line 8 in src/main/basedbot/fleet-strategies/get-fleet-strategy.ts

View workflow job for this annotation

GitHub Actions / build

'disbandAllStrategy' is declared but its value is never read.

Check warning on line 8 in src/main/basedbot/fleet-strategies/get-fleet-strategy.ts

View workflow job for this annotation

GitHub Actions / lint

'disbandAllStrategy' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 8 in src/main/basedbot/fleet-strategies/get-fleet-strategy.ts

View workflow job for this annotation

GitHub Actions / release

'disbandAllStrategy' is declared but its value is never read.

Check failure on line 8 in src/main/basedbot/fleet-strategies/get-fleet-strategy.ts

View workflow job for this annotation

GitHub Actions / release

'disbandAllStrategy' is declared but its value is never read.

Check warning on line 8 in src/main/basedbot/fleet-strategies/get-fleet-strategy.ts

View workflow job for this annotation

GitHub Actions / lint

'disbandAllStrategy' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 8 in src/main/basedbot/fleet-strategies/get-fleet-strategy.ts

View workflow job for this annotation

GitHub Actions / build

'disbandAllStrategy' is declared but its value is never read.
import { mainnetLuStrategy } from './mainnet-lu-strategy'
import { StrategyConfig } from './strategy-config'
Expand All @@ -20,16 +21,16 @@ export const getFleetStrategy = (
return mainnetLuStrategy(map, player, game)
case 'CgHvzwGbwWv3CwLTvEgeqSKeD8EwMdTfiiCG3dFrKVVC':
// return atlasnetFcStrategy(150)(map, player, game, 'mud')
return disbandAllStrategy(map, player, game)
return destructAllStrategy(map, player, game)
case '9KBrgWVjsmdZ3YEjcsa3wrbbJREgZgS7vDbgoz2aHaNm':
// return atlasnetFcStrategy(15)(map, player, game, 'ustur')
return disbandAllStrategy(map, player, game)
return destructAllStrategy(map, player, game)
case 'FUwHSqujzcPD44SDZYJXuk73NbkEyYQwcLMioHhpjbx2':
// return atlasnetFcStrategy(2)(map, player, game, 'oni')
return disbandAllStrategy(map, player, game)
return destructAllStrategy(map, player, game)
case '34ghznSJCYEMrS1aC55UYZZUuxfuurA9441aKnigmYyz':
// return atlasnetQtStrategy(1)(map, player, game, 'le.local')
return disbandAllStrategy(map, player, game)
return destructAllStrategy(map, player, game)
default:
throw new Error('Unknown strategy')
}
Expand Down
176 changes: 176 additions & 0 deletions src/main/basedbot/fsm/destruct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import { Game } from '@staratlas/sage'
import dayjs from 'dayjs'

import { now } from '../../../dayjs'
import { logger } from '../../../logger'
import { disbandFleet } from '../lib/sage/act/disband-fleet'
import { dock } from '../lib/sage/act/dock'
import { endMine } from '../lib/sage/act/end-mine'
import { endMove } from '../lib/sage/act/end-move'
import { selfDestruct } from '../lib/sage/act/self-destruct'
import { stopSubwarp } from '../lib/sage/act/stop-subwarp'
import { undock } from '../lib/sage/act/undock'
import { starbaseByCoordinates } from '../lib/sage/state/starbase-by-coordinates'
import { Player } from '../lib/sage/state/user-account'
import { FleetInfo } from '../lib/sage/state/user-fleets'
import { mineableByCoordinates, WorldMap } from '../lib/sage/state/world-map'
import { getName } from '../lib/sage/util'

import { DisbandConfig } from './configs/disband-config'
import { Strategy } from './strategy'

// eslint-disable-next-line complexity
const transition = async (
fleetInfo: FleetInfo,
player: Player,
game: Game,
config: DestructConfig,
): Promise<void> => {
const currentStarbase = await starbaseByCoordinates(fleetInfo.location)
const { fleetName, location } = fleetInfo
const homeBase = player.homeCoordinates
const isAtHomeBase = homeBase.equals(location)

switch (fleetInfo.fleetState.type) {
case 'Idle': {
logger.info(
`${fleetName} is idle at ${fleetInfo.fleetState.data.sector} [Starbase: ${currentStarbase ? getName(currentStarbase) : 'N/A'}]`,
)

if (isAtHomeBase) {
logger.info(`${fleetName} is at home base, docking to disband`)

return dock(fleetInfo, location, player, game)
}

return selfDestruct(fleetInfo, player, game)
}
case 'StarbaseLoadingBay': {
logger.info(
`${fleetInfo.fleetName} is in the loading bay at ${getName(fleetInfo.fleetState.data.starbase)}`,
)

if (isAtHomeBase) {
logger.info(
`${fleetInfo.fleetName} is at home base, disbanding...`,
)

return disbandFleet(
player,
game,
player.homeStarbase,
fleetInfo,
)
}
logger.info(
`${fleetInfo.fleetName} is at ${location}, undocking...`,
)

return undock(fleetInfo.fleet, fleetInfo.location, player, game)
}
case 'MoveWarp': {
const { fromSector, toSector, warpFinish } =
fleetInfo.fleetState.data

if (!homeBase.equals(toSector)) {
logger.info(`Stopping fleet ${fleetInfo.fleetName}`)

return endMove(fleetInfo, player, game)
}

if (warpFinish.isBefore(now())) {
logger.info(
`${fleetInfo.fleetName} has arrived at ${fleetInfo.fleetState.data.toSector}`,
)
} else {
logger.info(
`${fleetInfo.fleetName} warping from ${fromSector} to ${toSector}. Arrival in ${dayjs.duration(warpFinish.diff(now())).humanize(false)}. Current Position: ${fleetInfo.location}`,
)
}
break
}
case 'MoveSubwarp': {
const { fromSector, toSector, arrivalTime } =
fleetInfo.fleetState.data

if (!homeBase.equals(toSector)) {
logger.info(`Stopping fleet ${fleetInfo.fleetName}`)

return stopSubwarp(fleetInfo, player, game)
}

if (arrivalTime.isBefore(now())) {
logger.info(
`${fleetInfo.fleetName} has arrived at ${fleetInfo.fleetState.data.toSector}`,
)
} else {
logger.info(
`${fleetInfo.fleetName} subwarping from ${fromSector} to ${toSector}. Arrival in ${dayjs.duration(arrivalTime.diff(now())).humanize(false)}. Current Position: ${fleetInfo.location}`,
)
}
break
}
case 'MineAsteroid': {
const { mineItem, end, amountMined } = fleetInfo.fleetState.data

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

logger.info(
`${fleetInfo.fleetName} mining ${getName(mineItem)} for ${amountMined}. Ending...`,
)
const resource = mineableByCoordinates(
config.worldMap,
fleetInfo.location,
)
.values()
.next().value

return endMine(fleetInfo, player, game, resource)
}
case 'Respawn': {
const { destructionTime, ETA } = fleetInfo.fleetState.data

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

return Promise.resolve()
}
}

export type DestructConfig = {
worldMap: WorldMap
}

export const destructConfig = (
config: Partial<DisbandConfig> & {
worldMap: WorldMap
},
): DestructConfig => ({
worldMap: config.worldMap,
})

export const createDestructStrategy = (
config: DestructConfig,
player: Player,
game: Game,
): Strategy => {
return {
apply: (fleetInfo: FleetInfo): Promise<void> =>
transition(fleetInfo, player, game, config),
}
}
3 changes: 2 additions & 1 deletion src/main/basedbot/fsm/disband.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { dock } from '../lib/sage/act/dock'
import { endMine } from '../lib/sage/act/end-mine'
import { endMove } from '../lib/sage/act/end-move'
import { move } from '../lib/sage/act/move'
import { selfDestruct } from '../lib/sage/act/self-destruct'
import { stopSubwarp } from '../lib/sage/act/stop-subwarp'
import { undock } from '../lib/sage/act/undock'
import { starbaseByCoordinates } from '../lib/sage/state/starbase-by-coordinates'
Expand Down Expand Up @@ -44,7 +45,7 @@ const transition = async (
`${fleetName} is out of fuel and not at a starbase, need self destruction`,
)

return Promise.resolve()
return selfDestruct(fleetInfo, player, game)
}
if (isAtHomeBase) {
logger.info(`${fleetName} is at home base, docking to disband`)
Expand Down
3 changes: 2 additions & 1 deletion src/main/basedbot/fsm/mine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { endMine } from '../lib/sage/act/end-mine'
import { loadCargo } from '../lib/sage/act/load-cargo'

Check warning on line 8 in src/main/basedbot/fsm/mine.ts

View workflow job for this annotation

GitHub Actions / lint

Dependency cycle via ../../../basedbot:12=>./fleet-strategies/get-fleet-strategy:27=>./atlasnet-lu-strategy:6

Check warning on line 8 in src/main/basedbot/fsm/mine.ts

View workflow job for this annotation

GitHub Actions / lint

Dependency cycle via ../../../basedbot:12=>./fleet-strategies/get-fleet-strategy:27=>./atlasnet-lu-strategy:6
import { mine } from '../lib/sage/act/mine'
import { move } from '../lib/sage/act/move'
import { selfDestruct } from '../lib/sage/act/self-destruct'
import { undock } from '../lib/sage/act/undock'
import { unloadAllCargo } from '../lib/sage/act/unload-all-cargo'
import { starbaseByCoordinates } from '../lib/sage/state/starbase-by-coordinates'
Expand Down Expand Up @@ -71,7 +72,7 @@ const transition = async (
`${fleetName} is out of fuel and not at a starbase, need self destruction`,
)

return Promise.resolve()
return selfDestruct(fleetInfo, player, game)
}
if (isAtHomeBase) {
logger.info(`${fleetName} is at home base`)
Expand Down
3 changes: 2 additions & 1 deletion src/main/basedbot/fsm/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getTokenBalance } from '../basedbot'
import { dock } from '../lib/sage/act/dock'
import { loadCargo } from '../lib/sage/act/load-cargo'
import { move, WarpMode } from '../lib/sage/act/move'
import { selfDestruct } from '../lib/sage/act/self-destruct'
import { undock } from '../lib/sage/act/undock'
import { getHold, unloadCargo } from '../lib/sage/act/unload-cargo'
import { starbaseByCoordinates } from '../lib/sage/state/starbase-by-coordinates'
Expand Down Expand Up @@ -63,7 +64,7 @@ const transition = async (
`${fleetName} is out of fuel and not at a starbase, need self destruction`,
)

return Promise.resolve()
return selfDestruct(fleetInfo, player, game)
}
if (isSameBase) {
logger.warn(
Expand Down
60 changes: 49 additions & 11 deletions src/main/basedbot/lib/sage/act/exit-respawn.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { ixReturnsToIxs } from '@staratlas/data-source'
import { getAssociatedTokenAddressSync } from '@solana/spl-token'
import { PublicKey } from '@solana/web3.js'
import { InstructionReturn, ixReturnsToIxs } from '@staratlas/data-source'
import { Game, Starbase } from '@staratlas/sage'

import { logger } from '../../../../../logger'
import { connection } from '../../../../../service/sol'
import { sendAndConfirmInstructions } from '../../../../../service/sol/send-and-confirm-tx'
import { programs } from '../../programs'
import { exitRespawnIx } from '../ix/exit-respawn'
import { forceDropFleetCargoIx } from '../ix/force-drop-fleet-cargo'
import { getCargoStatsDefinition } from '../state/cargo-stats-definition'
import { getCargoType } from '../state/cargo-types'
import { getStarbasePlayer } from '../state/starbase-player'
import { Player } from '../state/user-account'
import { FleetInfo } from '../state/user-fleets'

import { getFleetCargoHold } from './load-cargo'

Check warning on line 18 in src/main/basedbot/lib/sage/act/exit-respawn.ts

View workflow job for this annotation

GitHub Actions / lint

Dependency cycle via ../../../basedbot:12=>./lib/sage/state/settle-fleet:36

Check warning on line 18 in src/main/basedbot/lib/sage/act/exit-respawn.ts

View workflow job for this annotation

GitHub Actions / lint

Dependency cycle via ../../../basedbot:12=>./lib/sage/state/settle-fleet:36

export const exitRespawn = async (
fleetInfo: FleetInfo,
starbase: Starbase,
Expand All @@ -25,15 +33,45 @@ export const exitRespawn = async (

const starbasePlayer = await getStarbasePlayer(player, starbase, programs)

const ix = exitRespawnIx(
fleetInfo,
player,
game,
starbase,
starbasePlayer,
programs,
)
const instructions = await ixReturnsToIxs(ix, player.signer)
const [cargoStatsDefinition] = await getCargoStatsDefinition()

const ixs: Array<InstructionReturn> = []

const cargoMints = player.cargoTypes.map((ct) => ct.data.mint)

for (const key of cargoMints) {
const mint = new PublicKey(key)
const cargoType = getCargoType(player.cargoTypes, game, mint)

await sendAndConfirmInstructions(instructions)
const cargoPod = getFleetCargoHold(mint, game, fleetInfo)
const tokenFrom = getAssociatedTokenAddressSync(mint, cargoPod, true)

const accountInfo = await connection.getAccountInfo(tokenFrom)

if (accountInfo) {
ixs.push(
forceDropFleetCargoIx(
fleetInfo,
game,
cargoStatsDefinition,
cargoPod,
cargoType.key,
tokenFrom,
mint,
programs,
),
)
}
}
ixs.push(
exitRespawnIx(
fleetInfo,
player,
game,
starbase,
starbasePlayer,
programs,
),
)
await ixReturnsToIxs(ixs, player.signer).then(sendAndConfirmInstructions)
}
4 changes: 2 additions & 2 deletions src/main/basedbot/lib/sage/act/load-cargo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Player } from '../state/user-account'
import { FleetInfo } from '../state/user-fleets'
import { getName } from '../util'

export const getHold = (
export const getFleetCargoHold = (
mint: PublicKey,
game: Game,
fleetInfo: FleetInfo,
Expand All @@ -47,7 +47,7 @@ export const loadCargo = async (
): Promise<void> => {
const starbase = await starbaseByCoordinates(fleetInfo.location)

const hold = getHold(mint, game, fleetInfo)
const hold = getFleetCargoHold(mint, game, fleetInfo)

if (!starbase) {
throw new Error(`No starbase found at ${fleetInfo.location}`)
Expand Down
Loading

0 comments on commit 34e2e49

Please sign in to comment.