@shipload/sdk 0.7.1 → 2.0.0-rc1
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 +1651 -226
- package/lib/shipload.js +4958 -1971
- package/lib/shipload.js.map +1 -1
- package/lib/shipload.m.js +4787 -1940
- package/lib/shipload.m.js.map +1 -1
- package/package.json +5 -4
- package/src/contracts/server.ts +585 -203
- package/src/data/goods.json +23 -0
- package/src/data/syllables.json +1184 -0
- package/src/entities/cargo-utils.ts +47 -0
- package/src/entities/entity-inventory.ts +39 -0
- package/src/entities/gamestate.ts +152 -0
- package/src/entities/location.ts +241 -0
- package/src/entities/player.ts +287 -0
- package/src/entities/ship.ts +559 -0
- package/src/entities/warehouse.ts +205 -0
- package/src/errors.ts +46 -9
- package/src/index-module.ts +119 -7
- package/src/managers/actions.ts +168 -0
- package/src/managers/base.ts +25 -0
- package/src/managers/context.ts +104 -0
- package/src/managers/entities.ts +86 -0
- package/src/managers/epochs.ts +47 -0
- package/src/managers/index.ts +9 -0
- package/src/managers/locations.ts +103 -0
- package/src/managers/players.ts +13 -0
- package/src/managers/trades.ts +119 -0
- package/src/market/goods.ts +31 -0
- package/src/{market.ts → market/market.ts} +32 -37
- package/src/{rolls.ts → market/rolls.ts} +3 -3
- package/src/{epoch.ts → scheduling/epoch.ts} +1 -1
- package/src/scheduling/projection.ts +218 -0
- package/src/scheduling/schedule.ts +155 -0
- package/src/shipload.ts +39 -157
- package/src/trading/collect.ts +939 -0
- package/src/trading/deal.ts +208 -0
- package/src/trading/trade.ts +203 -0
- package/src/travel/travel.ts +425 -0
- package/src/types.ts +60 -25
- package/src/utils/system.ts +27 -0
- package/src/goods.ts +0 -124
- package/src/ship.ts +0 -36
- package/src/state.ts +0 -0
- package/src/syllables.ts +0 -1184
- package/src/system.ts +0 -37
- package/src/travel.ts +0 -259
- /package/src/{hash.ts → utils/hash.ts} +0 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Int64,
|
|
3
|
+
Int64Type,
|
|
4
|
+
Name,
|
|
5
|
+
NameType,
|
|
6
|
+
UInt32,
|
|
7
|
+
UInt32Type,
|
|
8
|
+
UInt64,
|
|
9
|
+
UInt64Type,
|
|
10
|
+
} from '@wharfkit/antelope'
|
|
11
|
+
import {ServerContract} from '../contracts'
|
|
12
|
+
|
|
13
|
+
export interface PlayerStateInput {
|
|
14
|
+
owner: NameType
|
|
15
|
+
balance: UInt64Type
|
|
16
|
+
debt: UInt32Type
|
|
17
|
+
networth: Int64Type
|
|
18
|
+
}
|
|
19
|
+
|
|
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
|
+
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
|
+
static fromState(state: PlayerStateInput): Player {
|
|
30
|
+
const playerRow = ServerContract.Types.player_row.from({
|
|
31
|
+
owner: Name.from(state.owner),
|
|
32
|
+
balance: UInt64.from(state.balance),
|
|
33
|
+
debt: UInt32.from(state.debt),
|
|
34
|
+
networth: Int64.from(state.networth),
|
|
35
|
+
})
|
|
36
|
+
return new Player(playerRow)
|
|
37
|
+
}
|
|
38
|
+
// Constants for game rules (match smart contract)
|
|
39
|
+
private static readonly MAX_LOAN = 1000000
|
|
40
|
+
private static readonly BASE_SHIP_COST = 100 // pow(5, sequence) * 100
|
|
41
|
+
private static readonly SHIP_COST_MULTIPLIER = 5
|
|
42
|
+
|
|
43
|
+
// Optional ship count for nextShipCost calculation
|
|
44
|
+
private _shipCount?: number
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Set the current ship count (needed for nextShipCost calculation)
|
|
48
|
+
*/
|
|
49
|
+
setShipCount(count: number): void {
|
|
50
|
+
this._shipCount = count
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get the current ship count (if set)
|
|
55
|
+
*/
|
|
56
|
+
get shipCount(): number | undefined {
|
|
57
|
+
return this._shipCount
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Calculate the cost of the next ship based on current ship count
|
|
62
|
+
* Matches contract: pow(5, sequence) * 100
|
|
63
|
+
* @param shipCount - Optional ship count (uses cached value if not provided)
|
|
64
|
+
*/
|
|
65
|
+
getNextShipCost(shipCount?: number): UInt64 {
|
|
66
|
+
const count = shipCount ?? this._shipCount ?? 0
|
|
67
|
+
const cost = Math.pow(Player.SHIP_COST_MULTIPLIER, count) * Player.BASE_SHIP_COST
|
|
68
|
+
return UInt64.from(Math.floor(cost))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Get the cost of the next ship based on cached ship count
|
|
73
|
+
*/
|
|
74
|
+
get nextShipCost(): UInt64 {
|
|
75
|
+
return this.getNextShipCost()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Check if player can afford to buy a ship
|
|
80
|
+
* @param shipCount - Optional ship count (uses cached value if not provided)
|
|
81
|
+
*/
|
|
82
|
+
canBuyShip(shipCount?: number): boolean {
|
|
83
|
+
return UInt64.from(this.balance).gte(this.getNextShipCost(shipCount))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Calculate available loan amount (max loan - current debt)
|
|
88
|
+
*/
|
|
89
|
+
get availableLoan(): UInt64 {
|
|
90
|
+
const maxLoan = UInt64.from(Player.MAX_LOAN)
|
|
91
|
+
if (UInt64.from(this.debt).gte(maxLoan)) {
|
|
92
|
+
return UInt64.from(0)
|
|
93
|
+
}
|
|
94
|
+
return maxLoan.subtracting(this.debt)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Check if player can take out a loan
|
|
99
|
+
*/
|
|
100
|
+
get canTakeLoan(): boolean {
|
|
101
|
+
return this.availableLoan.gt(UInt64.zero)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Check if player can pay back loan
|
|
106
|
+
*/
|
|
107
|
+
get canPayLoan(): boolean {
|
|
108
|
+
return UInt64.from(this.debt).gt(UInt64.zero) && UInt64.from(this.balance).gt(UInt64.zero)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Calculate maximum payback amount (min of debt and balance)
|
|
113
|
+
*/
|
|
114
|
+
get maxPayback(): UInt64 {
|
|
115
|
+
return UInt64.from(this.debt).lt(this.balance) ? this.debt : this.balance
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Get the maximum loan amount (constant)
|
|
120
|
+
*/
|
|
121
|
+
static get MAX_LOAN_LIMIT(): number {
|
|
122
|
+
return Player.MAX_LOAN
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Check if player is in debt
|
|
127
|
+
*/
|
|
128
|
+
get hasDebt(): boolean {
|
|
129
|
+
return UInt64.from(this.debt).gt(UInt64.zero)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Check if player is solvent (positive networth)
|
|
134
|
+
*/
|
|
135
|
+
get isSolvent(): boolean {
|
|
136
|
+
return this.networth.gte(Int64.zero)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Create an optimistic update for balance changes
|
|
141
|
+
* Uses integer math to match contract
|
|
142
|
+
* @param delta - Amount to change (can be negative)
|
|
143
|
+
*/
|
|
144
|
+
withBalanceChange(delta: UInt64 | number): Player {
|
|
145
|
+
const newPlayer = Player.from(this)
|
|
146
|
+
const amount = typeof delta === 'number' ? UInt64.from(Math.abs(delta)) : delta
|
|
147
|
+
|
|
148
|
+
if (typeof delta === 'number' && delta < 0) {
|
|
149
|
+
// Subtract, ensuring we don't go below 0
|
|
150
|
+
newPlayer.balance = UInt64.from(this.balance).gte(amount)
|
|
151
|
+
? this.balance.subtracting(amount)
|
|
152
|
+
: UInt64.from(0)
|
|
153
|
+
} else {
|
|
154
|
+
// Add
|
|
155
|
+
newPlayer.balance = this.balance.adding(amount)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Calculate networth as Int64 (can be negative)
|
|
159
|
+
const balanceInt = Int64.from(newPlayer.balance)
|
|
160
|
+
const debtInt = Int64.from(newPlayer.debt)
|
|
161
|
+
newPlayer.networth = balanceInt.subtracting(debtInt)
|
|
162
|
+
return newPlayer
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Create an optimistic update for debt changes
|
|
167
|
+
* Uses integer math to match contract
|
|
168
|
+
* @param delta - Amount to change (can be negative)
|
|
169
|
+
*/
|
|
170
|
+
withDebtChange(delta: UInt64 | number): Player {
|
|
171
|
+
const newPlayer = Player.from(this)
|
|
172
|
+
const amount = typeof delta === 'number' ? UInt64.from(Math.abs(delta)) : delta
|
|
173
|
+
|
|
174
|
+
if (typeof delta === 'number' && delta < 0) {
|
|
175
|
+
// Subtract, ensuring we don't go below 0
|
|
176
|
+
newPlayer.debt = UInt64.from(this.debt).gte(amount)
|
|
177
|
+
? this.debt.subtracting(amount)
|
|
178
|
+
: UInt64.from(0)
|
|
179
|
+
} else {
|
|
180
|
+
// Add
|
|
181
|
+
newPlayer.debt = this.debt.adding(amount)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Calculate networth as Int64 (can be negative)
|
|
185
|
+
const balanceInt = Int64.from(newPlayer.balance)
|
|
186
|
+
const debtInt = Int64.from(newPlayer.debt)
|
|
187
|
+
newPlayer.networth = balanceInt.subtracting(debtInt)
|
|
188
|
+
return newPlayer
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Create an optimistic update for taking a loan
|
|
193
|
+
*/
|
|
194
|
+
withLoan(amount: UInt64): Player {
|
|
195
|
+
const newPlayer = Player.from(this)
|
|
196
|
+
newPlayer.balance = this.balance.adding(amount)
|
|
197
|
+
newPlayer.debt = this.debt.adding(amount)
|
|
198
|
+
// Calculate networth as Int64 (can be negative)
|
|
199
|
+
const balanceInt = Int64.from(newPlayer.balance)
|
|
200
|
+
const debtInt = Int64.from(newPlayer.debt)
|
|
201
|
+
newPlayer.networth = balanceInt.subtracting(debtInt)
|
|
202
|
+
return newPlayer
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Create an optimistic update for paying back a loan
|
|
207
|
+
*/
|
|
208
|
+
withLoanPayment(amount: UInt64): Player {
|
|
209
|
+
const actualPayment = this.maxPayback.lt(amount) ? this.maxPayback : amount
|
|
210
|
+
const newPlayer = Player.from(this)
|
|
211
|
+
newPlayer.balance = this.balance.subtracting(actualPayment)
|
|
212
|
+
newPlayer.debt = this.debt.subtracting(actualPayment)
|
|
213
|
+
// Calculate networth as Int64 (can be negative)
|
|
214
|
+
const balanceInt = Int64.from(newPlayer.balance)
|
|
215
|
+
const debtInt = Int64.from(newPlayer.debt)
|
|
216
|
+
newPlayer.networth = balanceInt.subtracting(debtInt)
|
|
217
|
+
return newPlayer
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Simulate networth update from selling goods.
|
|
222
|
+
* Matches contract: networth += (sellPrice - paid * quantity)
|
|
223
|
+
* Contract reference: market.cpp:75
|
|
224
|
+
*
|
|
225
|
+
* @param sellPrice - Total revenue from sale (price * quantity)
|
|
226
|
+
* @param paidPerUnit - Average cost per unit paid (from cargo.paid)
|
|
227
|
+
* @param quantity - Quantity being sold
|
|
228
|
+
* @returns New player with updated networth
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* // Sold 10 units at 150 each (revenue=1500), paid 100 per unit
|
|
232
|
+
* const newPlayer = player.withSaleNetworth(
|
|
233
|
+
* UInt64.from(1500),
|
|
234
|
+
* UInt64.from(100),
|
|
235
|
+
* UInt32.from(10)
|
|
236
|
+
* )
|
|
237
|
+
* // Networth increases by: 1500 - (100*10) = 500
|
|
238
|
+
*/
|
|
239
|
+
withSaleNetworth(sellPrice: UInt64, paidPerUnit: UInt64, quantity: UInt64): Player {
|
|
240
|
+
// Match contract: price - paid * quantity
|
|
241
|
+
const cost = paidPerUnit.multiplying(quantity)
|
|
242
|
+
const profit = sellPrice.gte(cost) ? sellPrice.subtracting(cost) : Int64.from(0)
|
|
243
|
+
|
|
244
|
+
const newPlayer = Player.from(this)
|
|
245
|
+
newPlayer.networth = Int64.from(this.networth).adding(profit)
|
|
246
|
+
return newPlayer
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Simulate complete sell goods transaction.
|
|
251
|
+
* Updates both balance (adds revenue) and networth (adds profit).
|
|
252
|
+
* Matches contract actions: update_balance + update_networth
|
|
253
|
+
*
|
|
254
|
+
* @param sellPrice - Total revenue from sale (price * quantity)
|
|
255
|
+
* @param paidPerUnit - Average cost per unit paid (from cargo.paid)
|
|
256
|
+
* @param quantity - Quantity being sold
|
|
257
|
+
* @returns New player with updated balance and networth
|
|
258
|
+
*/
|
|
259
|
+
withSellGoods(sellPrice: UInt64, paidPerUnit: UInt64, quantity: UInt64): Player {
|
|
260
|
+
const cost = paidPerUnit.multiplying(quantity)
|
|
261
|
+
const profit = sellPrice.gte(cost) ? sellPrice.subtracting(cost) : Int64.from(0)
|
|
262
|
+
|
|
263
|
+
const newPlayer = Player.from(this)
|
|
264
|
+
newPlayer.balance = this.balance.adding(sellPrice)
|
|
265
|
+
newPlayer.networth = Int64.from(this.networth).adding(profit)
|
|
266
|
+
return newPlayer
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Simulate complete buy goods transaction.
|
|
271
|
+
* Updates balance (subtracts cost).
|
|
272
|
+
*
|
|
273
|
+
* @param purchaseCost - Total cost of purchase (price * quantity)
|
|
274
|
+
* @returns New player with updated balance
|
|
275
|
+
*/
|
|
276
|
+
withBuyGoods(purchaseCost: UInt64): Player {
|
|
277
|
+
const newPlayer = Player.from(this)
|
|
278
|
+
newPlayer.balance = UInt64.from(this.balance).gte(purchaseCost)
|
|
279
|
+
? this.balance.subtracting(purchaseCost)
|
|
280
|
+
: UInt64.from(0)
|
|
281
|
+
// Calculate networth as Int64 (can be negative)
|
|
282
|
+
const balanceInt = Int64.from(newPlayer.balance)
|
|
283
|
+
const debtInt = Int64.from(newPlayer.debt)
|
|
284
|
+
newPlayer.networth = balanceInt.subtracting(debtInt)
|
|
285
|
+
return newPlayer
|
|
286
|
+
}
|
|
287
|
+
}
|