feat: implement daily character seed from environment variable and enhance selection logic with hashing
This commit is contained in:
@@ -7,3 +7,8 @@ ORIGIN=""
|
||||
# For production use 32 characters and generated with high entropy
|
||||
# https://www.better-auth.com/docs/installation
|
||||
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,6 +2,22 @@ import { db } from '$lib/server/db';
|
||||
import { arc, character, characterHistory, characterOverride, devilFruit } from '$lib/server/db/schema';
|
||||
import { desc, eq, inArray, and } from 'drizzle-orm';
|
||||
|
||||
// Get daily character seed from environment or use default
|
||||
const DAILY_CHARACTER_SEED_STRING = process.env.DAILY_CHARACTER_SEED || 'onepiecedle';
|
||||
|
||||
// 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 = {
|
||||
id: character.id,
|
||||
name: character.name,
|
||||
@@ -151,9 +167,9 @@ export function normalizeDay(date: Date = new Date()): Date {
|
||||
|
||||
function pickDailyCharacter(characters: CharacterWithRelations[], date: Date): CharacterWithRelations {
|
||||
const timestamp = getDateKey(date);
|
||||
const seed = Math.floor(timestamp / 1000 / 60 / 60 / 24);
|
||||
const index = seed % characters.length;
|
||||
return characters[index];
|
||||
const daysSinceEpoch = Math.floor(timestamp / 1000 / 60 / 60 / 24);
|
||||
const seed = (daysSinceEpoch + DAILY_CHARACTER_SEED) % characters.length;
|
||||
return characters[seed];
|
||||
}
|
||||
|
||||
export async function getDailyModeCharacters(): Promise<CharacterWithRelations[]> {
|
||||
|
||||
Reference in New Issue
Block a user