@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/errors.ts CHANGED
@@ -1,9 +1,46 @@
1
- export const ERROR_SYSTEM_NOT_INITIALIZED = 'System not initialized'
2
- export const GOOD_DOES_NOT_EXIST = 'Good does not exist'
3
- export const GOOD_NOT_AVAILABLE_AT_LOCATION = 'Good not available at location'
4
- export const PLAYER_NOT_FOUND = 'Player not found'
5
- export const SHIP_NOT_FOUND = 'Ship not found'
6
- export const SHIP_CANNOT_BUY_TRAVELING = 'Ship cannot buy while traveling'
7
- export const INSUFFICIENT_BALANCE = 'Insufficient balance'
8
- export const INSUFFICIENT_GOOD_QUANTITY = 'Insufficient good quantity'
9
- export const REQUIRES_MORE_THAN_ONE = 'Requires more than one'
1
+ export const COMPANY_NOT_FOUND = 'Cannot find company for given account.'
2
+ export const COMMIT_ALREADY_SET = 'Commit has already been set.'
3
+ export const COMMIT_CANNOT_MATCH = 'Next commit cannot match current commit.'
4
+ export const COMMIT_NOT_SET = 'Commit has not been set.'
5
+ export const EPOCH_NON_ZERO = 'Epoch must be greater than zero.'
6
+ export const EPOCH_NOT_READY = 'Current epoch is not ready to advance'
7
+ export const ERROR_SYSTEM_ALREADY_INITIALIZED = 'This game has already been initialized.'
8
+ export const ERROR_SYSTEM_DISABLED = 'This game is currently disabled.'
9
+ export const ERROR_SYSTEM_NOT_INITIALIZED = 'This game has not been initialized.'
10
+ export const GAME_NOT_FOUND = 'Cannot find game for given account name.'
11
+ export const GAME_SEED_NOT_SET = 'This game has not initialized an epoch seed value.'
12
+ export const GOOD_DOES_NOT_EXIST = 'Good does not exist.'
13
+ export const GOOD_NOT_AVAILABLE_AT_LOCATION = 'Good is not tradeable at ship location.'
14
+ export const INSUFFICIENT_BALANCE = 'Insufficient balance.'
15
+ export const INSUFFICIENT_GOOD_QUANTITY = 'Insufficient quantity in cargo.'
16
+ export const INSUFFICIENT_GOOD_SUPPLY = 'Insufficient supply of good at location.'
17
+ export const INVALID_AMOUNT = 'Invalid amount.'
18
+ export const REQUIRES_MORE_THAN_ONE = 'A value greater than one is required.'
19
+ export const REQUIRES_POSITIVE_VALUE = 'Value must be greater than zero.'
20
+ export const PLAYER_ALREADY_JOINED = 'Player has already joined the game.'
21
+ export const PLAYER_NOT_FOUND = 'Cannot find player for given account name.'
22
+ export const SHIP_ALREADY_THERE = 'Ship cannot travel to the location its already at.'
23
+ export const SHIP_ALREADY_TRAVELING = 'Ship is already traveling.'
24
+ export const SHIP_CANNOT_BUY_TRAVELING = 'Ship cannot buy goods while traveling.'
25
+ export const SHIP_CANNOT_UPDATE_TRAVELING = 'Ship cannot be updated while traveling.'
26
+ export const SHIP_INVALID_DESTINATION = 'Ship cannot travel, no system at specified destination.'
27
+ export const SHIP_INVALID_TRAVEL_DURATION =
28
+ 'This trip cannot be made as it would exceed the maximum travel duration.'
29
+ export const SHIP_NOT_ARRIVED = 'Ship has not yet arrived at its destination.'
30
+ export const SHIP_NOT_ENOUGH_ENERGY =
31
+ 'Ship does not have enough energy to travel to the destination.'
32
+ export const SHIP_NOT_ENOUGH_ENERGY_CAPACITY =
33
+ 'Ship does not have enough energy capacity to travel.'
34
+ export const SHIP_NOT_FOUND = 'Cannot find ship for given account.'
35
+ export const SHIP_NOT_OWNED = 'Ship is not owned by this account.'
36
+ export const NO_SCHEDULE = 'No scheduled tasks.'
37
+ export const SHIP_NOT_IDLE = 'Ship must be idle (no active schedule) for this action.'
38
+ export const SHIP_NO_COMPLETED_TASKS = 'No completed tasks to resolve.'
39
+ export const SHIP_CANNOT_CANCEL_TASK = 'Cannot cancel task that is immutable or in progress.'
40
+ export const SHIP_NO_TASKS_TO_CANCEL = 'No tasks to cancel.'
41
+ export const SHIP_INVALID_CARGO = 'Invalid cargo specified for load/unload.'
42
+ export const SHIP_CARGO_NOT_OWNED = 'Cannot load cargo that is not owned.'
43
+ export const SHIP_CARGO_NOT_LOADED = 'Cannot unload cargo that is not loaded.'
44
+ export const WAREHOUSE_NOT_FOUND = 'Cannot find warehouse for given id.'
45
+ export const WAREHOUSE_ALREADY_AT_LOCATION = 'Warehouse already exists at this location.'
46
+ export const WAREHOUSE_CAPACITY_EXCEEDED = 'Warehouse capacity would be exceeded.'
@@ -1,10 +1,155 @@
1
1
  export * from './contracts'
