@shipload/sdk 0.3.1 → 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/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 Checksum512.hash(bytes)
5
+ return Checksum256.hash(bytes)
6
6
  }
7
7
 
8
- export function hashBytes(seed: Checksum256Type, string: string): Uint8Array {
9
- return hash(seed, string).array
8
+ export function hash512(seed: Checksum256Type, string: string): Checksum512 {
9
+ const bytes = Bytes.from(`${seed}${string}`, 'utf8')
10
+ return Checksum512.hash(bytes)
10
11
  }
@@ -1,4 +1,5 @@
1
1
  export * from './contracts'
2
+ export * from './epoch'
2
3
  export * from './hash'
3
4
  export * from './travel'
4
5
  export * from './types'
package/src/rolls.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import {Checksum256Type} from '@wharfkit/antelope'
2
- import {hashBytes} from './hash'
2
+ import {hash512} from './hash'
3
3
 
4
4
  export function roll(game_seed: Checksum256Type, roll_seed: string): number {
5
- const byteArray = hashBytes(game_seed, roll_seed)
5
+ const hash = hash512(game_seed, roll_seed)
6
6
  // Combine the first two bytes to form a uint16_t value
7
- return (byteArray[0] << 8) | byteArray[1]
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
- contractName?: string
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
- private client: APIClient
20
- private server: Contract
21
+ public client: APIClient
22
+ public server: Contract
23
+ public platform: Contract
21
24
 
22
- constructor(chain: ChainDefinition, {client, serverContract}: ShiploadConstructorOptions) {
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.contractName) {
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.contractName)
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 new Shipload(chain, {...shiploadOptions, serverContract: server})
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 {seed, epochseed} = await this.getState()
52
- return marketprice(location, good_id, seed, epochseed)
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 {seed, epochseed} = await this.getState()
57
- return marketprices(location, seed, epochseed)
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,7 +1,7 @@
1
1
  import {Checksum256, Int64, UInt16, UInt16Type, UInt64, UInt64Type} from '@wharfkit/antelope'
2
2
 
3
3
  import {ServerContract} from './contracts'
4
- import {hash} from './hash'
4
+ import {hash512} from './hash'
5
5
  import {Distance} from './types'
6
6
 
7
7
  export function distanceTraveled(ship: ServerContract.Types.ship_row): number {
@@ -53,7 +53,7 @@ export function hasPlanet(
53
53
  coordinates: ServerContract.ActionParams.Type.coordinates
54
54
  ): boolean {
55
55
  const str = ['system', coordinates.x, coordinates.y].join('-')
56
- return String(hash(seed, str)).slice(0, 2) === '00'
56
+ return String(hash512(seed, str)).slice(0, 2) === '00'
57
57
  }
58
58
 
59
59
  export function findNearbyPlanets(