trucoshi 8.0.0 → 8.0.1
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.
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { RequestParams } from "lightning-accounts";
|
|
2
2
|
import { IRandom } from "../../types";
|
|
3
3
|
import { AxiosResponse } from "axios";
|
|
4
|
+
export declare const rng: IRng;
|
|
4
5
|
export declare const Random: () => IRandom;
|
|
5
6
|
export interface IRng {
|
|
6
7
|
combine(client: string, server: string, bitcoinHash: string, nonce: number): string;
|
|
@@ -8,23 +8,23 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import forge from "node-forge";
|
|
11
|
-
const
|
|
11
|
+
export const rng = Rng();
|
|
12
12
|
export const Random = () => {
|
|
13
13
|
const random = {
|
|
14
|
-
secret:
|
|
14
|
+
secret: rng.generateServerSeed(),
|
|
15
15
|
clients: [],
|
|
16
16
|
bitcoinHash: "",
|
|
17
17
|
bitcoinHeight: 0,
|
|
18
18
|
nonce: 0,
|
|
19
19
|
getLatestBitcoinBlock(fn) {
|
|
20
20
|
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
-
const { hash, height } = yield
|
|
21
|
+
const { hash, height } = yield rng.getBitcoinLatestBlockHash(fn);
|
|
22
22
|
random.bitcoinHash = hash;
|
|
23
23
|
random.bitcoinHeight = height;
|
|
24
24
|
});
|
|
25
25
|
},
|
|
26
26
|
pick(key, max) {
|
|
27
|
-
return
|
|
27
|
+
return rng.generateInteger(random.clients[key], random.secret, random.bitcoinHash, random.nonce, 0, max);
|
|
28
28
|
},
|
|
29
29
|
next() {
|
|
30
30
|
random.nonce++;
|
|
@@ -111,11 +111,18 @@ function Rng() {
|
|
|
111
111
|
* @param {number} max - The maximum value of the range.
|
|
112
112
|
* @returns {number} A random integer between min and max (inclusive).
|
|
113
113
|
*/
|
|
114
|
-
generateInteger
|
|
115
|
-
const
|
|
116
|
-
const
|
|
117
|
-
const
|
|
118
|
-
|
|
114
|
+
generateInteger(clientSeed, serverSeed, bitcoinHash, nonce, min, max) {
|
|
115
|
+
const range = BigInt(max - min + 1);
|
|
116
|
+
const maxHash = BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"); // 128-bit max (32 hex chars)
|
|
117
|
+
const threshold = maxHash - (maxHash % range); // Ensure divisibility
|
|
118
|
+
let localNonce = nonce;
|
|
119
|
+
let hash;
|
|
120
|
+
do {
|
|
121
|
+
const preHash = this.combine(clientSeed, serverSeed, bitcoinHash, localNonce);
|
|
122
|
+
hash = BigInt(`0x${this.sha512(preHash).slice(0, 32)}`); // Use 128 bits
|
|
123
|
+
localNonce++;
|
|
124
|
+
} while (hash >= threshold);
|
|
125
|
+
return Number(hash % range) + min;
|
|
119
126
|
},
|
|
120
127
|
/**
|
|
121
128
|
* Generates a random float between 0 and 1 using the fairjs library.
|
package/dist/types.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { IHand, IPlayInstance } from "./truco";
|
|
|
6
6
|
import { CARDS, ITable } from "./lib";
|
|
7
7
|
import { AxiosResponse } from "axios";
|
|
8
8
|
import { ITrucoshi } from "./server";
|
|
9
|
+
import { BotProfile } from "./truco/Bot";
|
|
9
10
|
export declare enum EMatchState {
|
|
10
11
|
UNREADY = "UNREADY",
|
|
11
12
|
READY = "READY",
|
|
@@ -283,7 +284,7 @@ export interface IPlayer {
|
|
|
283
284
|
avatarUrl: string | undefined | null;
|
|
284
285
|
name: string;
|
|
285
286
|
key: string;
|
|
286
|
-
bot:
|
|
287
|
+
bot: BotProfile | null;
|
|
287
288
|
session: string;
|
|
288
289
|
payRequestId?: number;
|
|
289
290
|
abandonedTime: number;
|
|
@@ -315,6 +316,7 @@ export interface IPlayer {
|
|
|
315
316
|
ready: boolean;
|
|
316
317
|
getRandomCard(): [number, ICard];
|
|
317
318
|
getHighestCard(): [number, ICard];
|
|
319
|
+
getLowestCard(): [number, ICard];
|
|
318
320
|
getHighestEnvido(): number;
|
|
319
321
|
saidEnvidoPoints(): void;
|
|
320
322
|
saidFlor(): void;
|