2
- export * from './epoch'
3
2
  export * from './errors'
4
- export * from './goods'
5
- export * from './hash'
6
- export * from './system'
7
- export * from './market'
8
- export * from './ship'
9
- export * from './travel'
10
3
  export * from './types'
4
+
5
+ import {ServerContract} from './contracts'
6
+
7
+ export {Shipload} from './shipload'
8
+ export {Ship} from './entities/ship'
9
+ export type {ShipStateInput} from './entities/ship'
10
+ export {Warehouse} from './entities/warehouse'
11
+ export type {WarehouseStateInput} from './entities/warehouse'
12
+ export {Container} from './entities/container'
13
+ export type {ContainerStateInput} from './entities/container'
14
+ export {makeShip, makeWarehouse, makeContainer} from './entities/makers'
15
+
16
+ export type movement_stats = ServerContract.Types.movement_stats
17
+ export type energy_stats = ServerContract.Types.energy_stats
18
+ export type loader_stats = ServerContract.Types.loader_stats
19
+ export type schedule = ServerContract.Types.schedule
20
+ export type task = ServerContract.Types.task
21
+ export type cargo_item = ServerContract.Types.cargo_item
22
+ export type warehouse_row = ServerContract.Types.warehouse_row
23
+ export type container_row = ServerContract.Types.container_row
24
+ export type extractor_stats = ServerContract.Types.extractor_stats
25
+ export type mixture_info = ServerContract.Types.mixture_info
26
+ export type mixture_component = ServerContract.Types.mixture_component
27
+ export type location_static = ServerContract.Types.location_static
28
+ export type location_epoch = ServerContract.Types.location_epoch
29
+ export type location_derived = ServerContract.Types.location_derived
30
+ export type location_row = ServerContract.Types.location_row
31
+ export {Player} from './entities/player'
32
+ export type {PlayerStateInput} from './entities/player'
33
+ export {EntityInventory} from './entities/entity-inventory'
34
+ export {Location, toLocation} from './entities/location'
35
+ export {GameState} from './entities/gamestate'
36
+
37
+ export {
38
+ EntitiesManager,
39
+ PlayersManager,
40
+ LocationsManager,
41
+ TradesManager,
42
+ EpochsManager,
43
+ ActionsManager,
44
+ } from './managers'
45
+ export type {EntityType} from './managers'
46
+ export type {EntityRefInput} from './managers/actions'
47
+
48
+ export {getGood, getGoods, goodIds} from './market/goods'
49
+ export {getCurrentEpoch, getEpochInfo} from './scheduling/epoch'
50
+ export type {EpochInfo} from './scheduling/epoch'
51
+ export {marketPrice, marketPrices, getRarity, Rarities} from './market/market'
52
+ export type {Rarity} from './market/market'
53
+ export {
54
+ getSystemName,
55
+ hasSystem,
56
+ getLocationType,
57
+ isExtractableLocation,
58
+ deriveLocationStatic,
59
+ deriveLocationEpoch,
60
+ deriveLocation,
61
+ deriveLocationMixture,
62
+ } from './utils/system'
63
+
64
+ export {hash, hash512} from './utils/hash'
65
+
66
+ export type {Deal, FindDealsOptions} from './trading/deal'
67
+ export {findDealsForShip, findBestDeal} from './trading/deal'
68
+
69
+ export type {
70
+ CollectActionType,
71
+ CollectOption,
72
+ CollectAnalysis,
73
+ CollectAnalysisOptions,
74
+ CollectAnalysisCallbacks,
75
+ BetterSaleLocation,
76
+ RepositionLocation,
77
+ DiscountedGoodInfo,
78
+ PotentialDeal,
79
+ CargoSaleItem,
80
+ } from './trading/collect'
81
+ export {
82
+ analyzeCollectOptions,
83
+ analyzeCargoSale,
84
+ createSellAndTradeOption,
85
+ createTravelToSellOption,
86
+ createSellAndRepositionOption,
87
+ createSellAndStayOption,
88
+ createExploreOption,
89
+ } from './trading/collect'
90
+
91
+ export {
92
+ distanceBetweenCoordinates,
93
+ distanceBetweenPoints,
94
+ findNearbyPlanets,
95
+ lerp,
96
+ rotation,
97
+ calc_ship_mass,
98
+ calc_acceleration,
99
+ calc_flighttime,
100
+ calc_ship_flighttime,
101
+ calc_ship_acceleration,
102
+ calc_rechargetime,
103
+ calc_ship_rechargetime,
104
+ calc_loader_flighttime,
105
+ calc_loader_acceleration,
106
+ calc_energyusage,
107
+ calc_orbital_altitude,
108
+ calc_transfer_duration,
109
+ calculateTransferTime,
110
+ calculateLoadTimeBreakdown,
111
+ calculateRefuelingTime,
112
+ calculateFlightTime,
113
+ estimateTravelTime,
114
+ estimateDealTravelTime,
115
+ hasEnergyForDistance,
116
+ getFlightOrigin,
117
+ getDestinationLocation,
118
+ getPositionAt,
119
+ } from './travel/travel'
120
+ export type {
121
+ LoadTimeBreakdown,
122
+ EstimatedTravelTime,
123
+ EstimateTravelTimeOptions,
124
+ TransferEntity,
125
+ HasScheduleAndLocation,
126
+ } from './travel/travel'
127
+
128
+ export {
129
+ calculateUpdatedCargoCost,
130
+ calculateMaxTradeQuantity,
131
+ calculateTradeProfit,
132
+ calculateProfitPerMass,
133
+ calculateProfitPerSecond,
134
+ findBestGoodToTrade,
135
+ calculateBreakEvenPrice,
136
+ isProfitable,
137
+ calculateROI,
138
+ } from './trading/trade'
139
+ export type {TradeCalculation, TradeProfitResult} from './trading/trade'
140
+
141
+ export * as schedule from './scheduling/schedule'
142
+ export type {Scheduleable, ScheduleData} from './scheduling/schedule'
143
+ export {ScheduleAccessor, createScheduleAccessor} from './scheduling/accessor'
144
+ export {InventoryAccessor, createInventoryAccessor} from './entities/inventory-accessor'
145
+ export type {HasCargo} from './entities/inventory-accessor'
146
+
147
+ export * as cargoUtils from './entities/cargo-utils'
148
+ export type {CargoData} from './entities/cargo-utils'
149
+
150
+ export {projectEntity, projectEntityAt, createProjectedEntity} from './scheduling/projection'
151
+ export type {Projectable, ProjectedEntity} from './scheduling/projection'
152
+
153
+ export * from './types/capabilities'
154
+ export * from './types/entity'
155
+ export * from './capabilities'
@@ -0,0 +1,200 @@
1
+ import {Action, Int64, Name, NameType, UInt16, UInt32, UInt64, UInt64Type} from '@wharfkit/antelope'
2
+ import {BaseManager} from './base'
3
+ import {Ship} from '../entities/ship'
4
+ import {CoordinatesType, EntityType, EntityTypeName} from '../types'
5
+ import {ServerContract} from '../contracts'
6
+
7
+ interface SellableCargo {
8
+ good_id: {toNumber(): number} | number
9
+ quantity: {toNumber(): number} | number
10
+ hasCargo: boolean
11
+ }
12
+
13
+ export type EntityRefInput = {
14
+ entityType: EntityTypeName
15
+ entityId: UInt64Type
16
+ }
17
+
18
+ export class ActionsManager extends BaseManager {
19
+ travel(shipId: UInt64Type, destination: CoordinatesType, recharge = true): Action {
20
+ const x = Int64.from(destination.x)
21
+ const y = Int64.from(destination.y)
22
+
23
+ return this.server.action('travel', {
24
+ entity_type: EntityType.SHIP,
25
+ id: UInt64.from(shipId),
26
+ x,
27
+ y,
28
+ recharge,
29
+ })
30
+ }
31
+
32
+ grouptravel(entities: EntityRefInput[], destination: CoordinatesType, recharge = true): Action {
33
+ const entityRefs = entities.map((e) =>
34
+ ServerContract.Types.entity_ref.from({
35
+ entity_type: e.entityType,
36
+ entity_id: UInt64.from(e.entityId),
37
+ })
38
+ )
39
+ const x = Int64.from(destination.x)
40
+ const y = Int64.from(destination.y)
41
+
42
+ return this.server.action('grouptravel', {
43
+ entities: entityRefs,
44
+ x,
45
+ y,
46
+ recharge,
47
+ })
48
+ }
49
+
50
+ resolve(entityId: UInt64Type, entityType: EntityTypeName = EntityType.SHIP): Action {
51
+ return this.server.action('resolve', {
52
+ entity_type: entityType,
53
+ id: UInt64.from(entityId),
54
+ })
55
+ }
56
+
57
+ cancel(
58
+ entityId: UInt64Type,
59
+ count: UInt64Type,
60
+ entityType: EntityTypeName = EntityType.SHIP
61
+ ): Action {
62
+ return this.server.action('cancel', {
63
+ entity_type: entityType,
64
+ id: UInt64.from(entityId),
65
+ count: UInt64.from(count),
66
+ })
67
+ }
68
+
69
+ recharge(shipId: UInt64Type): Action {
70
+ return this.server.action('recharge', {
71
+ entity_type: EntityType.SHIP,
72
+ id: UInt64.from(shipId),
73
+ })
74
+ }
75
+
76
+ transfer(
77
+ sourceType: EntityTypeName,
78
+ sourceId: UInt64Type,
79
+ destType: EntityTypeName,
80
+ destId: UInt64Type,
81
+ goodId: UInt64Type,
82
+ quantity: UInt64Type
83
+ ): Action {
84
+ return this.server.action('transfer', {
85
+ source_type: sourceType,
86
+ source_id: UInt64.from(sourceId),
87
+ dest_type: destType,
88
+ dest_id: UInt64.from(destId),
89
+ good_id: UInt16.from(goodId),
90
+ quantity: UInt32.from(quantity),
91
+ })
92
+ }
93
+
94
+ buyGoods(
95
+ entityId: UInt64Type,
96
+ goodId: UInt64Type,
97
+ quantity: UInt64Type,
98
+ entityType: EntityTypeName = EntityType.SHIP
99
+ ): Action {
100
+ return this.server.action('buygoods', {
101
+ entity_type: entityType,
102
+ id: UInt64.from(entityId),
103
+ good_id: UInt16.from(goodId),
104
+ quantity: UInt32.from(quantity),
105
+ })
106
+ }
107
+
108
+ sellGoods(
109
+ entityId: UInt64Type,
110
+ goodId: UInt64Type,
111
+ quantity: UInt64Type,
112
+ entityType: EntityTypeName = EntityType.SHIP
113
+ ): Action {
114
+ return this.server.action('sellgoods', {
115
+ entity_type: entityType,
116
+ id: UInt64.from(entityId),
117
+ good_id: UInt16.from(goodId),
118
+ quantity: UInt32.from(quantity),
119
+ })
120
+ }
121
+
122
+ buyShip(account: NameType, name: string): Action {
123
+ return this.server.action('buyship', {
124
+ account: Name.from(account),
125
+ name,
126
+ })
127
+ }
128
+
129
+ buyWarehouse(account: NameType, shipId: UInt64Type, name: string): Action {
130
+ return this.server.action('buywarehouse', {
131
+ account: Name.from(account),
132
+ ship_id: UInt64.from(shipId),
133
+ name,
134
+ })
135
+ }
136
+
137
+ buyContainer(account: NameType, shipId: UInt64Type, name: string): Action {
138
+ return this.server.action('buycontainer', {
139
+ account: Name.from(account),
140
+ ship_id: UInt64.from(shipId),
141
+ name,
142
+ })
143
+ }
144
+
145
+ takeLoan(account: NameType, amount: UInt64Type): Action {
146
+ return this.server.action('takeloan', {
147
+ account: Name.from(account),
148
+ amount: UInt64.from(amount),
149
+ })
150
+ }
151
+
152
+ payLoan(account: NameType, amount: UInt64Type): Action {
153
+ return this.server.action('payloan', {
154
+ account: Name.from(account),
155
+ amount: UInt64.from(amount),
156
+ })
157
+ }
158
+
159
+ foundCompany(account: NameType, name: string): Action {
160
+ return this.platform.action('foundcompany', {
161
+ account: Name.from(account),
162
+ name,
163
+ })
164
+ }
165
+
166
+ join(account: NameType): Action {
167
+ return this.server.action('join', {
168
+ account: Name.from(account),
169
+ })
170
+ }
171
+
172
+ extract(shipId: UInt64Type): Action {
173
+ return this.server.action('extract', {
174
+ ship_id: UInt64.from(shipId),
175
+ })
176
+ }
177
+
178
+ joinGame(account: NameType, companyName: string): Action[] {
179
+ return [this.foundCompany(account, companyName), this.join(account)]
180
+ }
181
+
182
+ sellAllCargo(ship: Ship | UInt64Type, cargo?: SellableCargo[]): Action[] {
183
+ let shipCargo: SellableCargo[]
184
+
185
+ if (ship instanceof Ship) {
186
+ shipCargo = cargo || ship.inventory
187
+ } else {
188
+ if (!cargo) {
189
+ throw new Error('cargo parameter required when ship is a UInt64Type')
190
+ }
191
+ shipCargo = cargo
192
+ }
193
+
194
+ const shipId = ship instanceof Ship ? ship.id : UInt64.from(ship)
195
+
196
+ return shipCargo
197
+ .filter((c) => c.hasCargo)
198
+ .map((c) => this.sellGoods(shipId, c.good_id, c.quantity, EntityType.SHIP))
199
+ }
200
+ }
@@ -0,0 +1,25 @@
1
+ import {GameContext} from './context'
2
+
3
+ export abstract class BaseManager {
4
+ constructor(protected readonly context: GameContext) {}
5
+
6
+ protected get client() {
7
+ return this.context.client
8
+ }
9
+
10
+ protected get server() {
11
+ return this.context.server
12
+ }
13
+
14
+ protected get platform() {
15
+ return this.context.platform
16
+ }
17
+
18
+ protected async getGame() {
19
+ return this.context.getGame()
20
+ }
21
+
22
+ protected async getState() {
23
+ return this.context.getState()
24
+ }
25
+ }
@@ -0,0 +1,104 @@
1
+ import {APIClient} from '@wharfkit/antelope'
2
+ import {Contract} from '@wharfkit/contract'
3
+ import {PlatformContract} from '../contracts'
4
+ import {GameState} from '../entities/gamestate'
5
+
6
+ import {EntitiesManager} from './entities'
7
+ import {PlayersManager} from './players'
8
+ import {LocationsManager} from './locations'
9
+ import {TradesManager} from './trades'
10
+ import {EpochsManager} from './epochs'
11
+ import {ActionsManager} from './actions'
12
+
13
+ export class GameContext {
14
+ private _entities?: EntitiesManager
15
+ private _players?: PlayersManager
16
+ private _locations?: LocationsManager
17
+ private _trades?: TradesManager
18
+ private _epochs?: EpochsManager
19
+ private _actions?: ActionsManager
20
+
21
+ private _gameCache?: PlatformContract.Types.game_row
22
+ private _stateCache?: GameState
23
+
24
+ constructor(
25
+ public readonly client: APIClient,
26
+ public readonly server: Contract,
27
+ public readonly platform: Contract
28
+ ) {}
29
+
30
+ get entities(): EntitiesManager {
31
+ if (!this._entities) {
32
+ this._entities = new EntitiesManager(this)
33
+ }
34
+ return this._entities
35
+ }
36
+
37
+ get players(): PlayersManager {
38
+ if (!this._players) {
39
+ this._players = new PlayersManager(this)
40
+ }
41
+ return this._players
42
+ }
43
+
44
+ get locations(): LocationsManager {
45
+ if (!this._locations) {
46
+ this._locations = new LocationsManager(this)
47
+ }
48
+ return this._locations
49
+ }
50
+
51
+ get trades(): TradesManager {
52
+ if (!this._trades) {
53
+ this._trades = new TradesManager(this)
54
+ }
55
+ return this._trades
56
+ }
57
+
58
+ get epochs(): EpochsManager {
59
+ if (!this._epochs) {
60
+ this._epochs = new EpochsManager(this)
61
+ }
62
+ return this._epochs
63
+ }
64
+
65
+ get actions(): ActionsManager {
66
+ if (!this._actions) {
67
+ this._actions = new ActionsManager(this)
68
+ }
69
+ return this._actions
70
+ }
71
+
72
+ async getGame(reload = false): Promise<PlatformContract.Types.game_row> {
73
+ if (!reload && this._gameCache) {
74
+ return this._gameCache
75
+ }
76
+ const game = await this.platform.table('games').get()
77
+ if (!game) {
78
+ throw new Error('Game not initialized')
79
+ }
80
+ this._gameCache = game
81
+ return game
82
+ }
83
+
84
+ async getState(reload = false): Promise<GameState> {
85
+ if (!reload && this._stateCache) {
86
+ return this._stateCache
87
+ }
88
+ const state = await this.server.table('state').get()
89
+ if (!state) {
90
+ throw new Error('Game state not initialized')
91
+ }
92
+ const game = this._gameCache
93
+ this._stateCache = GameState.from(state, game)
94
+ return this._stateCache
95
+ }
96
+
97
+ get cachedGame(): PlatformContract.Types.game_row | undefined {
98
+ return this._gameCache
99
+ }
100
+
101
+ get cachedState(): GameState | undefined {
102
+ return this._stateCache
103
+ }
104
+ }
@@ -0,0 +1,103 @@
1
+ import {Name, NameType, UInt64Type} from '@wharfkit/antelope'
2
+ import {BaseManager} from './base'
3
+ import {Ship} from '../entities/ship'
4
+ import {Warehouse} from '../entities/warehouse'
5
+ import {Container} from '../entities/container'
6
+ import {ServerContract} from '../contracts'
7
+
8
+ export type EntityType = 'ship' | 'warehouse' | 'container' | 'location'
9
+
10
+ export class EntitiesManager extends BaseManager {
11
+ async getEntity(type: EntityType, id: UInt64Type): Promise<Ship | Warehouse | Container> {
12
+ const result = await this.server.readonly('getentity', {
13
+ entity_type: Name.from(type),
14
+ entity_id: id,
15
+ })
16
+ const entityInfo = result as ServerContract.Types.entity_info
17
+ return this.wrapEntity(entityInfo)
18
+ }
19
+
20
+ async getEntities(
21
+ owner: NameType | ServerContract.Types.player_row,
22
+ type?: EntityType
23
+ ): Promise<(Ship | Warehouse | Container)[]> {
24
+ const ownerName = this.resolveOwner(owner)
25
+ const result = await this.server.readonly('getentities', {
26
+ owner: ownerName,
27
+ entity_type: type ? Name.from(type) : null,
28
+ })
29
+ const entities = result as ServerContract.Types.entity_info[]
30
+ return entities.map((entity) => this.wrapEntity(entity))
31
+ }
32
+
33
+ async getSummaries(
34
+ owner: NameType | ServerContract.Types.player_row,
35
+ type?: EntityType
36
+ ): Promise<ServerContract.Types.entity_summary[]> {
37
+ const ownerName = this.resolveOwner(owner)
38
+ const result = await this.server.readonly('getsummaries', {
39
+ owner: ownerName,
40
+ entity_type: type ? Name.from(type) : null,
41
+ })
42
+ return result as ServerContract.Types.entity_summary[]
43
+ }
44
+
45
+ async getShip(id: UInt64Type): Promise<Ship> {
46
+ return (await this.getEntity('ship', id)) as Ship
47
+ }
48
+
49
+ async getWarehouse(id: UInt64Type): Promise<Warehouse> {
50
+ return (await this.getEntity('warehouse', id)) as Warehouse
51
+ }
52
+
53
+ async getContainer(id: UInt64Type): Promise<Container> {
54
+ return (await this.getEntity('container', id)) as Container
55
+ }
56
+
57
+ async getShips(owner: NameType | ServerContract.Types.player_row): Promise<Ship[]> {
58
+ return (await this.getEntities(owner, 'ship')) as Ship[]
59
+ }
60
+
61
+ async getWarehouses(owner: NameType | ServerContract.Types.player_row): Promise<Warehouse[]> {
62
+ return (await this.getEntities(owner, 'warehouse')) as Warehouse[]
63
+ }
64
+
65
+ async getContainers(owner: NameType | ServerContract.Types.player_row): Promise<Container[]> {
66
+ return (await this.getEntities(owner, 'container')) as Container[]
67
+ }
68
+
69
+ async getShipSummaries(
70
+ owner: NameType | ServerContract.Types.player_row
71
+ ): Promise<ServerContract.Types.entity_summary[]> {
72
+ return this.getSummaries(owner, 'ship')
73
+ }
74
+
75
+ async getWarehouseSummaries(
76
+ owner: NameType | ServerContract.Types.player_row
77
+ ): Promise<ServerContract.Types.entity_summary[]> {
78
+ return this.getSummaries(owner, 'warehouse')
79
+ }
80
+
81
+ async getContainerSummaries(
82
+ owner: NameType | ServerContract.Types.player_row
83
+ ): Promise<ServerContract.Types.entity_summary[]> {
84
+ return this.getSummaries(owner, 'container')
85
+ }
86
+
87
+ private wrapEntity(entity: ServerContract.Types.entity_info): Ship | Warehouse | Container {
88
+ if (entity.type.equals('ship')) {
89
+ return new Ship(entity)
90
+ } else if (entity.type.equals('warehouse')) {
91
+ return new Warehouse(entity)
92
+ } else {
93
+ return new Container(entity)
94
+ }
95
+ }
96
+
97
+ private resolveOwner(owner: NameType | ServerContract.Types.player_row): Name {
98
+ if (typeof owner === 'object' && owner !== null && 'owner' in owner) {
99
+ return owner.owner
100
+ }
101
+ return Name.from(owner)
102
+ }
103
+ }