chore: improve distribution of rewardSeed (#1831)

This was previously not ideal due to float imprecision, but now it's 64 bits and there's enough entropy for all of them.

Reviewed-on: OpenWF/SpaceNinjaServer#1831
Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com>
Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
This commit is contained in:
Sainan 2025-04-25 11:53:54 -07:00 committed by Sainan
parent fd7f4c9e92
commit 0b75757277
3 changed files with 12 additions and 7 deletions

View File

@ -390,7 +390,7 @@ MailboxSchema.set("toJSON", {
const DuviriInfoSchema = new Schema<IDuviriInfo>( const DuviriInfoSchema = new Schema<IDuviriInfo>(
{ {
Seed: Number, Seed: BigInt,
NumCompletions: { type: Number, default: 0 } NumCompletions: { type: Number, default: 0 }
}, },
{ {

View File

@ -120,10 +120,15 @@ export const createInventory = async (
} }
}; };
export const generateRewardSeed = (): number => { export const generateRewardSeed = (): bigint => {
const min = -Number.MAX_SAFE_INTEGER; const hiDword = getRandomInt(0, 0x7fffffff);
const max = Number.MAX_SAFE_INTEGER; const loDword = getRandomInt(0, 0xffffffff);
return Math.floor(Math.random() * (max - min + 1)) + min; let seed = (BigInt(hiDword) << 32n) | BigInt(loDword);
if (Math.random() < 0.5) {
seed *= -1n;
seed -= 1n;
}
return seed;
}; };
//TODO: RawUpgrades might need to return a LastAdded //TODO: RawUpgrades might need to return a LastAdded

View File

@ -134,7 +134,7 @@ export const equipmentKeys = [
export type TEquipmentKey = (typeof equipmentKeys)[number]; export type TEquipmentKey = (typeof equipmentKeys)[number];
export interface IDuviriInfo { export interface IDuviriInfo {
Seed: number; Seed: bigint;
NumCompletions: number; NumCompletions: number;
} }
@ -202,7 +202,7 @@ export interface IInventoryClient extends IDailyAffiliations, InventoryClientEqu
Mailbox?: IMailboxClient; Mailbox?: IMailboxClient;
SubscribedToEmails: number; SubscribedToEmails: number;
Created: IMongoDate; Created: IMongoDate;
RewardSeed: number | bigint; RewardSeed: bigint;
RegularCredits: number; RegularCredits: number;
PremiumCredits: number; PremiumCredits: number;
PremiumCreditsFree: number; PremiumCreditsFree: number;