Skip to content

Commit

Permalink
fix: stop mining asteroid
Browse files Browse the repository at this point in the history
  • Loading branch information
mindrunner committed Jul 15, 2024
1 parent e675811 commit efdb231
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 98 deletions.
113 changes: 65 additions & 48 deletions src/main/basedbot/lib/sage/act/end-mine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,71 @@ export const endMine = async (
return
}

const ix1 = []
const ix2 = []

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

ix1.push(foodTokenFromResult.instructions)

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

ix1.push(ammoTokenFromResult.instructions)

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

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

ix1.push(resourceTokenToResult.instructions)

ix1.push(
miningHandlerIx(
fleetInfo,
player,
mineable,
foodTokenFromResult.address,
ammoTokenFromResult.address,
resourceTokenFromResult.address,
resourceTokenToResult.address,
programs,
const [
foodToken,
ammoToken,
resourceFromToken,
resourceToToken,
fuelToken,
] = [
createAssociatedTokenAccountIdempotent(
player.game.data.mints.food,
fleet.data.cargoHold,
true,
),
createAssociatedTokenAccountIdempotent(
player.game.data.mints.ammo,
fleet.data.ammoBank,
true,
),
createAssociatedTokenAccountIdempotent(
mineable.mineItem.data.mint,
mineable.resource.data.mineItem,
true,
),
)
ix2.push(stopMiningIx(fleetInfo, player, mineable, programs))
createAssociatedTokenAccountIdempotent(
mineable.mineItem.data.mint,
fleet.data.cargoHold,
true,
),
createAssociatedTokenAccountIdempotent(
player.game.data.mints.fuel,
fleet.data.fuelTank,
true,
),
]

await ixReturnsToIxs(
[
foodToken.instructions,
ammoToken.instructions,
resourceFromToken.instructions,
resourceToToken.instructions,
miningHandlerIx(
fleetInfo,
player,
mineable,
foodToken.address,
ammoToken.address,
resourceFromToken.address,
resourceToToken.address,
programs,
),
],
player.signer,
).then(sendAndConfirmInstructions)

await sendAndConfirmInstructions(await ixReturnsToIxs(ix1, player.signer))
await sendAndConfirmInstructions(await ixReturnsToIxs(ix2, player.signer))
await ixReturnsToIxs(
[
fuelToken.instructions,
stopMiningIx(
fleetInfo,
player,
mineable,
fuelToken.address,
programs,
),
],
player.signer,
).then(sendAndConfirmInstructions)
}
4 changes: 3 additions & 1 deletion src/main/basedbot/lib/sage/ix/stop-mining.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PublicKey } from '@solana/web3.js'
import { InstructionReturn } from '@staratlas/data-source'
import { Fleet } from '@staratlas/sage'

Expand All @@ -10,6 +11,7 @@ export const stopMiningIx = (
fleetInfo: FleetInfo,
player: Player,
mineable: Mineable,
fuelTokenAccount: PublicKey,
programs: StarAtlasPrograms,
// eslint-disable-next-line max-params
): InstructionReturn =>
Expand Down Expand Up @@ -38,7 +40,7 @@ export const stopMiningIx = (
player.xpAccounts.councilRank.pointsModifierAccount,
player.game.data.gameState,
player.game.key,
fleetInfo.fuelTokenAccount,
fuelTokenAccount,
player.game.data.mints.fuel,
{
keyIndex: player.keyIndex,
Expand Down
72 changes: 23 additions & 49 deletions src/main/basedbot/lib/sage/state/user-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import {
readAllFromRPC,
} from '@staratlas/data-source'
import { PlayerProfile } from '@staratlas/player-profile'
import { PointsModifier } from '@staratlas/points'
import { UserPoints } from '@staratlas/points'
import { ProfileFactionAccount } from '@staratlas/profile-faction'
import { Game, Starbase } from '@staratlas/sage'
import { Game, SagePointsCategory, Starbase } from '@staratlas/sage'

import { connection } from '../../../../../service/sol'
import { programs, xpCategoryIds } from '../../programs'
import { programs } from '../../programs'
import { Coordinates } from '../../util/coordinates'

import { getCargoType, getCargoTypes } from './cargo-types'
Expand Down Expand Up @@ -47,42 +47,22 @@ export type Player = {
ammoCargoType: CargoType
}

const getXpAccount = async (
const getXpAccount = (
playerProfile: PublicKey,
xpCategory: PublicKey,
): Promise<XpAccount> => {
const [userXpAccount] = PublicKey.findProgramAddressSync(
[
Buffer.from('UserPointsAccount'),
xpCategory.toBuffer(),
playerProfile.toBuffer(),
],
programs.points.programId,
)

const [pointsModifierAccount] = await readAllFromRPC(
connection,
pointsCategory: SagePointsCategory,
): XpAccount => {
const pointsCategoryKey = pointsCategory.category
const pointsModifierAccount = pointsCategory.modifier
const [userPointsAccount] = UserPoints.findAddress(
programs.points,
PointsModifier,
'processed',
[
{
memcmp: {
offset: 9,
bytes: xpCategory.toBase58(),
},
},
],
pointsCategoryKey,
playerProfile,
)

if (pointsModifierAccount.type === 'error') {
throw new Error('Error reading points modifier account')
}

return {
userPointsAccount: userXpAccount,
pointsModifierAccount: pointsModifierAccount.key,
pointsCategory: xpCategory,
userPointsAccount,
pointsModifierAccount,
pointsCategory: pointsCategoryKey,
}
}

Expand Down Expand Up @@ -140,31 +120,25 @@ export const getPlayerContext = async (
if (profileFaction.type === 'error') {
throw new Error('Error reading faction account')
}
const game = await sageGame()

const xpAccounts = {
councilRank: await getXpAccount(
councilRank: getXpAccount(
profile.key,
new PublicKey(xpCategoryIds.councilRankXpCategory),
game.data.points.councilRankXpCategory,
),
dataRunning: await getXpAccount(
dataRunning: getXpAccount(
profile.key,
new PublicKey(xpCategoryIds.dataRunningXpCategory),
game.data.points.dataRunningXpCategory,
),
piloting: await getXpAccount(
piloting: getXpAccount(profile.key, game.data.points.pilotXpCategory),
mining: getXpAccount(profile.key, game.data.points.miningXpCategory),
crafting: getXpAccount(
profile.key,
new PublicKey(xpCategoryIds.pilotingXpCategory),
),
mining: await getXpAccount(
profile.key,
new PublicKey(xpCategoryIds.miningXpCategory),
),
crafting: await getXpAccount(
profile.key,
new PublicKey(xpCategoryIds.craftingXpCategory),
game.data.points.craftingXpCategory,
),
}

const game = await sageGame()
const cargoTypes = await getCargoTypes()

let homeCoords
Expand Down

0 comments on commit efdb231

Please sign in to comment.