Skip to content

Commit

Permalink
feat: basedbot
Browse files Browse the repository at this point in the history
  • Loading branch information
mindrunner committed Jul 14, 2024
1 parent 6bc2695 commit 66ac0a4
Show file tree
Hide file tree
Showing 85 changed files with 4,182 additions and 158 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ jobs:

lint:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/install
- run: pnpm run lint

release:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
3 changes: 3 additions & 0 deletions docker/scripts/basedbot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

node main/basedbot
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
"@sentry/integrations": "^7.114.0",
"@sentry/node": "^8.11.0",
"@sentry/tracing": "^7.114.0",
"@solana/spl-token": "^0.4.6",
"@solana/web3.js": "^1.91.8",
"@solana/spl-token": "^0.4.8",
"@solana/web3.js": "^1.95.0",
"@staratlas/atlas-prime": "^0.13.1",
"@staratlas/cargo": "^1.0.2",
"@staratlas/points": "^1.0.4",
"@staratlas/claim-stake": "^0.11.5",
"@staratlas/crafting": "^1.0.2",
"@staratlas/data-source": "^0.7.4",
"@staratlas/cargo": "^1.0.5",
"@staratlas/claim-stake": "^0.11.6",
"@staratlas/crafting": "^1.0.5",
"@staratlas/data-source": "^0.7.6",
"@staratlas/factory": "^0.7.0",
"@staratlas/player-profile": "^0.9.1",
"@staratlas/points": "^1.0.5",
"@staratlas/profile-faction": "^0.4.1",
"@staratlas/sage": "^1.0.2",
"@staratlas/sage": "^1.4.0",
"big.js": "^6.2.1",
"bip39": "^3.1.0",
"bn.js": "^5.2.1",
Expand All @@ -55,6 +55,7 @@
"superagent": "^9.0.2",
"telegraf": "^4.16.3",
"typeorm": "^0.3.20",
"undici": "^6.19.2",
"winston": "^3.13.0"
},
"devDependencies": {
Expand All @@ -73,5 +74,5 @@
"ts-node-dev": "^2.0.0",
"typescript": "^5.4.5"
},
"packageManager": "pnpm@9.1.0"
"packageManager": "pnpm@9.4.0"
}
409 changes: 277 additions & 132 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Config {
user: {
keyMode: string
mnemonic: string
pubKey: string
secretKey: number[]
walletId: number
address1: string
Expand Down Expand Up @@ -61,6 +62,7 @@ export const config: Config = {
user: {
keyMode: env.get('KEY_MODE'),
secretKey: env.get('SECRET_KEY').split(',').map(s => Number(s)),
pubKey: env.get('PUBKEY'),
mnemonic: env.get('MNEMONIC'),
walletId: Number(env.get('WALLET_ID')),
address1: env.get('BOT_ADDRESS_1'),
Expand Down
2 changes: 2 additions & 0 deletions src/dayjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import utc from 'dayjs/plugin/utc'
export { Dayjs } from 'dayjs'
export { Duration } from 'dayjs/plugin/duration'

export const now = (): dayjs.Dayjs => dayjs()

dayjs.extend(customParseFormat)
dayjs.extend(advancedFormat)
dayjs.extend(duration)
Expand Down
117 changes: 117 additions & 0 deletions src/main/basedbot/basedbot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { config } from '../../config'

Check failure on line 1 in src/main/basedbot/basedbot.ts

View workflow job for this annotation

GitHub Actions / lint

There should be no empty line within import group

Check failure on line 1 in src/main/basedbot/basedbot.ts

View workflow job for this annotation

GitHub Actions / lint

There should be no empty line within import group

import { logger } from '../../logger'

Check failure on line 3 in src/main/basedbot/basedbot.ts

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups

Check failure on line 3 in src/main/basedbot/basedbot.ts

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups
import { Sentry } from '../../sentry'

Check failure on line 4 in src/main/basedbot/basedbot.ts

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups

Check failure on line 4 in src/main/basedbot/basedbot.ts

View workflow job for this annotation

GitHub Actions / lint

`../../sentry` import should occur before import of `../../config`

Check failure on line 4 in src/main/basedbot/basedbot.ts

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups

Check failure on line 4 in src/main/basedbot/basedbot.ts

View workflow job for this annotation

GitHub Actions / lint

`../../sentry` import should occur before import of `../../config`
import { sleep } from '../../service/sleep'
import { keyPair } from '../../service/wallet'

import { mineBiomass } from './fsm/configs/mine-biomass'
import { mineCarbon } from './fsm/configs/mine-carbon'
import { mineConfig } from './fsm/configs/mine-config'
import { mineCopperOre } from './fsm/configs/mine-copper-ore'
import { mineHydrogen } from './fsm/configs/mine-hydrogen'
import { mineIronOre } from './fsm/configs/mine-iron-ore'
import { mineLumanite } from './fsm/configs/mine-lumanite'
import { mineNitrogen } from './fsm/configs/mine-nitrogen'
import { mineRochinol } from './fsm/configs/mine-rochinol'
import { mineSilicia } from './fsm/configs/mine-silicia'
import { mineTitaniumOre } from './fsm/configs/mine-titanium-ore'
import { createMiningStrategy } from './fsm/mine'
import { Strategy } from './fsm/strategy'
import { settleFleet } from './lib/sage/state/settle-fleet'
import { getPlayerContext, Player } from './lib/sage/state/user-account'
import { FleetInfo, getFleetInfo, getUserFleets } from './lib/sage/state/user-fleets'
import { getMapContext, mineableByCoordinates, WorldMap } from './lib/sage/state/world-map'
import { Coordinates } from './lib/util/coordinates'

// eslint-disable-next-line require-await
export const create = async (): Promise<void> => {
logger.info('Starting basedbot...')
}

// eslint-disable-next-line require-await
export const stop = async (): Promise<void> => {
logger.info('Stopping basedbot')
}

type FleetStrategies = Map<string, Strategy>

type BotConfig = {
player: Player
map: WorldMap
fleetStrategies: FleetStrategies
}

const applyStrategy = (fleetInfo: FleetInfo, fleetStrategies: FleetStrategies): Promise<void> => {
const strategy = fleetStrategies.get(fleetInfo.fleetName)

if (!strategy) {
return Promise.resolve()
// logger.info(`No strategy for fleet: ${fleetInfo.fleetName}. Lazily loading Info Strategy...`)
// const infoStrategy = createInfoStrategy()
//
// fleetStrategies.set(fleetInfo.fleetName, infoStrategy)
//
// return infoStrategy.send(fleetInfo)
}

return strategy.send(fleetInfo)
}

const basedbot = async (botConfig: BotConfig) => {
const { player, map } = botConfig
const fleets = await getUserFleets(player)
const fleetInfos = await Promise.all(fleets.map(f => getFleetInfo(f, player, map)))

await Promise.all(fleetInfos.map(fleetInfo => settleFleet(fleetInfo, player, map)))
await Promise.all(fleetInfos.map(fleetInfo => applyStrategy(fleetInfo, botConfig.fleetStrategies)))
}

export const start = async (): Promise<void> => {
const player = await getPlayerContext(keyPair.publicKey, keyPair)
const map = await getMapContext(player.game)

const fleetStrategies: Map<string, Strategy> = config.sol.rpcEndpoint.includes('atlasnet') ? new Map([

Check failure on line 74 in src/main/basedbot/basedbot.ts

View workflow job for this annotation

GitHub Actions / lint

Expected newline between test and consequent of ternary expression

Check failure on line 74 in src/main/basedbot/basedbot.ts

View workflow job for this annotation

GitHub Actions / lint

Expected newline between consequent and alternate of ternary expression

Check failure on line 74 in src/main/basedbot/basedbot.ts

View workflow job for this annotation

GitHub Actions / lint

Expected newline between test and consequent of ternary expression

Check failure on line 74 in src/main/basedbot/basedbot.ts

View workflow job for this annotation

GitHub Actions / lint

Expected newline between consequent and alternate of ternary expression
['Atlantic Goliath Grouper Fleet', createMiningStrategy(mineBiomass(map), player)],
['Baboon Fleet', createMiningStrategy(mineConfig({
homeBase: Coordinates.fromNumber(-40, 30),
targetBase: Coordinates.fromNumber(-19, 40),
resource: mineableByCoordinates(map, Coordinates.fromNumber(-19, 40)).values().next().value
}), player)],
['Silkworm Fleet', createMiningStrategy(mineConfig({
homeBase: Coordinates.fromNumber(-40, 30),
targetBase: Coordinates.fromNumber(-18, 23),
resource: mineableByCoordinates(map, Coordinates.fromNumber(-18, 23)).values().next().value
}), player)],
['Broadclub Cuttlefish Fleet', createMiningStrategy(mineCarbon(map), player)],
['Elephant Fleet', createMiningStrategy(mineNitrogen(map), player)],
['Gelada Fleet', createMiningStrategy(mineSilicia(map), player)],
['Hectors Dolphin Fleet', createMiningStrategy(mineLumanite(map), player)],
['Groundhog Fleet', createMiningStrategy(mineCopperOre(map), player)],
['Lion Fleet', createMiningStrategy(mineIronOre(map), player)],
['Rock Hyrax Fleet', createMiningStrategy(mineHydrogen(map), player)],
['Snakes Fleet', createMiningStrategy(mineRochinol(map), player)],
['Sugar Gliders Fleet', createMiningStrategy(mineHydrogen(map), player)],
['Tortoise Fleet', createMiningStrategy(mineTitaniumOre(map), player)]
]) : new Map([
['Bearded Dragon Fleet', createMiningStrategy(mineLumanite(map), player)],
['Deer Mouse Fleet', createMiningStrategy(mineBiomass(map), player)],
['Emu Fleet', createMiningStrategy(mineNitrogen(map), player)],
['Toad Fleet', createMiningStrategy(mineSilicia(map), player)],
['Tortoise Fleet', createMiningStrategy(mineHydrogen(map), player)]
])

// eslint-disable-next-line no-constant-condition
while (true) {
try {
await basedbot({ player, map, fleetStrategies })
}
catch (e) {
Sentry.captureException(e)
logger.error(e)
}
finally {
await sleep(10000)
}
}
}
Loading

0 comments on commit 66ac0a4

Please sign in to comment.