@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/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "@shipload/sdk",
3
3
  "description": "SDKs for Shipload",
4
- "version": "0.7.1",
4
+ "version": "2.0.0-rc2",
5
5
  "homepage": "https://github.com/shipload/sdk",
6
6
  "license": "MIT",
7
7
  "main": "lib/shipload.js",
8
8
  "module": "lib/shipload.m.js",
9
9
  "types": "lib/shipload.d.ts",
10
- "unpkg": "lib/shipload.bundle.js",
11
10
  "sideEffects": false,
12
11
  "files": [
13
12
  "lib/*",
@@ -17,8 +16,8 @@
17
16
  "prepare": "make"
18
17
  },
19
18
  "dependencies": {
20
- "@wharfkit/antelope": "^1.0.2",
21
- "@wharfkit/contract": "^1.1.5",
19
+ "@wharfkit/antelope": "^1.1.1",
20
+ "@wharfkit/contract": "^1.2.1",
22
21
  "@wharfkit/session": "^1.3.1",
23
22
  "tslib": "^2.1.0"
24
23
  },
@@ -59,5 +58,6 @@
59
58
  "typedoc-plugin-mermaid": "^1.10.0",
60
59
  "typescript": "^4.1.2",
61
60
  "yarn-deduplicate": "^6.0.2"
62
- }
61
+ },
62
+ "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
63
63
  }
