mirror of
https://github.com/tiennm99/loldle.git
synced 2026-09-12 02:19:59 +00:00
Vanilla JS web game with modular architecture: - data-loader: champion data fetching, seeded random selection - game-engine: mode-agnostic state machine with localStorage persistence - classic-mode: 7-attribute comparison logic (gender, genre, range, resource, region, lane, year) - ui-renderer: search autocomplete, colored feedback grid, stats display - Dark theme responsive UI with Data Dragon CDN champion images
53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
// Data layer: load, search, and select champions from champions.json
|
|
|
|
let champions = [];
|
|
|
|
/** Fetch and parse champions data */
|
|
export async function loadChampions() {
|
|
const response = await fetch("assets/champions.json");
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to load champions: ${response.status}`);
|
|
}
|
|
champions = await response.json();
|
|
return champions;
|
|
}
|
|
|
|
/** Get all loaded champions */
|
|
export function getAllChampions() {
|
|
return champions;
|
|
}
|
|
|
|
/** Case-insensitive lookup by name */
|
|
export function getChampionByName(name) {
|
|
const lower = name.toLowerCase();
|
|
return champions.find((c) => c.name.toLowerCase() === lower) || null;
|
|
}
|
|
|
|
|
|
/** Seeded random champion selection (deterministic for same seed string) */
|
|
export function getRandomChampion(seed) {
|
|
if (!champions.length) return null;
|
|
const hash = hashString(seed);
|
|
const index = hash % champions.length;
|
|
return champions[index];
|
|
}
|
|
|
|
/** Simple string hash (djb2) */
|
|
function hashString(str) {
|
|
let hash = 5381;
|
|
for (let i = 0; i < str.length; i++) {
|
|
hash = (hash * 33) ^ str.charCodeAt(i);
|
|
}
|
|
return hash >>> 0;
|
|
}
|
|
|
|
/** Get today's date string for daily seed */
|
|
export function getTodaySeed() {
|
|
return new Date().toISOString().slice(0, 10);
|
|
}
|
|
|
|
/** Get champion image URL from Data Dragon CDN */
|
|
export function getChampionImageUrl(championId) {
|
|
return `https://ddragon.leagueoflegends.com/cdn/img/champion/tiles/${championId}_0.jpg`;
|
|
}
|