From efdb231d2fcf7011871faa24a5bb233f3f1f2e19 Mon Sep 17 00:00:00 2001 From: lukas Date: Mon, 15 Jul 2024 13:22:42 +0200 Subject: [PATCH] fix: stop mining asteroid --- src/main/basedbot/lib/sage/act/end-mine.ts | 113 ++++++++++-------- src/main/basedbot/lib/sage/ix/stop-mining.ts | 4 +- .../basedbot/lib/sage/state/user-account.ts | 72 ++++------- 3 files changed, 91 insertions(+), 98 deletions(-) diff --git a/src/main/basedbot/lib/sage/act/end-mine.ts b/src/main/basedbot/lib/sage/act/end-mine.ts index 361576d2..8379176c 100644 --- a/src/main/basedbot/lib/sage/act/end-mine.ts +++ b/src/main/basedbot/lib/sage/act/end-mine.ts @@ -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) } diff --git a/src/main/basedbot/lib/sage/ix/stop-mining.ts b/src/main/basedbot/lib/sage/ix/stop-mining.ts index 431bbdb7..a2c46680 100644 --- a/src/main/basedbot/lib/sage/ix/stop-mining.ts +++ b/src/main/basedbot/lib/sage/ix/stop-mining.ts @@ -1,3 +1,4 @@ +import { PublicKey } from '@solana/web3.js' import { InstructionReturn } from '@staratlas/data-source' import { Fleet } from '@staratlas/sage' @@ -10,6 +11,7 @@ export const stopMiningIx = ( fleetInfo: FleetInfo, player: Player, mineable: Mineable, + fuelTokenAccount: PublicKey, programs: StarAtlasPrograms, // eslint-disable-next-line max-params ): InstructionReturn => @@ -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, diff --git a/src/main/basedbot/lib/sage/state/user-account.ts b/src/main/basedbot/lib/sage/state/user-account.ts index 2b0f390c..d4587acd 100644 --- a/src/main/basedbot/lib/sage/state/user-account.ts +++ b/src/main/basedbot/lib/sage/state/user-account.ts @@ -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' @@ -47,42 +47,22 @@ export type Player = { ammoCargoType: CargoType } -const getXpAccount = async ( +const getXpAccount = ( playerProfile: PublicKey, - xpCategory: PublicKey, -): Promise => { - 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, } } @@ -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