@pokertools/sdk 1.0.4
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/README.md +199 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/index.cjs +1126 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +750 -0
- package/dist/index.d.ts +750 -0
- package/dist/index.js +1096 -0
- package/dist/index.js.map +1 -0
- package/dist/react/index.cjs +1099 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +683 -0
- package/dist/react/index.d.ts +683 -0
- package/dist/react/index.js +1090 -0
- package/dist/react/index.js.map +1 -0
- package/package.json +82 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1096 @@
|
|
|
1
|
+
import { safeParseServerMessage } from '@pokertools/types';
|
|
2
|
+
|
|
3
|
+
// src/types.ts
|
|
4
|
+
var PokerSDKError = class extends Error {
|
|
5
|
+
constructor(message, code, statusCode, details) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.statusCode = statusCode;
|
|
9
|
+
this.details = details;
|
|
10
|
+
this.name = "PokerSDKError";
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// src/client.ts
|
|
15
|
+
var DEFAULT_CONFIG = {
|
|
16
|
+
timeout: 3e4,
|
|
17
|
+
retry: {
|
|
18
|
+
count: 3,
|
|
19
|
+
delay: 1e3,
|
|
20
|
+
backoff: 2
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var PokerClient = class {
|
|
24
|
+
constructor(config) {
|
|
25
|
+
this.baseUrl = config.baseUrl.replace(/\/$/, "");
|
|
26
|
+
this.timeout = config.timeout ?? DEFAULT_CONFIG.timeout;
|
|
27
|
+
this.retry = {
|
|
28
|
+
count: config.retry?.count ?? DEFAULT_CONFIG.retry.count,
|
|
29
|
+
delay: config.retry?.delay ?? DEFAULT_CONFIG.retry.delay,
|
|
30
|
+
backoff: config.retry?.backoff ?? DEFAULT_CONFIG.retry.backoff
|
|
31
|
+
};
|
|
32
|
+
this.fetchFn = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
33
|
+
this.debug = config.debug ?? false;
|
|
34
|
+
this.token = config.token ?? null;
|
|
35
|
+
}
|
|
36
|
+
// ============================================================================
|
|
37
|
+
// Configuration
|
|
38
|
+
// ============================================================================
|
|
39
|
+
/**
|
|
40
|
+
* Set the authentication token
|
|
41
|
+
*/
|
|
42
|
+
setToken(token) {
|
|
43
|
+
this.token = token;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Get current token
|
|
47
|
+
*/
|
|
48
|
+
getToken() {
|
|
49
|
+
return this.token;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Check if client is authenticated
|
|
53
|
+
*/
|
|
54
|
+
isAuthenticated() {
|
|
55
|
+
return this.token !== null;
|
|
56
|
+
}
|
|
57
|
+
// ============================================================================
|
|
58
|
+
// Authentication
|
|
59
|
+
// ============================================================================
|
|
60
|
+
/**
|
|
61
|
+
* Get a nonce for SIWE authentication
|
|
62
|
+
*/
|
|
63
|
+
async getNonce() {
|
|
64
|
+
const response = await this.request("POST", "/auth/nonce");
|
|
65
|
+
return response.nonce;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Login with SIWE signature
|
|
69
|
+
*/
|
|
70
|
+
async login(request) {
|
|
71
|
+
const response = await this.request("POST", "/auth/login", request);
|
|
72
|
+
this.token = response.token;
|
|
73
|
+
return response;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Logout and revoke session
|
|
77
|
+
*/
|
|
78
|
+
async logout() {
|
|
79
|
+
await this.request("POST", "/auth/logout");
|
|
80
|
+
this.token = null;
|
|
81
|
+
}
|
|
82
|
+
// ============================================================================
|
|
83
|
+
// Tables
|
|
84
|
+
// ============================================================================
|
|
85
|
+
/**
|
|
86
|
+
* Get list of active tables
|
|
87
|
+
*/
|
|
88
|
+
async getTables() {
|
|
89
|
+
const response = await this.request("GET", "/tables");
|
|
90
|
+
return response.tables;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Create a new table
|
|
94
|
+
*/
|
|
95
|
+
async createTable(config) {
|
|
96
|
+
const response = await this.request("POST", "/tables", config);
|
|
97
|
+
return response.tableId;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Get table state
|
|
101
|
+
* @param tableId - Table ID
|
|
102
|
+
* @param since - Optional version for conditional fetch (returns null if unchanged)
|
|
103
|
+
*/
|
|
104
|
+
async getTableState(tableId, since) {
|
|
105
|
+
const query = since !== void 0 ? `?since=${since}` : "";
|
|
106
|
+
try {
|
|
107
|
+
const response = await this.request(
|
|
108
|
+
"GET",
|
|
109
|
+
`/tables/${tableId}${query}`
|
|
110
|
+
);
|
|
111
|
+
return response.state;
|
|
112
|
+
} catch (error) {
|
|
113
|
+
if (error instanceof PokerSDKError && error.statusCode === 304) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Buy in to a table
|
|
121
|
+
*/
|
|
122
|
+
async buyIn(tableId, request) {
|
|
123
|
+
await this.request("POST", `/tables/${tableId}/buy-in`, request);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Execute a game action
|
|
127
|
+
*/
|
|
128
|
+
async action(tableId, request) {
|
|
129
|
+
const response = await this.request(
|
|
130
|
+
"POST",
|
|
131
|
+
`/tables/${tableId}/action`,
|
|
132
|
+
request
|
|
133
|
+
);
|
|
134
|
+
return response.state;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Add chips to stack (rebuy/top-up)
|
|
138
|
+
*/
|
|
139
|
+
async addChips(tableId, request) {
|
|
140
|
+
await this.request("POST", `/tables/${tableId}/add-chips`, request);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Stand from table (leave and cash out)
|
|
144
|
+
*/
|
|
145
|
+
async stand(tableId) {
|
|
146
|
+
await this.request("POST", `/tables/${tableId}/stand`);
|
|
147
|
+
}
|
|
148
|
+
// ============================================================================
|
|
149
|
+
// Convenience Action Methods
|
|
150
|
+
// ============================================================================
|
|
151
|
+
/**
|
|
152
|
+
* Fold hand
|
|
153
|
+
*/
|
|
154
|
+
async fold(tableId) {
|
|
155
|
+
return this.action(tableId, { type: "FOLD" });
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Check (pass action)
|
|
159
|
+
*/
|
|
160
|
+
async check(tableId) {
|
|
161
|
+
return this.action(tableId, { type: "CHECK" });
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Call current bet
|
|
165
|
+
*/
|
|
166
|
+
async call(tableId) {
|
|
167
|
+
return this.action(tableId, { type: "CALL" });
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Place a bet
|
|
171
|
+
*/
|
|
172
|
+
async bet(tableId, amount) {
|
|
173
|
+
return this.action(tableId, { type: "BET", amount });
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Raise the current bet
|
|
177
|
+
*/
|
|
178
|
+
async raise(tableId, amount) {
|
|
179
|
+
return this.action(tableId, { type: "RAISE", amount });
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Deal new hand
|
|
183
|
+
*/
|
|
184
|
+
async deal(tableId) {
|
|
185
|
+
return this.action(tableId, { type: "DEAL" });
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Show cards at showdown
|
|
189
|
+
*/
|
|
190
|
+
async show(tableId, cardIndices) {
|
|
191
|
+
return this.action(tableId, { type: "SHOW", cardIndices });
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Muck cards at showdown
|
|
195
|
+
*/
|
|
196
|
+
async muck(tableId) {
|
|
197
|
+
return this.action(tableId, { type: "MUCK" });
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Use time bank
|
|
201
|
+
*/
|
|
202
|
+
async timeBank(tableId) {
|
|
203
|
+
return this.action(tableId, { type: "TIME_BANK" });
|
|
204
|
+
}
|
|
205
|
+
// ============================================================================
|
|
206
|
+
// User
|
|
207
|
+
// ============================================================================
|
|
208
|
+
/**
|
|
209
|
+
* Get current user profile and balances
|
|
210
|
+
*/
|
|
211
|
+
async getProfile() {
|
|
212
|
+
return this.request("GET", "/user/me");
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Get hand history
|
|
216
|
+
*/
|
|
217
|
+
async getHandHistory() {
|
|
218
|
+
const response = await this.request("GET", "/user/history");
|
|
219
|
+
return response.history;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Request a withdrawal
|
|
223
|
+
*/
|
|
224
|
+
async withdraw(request) {
|
|
225
|
+
return this.request("POST", "/user/withdraw", request);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Get withdrawal history
|
|
229
|
+
*/
|
|
230
|
+
async getWithdrawals() {
|
|
231
|
+
const response = await this.request(
|
|
232
|
+
"GET",
|
|
233
|
+
"/user/withdrawals"
|
|
234
|
+
);
|
|
235
|
+
return response.withdrawals;
|
|
236
|
+
}
|
|
237
|
+
// ============================================================================
|
|
238
|
+
// Finance
|
|
239
|
+
// ============================================================================
|
|
240
|
+
/**
|
|
241
|
+
* Get supported blockchains and tokens
|
|
242
|
+
*/
|
|
243
|
+
async getChains() {
|
|
244
|
+
return this.request("GET", "/finance/chains");
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Start deposit monitoring session
|
|
248
|
+
*/
|
|
249
|
+
async startDeposit() {
|
|
250
|
+
return this.request("POST", "/finance/deposit/start");
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Get deposit address
|
|
254
|
+
*/
|
|
255
|
+
async getDepositAddress() {
|
|
256
|
+
const response = await this.request("GET", "/finance/deposit/address");
|
|
257
|
+
return response.address;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Get deposit history
|
|
261
|
+
*/
|
|
262
|
+
async getDeposits() {
|
|
263
|
+
const response = await this.request("GET", "/finance/deposits");
|
|
264
|
+
return response.deposits;
|
|
265
|
+
}
|
|
266
|
+
// ============================================================================
|
|
267
|
+
// Notes
|
|
268
|
+
// ============================================================================
|
|
269
|
+
/**
|
|
270
|
+
* Get all notes by current user
|
|
271
|
+
*/
|
|
272
|
+
async getNotes() {
|
|
273
|
+
const response = await this.request("GET", "/notes");
|
|
274
|
+
return response.notes;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Get note for specific player
|
|
278
|
+
*/
|
|
279
|
+
async getNote(targetId) {
|
|
280
|
+
const response = await this.request("GET", `/notes/${targetId}`);
|
|
281
|
+
return response.note;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Save or update note
|
|
285
|
+
*/
|
|
286
|
+
async saveNote(targetId, content, label) {
|
|
287
|
+
const response = await this.request("POST", "/notes", {
|
|
288
|
+
targetId,
|
|
289
|
+
content,
|
|
290
|
+
label
|
|
291
|
+
});
|
|
292
|
+
return response.note;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Delete note
|
|
296
|
+
*/
|
|
297
|
+
async deleteNote(targetId) {
|
|
298
|
+
await this.request("DELETE", `/notes/${targetId}`);
|
|
299
|
+
}
|
|
300
|
+
// ============================================================================
|
|
301
|
+
// Health
|
|
302
|
+
// ============================================================================
|
|
303
|
+
/**
|
|
304
|
+
* Health check
|
|
305
|
+
*/
|
|
306
|
+
async health() {
|
|
307
|
+
return this.request("GET", "/health");
|
|
308
|
+
}
|
|
309
|
+
// ============================================================================
|
|
310
|
+
// Private Methods
|
|
311
|
+
// ============================================================================
|
|
312
|
+
/**
|
|
313
|
+
* Make HTTP request with retry logic
|
|
314
|
+
*/
|
|
315
|
+
async request(method, path, body) {
|
|
316
|
+
const url = `${this.baseUrl}${path}`;
|
|
317
|
+
const headers = {
|
|
318
|
+
"Content-Type": "application/json"
|
|
319
|
+
};
|
|
320
|
+
if (this.token) {
|
|
321
|
+
headers.Authorization = `Bearer ${this.token}`;
|
|
322
|
+
}
|
|
323
|
+
let lastError = null;
|
|
324
|
+
for (let attempt = 0; attempt <= this.retry.count; attempt++) {
|
|
325
|
+
try {
|
|
326
|
+
const controller = new AbortController();
|
|
327
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
328
|
+
if (this.debug) {
|
|
329
|
+
console.log(`[PokerSDK] ${method} ${path}`, body);
|
|
330
|
+
}
|
|
331
|
+
const response = await this.fetchFn(url, {
|
|
332
|
+
method,
|
|
333
|
+
headers,
|
|
334
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
335
|
+
signal: controller.signal
|
|
336
|
+
});
|
|
337
|
+
clearTimeout(timeoutId);
|
|
338
|
+
if (response.status === 304) {
|
|
339
|
+
throw new PokerSDKError("Not Modified", "NOT_MODIFIED", 304);
|
|
340
|
+
}
|
|
341
|
+
if (!response.ok) {
|
|
342
|
+
const errorData = await response.json().catch(() => ({}));
|
|
343
|
+
throw new PokerSDKError(
|
|
344
|
+
errorData.message ?? errorData.error ?? `HTTP ${response.status}`,
|
|
345
|
+
errorData.code ?? errorData.error ?? "HTTP_ERROR",
|
|
346
|
+
response.status,
|
|
347
|
+
errorData
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
const data = await response.json();
|
|
351
|
+
if (this.debug) {
|
|
352
|
+
console.log(`[PokerSDK] Response:`, data);
|
|
353
|
+
}
|
|
354
|
+
return data;
|
|
355
|
+
} catch (error) {
|
|
356
|
+
lastError = error;
|
|
357
|
+
if (error instanceof PokerSDKError) {
|
|
358
|
+
if (error.statusCode && error.statusCode >= 400 && error.statusCode < 500 && error.statusCode !== 429) {
|
|
359
|
+
throw error;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
363
|
+
throw new PokerSDKError("Request timeout", "TIMEOUT", void 0, {
|
|
364
|
+
timeout: this.timeout
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
if (attempt < this.retry.count) {
|
|
368
|
+
const delay = this.retry.delay * Math.pow(this.retry.backoff, attempt);
|
|
369
|
+
if (this.debug) {
|
|
370
|
+
console.log(`[PokerSDK] Retry ${attempt + 1}/${this.retry.count} in ${delay}ms`);
|
|
371
|
+
}
|
|
372
|
+
await this.sleep(delay);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
throw lastError ?? new PokerSDKError("Request failed", "REQUEST_FAILED");
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Sleep helper
|
|
380
|
+
*/
|
|
381
|
+
sleep(ms) {
|
|
382
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
var DEFAULT_SOCKET_CONFIG = {
|
|
386
|
+
heartbeatInterval: 25e3,
|
|
387
|
+
reconnectAttempts: 10,
|
|
388
|
+
reconnectDelay: 1e3,
|
|
389
|
+
maxReconnectDelay: 3e4
|
|
390
|
+
};
|
|
391
|
+
var PokerSocket = class _PokerSocket {
|
|
392
|
+
constructor(config) {
|
|
393
|
+
this.ws = null;
|
|
394
|
+
this.connectionState = "disconnected";
|
|
395
|
+
this.reconnectCount = 0;
|
|
396
|
+
this.heartbeatTimer = null;
|
|
397
|
+
this.pendingRequests = /* @__PURE__ */ new Map();
|
|
398
|
+
this.joinedTables = /* @__PURE__ */ new Set();
|
|
399
|
+
this.listeners = /* @__PURE__ */ new Map();
|
|
400
|
+
this.shouldReconnect = true;
|
|
401
|
+
// Latest state cache for each table
|
|
402
|
+
this.stateCache = /* @__PURE__ */ new Map();
|
|
403
|
+
const wsUrl = new URL(config.url);
|
|
404
|
+
wsUrl.searchParams.set("token", config.token);
|
|
405
|
+
this.url = wsUrl.toString();
|
|
406
|
+
this.token = config.token;
|
|
407
|
+
this.heartbeatInterval = config.heartbeatInterval ?? DEFAULT_SOCKET_CONFIG.heartbeatInterval;
|
|
408
|
+
this.reconnectAttempts = config.reconnectAttempts ?? DEFAULT_SOCKET_CONFIG.reconnectAttempts;
|
|
409
|
+
this.reconnectDelay = config.reconnectDelay ?? DEFAULT_SOCKET_CONFIG.reconnectDelay;
|
|
410
|
+
this.maxReconnectDelay = config.maxReconnectDelay ?? DEFAULT_SOCKET_CONFIG.maxReconnectDelay;
|
|
411
|
+
this.WebSocketImpl = config.WebSocket ?? globalThis.WebSocket;
|
|
412
|
+
this.debug = config.debug ?? false;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Create a PokerSocket from SDK config
|
|
416
|
+
*/
|
|
417
|
+
static fromConfig(config) {
|
|
418
|
+
if (!config.token) {
|
|
419
|
+
throw new PokerSDKError("Token is required for WebSocket connection", "AUTH_REQUIRED");
|
|
420
|
+
}
|
|
421
|
+
const baseUrl = config.baseUrl.replace(/\/$/, "");
|
|
422
|
+
const wsUrl = config.wsUrl ?? baseUrl.replace(/^http/, "ws") + "/ws/play";
|
|
423
|
+
return new _PokerSocket({
|
|
424
|
+
url: wsUrl,
|
|
425
|
+
token: config.token,
|
|
426
|
+
WebSocket: config.WebSocket,
|
|
427
|
+
debug: config.debug
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
// ============================================================================
|
|
431
|
+
// Connection Management
|
|
432
|
+
// ============================================================================
|
|
433
|
+
/**
|
|
434
|
+
* Connect to the WebSocket server
|
|
435
|
+
*/
|
|
436
|
+
connect() {
|
|
437
|
+
return new Promise((resolve, reject) => {
|
|
438
|
+
if (this.connectionState === "connected") {
|
|
439
|
+
resolve();
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
if (this.connectionState === "connecting") {
|
|
443
|
+
const checkConnection = () => {
|
|
444
|
+
if (this.connectionState === "connected") {
|
|
445
|
+
resolve();
|
|
446
|
+
} else if (this.connectionState === "disconnected") {
|
|
447
|
+
reject(new PokerSDKError("Connection failed", "CONNECTION_FAILED"));
|
|
448
|
+
} else {
|
|
449
|
+
setTimeout(checkConnection, 100);
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
checkConnection();
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
this.shouldReconnect = true;
|
|
456
|
+
this.connectionState = "connecting";
|
|
457
|
+
this.log("Connecting to", this.url);
|
|
458
|
+
try {
|
|
459
|
+
this.ws = new this.WebSocketImpl(this.url);
|
|
460
|
+
this.ws.onopen = () => {
|
|
461
|
+
this.connectionState = "connected";
|
|
462
|
+
this.reconnectCount = 0;
|
|
463
|
+
this.startHeartbeat();
|
|
464
|
+
this.emit("connect");
|
|
465
|
+
this.log("Connected");
|
|
466
|
+
void this.rejoinTables();
|
|
467
|
+
resolve();
|
|
468
|
+
};
|
|
469
|
+
this.ws.onclose = (event) => {
|
|
470
|
+
this.handleDisconnect(event.reason || "Connection closed");
|
|
471
|
+
};
|
|
472
|
+
this.ws.onerror = (event) => {
|
|
473
|
+
this.log("WebSocket error:", event);
|
|
474
|
+
if (this.connectionState === "connecting") {
|
|
475
|
+
reject(new PokerSDKError("Connection failed", "CONNECTION_FAILED"));
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
this.ws.onmessage = (event) => {
|
|
479
|
+
this.handleMessage(event.data);
|
|
480
|
+
};
|
|
481
|
+
} catch (error) {
|
|
482
|
+
this.connectionState = "disconnected";
|
|
483
|
+
reject(error);
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Disconnect from the WebSocket server
|
|
489
|
+
*/
|
|
490
|
+
disconnect() {
|
|
491
|
+
this.shouldReconnect = false;
|
|
492
|
+
this.stopHeartbeat();
|
|
493
|
+
this.clearPendingRequests("Connection closed");
|
|
494
|
+
if (this.ws) {
|
|
495
|
+
this.ws.close(1e3, "Client disconnect");
|
|
496
|
+
this.ws = null;
|
|
497
|
+
}
|
|
498
|
+
this.connectionState = "disconnected";
|
|
499
|
+
this.emit("disconnect", "Client disconnect");
|
|
500
|
+
this.log("Disconnected");
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Get current connection state
|
|
504
|
+
*/
|
|
505
|
+
getState() {
|
|
506
|
+
return this.connectionState;
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* Check if connected
|
|
510
|
+
*/
|
|
511
|
+
isConnected() {
|
|
512
|
+
return this.connectionState === "connected";
|
|
513
|
+
}
|
|
514
|
+
// ============================================================================
|
|
515
|
+
// Table Subscription
|
|
516
|
+
// ============================================================================
|
|
517
|
+
/**
|
|
518
|
+
* Join a table to receive real-time updates
|
|
519
|
+
*/
|
|
520
|
+
async join(tableId) {
|
|
521
|
+
if (this.connectionState !== "connected") {
|
|
522
|
+
throw new PokerSDKError("Not connected", "NOT_CONNECTED");
|
|
523
|
+
}
|
|
524
|
+
const requestId = this.generateRequestId();
|
|
525
|
+
const message = {
|
|
526
|
+
type: "JOIN",
|
|
527
|
+
tableId,
|
|
528
|
+
requestId
|
|
529
|
+
};
|
|
530
|
+
this.joinedTables.add(tableId);
|
|
531
|
+
this.send(message);
|
|
532
|
+
return new Promise((resolve, reject) => {
|
|
533
|
+
const timeout = setTimeout(() => {
|
|
534
|
+
this.pendingRequests.delete(requestId);
|
|
535
|
+
reject(new PokerSDKError("Join timeout", "TIMEOUT"));
|
|
536
|
+
}, 1e4);
|
|
537
|
+
this.pendingRequests.set(`snapshot:${tableId}`, {
|
|
538
|
+
resolve: (state) => {
|
|
539
|
+
clearTimeout(timeout);
|
|
540
|
+
resolve(state);
|
|
541
|
+
},
|
|
542
|
+
reject,
|
|
543
|
+
timeout
|
|
544
|
+
});
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Leave a table
|
|
549
|
+
*/
|
|
550
|
+
leave(tableId) {
|
|
551
|
+
if (this.connectionState !== "connected") {
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
this.joinedTables.delete(tableId);
|
|
555
|
+
this.stateCache.delete(tableId);
|
|
556
|
+
const message = {
|
|
557
|
+
type: "LEAVE",
|
|
558
|
+
tableId
|
|
559
|
+
};
|
|
560
|
+
this.send(message);
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Get currently joined tables
|
|
564
|
+
*/
|
|
565
|
+
getJoinedTables() {
|
|
566
|
+
return Array.from(this.joinedTables);
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Get cached state for a table
|
|
570
|
+
*/
|
|
571
|
+
getCachedState(tableId) {
|
|
572
|
+
return this.stateCache.get(tableId);
|
|
573
|
+
}
|
|
574
|
+
// ============================================================================
|
|
575
|
+
// Event Handling
|
|
576
|
+
// ============================================================================
|
|
577
|
+
/**
|
|
578
|
+
* Subscribe to an event
|
|
579
|
+
*/
|
|
580
|
+
on(event, listener) {
|
|
581
|
+
if (!this.listeners.has(event)) {
|
|
582
|
+
this.listeners.set(event, /* @__PURE__ */ new Set());
|
|
583
|
+
}
|
|
584
|
+
this.listeners.get(event).add(listener);
|
|
585
|
+
return () => {
|
|
586
|
+
this.off(event, listener);
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Unsubscribe from an event
|
|
591
|
+
*/
|
|
592
|
+
off(event, listener) {
|
|
593
|
+
const listeners = this.listeners.get(event);
|
|
594
|
+
if (listeners) {
|
|
595
|
+
listeners.delete(listener);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Subscribe to an event (once)
|
|
600
|
+
*/
|
|
601
|
+
once(event, listener) {
|
|
602
|
+
const onceWrapper = ((...args) => {
|
|
603
|
+
this.off(event, onceWrapper);
|
|
604
|
+
listener(...args);
|
|
605
|
+
});
|
|
606
|
+
return this.on(event, onceWrapper);
|
|
607
|
+
}
|
|
608
|
+
// ============================================================================
|
|
609
|
+
// Ping
|
|
610
|
+
// ============================================================================
|
|
611
|
+
/**
|
|
612
|
+
* Send application-level ping (not WebSocket ping)
|
|
613
|
+
*/
|
|
614
|
+
async ping() {
|
|
615
|
+
if (this.connectionState !== "connected") {
|
|
616
|
+
throw new PokerSDKError("Not connected", "NOT_CONNECTED");
|
|
617
|
+
}
|
|
618
|
+
const requestId = this.generateRequestId();
|
|
619
|
+
const startTime = Date.now();
|
|
620
|
+
const message = {
|
|
621
|
+
type: "PING",
|
|
622
|
+
requestId,
|
|
623
|
+
timestamp: startTime
|
|
624
|
+
};
|
|
625
|
+
this.send(message);
|
|
626
|
+
return new Promise((resolve, reject) => {
|
|
627
|
+
const timeout = setTimeout(() => {
|
|
628
|
+
this.pendingRequests.delete(requestId);
|
|
629
|
+
reject(new PokerSDKError("Ping timeout", "TIMEOUT"));
|
|
630
|
+
}, 5e3);
|
|
631
|
+
this.pendingRequests.set(requestId, {
|
|
632
|
+
resolve: () => {
|
|
633
|
+
resolve(Date.now() - startTime);
|
|
634
|
+
},
|
|
635
|
+
reject,
|
|
636
|
+
timeout
|
|
637
|
+
});
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
// ============================================================================
|
|
641
|
+
// Private Methods
|
|
642
|
+
// ============================================================================
|
|
643
|
+
/**
|
|
644
|
+
* Send a message to the server
|
|
645
|
+
*/
|
|
646
|
+
send(message) {
|
|
647
|
+
if (this.ws?.readyState !== this.WebSocketImpl.OPEN) {
|
|
648
|
+
throw new PokerSDKError("WebSocket not open", "NOT_CONNECTED");
|
|
649
|
+
}
|
|
650
|
+
this.log("Sending:", message);
|
|
651
|
+
this.ws.send(JSON.stringify(message));
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Handle incoming message
|
|
655
|
+
*/
|
|
656
|
+
handleMessage(data) {
|
|
657
|
+
try {
|
|
658
|
+
const parsed = JSON.parse(data);
|
|
659
|
+
const result = safeParseServerMessage(parsed);
|
|
660
|
+
if (!result.success) {
|
|
661
|
+
this.log("Invalid server message:", result.error);
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
const message = result.data;
|
|
665
|
+
this.log("Received:", message);
|
|
666
|
+
switch (message.type) {
|
|
667
|
+
case "SNAPSHOT": {
|
|
668
|
+
this.stateCache.set(message.tableId, message.state);
|
|
669
|
+
const pending = this.pendingRequests.get(`snapshot:${message.tableId}`);
|
|
670
|
+
if (pending) {
|
|
671
|
+
this.pendingRequests.delete(`snapshot:${message.tableId}`);
|
|
672
|
+
pending.resolve(message.state);
|
|
673
|
+
}
|
|
674
|
+
this.emit("snapshot", message.tableId, message.state);
|
|
675
|
+
break;
|
|
676
|
+
}
|
|
677
|
+
case "STATE_UPDATE": {
|
|
678
|
+
const cachedState = this.stateCache.get(message.tableId);
|
|
679
|
+
if (cachedState) {
|
|
680
|
+
const updatedState = { ...cachedState, version: message.version };
|
|
681
|
+
this.stateCache.set(message.tableId, updatedState);
|
|
682
|
+
this.emit("stateUpdate", message.tableId, updatedState);
|
|
683
|
+
} else {
|
|
684
|
+
this.emit("stateUpdate", message.tableId, {
|
|
685
|
+
version: message.version
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
break;
|
|
689
|
+
}
|
|
690
|
+
case "ACTION": {
|
|
691
|
+
this.emit(
|
|
692
|
+
"action",
|
|
693
|
+
message.tableId,
|
|
694
|
+
message.playerId,
|
|
695
|
+
message.actionType,
|
|
696
|
+
message.amount
|
|
697
|
+
);
|
|
698
|
+
break;
|
|
699
|
+
}
|
|
700
|
+
case "ACK": {
|
|
701
|
+
const pending = this.pendingRequests.get(message.requestId);
|
|
702
|
+
if (pending) {
|
|
703
|
+
clearTimeout(pending.timeout);
|
|
704
|
+
this.pendingRequests.delete(message.requestId);
|
|
705
|
+
pending.resolve(void 0);
|
|
706
|
+
}
|
|
707
|
+
break;
|
|
708
|
+
}
|
|
709
|
+
case "PONG": {
|
|
710
|
+
const pending = this.pendingRequests.get(message.requestId);
|
|
711
|
+
if (pending) {
|
|
712
|
+
clearTimeout(pending.timeout);
|
|
713
|
+
this.pendingRequests.delete(message.requestId);
|
|
714
|
+
pending.resolve(message.timestamp);
|
|
715
|
+
}
|
|
716
|
+
break;
|
|
717
|
+
}
|
|
718
|
+
case "ERROR": {
|
|
719
|
+
this.log("Server error:", message);
|
|
720
|
+
if (message.requestId) {
|
|
721
|
+
const pending = this.pendingRequests.get(message.requestId);
|
|
722
|
+
if (pending) {
|
|
723
|
+
clearTimeout(pending.timeout);
|
|
724
|
+
this.pendingRequests.delete(message.requestId);
|
|
725
|
+
pending.reject(new PokerSDKError(message.message, message.code));
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
this.emit("error", new PokerSDKError(message.message, message.code));
|
|
729
|
+
break;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
} catch (error) {
|
|
733
|
+
this.log("Failed to parse message:", error);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* Handle disconnect
|
|
738
|
+
*/
|
|
739
|
+
handleDisconnect(reason) {
|
|
740
|
+
this.stopHeartbeat();
|
|
741
|
+
this.ws = null;
|
|
742
|
+
const wasConnected = this.connectionState === "connected";
|
|
743
|
+
this.connectionState = "disconnected";
|
|
744
|
+
if (wasConnected) {
|
|
745
|
+
this.emit("disconnect", reason);
|
|
746
|
+
}
|
|
747
|
+
if (this.shouldReconnect && this.reconnectCount < this.reconnectAttempts) {
|
|
748
|
+
void this.reconnect();
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* Attempt to reconnect
|
|
753
|
+
*/
|
|
754
|
+
async reconnect() {
|
|
755
|
+
this.reconnectCount++;
|
|
756
|
+
this.connectionState = "reconnecting";
|
|
757
|
+
const delay = Math.min(
|
|
758
|
+
this.reconnectDelay * Math.pow(2, this.reconnectCount - 1),
|
|
759
|
+
this.maxReconnectDelay
|
|
760
|
+
);
|
|
761
|
+
this.log(
|
|
762
|
+
`Reconnecting in ${delay}ms (attempt ${this.reconnectCount}/${this.reconnectAttempts})`
|
|
763
|
+
);
|
|
764
|
+
this.emit("reconnect", this.reconnectCount);
|
|
765
|
+
await this.sleep(delay);
|
|
766
|
+
if (!this.shouldReconnect) {
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
try {
|
|
770
|
+
await this.connect();
|
|
771
|
+
} catch {
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
/**
|
|
775
|
+
* Rejoin previously joined tables
|
|
776
|
+
*/
|
|
777
|
+
async rejoinTables() {
|
|
778
|
+
for (const tableId of this.joinedTables) {
|
|
779
|
+
try {
|
|
780
|
+
await this.join(tableId);
|
|
781
|
+
} catch (error) {
|
|
782
|
+
this.log("Failed to rejoin table:", tableId, error);
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
/**
|
|
787
|
+
* Start heartbeat timer
|
|
788
|
+
*/
|
|
789
|
+
startHeartbeat() {
|
|
790
|
+
this.stopHeartbeat();
|
|
791
|
+
this.heartbeatTimer = setInterval(() => {
|
|
792
|
+
if (this.connectionState === "connected") {
|
|
793
|
+
void this.ping().catch(() => {
|
|
794
|
+
this.log("Heartbeat failed");
|
|
795
|
+
});
|
|
796
|
+
}
|
|
797
|
+
}, this.heartbeatInterval);
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Stop heartbeat timer
|
|
801
|
+
*/
|
|
802
|
+
stopHeartbeat() {
|
|
803
|
+
if (this.heartbeatTimer) {
|
|
804
|
+
clearInterval(this.heartbeatTimer);
|
|
805
|
+
this.heartbeatTimer = null;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
/**
|
|
809
|
+
* Clear all pending requests
|
|
810
|
+
*/
|
|
811
|
+
clearPendingRequests(reason) {
|
|
812
|
+
for (const pending of this.pendingRequests.values()) {
|
|
813
|
+
clearTimeout(pending.timeout);
|
|
814
|
+
pending.reject(new PokerSDKError(reason, "CONNECTION_CLOSED"));
|
|
815
|
+
}
|
|
816
|
+
this.pendingRequests.clear();
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Emit event to listeners
|
|
820
|
+
*/
|
|
821
|
+
emit(event, ...args) {
|
|
822
|
+
const listeners = this.listeners.get(event);
|
|
823
|
+
if (listeners) {
|
|
824
|
+
for (const listener of listeners) {
|
|
825
|
+
try {
|
|
826
|
+
listener(...args);
|
|
827
|
+
} catch (error) {
|
|
828
|
+
this.log("Listener error:", error);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
/**
|
|
834
|
+
* Generate unique request ID
|
|
835
|
+
*/
|
|
836
|
+
generateRequestId() {
|
|
837
|
+
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
838
|
+
}
|
|
839
|
+
/**
|
|
840
|
+
* Sleep helper
|
|
841
|
+
*/
|
|
842
|
+
sleep(ms) {
|
|
843
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
844
|
+
}
|
|
845
|
+
/**
|
|
846
|
+
* Debug logger
|
|
847
|
+
*/
|
|
848
|
+
log(...args) {
|
|
849
|
+
if (this.debug) {
|
|
850
|
+
console.log("[PokerSocket]", ...args);
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
};
|
|
854
|
+
|
|
855
|
+
// src/auth.ts
|
|
856
|
+
function createSiweMessage(params) {
|
|
857
|
+
const {
|
|
858
|
+
domain,
|
|
859
|
+
address,
|
|
860
|
+
statement,
|
|
861
|
+
uri,
|
|
862
|
+
version = "1",
|
|
863
|
+
chainId = 1,
|
|
864
|
+
nonce,
|
|
865
|
+
issuedAt = (/* @__PURE__ */ new Date()).toISOString(),
|
|
866
|
+
expirationTime,
|
|
867
|
+
notBefore,
|
|
868
|
+
requestId,
|
|
869
|
+
resources
|
|
870
|
+
} = params;
|
|
871
|
+
const lines = [];
|
|
872
|
+
lines.push(`${domain} wants you to sign in with your Ethereum account:`);
|
|
873
|
+
lines.push(address);
|
|
874
|
+
if (statement) {
|
|
875
|
+
lines.push("");
|
|
876
|
+
lines.push(statement);
|
|
877
|
+
}
|
|
878
|
+
lines.push("");
|
|
879
|
+
lines.push(`URI: ${uri}`);
|
|
880
|
+
lines.push(`Version: ${version}`);
|
|
881
|
+
lines.push(`Chain ID: ${chainId}`);
|
|
882
|
+
lines.push(`Nonce: ${nonce}`);
|
|
883
|
+
lines.push(`Issued At: ${issuedAt}`);
|
|
884
|
+
if (expirationTime) {
|
|
885
|
+
lines.push(`Expiration Time: ${expirationTime}`);
|
|
886
|
+
}
|
|
887
|
+
if (notBefore) {
|
|
888
|
+
lines.push(`Not Before: ${notBefore}`);
|
|
889
|
+
}
|
|
890
|
+
if (requestId) {
|
|
891
|
+
lines.push(`Request ID: ${requestId}`);
|
|
892
|
+
}
|
|
893
|
+
if (resources && resources.length > 0) {
|
|
894
|
+
lines.push(`Resources:`);
|
|
895
|
+
for (const resource of resources) {
|
|
896
|
+
lines.push(`- ${resource}`);
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
return lines.join("\n");
|
|
900
|
+
}
|
|
901
|
+
function parseSiweMessage(message) {
|
|
902
|
+
const lines = message.split("\n");
|
|
903
|
+
const result = {};
|
|
904
|
+
const domainMatch = /^(.+) wants you to sign in with your Ethereum account:$/.exec(lines[0]);
|
|
905
|
+
if (domainMatch) {
|
|
906
|
+
result.domain = domainMatch[1];
|
|
907
|
+
}
|
|
908
|
+
if (lines[1]) {
|
|
909
|
+
result.address = lines[1];
|
|
910
|
+
}
|
|
911
|
+
for (const line of lines) {
|
|
912
|
+
if (line.startsWith("URI: ")) {
|
|
913
|
+
result.uri = line.slice(5);
|
|
914
|
+
} else if (line.startsWith("Version: ")) {
|
|
915
|
+
result.version = line.slice(9);
|
|
916
|
+
} else if (line.startsWith("Chain ID: ")) {
|
|
917
|
+
result.chainId = parseInt(line.slice(10), 10);
|
|
918
|
+
} else if (line.startsWith("Nonce: ")) {
|
|
919
|
+
result.nonce = line.slice(7);
|
|
920
|
+
} else if (line.startsWith("Issued At: ")) {
|
|
921
|
+
result.issuedAt = line.slice(11);
|
|
922
|
+
} else if (line.startsWith("Expiration Time: ")) {
|
|
923
|
+
result.expirationTime = line.slice(17);
|
|
924
|
+
} else if (line.startsWith("Not Before: ")) {
|
|
925
|
+
result.notBefore = line.slice(12);
|
|
926
|
+
} else if (line.startsWith("Request ID: ")) {
|
|
927
|
+
result.requestId = line.slice(12);
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
const uriIndex = lines.findIndex((l) => l.startsWith("URI: "));
|
|
931
|
+
if (uriIndex > 3) {
|
|
932
|
+
const statementLines = lines.slice(3, uriIndex - 1).filter((l) => l.trim());
|
|
933
|
+
if (statementLines.length > 0) {
|
|
934
|
+
result.statement = statementLines.join("\n");
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
return result;
|
|
938
|
+
}
|
|
939
|
+
function isSiweExpired(message) {
|
|
940
|
+
const parsed = parseSiweMessage(message);
|
|
941
|
+
if (!parsed.expirationTime) {
|
|
942
|
+
return false;
|
|
943
|
+
}
|
|
944
|
+
return new Date(parsed.expirationTime) < /* @__PURE__ */ new Date();
|
|
945
|
+
}
|
|
946
|
+
function createWithdrawalMessage(amount, destinationAddress) {
|
|
947
|
+
return `Withdraw ${amount} USD to ${destinationAddress}`;
|
|
948
|
+
}
|
|
949
|
+
function generateIdempotencyKey() {
|
|
950
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
951
|
+
return crypto.randomUUID();
|
|
952
|
+
}
|
|
953
|
+
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}-${Math.random().toString(36).substr(2, 9)}`;
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
// src/utils.ts
|
|
957
|
+
function formatChips(chips, currency = "$") {
|
|
958
|
+
const dollars = chips / 100;
|
|
959
|
+
return `${currency}${dollars.toFixed(2)}`;
|
|
960
|
+
}
|
|
961
|
+
function parseChips(amount) {
|
|
962
|
+
const cleaned = amount.replace(/[$€£¥,\s]/g, "");
|
|
963
|
+
const value = parseFloat(cleaned);
|
|
964
|
+
if (isNaN(value)) {
|
|
965
|
+
throw new Error(`Invalid amount: ${amount}`);
|
|
966
|
+
}
|
|
967
|
+
if (Number.isInteger(value) && value >= 100) {
|
|
968
|
+
return value;
|
|
969
|
+
}
|
|
970
|
+
return Math.round(value * 100);
|
|
971
|
+
}
|
|
972
|
+
function getActivePlayer(state) {
|
|
973
|
+
if (state.actionTo === null) {
|
|
974
|
+
return null;
|
|
975
|
+
}
|
|
976
|
+
return state.players[state.actionTo] ?? null;
|
|
977
|
+
}
|
|
978
|
+
function getPlayerById(state, playerId) {
|
|
979
|
+
return state.players.find((p) => p?.id === playerId) ?? null;
|
|
980
|
+
}
|
|
981
|
+
function getPlayerSeat(state, playerId) {
|
|
982
|
+
const index = state.players.findIndex((p) => p?.id === playerId);
|
|
983
|
+
return index === -1 ? null : index;
|
|
984
|
+
}
|
|
985
|
+
function isPlayerTurn(state, playerId) {
|
|
986
|
+
if (state.actionTo === null) {
|
|
987
|
+
return false;
|
|
988
|
+
}
|
|
989
|
+
const player = state.players[state.actionTo];
|
|
990
|
+
return player?.id === playerId;
|
|
991
|
+
}
|
|
992
|
+
function getCallAmount(state, playerId) {
|
|
993
|
+
const player = getPlayerById(state, playerId);
|
|
994
|
+
if (!player) {
|
|
995
|
+
return 0;
|
|
996
|
+
}
|
|
997
|
+
const currentBet = player.betThisStreet;
|
|
998
|
+
const activePlayers = state.players.filter((p) => p !== null);
|
|
999
|
+
const bets = activePlayers.map((p) => p.betThisStreet);
|
|
1000
|
+
const highestBet = Math.max(...bets);
|
|
1001
|
+
return Math.min(highestBet - currentBet, player.stack);
|
|
1002
|
+
}
|
|
1003
|
+
function getMinRaise(state) {
|
|
1004
|
+
return state.minRaise ?? state.config.bigBlind;
|
|
1005
|
+
}
|
|
1006
|
+
function canCheck(state, playerId) {
|
|
1007
|
+
const player = getPlayerById(state, playerId);
|
|
1008
|
+
if (!player || !isPlayerTurn(state, playerId)) {
|
|
1009
|
+
return false;
|
|
1010
|
+
}
|
|
1011
|
+
const currentBet = player.betThisStreet;
|
|
1012
|
+
const activePlayers = state.players.filter((p) => p !== null);
|
|
1013
|
+
const bets = activePlayers.map((p) => p.betThisStreet);
|
|
1014
|
+
const highestBet = Math.max(...bets);
|
|
1015
|
+
return currentBet >= highestBet;
|
|
1016
|
+
}
|
|
1017
|
+
function canBet(state, playerId) {
|
|
1018
|
+
const player = getPlayerById(state, playerId);
|
|
1019
|
+
if (!player || !isPlayerTurn(state, playerId)) {
|
|
1020
|
+
return false;
|
|
1021
|
+
}
|
|
1022
|
+
const activePlayers = state.players.filter((p) => p !== null);
|
|
1023
|
+
const bets = activePlayers.map((p) => p.betThisStreet);
|
|
1024
|
+
const highestBet = Math.max(...bets);
|
|
1025
|
+
return highestBet === 0 && player.stack > 0;
|
|
1026
|
+
}
|
|
1027
|
+
function getTotalPot(state) {
|
|
1028
|
+
return state.pots.reduce((sum, pot) => sum + pot.amount, 0);
|
|
1029
|
+
}
|
|
1030
|
+
function getActivePlayers(state) {
|
|
1031
|
+
return state.players.filter(
|
|
1032
|
+
(p) => p !== null && p.status !== "FOLDED" && p.stack > 0
|
|
1033
|
+
);
|
|
1034
|
+
}
|
|
1035
|
+
function getPlayersInHand(state) {
|
|
1036
|
+
return state.players.filter((p) => p !== null && p.status !== "FOLDED");
|
|
1037
|
+
}
|
|
1038
|
+
function suitToEmoji(suit) {
|
|
1039
|
+
const suits = {
|
|
1040
|
+
s: "\u2660",
|
|
1041
|
+
h: "\u2665",
|
|
1042
|
+
d: "\u2666",
|
|
1043
|
+
c: "\u2663"
|
|
1044
|
+
};
|
|
1045
|
+
return suits[suit.toLowerCase()] ?? suit;
|
|
1046
|
+
}
|
|
1047
|
+
function formatCard(card) {
|
|
1048
|
+
if (card.length !== 2) {
|
|
1049
|
+
return card;
|
|
1050
|
+
}
|
|
1051
|
+
const rank = card[0].toUpperCase();
|
|
1052
|
+
const suit = suitToEmoji(card[1]);
|
|
1053
|
+
return `${rank}${suit}`;
|
|
1054
|
+
}
|
|
1055
|
+
function formatCards(cards) {
|
|
1056
|
+
if (!cards) {
|
|
1057
|
+
return "\u{1F0A0}\u{1F0A0}";
|
|
1058
|
+
}
|
|
1059
|
+
return cards.map((c) => c ? formatCard(c) : "\u{1F0A0}").join(" ");
|
|
1060
|
+
}
|
|
1061
|
+
function getStreetName(street) {
|
|
1062
|
+
const names = {
|
|
1063
|
+
PREFLOP: "Pre-Flop",
|
|
1064
|
+
FLOP: "Flop",
|
|
1065
|
+
TURN: "Turn",
|
|
1066
|
+
RIVER: "River",
|
|
1067
|
+
SHOWDOWN: "Showdown"
|
|
1068
|
+
};
|
|
1069
|
+
return names[street] ?? street;
|
|
1070
|
+
}
|
|
1071
|
+
function isShowdown(state) {
|
|
1072
|
+
return state.street === "SHOWDOWN";
|
|
1073
|
+
}
|
|
1074
|
+
function isHandComplete(state) {
|
|
1075
|
+
return state.winners !== void 0 && state.winners !== null;
|
|
1076
|
+
}
|
|
1077
|
+
function getPotOdds(state, playerId) {
|
|
1078
|
+
const callAmount = getCallAmount(state, playerId);
|
|
1079
|
+
if (callAmount === 0) {
|
|
1080
|
+
return Infinity;
|
|
1081
|
+
}
|
|
1082
|
+
return getTotalPot(state) / callAmount;
|
|
1083
|
+
}
|
|
1084
|
+
function abbreviateNumber(num) {
|
|
1085
|
+
if (num >= 1e6) {
|
|
1086
|
+
return `${(num / 1e6).toFixed(1)}M`;
|
|
1087
|
+
}
|
|
1088
|
+
if (num >= 1e3) {
|
|
1089
|
+
return `${(num / 1e3).toFixed(1)}K`;
|
|
1090
|
+
}
|
|
1091
|
+
return num.toString();
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
export { PokerClient, PokerSDKError, PokerSocket, abbreviateNumber, canBet, canCheck, createSiweMessage, createWithdrawalMessage, formatCard, formatCards, formatChips, generateIdempotencyKey, getActivePlayer, getActivePlayers, getCallAmount, getMinRaise, getPlayerById, getPlayerSeat, getPlayersInHand, getPotOdds, getStreetName, getTotalPot, isHandComplete, isPlayerTurn, isShowdown, isSiweExpired, parseChips, parseSiweMessage, suitToEmoji };
|
|
1095
|
+
//# sourceMappingURL=index.js.map
|
|
1096
|
+
//# sourceMappingURL=index.js.map
|