@shipload/sdk 0.7.1 → 2.0.0-rc10

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 (79) hide show
  1. package/lib/shipload.d.ts +2203 -288
  2. package/lib/shipload.js +8441 -2210
  3. package/lib/shipload.js.map +1 -1
  4. package/lib/shipload.m.js +8160 -2167
  5. package/lib/shipload.m.js.map +1 -1
  6. package/package.json +7 -6
  7. package/src/capabilities/crafting.ts +26 -0
  8. package/src/capabilities/extraction.ts +36 -0
  9. package/src/capabilities/guards.ts +38 -0
  10. package/src/capabilities/hauling.ts +22 -0
  11. package/src/capabilities/index.ts +8 -0
  12. package/src/capabilities/loading.ts +8 -0
  13. package/src/capabilities/modules.ts +57 -0
  14. package/src/capabilities/movement.ts +29 -0
  15. package/src/capabilities/storage.ts +72 -0
  16. package/src/contracts/server.ts +1217 -254
  17. package/src/data/capabilities.ts +408 -0
  18. package/src/data/categories.ts +58 -0
  19. package/src/data/colors.ts +52 -0
  20. package/src/data/items.json +17 -0
  21. package/src/data/locations.ts +53 -0
  22. package/src/data/nebula-adjectives.json +211 -0
  23. package/src/data/nebula-nouns.json +151 -0
  24. package/src/data/recipes.ts +571 -0
  25. package/src/data/syllables.json +1790 -0
  26. package/src/data/tiers.ts +45 -0
  27. package/src/derivation/crafting.ts +197 -0
  28. package/src/derivation/index.ts +28 -0
  29. package/src/derivation/location-size.ts +15 -0
  30. package/src/derivation/resources.ts +142 -0
  31. package/src/derivation/stats.ts +146 -0
  32. package/src/derivation/stratum.ts +118 -0
  33. package/src/entities/cargo-utils.ts +84 -0
  34. package/src/entities/container.ts +106 -0
  35. package/src/entities/entity-inventory.ts +39 -0
  36. package/src/entities/gamestate.ts +152 -0
  37. package/src/entities/inventory-accessor.ts +42 -0
  38. package/src/entities/location.ts +60 -0
  39. package/src/entities/makers.ts +72 -0
  40. package/src/entities/player.ts +15 -0
  41. package/src/entities/ship-deploy.ts +263 -0
  42. package/src/entities/ship.ts +199 -0
  43. package/src/entities/warehouse.ts +91 -0
  44. package/src/errors.ts +46 -9
  45. package/src/index-module.ts +302 -7
  46. package/src/managers/actions.ts +197 -0
  47. package/src/managers/base.ts +25 -0
  48. package/src/managers/context.ts +95 -0
  49. package/src/managers/entities.ts +103 -0
  50. package/src/managers/epochs.ts +47 -0
  51. package/src/managers/index.ts +8 -0
  52. package/src/managers/locations.ts +39 -0
  53. package/src/managers/players.ts +13 -0
  54. package/src/market/items.ts +30 -0
  55. package/src/nft/description.ts +175 -0
  56. package/src/nft/deserializers.ts +81 -0
  57. package/src/nft/index.ts +2 -0
  58. package/src/resolution/resolve-item.ts +313 -0
  59. package/src/scheduling/accessor.ts +82 -0
  60. package/src/{epoch.ts → scheduling/epoch.ts} +1 -1
  61. package/src/scheduling/projection.ts +322 -0
  62. package/src/scheduling/schedule.ts +179 -0
  63. package/src/shipload.ts +36 -159
  64. package/src/travel/travel.ts +499 -0
  65. package/src/types/capabilities.ts +71 -0
  66. package/src/types/entity-traits.ts +69 -0
  67. package/src/types/entity.ts +39 -0
  68. package/src/types/index.ts +3 -0
  69. package/src/types.ts +113 -35
  70. package/src/{hash.ts → utils/hash.ts} +1 -1
  71. package/src/utils/system.ts +155 -0
  72. package/src/goods.ts +0 -124
  73. package/src/market.ts +0 -214
  74. package/src/rolls.ts +0 -8
  75. package/src/ship.ts +0 -36
  76. package/src/state.ts +0 -0
  77. package/src/syllables.ts +0 -1184
  78. package/src/system.ts +0 -37
  79. package/src/travel.ts +0 -259
