@shipload/sdk 2.0.0-rc2 → 2.0.0-rc21

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 (84) hide show
  1. package/README.md +1 -349
  2. package/lib/shipload.d.ts +1729 -1127
  3. package/lib/shipload.js +7944 -3165
  4. package/lib/shipload.js.map +1 -1
  5. package/lib/shipload.m.js +7487 -2840
  6. package/lib/shipload.m.js.map +1 -1
  7. package/package.json +6 -4
  8. package/src/capabilities/crafting.ts +22 -0
  9. package/src/capabilities/gathering.ts +36 -0
  10. package/src/capabilities/guards.ts +3 -8
  11. package/src/capabilities/hauling.ts +22 -0
  12. package/src/capabilities/index.ts +4 -1
  13. package/src/capabilities/modules.ts +86 -0
  14. package/src/capabilities/storage.ts +101 -9
  15. package/src/contracts/server.ts +785 -293
  16. package/src/data/capabilities.ts +408 -0
  17. package/src/data/categories.ts +55 -0
  18. package/src/data/colors.ts +71 -0
  19. package/src/data/entities.json +50 -0
  20. package/src/data/item-ids.ts +75 -0
  21. package/src/data/items.json +252 -0
  22. package/src/data/locations.ts +53 -0
  23. package/src/data/metadata.ts +208 -0
  24. package/src/data/nebula-adjectives.json +211 -0
  25. package/src/data/nebula-nouns.json +151 -0
  26. package/src/data/recipes-runtime.ts +65 -0
  27. package/src/data/recipes.json +878 -0
  28. package/src/data/syllables.json +1386 -780
  29. package/src/data/tiers.ts +45 -0
  30. package/src/derivation/crafting.ts +348 -0
  31. package/src/derivation/index.ts +30 -0
  32. package/src/derivation/location-size.ts +15 -0
  33. package/src/derivation/resources.ts +112 -0
  34. package/src/derivation/stats.ts +146 -0
  35. package/src/derivation/stratum.ts +134 -0
  36. package/src/derivation/tiers.ts +54 -0
  37. package/src/entities/cargo-utils.ts +10 -68
  38. package/src/entities/container.ts +37 -0
  39. package/src/entities/entity-inventory.ts +13 -13
  40. package/src/entities/inventory-accessor.ts +2 -6
  41. package/src/entities/location.ts +5 -200
  42. package/src/entities/makers.ts +144 -17
  43. package/src/entities/player.ts +1 -274
  44. package/src/entities/ship-deploy.ts +258 -0
  45. package/src/entities/ship.ts +28 -34
  46. package/src/entities/warehouse.ts +35 -7
  47. package/src/errors.ts +59 -5
  48. package/src/format.ts +12 -0
  49. package/src/index-module.ts +188 -50
  50. package/src/managers/actions.ts +138 -88
  51. package/src/managers/context.ts +19 -9
  52. package/src/managers/index.ts +0 -1
  53. package/src/managers/locations.ts +2 -85
  54. package/src/market/items.ts +41 -0
  55. package/src/nft/description.ts +176 -0
  56. package/src/nft/deserializers.ts +83 -0
  57. package/src/nft/index.ts +2 -0
  58. package/src/resolution/describe-module.ts +165 -0
  59. package/src/resolution/display-name.ts +43 -0
  60. package/src/resolution/resolve-item.ts +358 -0
  61. package/src/scheduling/projection.ts +200 -67
  62. package/src/scheduling/schedule.ts +2 -2
  63. package/src/shipload.ts +10 -5
  64. package/src/subscriptions/connection.ts +154 -0
  65. package/src/subscriptions/debug.ts +17 -0
  66. package/src/subscriptions/index.ts +5 -0
  67. package/src/subscriptions/manager.ts +240 -0
  68. package/src/subscriptions/mappers.ts +28 -0
  69. package/src/subscriptions/types.ts +143 -0
  70. package/src/travel/travel.ts +37 -23
  71. package/src/types/capabilities.ts +11 -14
  72. package/src/types/entity-traits.ts +3 -4
  73. package/src/types/entity.ts +9 -6
  74. package/src/types.ts +72 -72
  75. package/src/utils/system.ts +66 -53
  76. package/src/capabilities/extraction.ts +0 -37
  77. package/src/data/goods.json +0 -23
  78. package/src/managers/trades.ts +0 -119
  79. package/src/market/goods.ts +0 -31
  80. package/src/market/market.ts +0 -208
  81. package/src/market/rolls.ts +0 -8
  82. package/src/trading/collect.ts +0 -938
  83. package/src/trading/deal.ts +0 -207
  84. package/src/trading/trade.ts +0 -203
