@shipload/sdk 2.0.0-rc4 → 2.0.0-rc6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/lib/shipload.d.ts +411 -1025
  2. package/lib/shipload.js +879 -2057
  3. package/lib/shipload.js.map +1 -1
  4. package/lib/shipload.m.js +852 -2028
  5. package/lib/shipload.m.js.map +1 -1
  6. package/package.json +1 -1
  7. package/src/capabilities/crafting.ts +10 -0
  8. package/src/capabilities/guards.ts +0 -5
  9. package/src/capabilities/index.ts +1 -0
  10. package/src/capabilities/storage.ts +0 -8
  11. package/src/contracts/server.ts +106 -225
  12. package/src/data/items.json +15 -14
  13. package/src/data/recipes.ts +129 -0
  14. package/src/derivation/crafting.ts +120 -0
  15. package/src/derivation/index.ts +9 -6
  16. package/src/derivation/resources.ts +54 -53
  17. package/src/derivation/stats.ts +146 -0
  18. package/src/derivation/stratum.ts +14 -14
  19. package/src/entities/cargo-utils.ts +6 -64
  20. package/src/entities/container.ts +18 -0
  21. package/src/entities/entity-inventory.ts +0 -4
  22. package/src/entities/inventory-accessor.ts +0 -4
  23. package/src/entities/location.ts +2 -197
  24. package/src/entities/player.ts +1 -274
  25. package/src/entities/ship.ts +0 -21
  26. package/src/entities/warehouse.ts +0 -4
  27. package/src/index-module.ts +43 -47
  28. package/src/managers/actions.ts +38 -90
  29. package/src/managers/context.ts +0 -9
  30. package/src/managers/index.ts +0 -1
  31. package/src/managers/locations.ts +2 -85
  32. package/src/market/items.ts +1 -2
  33. package/src/scheduling/projection.ts +0 -10
  34. package/src/shipload.ts +0 -5
  35. package/src/types/capabilities.ts +1 -9
  36. package/src/types/entity-traits.ts +3 -4
  37. package/src/types/entity.ts +0 -1
  38. package/src/types.ts +8 -28
  39. package/src/utils/system.ts +5 -4
  40. package/src/managers/trades.ts +0 -119
  41. package/src/market/market.ts +0 -208
  42. package/src/market/rolls.ts +0 -8
  43. package/src/trading/collect.ts +0 -938
  44. package/src/trading/deal.ts +0 -207
  45. package/src/trading/trade.ts +0 -203
@@ -1,55 +1,11 @@
1
- import {Bytes, Checksum256, UInt16Type, UInt64, UInt64Type} from '@wharfkit/antelope'
1
+ import {UInt16Type, UInt64, UInt64Type} from '@wharfkit/antelope'
2
2
  import {BaseManager} from './base'
3
- import {CoordinatesType, coordsToLocationId, Distance, ItemPrice} from '../types'
4
- import {marketPrice, marketPrices} from '../market/market'
3
+ import {CoordinatesType, coordsToLocationId, Distance} from '../types'
5
4
  import {hasSystem} from '../utils/system'
6
5
  import {findNearbyPlanets} from '../travel/travel'
7
- import {Location, toLocation} from '../entities/location'
8
6
  import {ServerContract} from '../contracts'
9
7
 
