feat: implement daily character seed from environment variable and enhance selection logic with hashing

This commit is contained in:
2026-03-02 12:26:11 +01:00
parent 60d2474f51
commit b70e47b0d6
2 changed files with 24 additions and 3 deletions

View File

@@ -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[]> {