@shipload/sdk 0.7.1 → 2.0.0-rc2

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 (62) hide show
  1. package/README.md +349 -1
  2. package/lib/shipload.d.ts +2016 -230
  3. package/lib/shipload.js +5733 -2094
  4. package/lib/shipload.js.map +1 -1
  5. package/lib/shipload.m.js +5425 -2012
  6. package/lib/shipload.m.js.map +1 -1
  7. package/package.json +5 -5
  8. package/src/capabilities/extraction.ts +37 -0
  9. package/src/capabilities/guards.ts +43 -0
  10. package/src/capabilities/index.ts +5 -0
  11. package/src/capabilities/loading.ts +8 -0
  12. package/src/capabilities/movement.ts +29 -0
  13. package/src/capabilities/storage.ts +67 -0
  14. package/src/contracts/server.ts +788 -202
  15. package/src/data/goods.json +23 -0
  16. package/src/data/syllables.json +1184 -0
  17. package/src/entities/cargo-utils.ts +142 -0
  18. package/src/entities/container.ts +70 -0
  19. package/src/entities/entity-inventory.ts +39 -0
  20. package/src/entities/gamestate.ts +152 -0
  21. package/src/entities/inventory-accessor.ts +46 -0
  22. package/src/entities/location.ts +255 -0
  23. package/src/entities/makers.ts +69 -0
  24. package/src/entities/player.ts +288 -0
  25. package/src/entities/ship.ts +208 -0
  26. package/src/entities/warehouse.ts +89 -0
  27. package/src/errors.ts +46 -9
  28. package/src/index-module.ts +152 -7
  29. package/src/managers/actions.ts +200 -0
  30. package/src/managers/base.ts +25 -0
  31. package/src/managers/context.ts +104 -0
  32. package/src/managers/entities.ts +103 -0
  33. package/src/managers/epochs.ts +47 -0
  34. package/src/managers/index.ts +9 -0
  35. package/src/managers/locations.ts +122 -0
  36. package/src/managers/players.ts +13 -0
  37. package/src/managers/trades.ts +119 -0
  38. package/src/market/goods.ts +31 -0
  39. package/src/{market.ts → market/market.ts} +31 -37
  40. package/src/{rolls.ts → market/rolls.ts} +3 -3
  41. package/src/scheduling/accessor.ts +82 -0
  42. package/src/{epoch.ts → scheduling/epoch.ts} +1 -1
  43. package/src/scheduling/projection.ts +290 -0
  44. package/src/scheduling/schedule.ts +179 -0
  45. package/src/shipload.ts +39 -157
  46. package/src/trading/collect.ts +938 -0
  47. package/src/trading/deal.ts +207 -0
  48. package/src/trading/trade.ts +203 -0
  49. package/src/travel/travel.ts +486 -0
  50. package/src/types/capabilities.ts +79 -0
  51. package/src/types/entity-traits.ts +70 -0
  52. package/src/types/entity.ts +36 -0
  53. package/src/types/index.ts +3 -0
  54. package/src/types.ts +127 -25
  55. package/src/{hash.ts → utils/hash.ts} +1 -1
  56. package/src/utils/system.ts +155 -0
  57. package/src/goods.ts +0 -124
  58. package/src/ship.ts +0 -36
  59. package/src/state.ts +0 -0
  60. package/src/syllables.ts +0 -1184
  61. package/src/system.ts +0 -37
  62. package/src/travel.ts +0 -259
package/src/shipload.ts CHANGED
@@ -1,25 +1,16 @@
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 {TradesManager} from './managers/trades'
11
+ import {EpochsManager} from './managers/epochs'
12
+ import {ActionsManager} from './managers/actions'
13
+ import {GameState} from './entities/gamestate'
23
14
 
24
15
  interface ShiploadOptions {
25
16
  platformContractName?: string
@@ -33,22 +24,21 @@ interface ShiploadConstructorOptions extends ShiploadOptions {
33
24
  }
34
25
 
35
26
  export class Shipload {
36
- public client: APIClient
37
- public server: Contract
38
- public platform: Contract
39
- public game: PlatformContract.Types.game_row | undefined
27
+ private readonly _context: GameContext
40
28
 
41
29
  constructor(chain: ChainDefinition, constructorOptions?: ShiploadConstructorOptions) {
42
30
  const {client, platformContract, serverContract} = constructorOptions || {}
43
- this.client = client || new APIClient({url: chain.url})
31
+ const apiClient = client || new APIClient({url: chain.url})
44
32
 
45
- this.platform = platformContract
33
+ const platform = platformContract
46
34
  ? platformContract
47
- : new PlatformContract.Contract({client: this.client})
35
+ : new PlatformContract.Contract({client: apiClient})
48
36
 
49
- this.server = serverContract
37
+ const server = serverContract
50
38
  ? serverContract
51
- : new ServerContract.Contract({client: this.client})
39
+ : new ServerContract.Contract({client: apiClient})
40
+
41
+ this._context = new GameContext(apiClient, server, platform)
52
42
  }
53
43
 
54
44
  static async load(
@@ -80,155 +70,47 @@ export class Shipload {
80
70
  })
81
71
  }
82
72
 
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))
73
+ get client(): APIClient {
74
+ return this._context.client
140
75
  }
141
76
 
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)
77
+ get server(): Contract {
78
+ return this._context.server
149
79
  }
150
80
 
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)
81
+ get platform(): Contract {
82
+ return this._context.platform
157
83
  }
158
84
 
159
- async hasSystem(location: ServerContract.ActionParams.Type.coordinates): Promise<boolean> {
160
- const game = await this.getGame()
161
- return hasSystem(game.config.seed, location)
85
+ get entities(): EntitiesManager {
86
+ return this._context.entities
162
87
  }
163
88
 
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)
89
+ get players(): PlayersManager {
90
+ return this._context.players
170
91
  }
171
92
 
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)
93
+ get locations(): LocationsManager {
94
+ return this._context.locations
185
95
  }
186
96
 
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
97
+ get trades(): TradesManager {
98
+ return this._context.trades
208
99
  }
209
100
 
210
- async getCurrentEpochHeight(): Promise<UInt64> {
211
- const game = await this.getGame()
212
- return getCurrentEpoch(game)
101
+ get epochs(): EpochsManager {
102
+ return this._context.epochs
213
103
  }
214
104
 
215
- async getCurrentEpoch(): Promise<EpochInfo> {
216
- const game = await this.getGame()
217
- const epoch = await this.getCurrentEpochHeight()
218
- return getEpochInfo(game, epoch)
105
+ get actions(): ActionsManager {
106
+ return this._context.actions
219
107
  }
220
108
 
221
- async getEpoch(height: UInt64Type): Promise<EpochInfo> {
222
- const game = await this.getGame()
223
- return getEpochInfo(game, UInt64.from(height))
109
+ async getGame(reload = false): Promise<PlatformContract.Types.game_row> {
110
+ return this._context.getGame(reload)
224
111
  }
225
112
 
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
- })
113
+ async getState(reload = false): Promise<GameState> {
114
+ return this._context.getState(reload)
233
115
  }
234
116
  }