10
8
  export class LocationsManager extends BaseManager {
11
- async getMarketPrice(location: CoordinatesType, goodId: number): Promise<ItemPrice> {
12
- const game = await this.getGame()
13
- const state = await this.getState()
14
- return marketPrice(location, goodId, game.config.seed, state)
15
- }
16
-
17
- async getMarketPrices(location: CoordinatesType): Promise<ItemPrice[]> {
18
- const game = await this.getGame()
19
- const state = await this.getState()
20
- return marketPrices(location, game.config.seed, state)
21
- }
22
-
23
- async getMarketPricesWithSupply(location: CoordinatesType): Promise<ItemPrice[]> {
24
- const [game, state, supplyRows] = await Promise.all([
25
- this.getGame(),
26
- this.getState(),
27
- this.getSupplyRows(location),
28
- ])
29
-
30
- const prices = marketPrices(location, game.config.seed, state)
31
-
32
- const supplyMap = new Map<number, number>()
33
- for (const row of supplyRows) {
34
- if (UInt64.from(row.epoch).equals(state.epoch)) {
35
- supplyMap.set(Number(row.item_id), Number(row.supply))
36
- }
37
- }
38
-
39
- return prices.map((price) => {
40
- const actualSupply = supplyMap.get(Number(price.id))
41
- if (actualSupply !== undefined) {
42
- return ItemPrice.from({
43
- id: price.id,
44
- item: price.item,
45
- price: price.price,
46
- supply: UInt64.from(actualSupply),
47
- })
48
- }
49
- return price
50
- })
51
- }
52
-
53
9
  async hasSystem(location: CoordinatesType): Promise<boolean> {
54
10
  const game = await this.getGame()
55
11
  return hasSystem(game.config.seed, location)
@@ -63,45 +19,6 @@ export class LocationsManager extends BaseManager {
63
19
  return findNearbyPlanets(game.config.seed, origin, maxDistance)
64
20
  }
65
21
 
66
- async getSupplyRows(location: CoordinatesType) {
67
- const hash = Checksum256.hash(Bytes.from(`${location.x}-${location.y}`, 'utf8'))
68
- return this.server.table('supply').all({
69
- index_position: 'secondary',
70
- from: hash,
71
- to: hash,
72
- })
73
- }
74
-
75
- async getLocationWithPrices(coords: CoordinatesType): Promise<Location> {
76
- const location = toLocation(coords)
77
- const prices = await this.getMarketPrices(location.coordinates)
78
- location.setMarketPrices(prices)
79
- return location
80
- }
81
-
82
- async getLocationWithSupply(coords: CoordinatesType): Promise<Location> {
83
- const location = toLocation(coords)
84
- const [rows, state] = await Promise.all([
85
- this.getSupplyRows(location.coordinates),
86
- this.getState(),
87
- ])
88
- location.setLocationRows(rows, state.epoch)
89
- return location
90
- }
91
-
92
- async getLocationComplete(coords: CoordinatesType): Promise<Location> {
93
- const location = toLocation(coords)
94
- const [prices, rows, state] = await Promise.all([
95
- this.getMarketPrices(location.coordinates),
96
- this.getSupplyRows(location.coordinates),
97
- this.getState(),
98
- ])
99
-
100
- location.setMarketPrices(prices)
101
- location.setLocationRows(rows, state.epoch)
102
- return location
103
- }
104
-
105
22
  async getLocationEntity(
106
23
  id: UInt64Type
107
24
  ): Promise<ServerContract.Types.location_row | undefined> {
@@ -7,10 +7,9 @@ const items: Item[] = itemsData.map((g) =>
7
7
  id: g.id,
8
8
  name: g.name,
9
9
  description: g.description,
10
- base_price: g.base_price,
11
10
  mass: g.mass,
12
11
  category: g.category,
13
- rarity: g.rarity,
12
+ tier: g.tier,
14
13
  color: g.color,
15
14
  })
16
15
  )
@@ -23,13 +23,11 @@ export interface ProjectedEntity {
23
23
  engines?: ServerContract.Types.movement_stats
24
24
  loaders?: ServerContract.Types.loader_stats
25
25
  generator?: ServerContract.Types.energy_stats
26
- trade?: ServerContract.Types.trade_stats
27
26
  readonly totalMass: UInt64
28
27
 
29
28
  hasMovement(): boolean
30
29
  hasStorage(): boolean
31
30
  hasLoaders(): boolean
32
- hasTrade(): boolean
33
31
 
34
32
  capabilities(): EntityCapabilities
35
33
  state(): EntityState
@@ -42,7 +40,6 @@ export interface Projectable extends ScheduleData {
42
40
  generator?: ServerContract.Types.energy_stats
43
41
  engines?: ServerContract.Types.movement_stats
44
42
  loaders?: ServerContract.Types.loader_stats
45
- trade?: ServerContract.Types.trade_stats
46
43
  capacity?: UInt32
47
44
  cargo: ServerContract.Types.cargo_item[]
48
45
  cargomass: UInt32
@@ -59,7 +56,6 @@ export function createProjectedEntity(entity: Projectable): ProjectedEntity {
59
56
  const loaders = entity.loaders
60
57
  const engines = entity.engines
61
58
  const generator = entity.generator
62
- const trade = entity.trade
63
59
  const capacity = entity.capacity
64
60
 
65
61
  const projected: ProjectedEntity = {
@@ -71,7 +67,6 @@ export function createProjectedEntity(entity: Projectable): ProjectedEntity {
71
67
  engines,
72
68
  generator,
73
69
  loaders,
74
- trade,
75
70
 
76
71
  get totalMass() {
77
72
  let mass = UInt64.from(this.shipMass).adding(this.cargoMass)
@@ -93,10 +88,6 @@ export function createProjectedEntity(entity: Projectable): ProjectedEntity {
93
88
  return capsHasLoaders(this.capabilities())
94
89
  },
95
90
 
96
- hasTrade() {
97
- return this.trade !== undefined
98
- },
99
-
100
91
  capabilities(): EntityCapabilities {
101
92
  return {
102
93
  hullmass: this.shipMass,
@@ -104,7 +95,6 @@ export function createProjectedEntity(entity: Projectable): ProjectedEntity {
104
95
  engines: this.engines,
105
96
  generator: this.generator,
106
97
  loaders: this.loaders,
107
- trade: this.trade,
108
98
  }
109
99
  },
110
100
 
package/src/shipload.ts CHANGED
@@ -7,7 +7,6 @@ import {GameContext} from './managers/context'
7
7
  import {EntitiesManager} from './managers/entities'
8
8
  import {PlayersManager} from './managers/players'
9
9
  import {LocationsManager} from './managers/locations'
10
- import {TradesManager} from './managers/trades'
11
10
  import {EpochsManager} from './managers/epochs'
12
11
  import {ActionsManager} from './managers/actions'
13
12
  import {GameState} from './entities/gamestate'
@@ -94,10 +93,6 @@ export class Shipload {
94
93
  return this._context.locations
95
94
  }
96
95
 
97
- get trades(): TradesManager {
98
- return this._context.trades
99
- }
100
-
101
96
  get epochs(): EpochsManager {
102
97
  return this._context.epochs
103
98
  }
@@ -20,10 +20,6 @@ export interface LoaderCapability {
20
20
  loaders: ServerContract.Types.loader_stats
21
21
  }
22
22
 
23
- export interface TradeCapability {
24
- trade: ServerContract.Types.trade_stats
25
- }
26
-
27
23
  export interface ExtractorCapability {
28
24
  extractor: ServerContract.Types.extractor_stats
29
25
  }
@@ -42,8 +38,8 @@ export interface EntityCapabilities {
42
38
  engines?: ServerContract.Types.movement_stats
43
39
  generator?: ServerContract.Types.energy_stats
44
40
  loaders?: ServerContract.Types.loader_stats
45
- trade?: ServerContract.Types.trade_stats
46
41
  extractor?: ServerContract.Types.extractor_stats
42
+ crafter?: ServerContract.Types.crafter_stats
47
43
  }
48
44
 
49
45
  export interface EntityState {
@@ -66,10 +62,6 @@ export function capsHasLoaders(caps: EntityCapabilities): boolean {
66
62
  return caps.loaders !== undefined
67
63
  }
68
64
 
69
- export function capsHasTrade(caps: EntityCapabilities): boolean {
70
- return caps.trade !== undefined
71
- }
72
-
73
65
  export function capsHasExtractor(caps: EntityCapabilities): boolean {
74
66
  return caps.extractor !== undefined
75
67
  }
@@ -11,7 +11,6 @@ export interface EntityTraits {
11
11
  isMovable: boolean
12
12
  hasEnergy: boolean
13
13
  hasLoaders: boolean
14
- hasTrade: boolean
15
14
  notFoundError: string
16
15
  }
17
16
 
@@ -20,7 +19,7 @@ export const shipTraits: EntityTraits = {
20
19
  isMovable: true,
21
20
  hasEnergy: true,
22
21
  hasLoaders: true,
23
- hasTrade: true,
22
+
24
23
  notFoundError: 'ship not found',
25
24
  }
26
25
 
@@ -29,7 +28,7 @@ export const warehouseTraits: EntityTraits = {
29
28
  isMovable: false,
30
29
  hasEnergy: false,
31
30
  hasLoaders: true,
32
- hasTrade: false,
31
+
33
32
  notFoundError: 'warehouse not found',
34
33
  }
35
34
 
@@ -38,7 +37,7 @@ export const containerTraits: EntityTraits = {
38
37
  isMovable: true,
39
38
  hasEnergy: false,
40
39
  hasLoaders: false,
41
- hasTrade: false,
40
+
42
41
  notFoundError: 'container not found',
43
42
  }
44
43
 
@@ -25,7 +25,6 @@ export type ShipEntity = Entity &
25
25
  LoaderCapability &
26
26
  MassCapability &
27
27
  ScheduleCapability & {
28
- trade?: ServerContract.Types.trade_stats
29
28
  extractor?: ServerContract.Types.extractor_stats
30
29
  }
31
30
 
package/src/types.ts CHANGED
@@ -12,7 +12,6 @@ import {ServerContract} from './contracts'
12
12
 
13
13
  export const PRECISION = 10000
14
14
 
15
- // Ship constants
16
15
  export const INITIAL_SHIP_GENERATOR_CAPACITY = 350
17
16
  export const INITIAL_SHIP_DRAIN = 25
18
17
  export const INITIAL_SHIP_ENERGY = 350
@@ -22,31 +21,24 @@ export const INITIAL_SHIP_Z = 800
22
21
  export const INITIAL_SHIP_RECHARGE = 10
23
22
  export const INITIAL_SHIP_THRUST = 250
24
23
 
25
- // Loader constants
26
24
  export const INITIAL_LOADER_MASS = 1000
27
25
  export const INITIAL_LOADER_QUANTITY = 1
28
26
  export const INITIAL_LOADER_THRUST = 1
29
27
 
30
- // Warehouse constants
31
28
  export const WAREHOUSE_Z = 500
32
29
  export const INITIAL_WAREHOUSE_CAPACITY = 10000000
33
30
 
34
- // Container constants
35
31
  export const CONTAINER_Z = 300
36
32
  export const INITIAL_CONTAINER_HULLMASS = 50000
37
33
  export const INITIAL_CONTAINER_CAPACITY = 2000000
38
34
 
39
- // Mechanics
40
35
  export const TRAVEL_MAX_DURATION = 86400
41
36
 
42
- // Altitude limits (for UI/calculations)
43
37
  export const MIN_ORBITAL_ALTITUDE = 800
44
38
  export const MAX_ORBITAL_ALTITUDE = 3000
45
39
 
46
- // Legacy alias (deprecated, use INITIAL_SHIP_CAPACITY)
47
40
  export const INITIAL_SHIP_MASS = 500000
48
41
 
49
- // Extractor constants
50
42
  export const INITIAL_EXTRACTOR_RATE = 700
51
43
  export const INITIAL_EXTRACTOR_DRAIN = 2500
52
44
  export const INITIAL_EXTRACTOR_EFFICIENCY = 5000
@@ -74,6 +66,8 @@ export enum TaskType {
74
66
  UNLOAD = 4,
75
67
  EXTRACT = 5,
76
68
  WARP = 6,
69
+ CRAFT = 7,
70
+ DEPLOY = 8,
77
71
  }
78
72
 
79
73
  export enum LocationType {
@@ -84,9 +78,9 @@ export enum LocationType {
84
78
  }
85
79
 
86
80
  export enum TaskCancelable {
87
- NEVER = 0, // Task cannot be cancelled
88
- BEFORE_START = 1, // Task can only be cancelled before it starts
89
- ALWAYS = 2, // Task can always be cancelled
81
+ NEVER = 0,
82
+ BEFORE_START = 1,
83
+ ALWAYS = 2,
90
84
  }
91
85
 
92
86
  export const EntityType = {
@@ -132,8 +126,8 @@ export interface Distance {
132
126
  distance: UInt16
133
127
  }
134
128
 
135
- export type ResourceCategory = 'metal' | 'gas' | 'mineral' | 'organic'
136
- export type ResourceRarity = 'common' | 'uncommon' | 'rare' | 'epic' | 'legendary'
129
+ export type ResourceCategory = 'metal' | 'precious' | 'gas' | 'mineral' | 'organic'
130
+ export type ResourceTier = 't1' | 't2' | 't3' | 't4' | 't5'
137
131
 
138
132
  @Struct.type('item')
139
133
  export class Item extends Struct {
@@ -144,25 +138,11 @@ export class Item extends Struct {
144
138
  @Struct.field('string')
145
139
  description!: string
146
140
  @Struct.field(UInt32)
147
- base_price!: UInt32
148
- @Struct.field(UInt32)
149
141
  mass!: UInt32
150
142
  @Struct.field('string')
151
143
  category!: ResourceCategory
152
144
  @Struct.field('string')
153
- rarity!: ResourceRarity
145
+ tier!: ResourceTier
154
146
  @Struct.field('string')
155
147
  color!: string
156
148
  }
157
-
158
- @Struct.type('ItemPrice')
159
- export class ItemPrice extends Struct {
160
- @Struct.field(UInt16)
161
- id!: UInt16
162
- @Struct.field(Item)
163
- item!: Item
164
- @Struct.field(UInt32)
165
- price!: UInt32
166
- @Struct.field(UInt16)
167
- supply!: UInt16
168
- }
@@ -2,6 +2,7 @@ import {Checksum256, Checksum256Type, Checksum512, UInt8} from '@wharfkit/antelo
2
2
  import {hash512} from './hash'
3
3
  import {Coordinates, CoordinatesType, LocationType} from '../types'
4
4
  import {ServerContract} from '../contracts'
5
+ import {deriveLocationSize} from '../derivation/location-size'
5
6
  import syllables from '../data/syllables.json'
6
7
  import nebulaAdjectives from '../data/nebula-adjectives.json'
7
8
  import nebulaNouns from '../data/nebula-nouns.json'
@@ -116,9 +117,7 @@ export function deriveLocationStatic(
116
117
  }
117
118
 
118
119
  loc.subtype = UInt8.from(
119
- Number(loc.type) === LocationType.PLANET
120
- ? hashResult.array[2] % 6
121
- : hashResult.array[2]
120
+ Number(loc.type) === LocationType.PLANET ? hashResult.array[2] % 6 : hashResult.array[2]
122
121
  )
123
122
  loc.seed0 = UInt8.from(hashResult.array[3])
124
123
  loc.seed1 = UInt8.from(hashResult.array[4])
@@ -147,8 +146,10 @@ export function deriveLocation(
147
146
  epochSeed: Checksum256Type,
148
147
  coordinates: CoordinatesType
149
148
  ): ServerContract.Types.location_derived {
149
+ const staticProps = deriveLocationStatic(gameSeed, coordinates)
150
150
  return ServerContract.Types.location_derived.from({
151
- static_props: deriveLocationStatic(gameSeed, coordinates),
151
+ static_props: staticProps,
152
152
  epoch_props: deriveLocationEpoch(epochSeed, coordinates),
153
+ size: deriveLocationSize(staticProps),
153
154
  })
154
155
  }
@@ -1,119 +0,0 @@
1
- import {UInt64} from '@wharfkit/antelope'
2
- import {BaseManager} from './base'
3
- import {Ship} from '../entities/ship'
4
- import {Deal, findDealsForShip, FindDealsOptions} from '../trading/deal'
5
- import {
6
- analyzeCollectOptions,
7
- CollectAnalysis,
8
- CollectAnalysisCallbacks,
9
- CollectAnalysisOptions,
10
- } from '../trading/collect'
11
- import {Coordinates, ItemPrice} from '../types'
12
- import {Location, toLocation} from '../entities/location'
13
- import {findNearbyPlanets} from '../travel/travel'
14
- import {getCurrentEpoch} from '../scheduling/epoch'
15
-
16
- export class TradesManager extends BaseManager {
17
- private priceCache = new Map<string, ItemPrice[]>()
18
- private priceCacheEpoch: UInt64 | undefined
19
-
20
- private makePriceCacheKey(location: Coordinates): string {
21
- return `${location.x},${location.y}`
22
- }
23
-
24
- private async createCallbacks(): Promise<CollectAnalysisCallbacks> {
25
- const game = await this.getGame()
26
- const serverState = await this.getState()
27
- const currentEpoch = getCurrentEpoch(game)
28
-
29
- if (!this.priceCacheEpoch || !this.priceCacheEpoch.equals(currentEpoch)) {
30
- this.priceCache.clear()
31
- this.priceCacheEpoch = currentEpoch
32
- }
33
-
34
- const getNearbyLocations = async (
35
- origin: Coordinates,
36
- maxDistance: number
37
- ): Promise<Location[]> => {
38
- const nearby = findNearbyPlanets(game.config.seed, origin, maxDistance)
39
- return nearby.map((d) => toLocation(d.destination))
40
- }
41
-
42
- const getMarketPrices = async (location: Coordinates): Promise<ItemPrice[]> => {
43
- const cacheKey = this.makePriceCacheKey(location)
44
- const cached = this.priceCache.get(cacheKey)
45
- if (cached) {
46
- return cached
47
- }
48
-
49
- const locationWithSupply = await this.context.locations.getLocationComplete(location)
50
- const prices = locationWithSupply.marketPrices || []
51
-
52
- const result = prices.map((price) => {
53
- const actualSupply = locationWithSupply.getSupply(price.id)
54
-
55
- if (actualSupply !== undefined) {
56
- return ItemPrice.from({
57
- id: price.id,
58
- item: price.item,
59
- price: price.price,
60
- supply: actualSupply,
61
- })
62
- }
63
- return price
64
- })
65
-
66
- this.priceCache.set(cacheKey, result)
67
- return result
68
- }
69
-
70
- const getGameSeed = () => game.config.seed
71
- const getState = () => serverState
72
-
73
- return {getNearbyLocations, getMarketPrices, getGameSeed, getState}
74
- }
75
-
76
- clearPriceCache(): void {
77
- this.priceCache.clear()
78
- this.priceCacheEpoch = undefined
79
- }
80
-
81
- async findDeals(
82
- ship: Ship,
83
- originLocation?: Coordinates,
84
- options: FindDealsOptions = {}
85
- ): Promise<Deal[]> {
86
- const origin = originLocation || Coordinates.from(ship.coordinates)
87
- const callbacks = await this.createCallbacks()
88
-
89
- const deals = await findDealsForShip(
90
- ship,
91
- origin,
92
- callbacks.getNearbyLocations,
93
- callbacks.getMarketPrices,
94
- options
95
- )
96
-
97
- return deals
98
- }
99
-
100
- async findBestDeal(
101
- ship: Ship,
102
- originLocation?: Coordinates,
103
- options: FindDealsOptions = {}
104
- ): Promise<Deal | undefined> {
105
- const deals = await this.findDeals(ship, originLocation, {...options, maxDeals: 1})
106
- return deals[0]
107
- }
108
-
109
- async getCollectOptions(
110
- ship: Ship,
111
- arrivedAt?: Coordinates,
112
- options: CollectAnalysisOptions = {}
113
- ): Promise<CollectAnalysis> {
114
- const location = arrivedAt || Coordinates.from(ship.coordinates)
115
- const callbacks = await this.createCallbacks()
116
-
117
- return analyzeCollectOptions(ship, location, callbacks, options)
118
- }
119
- }