package/src/shipload.ts CHANGED
@@ -1,25 +1,15 @@
1
- import {
2
- APIClient,
3
- Bytes,
4
- Checksum256,
5
- Name,
6
- NameType,
7
- Serializer,
8
- UInt16Type,
9
- UInt64,
10
- UInt64Type,
11
- } from '@wharfkit/antelope'
12
- import {Coordinates, Distance, GoodPrice} from './types'
13
- import {marketprice, marketprices} from './market'
1
+ import {APIClient} from '@wharfkit/antelope'
14
2
  import {PlatformContract, ServerContract} from './contracts'
15
- import {ERROR_SYSTEM_NOT_INITIALIZED} from './errors'
16
3
  import {ChainDefinition} from '@wharfkit/session'
17
4
  import ContractKit, {Contract} from '@wharfkit/contract'
18
- import {findNearbyPlanets, travelplan} from './travel'
19
5
 
20
- import {Ship} from './ship'
21
- import {EpochInfo, getCurrentEpoch, getEpochInfo} from './epoch'
22
- import {hasSystem} from './system'
6
+ import {GameContext} from './managers/context'
7
+ import {EntitiesManager} from './managers/entities'
8
+ import {PlayersManager} from './managers/players'
9
+ import {LocationsManager} from './managers/locations'
10
+ import {EpochsManager} from './managers/epochs'
11
+ import {ActionsManager} from './managers/actions'
12
+ import {GameState} from './entities/gamestate'
23
13
 
