feat: remove daily character seed from environment and implement random seed generation for selection
This commit is contained in:
@@ -7,8 +7,3 @@ ORIGIN=""
|
|||||||
# For production use 32 characters and generated with high entropy
|
# For production use 32 characters and generated with high entropy
|
||||||
# https://www.better-auth.com/docs/installation
|
# https://www.better-auth.com/docs/installation
|
||||||
BETTER_AUTH_SECRET=""
|
BETTER_AUTH_SECRET=""
|
||||||
|
|
||||||
# Daily Character Selection
|
|
||||||
# Seed for daily character selection (default: "onepiecedle")
|
|
||||||
# Can be any string - will be hashed to generate the rotation
|
|
||||||
DAILY_CHARACTER_SEED=onepiecedle
|
|
||||||
|
|||||||
@@ -2,21 +2,8 @@ import { db } from '$lib/server/db';
|
|||||||
import { arc, character, characterHistory, characterOverride, devilFruit } from '$lib/server/db/schema';
|
import { arc, character, characterHistory, characterOverride, devilFruit } from '$lib/server/db/schema';
|
||||||
import { desc, eq, inArray, and } from 'drizzle-orm';
|
import { desc, eq, inArray, and } from 'drizzle-orm';
|
||||||
|
|
||||||
// Get daily character seed from environment or use default
|
// Generate or get random seed for daily character selection
|
||||||
const DAILY_CHARACTER_SEED_STRING = process.env.DAILY_CHARACTER_SEED || 'onepiecedle';
|
const RANDOM_SEED = Math.random();
|
||||||
|
|
||||||
// Convert string seed to number using a simple hash function
|
|
||||||
function hashString(str: string): number {
|
|
||||||
let hash = 0;
|
|
||||||
for (let i = 0; i < str.length; i++) {
|
|
||||||
const char = str.charCodeAt(i);
|
|
||||||
hash = ((hash << 5) - hash) + char;
|
|
||||||
hash = hash & hash; // Convert to 32bit integer
|
|
||||||
}
|
|
||||||
return Math.abs(hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
const DAILY_CHARACTER_SEED = hashString(DAILY_CHARACTER_SEED_STRING);
|
|
||||||
|
|
||||||
const characterWithRelationsSelect = {
|
const characterWithRelationsSelect = {
|
||||||
id: character.id,
|
id: character.id,
|
||||||
@@ -168,8 +155,9 @@ export function normalizeDay(date: Date = new Date()): Date {
|
|||||||
function pickDailyCharacter(characters: CharacterWithRelations[], date: Date): CharacterWithRelations {
|
function pickDailyCharacter(characters: CharacterWithRelations[], date: Date): CharacterWithRelations {
|
||||||
const timestamp = getDateKey(date);
|
const timestamp = getDateKey(date);
|
||||||
const daysSinceEpoch = Math.floor(timestamp / 1000 / 60 / 60 / 24);
|
const daysSinceEpoch = Math.floor(timestamp / 1000 / 60 / 60 / 24);
|
||||||
const seed = (daysSinceEpoch + DAILY_CHARACTER_SEED) % characters.length;
|
// Combine timestamp with random seed to avoid predictable results
|
||||||
return characters[seed];
|
const combinedSeed = (daysSinceEpoch + Math.floor(RANDOM_SEED * 1000000)) % characters.length;
|
||||||
|
return characters[combinedSeed];
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getDailyModeCharacters(): Promise<CharacterWithRelations[]> {
|
export async function getDailyModeCharacters(): Promise<CharacterWithRelations[]> {
|
||||||
|
|||||||
Reference in New Issue
Block a user