@@ -1,35 +1,140 @@
1
- import {Name, UInt16, UInt32, UInt64} from '@wharfkit/antelope'
1
+ import {Name, UInt16, UInt32, UInt64, UInt8} from '@wharfkit/antelope'
2
2
  import {ServerContract} from '../contracts'
3
- import {Ship, ShipStateInput} from './ship'
4
- import {Warehouse, WarehouseStateInput} from './warehouse'
3
+ import {PackedModuleInput, Ship, ShipStateInput} from './ship'
4
+ import {computeWarehouseCapabilities, Warehouse, WarehouseStateInput} from './warehouse'
5
5
  import {Container, ContainerStateInput} from './container'
6
+ import {ITEM_SHIP_T1_PACKED, ITEM_WAREHOUSE_T1_PACKED} from '../data/item-ids'
7
+ import {getEntityLayout} from '../data/recipes-runtime'
8
+ import {itemMetadata} from '../data/metadata'
9
+ import {getItem} from '../market/items'
10
+ import {
11
+ getModuleCapabilityType,
12
+ MODULE_STORAGE,
13
+ moduleAccepts,
14
+ moduleSlotTypeToCode,
15
+ } from '../capabilities/modules'
16
+ import {computeShipCapabilities, computeStorageCapabilities} from './ship-deploy'
17
+ import {decodeCraftedItemStats} from '../derivation/crafting'
18
+
19
+ function assignModulesToSlots(
20
+ packedEntityItemId: number,
21
+ modules: PackedModuleInput[],
22
+ entityLabel: string
23
+ ): ServerContract.Types.module_entry[] {
24
+ const layout = getEntityLayout(packedEntityItemId)
25
+ const slots = layout?.slots ?? []
26
+ const result: Array<{type: number; installed?: ServerContract.Types.packed_module}> = slots.map(
27
+ (s) => ({type: moduleSlotTypeToCode(s.type), installed: undefined})
28
+ )
29
+
30
+ for (const mod of modules) {
31
+ const itemId = Number(UInt16.from(mod.itemId).value.toString())
32
+ const modType = getModuleCapabilityType(itemId)
33
+ const slotIdx = result.findIndex((r) => !r.installed && moduleAccepts(r.type, modType))
34
+ if (slotIdx === -1) {
35
+ let modName: string
36
+ try {
37
+ modName = getItem(itemId).name
38
+ } catch {
39
+ modName = itemMetadata[itemId]?.name ?? `item ${itemId}`
40
+ }
41
+ throw new Error(
42
+ `No compatible slot for module ${modName} (type ${modType}) on ${entityLabel}`
43
+ )
44
+ }
45
+ result[slotIdx].installed = ServerContract.Types.packed_module.from({
46
+ item_id: UInt16.from(mod.itemId),
47
+ stats: UInt64.from(mod.stats),
48
+ })
49
+ }
50
+
51
+ return result.map((r) =>
52
+ ServerContract.Types.module_entry.from({
53
+ type: UInt8.from(r.type),
54
+ installed: r.installed,
55
+ })
56
+ )
57
+ }
58
+
59
+ function decodePackedInput(m: PackedModuleInput): {itemId: number; stats: bigint} {
60
+ return {
61
+ itemId: Number(UInt16.from(m.itemId).value.toString()),
62
+ stats: BigInt(UInt64.from(m.stats).toString()),
63
+ }
64
+ }
65
+
66
+ function computeStorageBonus(
67
+ decoded: {itemId: number; stats: bigint}[],
68
+ baseCapacity: number
69
+ ): number {
70
+ let totalBonus = 0
71
+ for (const m of decoded) {
72
+ if (getModuleCapabilityType(m.itemId) !== MODULE_STORAGE) continue
73
+ const stats = decodeCraftedItemStats(m.itemId, m.stats)
74
+ const {capacityBonus} = computeStorageCapabilities(stats, baseCapacity)
75
+ totalBonus += capacityBonus
76
+ }
77
+ return totalBonus
78
+ }
79
+
80
+ function deriveShipFromModules(
81
+ modules: PackedModuleInput[],
82
+ baseCapacity: number
83
+ ): {
84
+ capabilities: ReturnType<typeof computeShipCapabilities>
85
+ finalCapacity: number
86
+ } {
87
+ const decoded = modules.map(decodePackedInput)
88
+ const capabilities = computeShipCapabilities(decoded)
89
+ const totalBonus = computeStorageBonus(decoded, baseCapacity)
90
+ return {capabilities, finalCapacity: baseCapacity + totalBonus}
91
+ }
6
92
 