24
14
  interface ShiploadOptions {
25
15
  platformContractName?: string
@@ -33,22 +23,21 @@ interface ShiploadConstructorOptions extends ShiploadOptions {
33
23
  }
34
24
 
35
25
  export class Shipload {
36
- public client: APIClient
37
- public server: Contract
38
- public platform: Contract
39
- public game: PlatformContract.Types.game_row | undefined
26
+ private readonly _context: GameContext
40
27
 
41
28
  constructor(chain: ChainDefinition, constructorOptions?: ShiploadConstructorOptions) {
42
29
  const {client, platformContract, serverContract} = constructorOptions || {}
43
- this.client = client || new APIClient({url: chain.url})
30
+ const apiClient = client || new APIClient({url: chain.url})
44
31
 
45
- this.platform = platformContract
32
+ const platform = platformContract
46
33
  ? platformContract
47
- : new PlatformContract.Contract({client: this.client})
34
+ : new PlatformContract.Contract({client: apiClient})
48
35
 
49
- this.server = serverContract
36
+ const server = serverContract
50
37
  ? serverContract
51
- : new ServerContract.Contract({client: this.client})
38
+ : new ServerContract.Contract({client: apiClient})
39
+
40
+ this._context = new GameContext(apiClient, server, platform)
52
41
  }
53
42
 
54
43
  static async load(
@@ -80,155 +69,43 @@ export class Shipload {
80
69
  })
81
70
  }
82
71
 
83
- async getGame(reload = false): Promise<PlatformContract.Types.game_row> {
84
- if (!reload && this.game) {
85
- return this.game
86
- }
87
- const game = await this.platform.table('games').get()
88
- if (!game) {
89
- throw new Error(ERROR_SYSTEM_NOT_INITIALIZED)
90
- }
91
- this.game = game
92
- return game
93
- }
94
-
95
- async getState(): Promise<ServerContract.Types.state_row> {
96
- const state = await this.server.table('state').get()
97
- if (!state) {
98
- throw new Error(ERROR_SYSTEM_NOT_INITIALIZED)
99
- }
100
- return state
101
- }
102
-
103
- async getShip(ship_id: UInt64Type): Promise<Ship> {
104
- const ship = await this.server.table('ship').get(UInt64.from(ship_id))
105
- if (!ship) {
106
- throw new Error('No ship found')
107
- }
108
- return new Ship(ship)
109
- }
110
-
111
- async getShips(player: NameType | ServerContract.Types.player_row): Promise<Ship[]> {
112
- let account: Name
113
- if (player instanceof ServerContract.Types.player_row) {
114
- account = player.owner
115
- } else {
116
- account = Name.from(player)
117
- }
118
- const from = Serializer.decode({
119
- data:
120
- Serializer.encode({object: UInt64.from(UInt64.min)}).hexString +
121
- Serializer.encode({object: Name.from(account)}).hexString,
122
- type: 'uint128',
123
- })
124
- const to = Serializer.decode({
125
- data:
126
- Serializer.encode({object: UInt64.from(UInt64.max)}).hexString +
127
- Serializer.encode({object: Name.from(account)}).hexString,
128
- type: 'uint128',
129
- })
130
- const ships = await this.server
131
- .table('ship')
132
- .query({
133
- key_type: 'i128',
134
- index_position: 'secondary',
135
- from,
136
- to,
137
- })
138
- .all()
139
- return ships.map((ship) => new Ship(ship))
140
- }
141
-
142
- async marketprice(
143
- location: ServerContract.ActionParams.Type.coordinates,
144
- good_id: number
145
- ): Promise<GoodPrice> {
146
- const game = await this.getGame()
147
- const state = await this.getState()
148
- return marketprice(location, good_id, game.config.seed, state)
72
+ get client(): APIClient {
73
+ return this._context.client
149
74
  }
150
75
 
151
- async marketprices(
152
- location: ServerContract.ActionParams.Type.coordinates
153
- ): Promise<GoodPrice[]> {
154
- const game = await this.getGame()
155
- const state = await this.getState()
156
- return marketprices(location, game.config.seed, state)
76
+ get server(): Contract {
77
+ return this._context.server
157
78
  }
158
79
 
159
- async hasSystem(location: ServerContract.ActionParams.Type.coordinates): Promise<boolean> {
160
- const game = await this.getGame()
161
- return hasSystem(game.config.seed, location)
80
+ get platform(): Contract {
81
+ return this._context.platform
162
82
  }
163
83
 
164
- async findNearbyPlanets(
165
- origin: ServerContract.ActionParams.Type.coordinates,
166
- maxDistance: UInt16Type = 20
167
- ): Promise<Distance[]> {
168
- const game = await this.getGame()
169
- return findNearbyPlanets(game.config.seed, origin, maxDistance)
84
+ get entities(): EntitiesManager {
85
+ return this._context.entities
170
86
  }
171
87
 
172
- async travelplan(
173
- ship: ServerContract.Types.ship_row,
174
- origin: ServerContract.ActionParams.Type.coordinates,
175
- destination: ServerContract.ActionParams.Type.coordinates,
176
- recharge = false
177
- ): Promise<ServerContract.Types.travel_plan> {
178
- const game = await this.getGame()
179
- const cargos = await this.server.table('cargo').all({
180
- from: ship.id,
181
- to: ship.id,
182
- index_position: 'secondary',
183
- })
184
- return travelplan(game, ship, cargos, origin, destination, recharge)
88
+ get players(): PlayersManager {
89
+ return this._context.players
185
90
  }
186
91
 
187
- async getCargo(
188
- ship: UInt64Type | ServerContract.Types.ship_row
189
- ): Promise<ServerContract.Types.cargo_row[]> {
190
- let shipId: UInt64
191
- if (ship instanceof ServerContract.Types.ship_row) {
192
- shipId = UInt64.from(ship.id)
193
- } else {
194
- shipId = UInt64.from(ship)
195
- }
196
-
197
- const cargoItems = await this.server
198
- .table('cargo')
199
- .query({
200
- key_type: 'i64',
201
- index_position: 'secondary',
202
- from: shipId,
203
- to: shipId,
204
- })
205
- .all()
206
-
207
- return cargoItems
92
+ get locations(): LocationsManager {
93
+ return this._context.locations
208
94
  }
209
95
 
210
- async getCurrentEpochHeight(): Promise<UInt64> {
211
- const game = await this.getGame()
212
- return getCurrentEpoch(game)
96
+ get epochs(): EpochsManager {
97
+ return this._context.epochs
213
98
  }
214
99
 
215
- async getCurrentEpoch(): Promise<EpochInfo> {
216
- const game = await this.getGame()
217
- const epoch = await this.getCurrentEpochHeight()
218
- return getEpochInfo(game, epoch)
100
+ get actions(): ActionsManager {
101
+ return this._context.actions
219
102
  }
220
103
 
221
- async getEpoch(height: UInt64Type): Promise<EpochInfo> {
222
- const game = await this.getGame()
223
- return getEpochInfo(game, UInt64.from(height))
104
+ async getGame(reload = false): Promise<PlatformContract.Types.game_row> {
105
+ return this._context.getGame(reload)
224
106
  }
225
107
 
226
- async getLocation(location: Coordinates) {
227
- const hash = Checksum256.hash(Bytes.from(`${location.x}-${location.y}`, 'utf8'))
228
- return this.server.table('location').all({
229
- index_position: 'secondary',
230
- from: hash,
231
- to: hash,
232
- })
108
+ async getState(reload = false): Promise<GameState> {
109
+ return this._context.getState(reload)
233
110
  }
234
111
  }