@@ -0,0 +1,37 @@
1
+ import {UInt16, UInt32} from '@wharfkit/antelope'
2
+ import {ServerContract} from '../contracts'
3
+ import {PRECISION} from '../types'
4
+
5
+ function calc_load_time_internal(
6
+ loaders: ServerContract.Types.loader_stats,
7
+ shipZ: number,
8
+ cargoMass: number
9
+ ): number {
10
+ if (cargoMass === 0 || loaders.quantity.toNumber() === 0 || loaders.thrust.toNumber() === 0) {
11
+ return 0
12
+ }
13
+
14
+ const totalMass = cargoMass + loaders.mass.toNumber()
15
+ const acceleration = (loaders.thrust.toNumber() / totalMass) * PRECISION
16
+ const flightTime = Math.floor(2 * Math.sqrt(shipZ / acceleration))
17
+ return Math.floor(flightTime / loaders.quantity.toNumber())
18
+ }
19
+
20
+ export function calc_extraction_duration(
21
+ extractor: ServerContract.Types.extractor_stats,
22
+ loaders: ServerContract.Types.loader_stats,
23
+ shipZ: number,
24
+ batchMass: number
25
+ ): UInt32 {
26
+ const extractionTime = Math.floor(batchMass / extractor.rate.toNumber())
27
+ const loadingTime = calc_load_time_internal(loaders, shipZ, batchMass)
28
+ return UInt32.from(Math.max(extractionTime, loadingTime))
29
+ }
30
+
31
+ export function calc_extraction_energy(
32
+ extractor: ServerContract.Types.extractor_stats,
33
+ duration: number
34
+ ): UInt16 {
35
+ const energy = Math.floor((duration * extractor.drain.toNumber()) / PRECISION)
36
+ return UInt16.from(energy)
37
+ }
@@ -0,0 +1,43 @@
1
+ import {
2
+ EnergyCapability,
3
+ ExtractorCapability,
4
+ LoaderCapability,
5
+ MassCapability,
6
+ MovementCapability,
7
+ ScheduleCapability,
8
+ StorageCapability,
9
+ TradeCapability,
10
+ } from '../types/capabilities'
11
+ import {Entity} from '../types/entity'
12
+
13
+ export function canMove(e: Entity): e is Entity & MovementCapability & EnergyCapability {
14
+ return 'engines' in e && 'generator' in e && 'energy' in e
15
+ }
16
+
17
+ export function hasEnergy(e: Entity): e is Entity & EnergyCapability {
18
+ return 'energy' in e
19
+ }
20
+
21
+ export function hasStorage(e: Entity): e is Entity & StorageCapability {
22
+ return 'capacity' in e && 'cargo' in e
23
+ }
24
+
25
+ export function hasLoaders(e: Entity): e is Entity & LoaderCapability {
26
+ return 'loaders' in e && e.loaders !== undefined
27
+ }
28
+
29
+ export function hasTrade(e: Entity): e is Entity & TradeCapability {
30
+ return 'trade' in e && e.trade !== undefined
31
+ }
32
+
33
+ export function hasMass(e: Entity): e is Entity & MassCapability {
34
+ return 'hullmass' in e
35
+ }
36
+
37
+ export function hasSchedule(e: Entity): e is Entity & ScheduleCapability {
38
+ return 'schedule' in e
39
+ }
40
+
41
+ export function hasExtractor(e: Entity): e is Entity & ExtractorCapability {
42
+ return 'extractor' in e && e.extractor !== undefined
43
+ }
@@ -0,0 +1,5 @@
1
+ export * from './guards'
2
+ export * from './movement'
3
+ export * from './storage'
4
+ export * from './loading'
5
+ export * from './extraction'
@@ -0,0 +1,8 @@
1
+ import {UInt32, UInt64} from '@wharfkit/antelope'
2
+ import {LoaderCapability} from '../types/capabilities'
3
+
4
+ export function calcLoadDuration(entity: LoaderCapability, cargoMass: UInt64): UInt32 {
5
+ const totalThrust = entity.loaders.thrust.toNumber() * entity.loaders.quantity.toNumber()
6
+ if (totalThrust === 0) return UInt32.from(0)
7
+ return UInt32.from(Math.ceil(Number(cargoMass) / totalThrust))
8
+ }
@@ -0,0 +1,29 @@
1
+ import {UInt32, UInt64} from '@wharfkit/antelope'
2
+ import {EnergyCapability, MovementCapability} from '../types/capabilities'
3
+ import {PRECISION} from '../types'
4
+
5
+ export function maxTravelDistance(entity: MovementCapability): UInt32 {
6
+ return UInt32.from(entity.generator.capacity)
7
+ .dividing(entity.engines.drain)
8
+ .multiplying(PRECISION)
9
+ }
10
+
11
+ export function calcEnergyUsage(entity: MovementCapability, distance: UInt64): UInt64 {
12
+ return distance.dividing(PRECISION).multiplying(entity.engines.drain)
13
+ }
14
+
15
+ export function hasEnergyForDistance(
16
+ entity: MovementCapability & EnergyCapability,
17
+ distance: UInt64
18
+ ): boolean {
19
+ const usage = calcEnergyUsage(entity, distance)
20
+ return UInt64.from(entity.energy).gte(usage)
21
+ }
22
+
23
+ export function energyPercent(entity: MovementCapability & EnergyCapability): number {
24
+ return (Number(entity.energy) / Number(entity.generator.capacity)) * 100
25
+ }
26
+
27
+ export function needsRecharge(entity: MovementCapability & EnergyCapability): boolean {
28
+ return UInt64.from(entity.energy).lt(entity.generator.capacity)
29
+ }
@@ -0,0 +1,67 @@
1
+ import {UInt32, UInt64, UInt64Type} from '@wharfkit/antelope'
2
+ import {ServerContract} from '../contracts'
3
+ import {StorageCapability} from '../types/capabilities'
4
+ import {getGood} from '../market/goods'
5
+
6
+ export interface HasCargo {
7
+ cargo: ServerContract.Types.cargo_item[]
8
+ }
9
+
10
+ export interface HasCapacity {
11
+ capacity: UInt32
12
+ }
13
+
14
+ export interface HasCargomass {
15
+ cargomass: UInt32
16
+ }
17
+
18
+ export function calcCargoMass(entity: HasCargo): UInt64 {
19
+ let mass = UInt64.from(0)
20
+ for (const item of entity.cargo) {
21
+ const good = getGood(item.good_id)
22
+ mass = mass.adding(good.mass.multiplying(item.quantity))
23
+ }
24
+ return mass
25
+ }
26
+
27
+ export function calcCargoValue(entity: HasCargo): UInt64 {
28
+ let value = UInt64.from(0)
29
+ for (const item of entity.cargo) {
30
+ value = value.adding(item.unit_cost.multiplying(item.quantity))
31
+ }
32
+ return value
33
+ }
34
+
35
+ export function availableCapacity(entity: StorageCapability): UInt64 {
36
+ const cargoMass = calcCargoMass(entity)
37
+ return entity.capacity.gt(cargoMass)
38
+ ? UInt64.from(entity.capacity).subtracting(cargoMass)
39
+ : UInt64.from(0)
40
+ }
41
+
42
+ export function availableCapacityFromMass(capacity: UInt64Type, cargoMass: UInt64Type): UInt64 {
43
+ const cap = UInt64.from(capacity)
44
+ const mass = UInt64.from(cargoMass)
45
+ return cap.gt(mass) ? cap.subtracting(mass) : UInt64.from(0)
46
+ }
47
+
48
+ export function hasSpace(entity: StorageCapability, goodMass: UInt64, quantity: number): boolean {
49
+ const additional = goodMass.multiplying(quantity)
50
+ return availableCapacity(entity).gte(additional)
51
+ }
52
+
53
+ export function hasSpaceForMass(
54
+ capacity: UInt64Type,
55
+ currentMass: UInt64Type,
56
+ additionalMass: UInt64Type
57
+ ): boolean {
58
+ return UInt64.from(currentMass).adding(additionalMass).lte(capacity)
59
+ }
60
+
61
+ export function isFull(entity: HasCapacity & HasCargomass): boolean {
62
+ return UInt64.from(entity.cargomass).gte(entity.capacity)
63
+ }
64
+
65
+ export function isFullFromMass(capacity: UInt64Type, cargoMass: UInt64Type): boolean {
66
+ return UInt64.from(cargoMass).gte(capacity)
67
+ }