@shipload/sdk 0.3.0 → 0.3.2
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/lib/shipload.d.ts +136 -58
- package/lib/shipload.js +238 -95
- package/lib/shipload.js.map +1 -1
- package/lib/shipload.m.js +237 -96
- package/lib/shipload.m.js.map +1 -1
- package/package.json +1 -1
- package/src/contracts/platform.ts +109 -11
- package/src/contracts/server.ts +68 -53
- package/src/epoch.ts +11 -0
- package/src/hash.ts +5 -4
- package/src/index-module.ts +1 -0
- package/src/rolls.ts +3 -3
- package/src/shipload.ts +46 -12
- package/src/travel.ts +44 -21
- package/src/types.ts +4 -3
package/src/epoch.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {PlatformContract, ServerContract} from './contracts'
|
|
2
|
+
|
|
3
|
+
export async function getCurrentEpoch(
|
|
4
|
+
game: PlatformContract.Types.game_row,
|
|
5
|
+
state: ServerContract.Types.state_row
|
|
6
|
+
): Promise<number> {
|
|
7
|
+
const current = new Date().getTime()
|
|
8
|
+
const difference = (current - state.genesis.toDate().getTime()) / 1000
|
|
9
|
+
const epoch = Math.floor(difference / Number(game.config.epochtime)) + 1
|
|
10
|
+
return epoch
|
|
11
|
+
}
|
package/src/hash.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {Bytes, Checksum256Type, Checksum512} from '@wharfkit/antelope'
|
|
1
|
+
import {Bytes, Checksum256, Checksum256Type, Checksum512} from '@wharfkit/antelope'
|
|
2
2
|
|
|
3
3
|
export function hash(seed: Checksum256Type, string: string): Checksum512 {
|
|
4
4
|
const bytes = Bytes.from(`${seed}${string}`, 'utf8')
|
|
5
|
-
return
|
|
5
|
+
return Checksum256.hash(bytes)
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
export function
|
|
9
|
-
|
|
8
|
+
export function hash512(seed: Checksum256Type, string: string): Checksum512 {
|
|
9
|
+
const bytes = Bytes.from(`${seed}${string}`, 'utf8')
|
|
10
|
+
return Checksum512.hash(bytes)
|
|
10
11
|
}
|
package/src/index-module.ts
CHANGED
package/src/rolls.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {Checksum256Type} from '@wharfkit/antelope'
|
|
2
|
-
import {
|
|
2
|
+
import {hash512} from './hash'
|
|
3
3
|
|
|
4
4
|
export function roll(game_seed: Checksum256Type, roll_seed: string): number {
|
|
5
|
-
const
|
|
5
|
+
const hash = hash512(game_seed, roll_seed)
|
|
6
6
|
// Combine the first two bytes to form a uint16_t value
|
|
7
|
-
return (
|
|
7
|
+
return (hash.array[0] << 8) | hash.array[1]
|
|
8
8
|
}
|
package/src/shipload.ts
CHANGED
|
@@ -1,42 +1,74 @@
|
|
|
1
1
|
import {APIClient, UInt64} from '@wharfkit/antelope'
|
|
2
2
|
import {Coordinates, GoodPrice} from './types'
|
|
3
3
|
import {marketprice, marketprices} from './market'
|
|
4
|
-
import {ServerContract} from './contracts'
|
|
4
|
+
import {PlatformContract, ServerContract} from './contracts'
|
|
5
5
|
import {ERROR_SYSTEM_NOT_INITIALIZED} from './errors'
|
|
6
6
|
import {ChainDefinition} from '@wharfkit/session'
|
|
7
7
|
import ContractKit, {Contract} from '@wharfkit/contract'
|
|
8
8
|
|
|
9
9
|
interface ShiploadOptions {
|
|
10
|
-
|
|
10
|
+
platformContractName?: string
|
|
11
|
+
serverContractName?: string
|
|
11
12
|
client?: APIClient
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
interface ShiploadConstructorOptions extends ShiploadOptions {
|
|
16
|
+
platformContract: Contract
|
|
15
17
|
serverContract: Contract
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
export class Shipload {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
public client: APIClient
|
|
22
|
+
public server: Contract
|
|
23
|
+
public platform: Contract
|
|
21
24
|
|
|
22
|
-
constructor(
|
|
25
|
+
constructor(
|
|
26
|
+
chain: ChainDefinition,
|
|
27
|
+
{client, platformContract, serverContract}: ShiploadConstructorOptions
|
|
28
|
+
) {
|
|
23
29
|
this.client = client || new APIClient({url: chain.url})
|
|
24
30
|
|
|
31
|
+
this.platform = platformContract
|
|
32
|
+
? platformContract
|
|
33
|
+
: new PlatformContract.Contract({client: this.client})
|
|
34
|
+
|
|
25
35
|
this.server = serverContract
|
|
26
36
|
? serverContract
|
|
27
37
|
: new ServerContract.Contract({client: this.client})
|
|
28
38
|
}
|
|
29
39
|
|
|
30
40
|
static async load(chain: ChainDefinition, shiploadOptions: ShiploadOptions): Promise<Shipload> {
|
|
41
|
+
let platform: Contract = new PlatformContract.Contract({
|
|
42
|
+
client: new APIClient({url: chain.url}),
|
|
43
|
+
})
|
|
44
|
+
if (shiploadOptions.platformContractName) {
|
|
45
|
+
const client = shiploadOptions.client || new APIClient({url: chain.url})
|
|
46
|
+
const contractKit = new ContractKit({client})
|
|
47
|
+
platform = await contractKit.load(shiploadOptions.platformContractName)
|
|
48
|
+
}
|
|
49
|
+
|
|
31
50
|
let server: Contract = new ServerContract.Contract({
|
|
32
51
|
client: new APIClient({url: chain.url}),
|
|
33
52
|
})
|
|
34
|
-
if (shiploadOptions.
|
|
53
|
+
if (shiploadOptions.serverContractName) {
|
|
35
54
|
const client = shiploadOptions.client || new APIClient({url: chain.url})
|
|
36
55
|
const contractKit = new ContractKit({client})
|
|
37
|
-
server = await contractKit.load(shiploadOptions.
|
|
56
|
+
server = await contractKit.load(shiploadOptions.serverContractName)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return new Shipload(chain, {
|
|
60
|
+
...shiploadOptions,
|
|
61
|
+
platformContract: platform,
|
|
62
|
+
serverContract: server,
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async getGame(): Promise<PlatformContract.Types.game_row> {
|
|
67
|
+
const game = await this.platform.table('games').get()
|
|
68
|
+
if (!game) {
|
|
69
|
+
throw new Error(ERROR_SYSTEM_NOT_INITIALIZED)
|
|
38
70
|
}
|
|
39
|
-
return
|
|
71
|
+
return game
|
|
40
72
|
}
|
|
41
73
|
|
|
42
74
|
async getState(): Promise<ServerContract.Types.state_row> {
|
|
@@ -48,12 +80,14 @@ export class Shipload {
|
|
|
48
80
|
}
|
|
49
81
|
|
|
50
82
|
async marketprice(location: Coordinates, good_id: number): Promise<UInt64> {
|
|
51
|
-
const
|
|
52
|
-
|
|
83
|
+
const game = await this.getGame()
|
|
84
|
+
const state = await this.getState()
|
|
85
|
+
return marketprice(location, good_id, game.config.seed, state.seed)
|
|
53
86
|
}
|
|
54
87
|
|
|
55
88
|
async marketprices(location: Coordinates): Promise<GoodPrice[]> {
|
|
56
|
-
const
|
|
57
|
-
|
|
89
|
+
const game = await this.getGame()
|
|
90
|
+
const state = await this.getState()
|
|
91
|
+
return marketprices(location, game.config.seed, state.seed)
|
|
58
92
|
}
|
|
59
93
|
}
|
package/src/travel.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
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
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import {hash512} from './hash'
|
|
5
|
+
import {Distance} from './types'
|
|
6
6
|
|
|
7
7
|
export function distanceTraveled(ship: ServerContract.Types.ship_row): number {
|
|
8
8
|
if (ship.travelplan) {
|
|
@@ -12,46 +12,69 @@ export function distanceTraveled(ship: ServerContract.Types.ship_row): number {
|
|
|
12
12
|
return 0
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export function
|
|
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 {
|
|
16
28
|
const x = Math.pow(x1 - x2, 2)
|
|
17
29
|
const y = Math.pow(y1 - y2, 2)
|
|
18
|
-
return Math.sqrt(x + y)
|
|
30
|
+
return UInt16.from(Math.sqrt(x + y))
|
|
19
31
|
}
|
|
20
32
|
|
|
21
|
-
export function lerp(
|
|
33
|
+
export function lerp(
|
|
34
|
+
origin: ServerContract.ActionParams.Type.coordinates,
|
|
35
|
+
destination: ServerContract.ActionParams.Type.coordinates,
|
|
36
|
+
time: number
|
|
37
|
+
): ServerContract.ActionParams.Type.coordinates {
|
|
22
38
|
return {
|
|
23
|
-
x: (1 - time) * origin.x + time * destination.x,
|
|
24
|
-
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),
|
|
25
41
|
}
|
|
26
42
|
}
|
|
27
43
|
|
|
28
|
-
export function rotation(
|
|
44
|
+
export function rotation(
|
|
45
|
+
origin: ServerContract.ActionParams.Type.coordinates,
|
|
46
|
+
destination: ServerContract.ActionParams.Type.coordinates
|
|
47
|
+
) {
|
|
29
48
|
return Math.atan2(destination.y - origin.y, destination.x - origin.x) * (180 / Math.PI) + 90
|
|
30
49
|
}
|
|
31
50
|
|
|
32
|
-
export function hasPlanet(
|
|
33
|
-
|
|
34
|
-
|
|
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(hash512(seed, str)).slice(0, 2) === '00'
|
|
35
57
|
}
|
|
36
58
|
|
|
37
59
|
export function findNearbyPlanets(
|
|
38
60
|
seed: Checksum256,
|
|
39
|
-
origin:
|
|
40
|
-
maxDistance = 20
|
|
61
|
+
origin: ServerContract.ActionParams.Type.coordinates,
|
|
62
|
+
maxDistance: UInt16Type = 20
|
|
41
63
|
): Distance[] {
|
|
42
64
|
const nearbySystems: Distance[] = []
|
|
43
65
|
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
const
|
|
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)
|
|
48
71
|
|
|
49
|
-
for (let x = xMin; x <= xMax; x++) {
|
|
50
|
-
for (let y = yMin; y <= yMax; y++) {
|
|
72
|
+
for (let x = Number(xMin); x <= Number(xMax); x++) {
|
|
73
|
+
for (let y = Number(yMin); y <= Number(yMax); y++) {
|
|
51
74
|
const samePlace = x === origin.x && y === origin.y
|
|
52
75
|
if (!samePlace) {
|
|
53
76
|
const distance = distanceBetweenPoints(origin.x, origin.y, x, y)
|
|
54
|
-
if (distance <=
|
|
77
|
+
if (Number(distance) <= Number(max)) {
|
|
55
78
|
const system = hasPlanet(seed, {x, y})
|
|
56
79
|
if (system) {
|
|
57
80
|
nearbySystems.push({origin, destination: {x, y}, distance})
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {UInt16, UInt64} from '@wharfkit/antelope'
|
|
2
|
+
import {ServerContract} from './contracts'
|
|
2
3
|
|
|
3
4
|
export interface Coordinates {
|
|
4
5
|
x: number
|
|
@@ -15,9 +16,9 @@ export interface Dimensions {
|
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
export interface Distance {
|
|
18
|
-
origin:
|
|
19
|
-
destination:
|
|
20
|
-
distance:
|
|
19
|
+
origin: ServerContract.ActionParams.Type.coordinates
|
|
20
|
+
destination: ServerContract.ActionParams.Type.coordinates
|
|
21
|
+
distance: UInt16
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
export interface Good {
|