@shipload/sdk 0.2.1 → 0.3.1

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.
package/src/travel.ts CHANGED
@@ -1,10 +1,10 @@
1
- import {Checksum256} from '@wharfkit/antelope'
1
+ import {Checksum256, Int64, UInt16, UInt16Type, UInt64, UInt64Type} from '@wharfkit/antelope'
2
2
 
3
3
  import {ServerContract} from './contracts'
4
4
  import {hash} from './hash'
5
- import {Coordinates} from './types'
5
+ import {Distance} from './types'
6
6
 
7
- export function distance(ship: ServerContract.Types.ship_row): number {
7
+ export function distanceTraveled(ship: ServerContract.Types.ship_row): number {
8
8
  if (ship.travelplan) {
9
9
  const {departure, duration} = ship.travelplan
10
10
  return (+new Date() - +departure.toDate()) / (Number(duration) * 1000)
@@ -12,18 +12,77 @@ export function distance(ship: ServerContract.Types.ship_row): number {
12
12
  return 0
13
13
  }
14
14
 
15
- export function lerp(origin: Coordinates, destination: Coordinates, time: number) {
15
+ export function distanceBetweenCoordinates(
16
+ origin: ServerContract.ActionParams.Type.coordinates,
17
+ destination: ServerContract.ActionParams.Type.coordinates
18
+ ): UInt16 {
19
+ return distanceBetweenPoints(origin.x, origin.y, destination.x, destination.y)
20
+ }
21
+
22
+ export function distanceBetweenPoints(
23
+ x1: UInt64Type,
24
+ y1: UInt64Type,
25
+ x2: UInt64Type,
26
+ y2: UInt64Type
27
+ ): UInt16 {
28
+ const x = Math.pow(x1 - x2, 2)
29
+ const y = Math.pow(y1 - y2, 2)
30
+ return UInt16.from(Math.sqrt(x + y))
31
+ }
32
+
33
+ export function lerp(
34
+ origin: ServerContract.ActionParams.Type.coordinates,
35
+ destination: ServerContract.ActionParams.Type.coordinates,
36
+ time: number
37
+ ): ServerContract.ActionParams.Type.coordinates {
16
38
  return {
17
- x: (1 - time) * origin.x + time * destination.x,
18
- y: (1 - time) * origin.y + time * destination.y,
39
+ x: (1 - time) * Number(origin.x) + time * Number(destination.x),
40
+ y: (1 - time) * Number(origin.y) + time * Number(destination.y),
19
41
  }
20
42
  }
21
43
 
22
- export function rotation(origin: Coordinates, destination: Coordinates) {
44
+ export function rotation(
45
+ origin: ServerContract.ActionParams.Type.coordinates,
46
+ destination: ServerContract.ActionParams.Type.coordinates
47
+ ) {
23
48
  return Math.atan2(destination.y - origin.y, destination.x - origin.x) * (180 / Math.PI) + 90
24
49
  }
25
50
 
26
- export function hasPlanet(seed: Checksum256, coords: Coordinates): boolean {
27
- const str = ['system', coords.x, coords.y].join('-')
28
- return hash(seed, str).slice(0, 2) === '00'
51
+ export function hasPlanet(
52
+ seed: Checksum256,
53
+ coordinates: ServerContract.ActionParams.Type.coordinates
54
+ ): boolean {
55
+ const str = ['system', coordinates.x, coordinates.y].join('-')
56
+ return String(hash(seed, str)).slice(0, 2) === '00'
57
+ }
58
+
59
+ export function findNearbyPlanets(
60
+ seed: Checksum256,
61
+ origin: ServerContract.ActionParams.Type.coordinates,
62
+ maxDistance: UInt16Type = 20
63
+ ): Distance[] {
64
+ const nearbySystems: Distance[] = []
65
+
66
+ const max = UInt16.from(maxDistance)
67
+ const xMin = Int64.from(origin.x).subtracting(max)
68
+ const xMax = Int64.from(origin.x).adding(max)
69
+ const yMin = Int64.from(origin.y).subtracting(max)
70
+ const yMax = Int64.from(origin.y).adding(max)
71
+
72
+ for (let x = Number(xMin); x <= Number(xMax); x++) {
73
+ for (let y = Number(yMin); y <= Number(yMax); y++) {
74
+ const samePlace = x === origin.x && y === origin.y
75
+ if (!samePlace) {
76
+ const distance = distanceBetweenPoints(origin.x, origin.y, x, y)
77
+ if (Number(distance) <= Number(max)) {
78
+ const system = hasPlanet(seed, {x, y})
79
+ if (system) {
80
+ nearbySystems.push({origin, destination: {x, y}, distance})
81
+ }
82
+ }
83
+ }
84
+ }
85
+ }
86
+
87
+ return nearbySystems
29
88
  }
package/src/types.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import {UInt16, UInt64} from '@wharfkit/antelope'
2
+ import {ServerContract} from './contracts'
3
+
1
4
  export interface Coordinates {
2
5
  x: number
3
6
  y: number
@@ -13,7 +16,20 @@ export interface Dimensions {
13
16
  }
14
17
 
15
18
  export interface Distance {
16
- origin: Coordinates
17
- destination: Coordinates
18
- distance: number
19
+ origin: ServerContract.ActionParams.Type.coordinates
20
+ destination: ServerContract.ActionParams.Type.coordinates
21
+ distance: UInt16
22
+ }
23
+
24
+ export interface Good {
25
+ id: UInt16
26
+ name: string
27
+ description: string
28
+ base_price: UInt64
29
+ mass: UInt64
30
+ }
31
+
32
+ export interface GoodPrice {
33
+ id: UInt16
34
+ price: UInt64
19
35
  }