7
93
  export function makeShip(state: ShipStateInput): Ship {
8
- const entityInfo = ServerContract.Types.entity_info.from({
94
+ const info: Record<string, unknown> = {
9
95
  type: Name.from('ship'),
10
96
  id: UInt64.from(state.id),
11
97
  owner: Name.from(state.owner),
12
98
  entity_name: state.name,
13
99
  coordinates: ServerContract.Types.coordinates.from(state.coordinates),
14
- hullmass: UInt32.from(state.hullmass),
15
- capacity: UInt32.from(state.capacity),
16
- energy: UInt16.from(state.energy),
17
100
  cargomass: UInt32.from(0),
18
101
  cargo: state.cargo || [],
19
102
  is_idle: !state.schedule,
20
103
  current_task_elapsed: UInt32.from(0),
21
104
  current_task_remaining: UInt32.from(0),
22
105
  pending_tasks: [],
23
- engines: state.engines,
24
- generator: state.generator,
25
- loaders: state.loaders,
26
- schedule: state.schedule,
27
- })
106
+ }
107
+ if (state.hullmass !== undefined) info.hullmass = UInt32.from(state.hullmass)
108
+ if (state.energy !== undefined) info.energy = UInt16.from(state.energy)
109
+ if (state.schedule) info.schedule = state.schedule
110
+
111
+ let moduleEntries: ServerContract.Types.module_entry[] = []
112
+ if (state.modules && state.modules.length > 0) {
113
+ moduleEntries = assignModulesToSlots(ITEM_SHIP_T1_PACKED, state.modules, 'Ship T1')
114
+ const {capabilities, finalCapacity} = deriveShipFromModules(
115
+ state.modules,
116
+ state.capacity ?? 0
117
+ )
118
+ if (capabilities.engines) info.engines = capabilities.engines
119
+ if (capabilities.generator) info.generator = capabilities.generator
120
+ if (capabilities.gatherer) info.gatherer = capabilities.gatherer
121
+ if (capabilities.hauler) info.hauler = capabilities.hauler
122
+ if (capabilities.loaders) info.loaders = capabilities.loaders
123
+ if (capabilities.crafter) info.crafter = capabilities.crafter
124
+ if (state.capacity !== undefined) info.capacity = UInt32.from(finalCapacity)
125
+ } else {
126
+ moduleEntries = assignModulesToSlots(ITEM_SHIP_T1_PACKED, [], 'Ship T1')
127
+ if (state.capacity !== undefined) info.capacity = UInt32.from(state.capacity)
128
+ }
129
+
130
+ info.modules = moduleEntries
131
+
132
+ const entityInfo = ServerContract.Types.entity_info.from(info)
28
133
  return new Ship(entityInfo)
29
134
  }
30
135
 
