@shipload/sdk 2.0.0-rc21 → 2.0.0-rc22

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shipload/sdk",
3
3
  "description": "SDKs for Shipload",
4
- "version": "2.0.0-rc21",
4
+ "version": "2.0.0-rc22",
5
5
  "homepage": "https://github.com/shipload/sdk",
6
6
  "license": "MIT",
7
7
  "main": "lib/shipload.js",
@@ -8,8 +8,8 @@ import {
8
8
  } from '../travel/travel'
9
9
  import {
10
10
  ProjectedEntity,
11
- projectEntity as sharedProjectEntity,
12
- projectEntityAt as sharedProjectEntityAt,
11
+ projectFromCurrentState as sharedProjectFromCurrentState,
12
+ projectFromCurrentStateAt as sharedProjectFromCurrentStateAt,
13
13
  } from '../scheduling/projection'
14
14
  import {Location} from './location'
15
15
  import {ScheduleAccessor} from '../scheduling/accessor'
@@ -128,11 +128,11 @@ export class Ship extends ServerContract.Types.entity_info {
128
128
  }
129
129
 
130
130
  project(): ProjectedEntity {
131
- return sharedProjectEntity(this)
131
+ return sharedProjectFromCurrentState(this)
132
132
  }
133
133
 
134
134
  projectAt(now: Date): ProjectedEntity {
135
- return sharedProjectEntityAt(this, now)
135
+ return sharedProjectFromCurrentStateAt(this, now)
136
136
  }
137
137
 
138
138
  get location(): Location {
@@ -143,9 +143,16 @@ export {
143
143
  createProjectedEntity,
144
144
  projectEntity,
145
145
  projectEntityAt,
146
+ projectFromCurrentState,
147
+ projectFromCurrentStateAt,
146
148
  validateSchedule,
147
149
  } from './scheduling/projection'
148
- export type {Projectable, ProjectedEntity, ProjectionOptions} from './scheduling/projection'
150
+ export type {
151
+ Projectable,
152
+ ProjectableSnapshot,
153
+ ProjectedEntity,
154
+ ProjectionOptions,
155
+ } from './scheduling/projection'
149
156
 
150
157
  export * from './types/capabilities'
151
158
  export * from './types/entity'
@@ -1,4 +1,4 @@
1
- import {Name, UInt16, UInt32, UInt64} from '@wharfkit/antelope'
1
+ import {Name, TimePoint, UInt16, UInt32, UInt64} from '@wharfkit/antelope'
2
2
  import {ServerContract} from '../contracts'
3
3
  import {Coordinates, PRECISION, TaskType} from '../types'
4
4
  import {
@@ -290,6 +290,38 @@ export function projectEntity(entity: Projectable, options?: ProjectionOptions):
290
290
  return projected
291
291
  }
292
292
 
293
+ export interface ProjectableSnapshot extends Projectable {
294
+ current_task?: ServerContract.Types.task
295
+ pending_tasks?: ServerContract.Types.task[]
296
+ }
297
+
298
+ function buildRemainingProjectable(snapshot: ProjectableSnapshot): Projectable | null {
299
+ if (!snapshot.schedule) return null
300
+ const remainingTasks: ServerContract.Types.task[] = []
301
+ if (snapshot.current_task) remainingTasks.push(snapshot.current_task)
302
+ if (snapshot.pending_tasks?.length) remainingTasks.push(...snapshot.pending_tasks)
303
+ if (remainingTasks.length === 0) return null
304
+
305
+ const completedCount = snapshot.schedule.tasks.length - remainingTasks.length
306
+ let startedMs = snapshot.schedule.started.toMilliseconds()
307
+ for (let i = 0; i < completedCount; i++) {
308
+ startedMs += snapshot.schedule.tasks[i].duration.toNumber() * 1000
309
+ }
310
+
311
+ return {
312
+ ...snapshot,
313
+ schedule: ServerContract.Types.schedule.from({
314
+ started: TimePoint.fromMilliseconds(startedMs),
315
+ tasks: remainingTasks,
316
+ }),
317
+ }
318
+ }
319
+
320
+ export function projectFromCurrentState(snapshot: ProjectableSnapshot): ProjectedEntity {
321
+ const projectable = buildRemainingProjectable(snapshot)
322
+ return projectable ? projectEntity(projectable) : createProjectedEntity(snapshot)
323
+ }
324
+
293
325
  function getRecipeInputsForOutput(outputItemId: number): RecipeInput[] | undefined {
294
326
  const recipe = getRecipe(outputItemId)
295
327
  return recipe?.inputs
@@ -421,3 +453,11 @@ export function projectEntityAt(entity: Projectable, now: Date): ProjectedEntity
421
453
 
422
454
  return projected
423
455
  }
456
+
457
+ export function projectFromCurrentStateAt(
458
+ snapshot: ProjectableSnapshot,
459
+ now: Date
460
+ ): ProjectedEntity {
461
+ const projectable = buildRemainingProjectable(snapshot)
462
+ return projectable ? projectEntityAt(projectable, now) : createProjectedEntity(snapshot)
463
+ }