31
136
  export function makeWarehouse(state: WarehouseStateInput): Warehouse {
32
- const entityInfo = ServerContract.Types.entity_info.from({
137
+ const info: Record<string, unknown> = {
33
138
  type: Name.from('warehouse'),
34
139
  id: UInt64.from(state.id),
35
140
  owner: Name.from(state.owner),
@@ -38,13 +143,34 @@ export function makeWarehouse(state: WarehouseStateInput): Warehouse {
38
143
  capacity: UInt32.from(state.capacity),
39
144
  cargomass: UInt32.from(0),
40
145
  cargo: state.cargo || [],
41
- loaders: state.loaders,
42
146
  is_idle: !state.schedule,
43
147
  current_task_elapsed: UInt32.from(0),
44
148
  current_task_remaining: UInt32.from(0),
45
149
  pending_tasks: [],
46
- schedule: state.schedule,
47
- })
150
+ }
151
+ if (state.hullmass !== undefined) info.hullmass = UInt32.from(state.hullmass)
152
+ if (state.schedule) info.schedule = state.schedule
153
+
154
+ let moduleEntries: ServerContract.Types.module_entry[] = []
155
+ if (state.modules && state.modules.length > 0) {
156
+ moduleEntries = assignModulesToSlots(
157
+ ITEM_WAREHOUSE_T1_PACKED,
158
+ state.modules,
159
+ 'Warehouse T1'
160
+ )
161
+ const decoded = state.modules.map(decodePackedInput)
162
+ const capabilities = computeWarehouseCapabilities(decoded)
163
+ if (capabilities.loaders) info.loaders = capabilities.loaders
164
+
165
+ const totalBonus = computeStorageBonus(decoded, state.capacity)
166
+ info.capacity = UInt32.from(state.capacity + totalBonus)
167
+ } else {
168
+ moduleEntries = assignModulesToSlots(ITEM_WAREHOUSE_T1_PACKED, [], 'Warehouse T1')
169
+ }
170
+
171
+ info.modules = moduleEntries
172
+
173
+ const entityInfo = ServerContract.Types.entity_info.from(info)
48
174
  return new Warehouse(entityInfo)
49
175
  }
50
176
 
@@ -58,7 +184,8 @@ export function makeContainer(state: ContainerStateInput): Container {
58
184
  hullmass: UInt32.from(state.hullmass),
59
185
  capacity: UInt32.from(state.capacity),
60
186
  cargomass: UInt32.from(state.cargomass || 0),
61
- cargo: [],
187
+ cargo: state.cargo || [],
188
+ modules: [],
62
189
  is_idle: !state.schedule,
63
190
  current_task_elapsed: UInt32.from(0),
64
191
  current_task_remaining: UInt32.from(0),
@@ -1,288 +1,15 @@
1
- import {
2
- Int64,
3
- Int64Type,
4
- Name,
5
- NameType,
6
- UInt32,
7
- UInt32Type,
8
- UInt64,
9
- UInt64Type,
10
- } from '@wharfkit/antelope'
1
+ import {Name, NameType} from '@wharfkit/antelope'
11
2
  import {ServerContract} from '../contracts'
12
3
 
13
4
  export interface PlayerStateInput {
14
5
  owner: NameType
15
- balance: UInt64Type
16
- debt: UInt32Type
17
- networth: Int64Type
18
6
  }
19
7
 
20
- /**
21
- * Player helper class extending player_row with computed financial properties.
22
- * Provides easy access to balance, debt, networth, and loan calculations.
23
- */
24
8
  export class Player extends ServerContract.Types.player_row {
25
- /**
26
- * Construct a Player instance from individual state pieces.
27
- * Used by UI's ReactivePlayer to reconstruct Player from reactive state.
28
- */
29
9
  static fromState(state: PlayerStateInput): Player {
30
10
  const playerRow = ServerContract.Types.player_row.from({
31
11
  owner: Name.from(state.owner),
32
- balance: UInt64.from(state.balance),
33
- debt: UInt32.from(state.debt),
34
- networth: Int64.from(state.networth),
35
12
  })
36
13
  return new Player(playerRow)
37
14
  }
38
- // Constants for game rules (match smart contract)
39
- private static readonly MAX_LOAN = 1000000
40
- // Contract formula: 2500 * pow(5, sequence - 1) = 500 * pow(5, sequence)
41
- private static readonly BASE_SHIP_COST = 500
42
- private static readonly SHIP_COST_MULTIPLIER = 5
43
-
44
- // Optional ship count for nextShipCost calculation
45
- private _shipCount?: number
46
-
47
- /**
48
- * Set the current ship count (needed for nextShipCost calculation)
49
- */
50
- setShipCount(count: number): void {
51
- this._shipCount = count
52
- }
53
-
54
- /**
55
- * Get the current ship count (if set)
56
- */
57
- get shipCount(): number | undefined {
58
- return this._shipCount
59
- }
60
-
61
- /**
62
- * Calculate the cost of the next ship based on current ship count
63
- * Matches contract: pow(5, sequence) * 100
64
- * @param shipCount - Optional ship count (uses cached value if not provided)
65
- */
66
- getNextShipCost(shipCount?: number): UInt64 {
67
- const count = shipCount ?? this._shipCount ?? 0
68
- const cost = Math.pow(Player.SHIP_COST_MULTIPLIER, count) * Player.BASE_SHIP_COST
69
- return UInt64.from(Math.floor(cost))
70
- }
71
-
72
- /**
73
- * Get the cost of the next ship based on cached ship count
74
- */
75
- get nextShipCost(): UInt64 {
76
- return this.getNextShipCost()
77
- }
78
-
79
- /**
80
- * Check if player can afford to buy a ship
81
- * @param shipCount - Optional ship count (uses cached value if not provided)
82
- */
83
- canBuyShip(shipCount?: number): boolean {
84
- return UInt64.from(this.balance).gte(this.getNextShipCost(shipCount))
85
- }
86
-
87
- /**
88
- * Calculate available loan amount (max loan - current debt)
89
- */
90
- get availableLoan(): UInt64 {
91
- const maxLoan = UInt64.from(Player.MAX_LOAN)
92
- if (UInt64.from(this.debt).gte(maxLoan)) {
93
- return UInt64.from(0)
94
- }
95
- return maxLoan.subtracting(this.debt)
96
- }
97
-
98
- /**
99
- * Check if player can take out a loan
100
- */
101
- get canTakeLoan(): boolean {
102
- return this.availableLoan.gt(UInt64.zero)
103
- }
104
-
105
- /**
106
- * Check if player can pay back loan
107
- */
108
- get canPayLoan(): boolean {
109
- return UInt64.from(this.debt).gt(UInt64.zero) && UInt64.from(this.balance).gt(UInt64.zero)
110
- }
111
-
112
- /**
113
- * Calculate maximum payback amount (min of debt and balance)
114
- */
115
- get maxPayback(): UInt64 {
116
- return UInt64.from(this.debt).lt(this.balance) ? this.debt : this.balance
117
- }
118
-
119
- /**
120
- * Get the maximum loan amount (constant)
121
- */
122
- static get MAX_LOAN_LIMIT(): number {
123
- return Player.MAX_LOAN
124
- }
125
-
126
- /**
127
- * Check if player is in debt
128
- */
129
- get hasDebt(): boolean {
130
- return UInt64.from(this.debt).gt(UInt64.zero)
131
- }
132
-
133
- /**
134
- * Check if player is solvent (positive networth)
135
- */
136
- get isSolvent(): boolean {
137
- return this.networth.gte(Int64.zero)
138
- }
139
-
140
- /**
141
- * Create an optimistic update for balance changes
142
- * Uses integer math to match contract
143
- * @param delta - Amount to change (can be negative)
144
- */
145
- withBalanceChange(delta: UInt64 | number): Player {
146
- const newPlayer = Player.from(this)
147
- const amount = typeof delta === 'number' ? UInt64.from(Math.abs(delta)) : delta
148
-
149
- if (typeof delta === 'number' && delta < 0) {
150
- // Subtract, ensuring we don't go below 0
151
- newPlayer.balance = UInt64.from(this.balance).gte(amount)
152
- ? this.balance.subtracting(amount)
153
- : UInt64.from(0)
154
- } else {
155
- // Add
156
- newPlayer.balance = this.balance.adding(amount)
157
- }
158
-
159
- // Calculate networth as Int64 (can be negative)
160
- const balanceInt = Int64.from(newPlayer.balance)
161
- const debtInt = Int64.from(newPlayer.debt)
162
- newPlayer.networth = balanceInt.subtracting(debtInt)
163
- return newPlayer
164
- }
165
-
166
- /**
167
- * Create an optimistic update for debt changes
168
- * Uses integer math to match contract
169
- * @param delta - Amount to change (can be negative)
170
- */
171
- withDebtChange(delta: UInt64 | number): Player {
172
- const newPlayer = Player.from(this)
173
- const amount = typeof delta === 'number' ? UInt64.from(Math.abs(delta)) : delta
174
-
175
- if (typeof delta === 'number' && delta < 0) {
176
- // Subtract, ensuring we don't go below 0
177
- newPlayer.debt = UInt64.from(this.debt).gte(amount)
178
- ? this.debt.subtracting(amount)
179
- : UInt64.from(0)
180
- } else {
181
- // Add
182
- newPlayer.debt = this.debt.adding(amount)
183
- }
184
-
185
- // Calculate networth as Int64 (can be negative)
186
- const balanceInt = Int64.from(newPlayer.balance)
187
- const debtInt = Int64.from(newPlayer.debt)
188
- newPlayer.networth = balanceInt.subtracting(debtInt)
189
- return newPlayer
190
- }
191
-
192
- /**
193
- * Create an optimistic update for taking a loan
194
- */
195
- withLoan(amount: UInt64): Player {
196
- const newPlayer = Player.from(this)
197
- newPlayer.balance = this.balance.adding(amount)
198
- newPlayer.debt = this.debt.adding(amount)
199
- // Calculate networth as Int64 (can be negative)
200
- const balanceInt = Int64.from(newPlayer.balance)
201
- const debtInt = Int64.from(newPlayer.debt)
202
- newPlayer.networth = balanceInt.subtracting(debtInt)
203
- return newPlayer
204
- }
205
-
206
- /**
207
- * Create an optimistic update for paying back a loan
208
- */
209
- withLoanPayment(amount: UInt64): Player {
210
- const actualPayment = this.maxPayback.lt(amount) ? this.maxPayback : amount
211
- const newPlayer = Player.from(this)
212
- newPlayer.balance = this.balance.subtracting(actualPayment)
213
- newPlayer.debt = this.debt.subtracting(actualPayment)
214
- // Calculate networth as Int64 (can be negative)
215
- const balanceInt = Int64.from(newPlayer.balance)
216
- const debtInt = Int64.from(newPlayer.debt)
217
- newPlayer.networth = balanceInt.subtracting(debtInt)
218
- return newPlayer
219
- }
220
-
221
- /**
222
- * Simulate networth update from selling goods.
223
- * Matches contract: networth += (sellPrice - paid * quantity)
224
- * Contract reference: market.cpp:75
225
- *
226
- * @param sellPrice - Total revenue from sale (price * quantity)
227
- * @param paidPerUnit - Average cost per unit paid (from cargo.paid)
228
- * @param quantity - Quantity being sold
229
- * @returns New player with updated networth
230
- *
231
- * @example
232
- * // Sold 10 units at 150 each (revenue=1500), paid 100 per unit
233
- * const newPlayer = player.withSaleNetworth(
234
- * UInt64.from(1500),
235
- * UInt64.from(100),
236
- * UInt32.from(10)
237
- * )
238
- * // Networth increases by: 1500 - (100*10) = 500
239
- */
240
- withSaleNetworth(sellPrice: UInt64, paidPerUnit: UInt64, quantity: UInt64): Player {
241
- // Match contract: price - paid * quantity
242
- const cost = paidPerUnit.multiplying(quantity)
243
- const profit = sellPrice.gte(cost) ? sellPrice.subtracting(cost) : Int64.from(0)
244
-
245
- const newPlayer = Player.from(this)
246
- newPlayer.networth = Int64.from(this.networth).adding(profit)
247
- return newPlayer
248
- }
249
-
250
- /**
251
- * Simulate complete sell goods transaction.
252
- * Updates both balance (adds revenue) and networth (adds profit).
253
- * Matches contract actions: update_balance + update_networth
254
- *
255
- * @param sellPrice - Total revenue from sale (price * quantity)
256
- * @param paidPerUnit - Average cost per unit paid (from cargo.paid)
257
- * @param quantity - Quantity being sold
258
- * @returns New player with updated balance and networth
259
- */
260
- withSellGoods(sellPrice: UInt64, paidPerUnit: UInt64, quantity: UInt64): Player {
261
- const cost = paidPerUnit.multiplying(quantity)
262
- const profit = sellPrice.gte(cost) ? sellPrice.subtracting(cost) : Int64.from(0)
263
-
264
- const newPlayer = Player.from(this)
265
- newPlayer.balance = this.balance.adding(sellPrice)
266
- newPlayer.networth = Int64.from(this.networth).adding(profit)
267
- return newPlayer
268
- }
269
-
270
- /**
271
- * Simulate complete buy goods transaction.
272
- * Updates balance (subtracts cost).
273
- *
274
- * @param purchaseCost - Total cost of purchase (price * quantity)
275
- * @returns New player with updated balance
276
- */
277
- withBuyGoods(purchaseCost: UInt64): Player {
278
- const newPlayer = Player.from(this)
279
- newPlayer.balance = UInt64.from(this.balance).gte(purchaseCost)
280
- ? this.balance.subtracting(purchaseCost)
281
- : UInt64.from(0)
282
- // Calculate networth as Int64 (can be negative)
283
- const balanceInt = Int64.from(newPlayer.balance)
284
- const debtInt = Int64.from(newPlayer.debt)
285
- newPlayer.networth = balanceInt.subtracting(debtInt)
286
- return newPlayer
287
- }
288
15
  }