@ -2,114 +2,163 @@
'use strict' ;
const data = window . RAVENOUS _CRAFT _CATALOG || { entries : [ ] , themes : [ ] } ;
const $ = id => document . getElementById ( id ) ;
const CATALOG _HASH = '{{CATALOG_HASH}}' ;
const CUSTOM _KEY = 'ravenous-craft-custom-ingredients' ;
const RECIPES _KEY = 'ravenous-craft-recipes' ;
const state = { shown : 30 , selected : [ ] , custom : readLocal ( CUSTOM _KEY , [ ] ) } ;
const THEME _KEY = 'ravenous-craft-theme' ;
const state = { shown : 30 , selected : [ ] , custom : readLocal ( CUSTOM _KEY , [ ] ) , editingId : null , fileMode : '' } ;
let catalog = [ ... data . entries , ... state . custom ] ;
const numeric = value => value == null ? 0 : Number ( value ) || 0 ;
const optionalNumber = id => $ ( id ) . value === '' ? null : numeric ( $ ( id ) . value ) ;
const fold = value => String ( value || '' ) . normalize ( 'NFD' ) . replace ( /[\u0300-\u036f]/g , '' ) . toLowerCase ( ) ;
const stars = value => { const n = numeric ( value ) , a = Math . abs ( n ) ; return ` ${ n < 0 ? '-' : '' } ${ a > 3 ? a : '★' . repeat ( a ) } ${ a === 0 || a > 3 ? '★' : '' } ` ; } ;
const stars = value => { const n = numeric ( value ) , a = Math . abs ( n ) ; return ` ${ n < 0 ? '-' : '' } ${ a > 3 ? a : '★' . repeat ( a ) } ${ a === 0 || a > 3 ? '★' : '' } ` ; } ;
const text = item => Object . values ( item . fields || { } ) . join ( ' ' ) ;
const uid = prefix => ` ${ prefix } : ${ Date . now ( ) . toString ( 36 ) } : ${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 8 ) } ` ;
function readLocal ( key , fallback ) {
try { const value = JSON . parse ( localStorage . getItem ( key ) || 'null' ) ; return Array . isArray ( value ) ? value : fallback ; }
try { const value = JSON . parse ( localStorage . getItem ( key ) || 'null' ) ; return Array . isArray ( value ) ? value : fallback ; }
catch ( _ ) { return fallback ; }
}
function writeLocal ( key , value ) {
try { localStorage . setItem ( key , JSON . stringify ( value ) ) ; return true ; }
catch ( _ ) { return false ; }
try { localStorage . setItem ( key , JSON . stringify ( value ) ) ; return true ; }
catch ( _ ) { status ( 'Stockage local indisponible ou saturé.' , true ) ; return false ; }
}
function status ( message , bad = false ) { $ ( 'action-status' ) . textContent = message ; $ ( 'action-status' ) . classList . toggle ( 'bad' , bad ) ; }
function effect ( item ) {
const entries = Object . entries ( item . fields || { } ) . filter ( ( [ key ] ) => ! /^(Nom|Matériau|Réagents|Solvants|Type)$/i . test ( key ) ) ;
return entries . slice ( - 2 ) . map ( ( [ key , value ] ) => ` ${ key } : ${ value } ` ) . join ( ' · ' ) || 'Détails disponibles dans la source.' ;
const entries = Object . entries ( item . fields || { } ) . filter ( ( [ key ] ) => ! /^(Nom|Matériau|Réagents|Solvants|Type)$/i . test ( key ) ) ;
return entries . slice ( - 2 ) . map ( ( [ key , value ] ) => ` ${ key } : ${ value } ` ) . join ( ' · ' ) || 'Détails disponibles dans la source.' ;
}
function selectedDiscipline ( ) { return state . selected [ 0 ] ? . discipline || '' ; }
function totals ( ) {
const sum = key => state . selected . reduce ( ( total , item ) => total + numeric ( item [ key ] ) , 0 ) ;
return { tier : Math . max ( 0 , ... state . selected . map ( i => numeric ( i . tier ) ) ) , cost : sum ( 'cost' ) , difficulty : sum ( 'difficulty' ) , toxicity : sum ( 'toxicity' ) , finishing : sum ( 'finishing' ) } ;
}
function validation ( ) {
const value = totals ( ) , discipline = selectedDiscipline ( ) , errors = [ ] , warnings = [ ] ;
if ( ! state . selected . length ) errors . push ( 'Ajoutez au moins un composant.' ) ;
if ( new Set ( state . selected . map ( item => item . discipline ) ) . size > 1 ) errors . push ( 'Une recette ne peut contenir qu’ une discipline.' ) ;
if ( value . tier > numeric ( $ ( 'mastery' ) . value ) ) warnings . push ( ` Tier ${ value . tier } supérieur à la maîtrise ${ numeric ( $ ( 'mastery' ) . value ) } . ` ) ;
if ( value . difficulty > numeric ( $ ( 'craft-level' ) . textContent ) ) warnings . push ( ` Difficulté ${ value . difficulty } supérieure au niveau ${ $ ( 'craft-level' ) . textContent } . ` ) ;
if ( value . finishing > numeric ( $ ( 'finishing-limit' ) . value ) ) warnings . push ( ` Finition ${ value . finishing } supérieure à la spécialité ${ numeric ( $ ( 'finishing-limit' ) . value ) } . ` ) ;
if ( state . selected . some ( item => item . custom ) ) warnings . push ( 'Cette recette contient des ingrédients personnels à faire valider par le MJ.' ) ;
if ( discipline === 'Forge' || discipline === 'Pièges' ) warnings . push ( 'Les statistiques finales et compatibilités complexes restent à vérifier selon le livre.' ) ;
return { errors , warnings , value } ;
}
function selectedDiscipline ( ) { return state . selected [ 0 ] ? . discipline || '' ; }
function filtered ( ) {
const query = fold ( $ ( 'search' ) . value ) , discipline = $ ( 'discipline' ) . value , stage = $ ( 'stage' ) . value ;
const limits = [ [ 'tier' , 'tier' ] , [ 'difficulty' , 'difficulty' ] , [ 'toxicity' , 'toxicity' ] ] ;
const entries = catalog . filter ( item => ( ! discipline || item . discipline === discipline ) && ( ! stage || item . stage === stage ) &&
( ! query || fold ( ` ${ item . name } ${ item . stage } ${ text ( item ) } ` ) . includes ( query ) ) && limits . every ( ( [ id , key ] ) => ! $ ( id ) . value || item [ key ] == null || numeric ( item [ key ] ) <= numeric ( $ ( id ) . value ) ) ) ;
const sort = $ ( 'sort' ) . value ;
entries . sort ( ( a , b ) => sort === 'name' ? a . name . localeCompare ( b . name , 'fr' ) : numeric ( a [ sort ] ) - numeric ( b [ sort ] ) || a . name . localeCompare ( b . name , 'fr' ) ) ;
const query = fold ( $ ( 'search' ) . value ) , discipline = $ ( 'discipline' ) . value , stage = $ ( 'stage' ) . value ;
const limits = [ [ 'tier' , 'tier '] , [ 'cost' , 'cost '] , [ 'difficulty' , 'difficulty' ] , [ 'toxicity' , 'toxicity '] , [ 'finishing' , 'finishing '] ] ;
const entries = catalog . filter ( item => ( ! discipline || item . discipline === discipline ) && ( ! stage || item . stage === stage ) &&
( ! query || fold ( ` ${ item . name } ${ item . stage } ${ text ( item ) } ` ) . includes ( query ) ) && limits . every ( ( [ id , key ] ) => ! $ ( id ) . value || item [ key ] == null || numeric ( item [ key ] ) <= numeric ( $ ( id ) . value ) ) ) ;
const sort = $ ( 'sort' ) . value ;
entries . sort ( ( a , b ) => sort === 'name' ? a . name . localeCompare ( b . name , 'fr' ) : numeric ( a [ sort ] ) - numeric ( b [ sort ] ) || a . name . localeCompare ( b . name , 'fr' ) ) ;
return entries ;
}
function renderStages ( ) {
const current = $ ( 'stage' ) . value , discipline = $ ( 'discipline' ) . value ;
const stages = [ ... new Set ( catalog . filter ( i => ! discipline || i . discipline === discipline ) . map ( i => i . stage ) ) ] . sort ( ) ;
$ ( 'stage' ) . replaceChildren ( new Option ( 'Toutes les étapes' , '' ) , ... stages . map ( value => new Option ( value , value ) ) ) ;
if ( stages . includes ( current ) ) $ ( 'stage' ) . value = current ;
function persistFilterUrl ( ) {
const params = new URLSearchParams ( ) ;
[ 'search' , 'discipline' , 'stage' , 'tier' , 'cost' , 'difficulty' , 'toxicity' , 'finishing' , 'sort' ] . forEach ( id => { if ( $ ( id ) . value && ! ( id === 'sort' && $ ( id ) . value === 'name' ) ) params . set ( id , $ ( id ) . value ) ; } ) ;
history . replaceState ( null , '' , ` ${ location . pathname } ${ params . size ? ` ? ${ params } ` : '' } ${ location . hash } ` ) ;
}
function restoreFilterUrl ( ) {
const params = new URLSearchParams ( location . search ) ;
[ 'search' , 'discipline' , 'tier' , 'cost' , 'difficulty' , 'toxicity' , 'finishing' , 'sort' ] . forEach ( id => { if ( params . has ( id ) ) $ ( id ) . value = params . get ( id ) ; } ) ;
return params . get ( 'stage' ) || '' ;
}
function renderStages ( preferred = '' ) {
const current = preferred || $ ( 'stage' ) . value , discipline = $ ( 'discipline' ) . value ;
const stages = [ ... new Set ( catalog . filter ( i => ! discipline || i . discipline === discipline ) . map ( i => i . stage ) ) ] . sort ( ) ;
$ ( 'stage' ) . replaceChildren ( new Option ( 'Toutes les étapes' , '' ) , ... stages . map ( value => new Option ( value , value ) ) ) ;
if ( stages . includes ( current ) ) $ ( 'stage' ) . value = current ;
}
function addItem ( item ) {
const locked = selectedDiscipline ( ) ;
if ( locked && item . discipline !== locked ) return ;
state . selected . push ( item ) ; renderSelection ( ) ; location . hash = 'atelier' ;
const locked = selectedDiscipline ( ) ; if ( locked && item . discipline !== locked ) return ;
state . selected . push ( item ) ; renderSelection ( ) ; location . hash = 'atelier' ;
}
function removeCustom ( item ) {
if ( ! confirm ( ` Supprimer « ${ item . name } » de vos ingrédients personnels ? ` ) ) return ;
state . custom = state . custom . filter ( entry => entry . id !== item . id ) ;
state . selected = state . selected . filter ( entry => entry . id !== item . id ) ;
catalog = [ ... data . entries , ... state . custom ] ; writeLocal ( CUSTOM _KEY , state . custom ) ;
refreshCatalog ( ) ; renderSelection ( ) ;
if ( ! confirm ( ` Supprimer « ${ item . name } » de vos ingrédients personnels ? ` ) ) return ;
state . custom = state . custom . filter ( entry => entry . id !== item . id ) ; state . selected = state . selected . filter ( entry => entry . id !== item . id ) ;
catalog = [ ... data . entries , ... state . custom ] ; writeLocal ( CUSTOM _KEY , state . custom ) ; refreshCatalog ( ) ; renderSelection ( ) ;
}
function renderCards ( ) {
const entries = filtered ( ) , locked = selectedDiscipline ( ) ;
$ ( 'result-count' ) . textContent = ` ${ entries . length } résultat ${ entries . length > 1 ? 's' : '' } ` ;
$ ( 'cards' ) . replaceChildren ( ... entries . slice ( 0 , state . shown ) . map ( item => {
const card = document . createElement ( 'article' ) ; card . className = ` card ${ item . custom ? ' custom-card' : '' } ` ;
const metrics = [ [ 'T' , item . tier ] , [ 'Coût' , item . cost ] , [ 'Diff.' , item . difficulty ] , [ 'Tox.' , item . toxicity ] ] . filter ( ( [ , v ] ) => v != null ) ;
card . innerHTML = '<div class="card-meta"><span></span><span></span></div><h3></h3><p class="effect"></p><div class="metrics"></div><div class="card-actions"><button class="add-item" type="button">Ajouter</button></div>' ;
const meta = card . querySelectorAll ( '.card-meta span' ) ; meta [ 0 ] . textContent = item . discipline ; meta [ 1 ] . textContent = ` ${ item . custom ? 'Personnel · ' : '' } ${ item . stage } ` ;
card . querySelector ( 'h3' ) . textContent = item . name ; card . querySelector ( '.effect' ) . textContent = effect ( item ) ;
const box = card . querySelector ( '.metrics' ) ; metrics . forEach ( ( [ k , v ] ) => { const span = document . createElement ( 'span' ) ; span . textContent = ` ${ k } ${ k === 'T' ? v : stars ( v ) } ` ; box . append ( span ) ; } ) ;
const add = card . querySelector ( '.add-item' ) , incompatible = locked && item . discipline !== locked ;
add . disabled = Boolean ( incompatible ) ; add . textContent = incompatible ? ` Réservé : ${ locked } ` : 'Ajouter' ;
add . title = incompatible ? ` Cette recette utilise déjà l'artisanat ${ locked } . ` : '' ;
add . addEventListener ( 'click' , ( ) => addItem ( item ) ) ;
if ( item . custom ) { const remove = document . createElement ( 'button' ) ; remove . type = 'button' ; remove . className = 'delete-custom' ; remove . textContent = 'Supprimer' ; remove . onclick = ( ) => removeCustom ( item ) ; card . querySelector ( '.card-actions' ) . append ( remove ) ; }
const entries = filtered ( ) , locked = selectedDiscipline ( ) ; $ ( 'result-count' ) . textContent = ` ${ entries . length } résultat ${ entries . length > 1 ? 's' : '' } ` ;
$ ( 'cards' ) . replaceChildren ( ... entries . slice ( 0 , state . shown ) . map ( item => {
const card = document . createElement ( 'article' ) ; card . className = ` card ${ item . custom ? ' custom-card' : '' } ` ;
card . innerHTML = '<div class="card-meta"><span></span><span></span></div><h3></h3><p class="effect"></p><div class="metrics"></div><div class="card-actions"><button class="add-item" type="button">Ajouter</button></div>' ;
const meta = card . querySelectorAll ( '.card-meta span' ) ; meta [ 0 ] . textContent = item . discipline ; meta [ 1 ] . textContent = ` ${ item . custom ? 'Personnel · ' : '' } ${ item . stage } ` ;
card . querySelector ( 'h3' ) . textContent = item . name ; card . querySelector ( '.effect' ) . textContent = effect ( item ) ;
const metrics = [ [ 'T' , item . tier ] , [ 'Coût' , item . cost ] , [ 'Diff.' , item . difficulty ] , [ 'Tox.' , item . toxicity ] , [ 'Fin.' , item . finishing ] ] . filter ( ( [ , v ] ) => v != null ) , box = card . querySelector ( '.metrics' ) ;
metrics . forEach ( ( [ key , value ] ) => { const span = document . createElement ( 'span' ) ; span . textContent = ` ${ key } ${ key === 'T' ? value : stars ( value ) } ` ; box . append ( span ) ; } ) ;
const add = card . querySelector ( '.add-item' ) , incompatible = locked && item . discipline !== locked ; add . disabled = Boolean ( incompatible ) ; add . textContent = incompatible ? ` Réservé : ${ locked } ` : 'Ajouter' ; add . title = incompatible ? ` Cette recette utilise déjà l'artisanat ${ locked } . ` : '' ; add . onclick = ( ) => addItem ( item ) ;
if ( item . custom ) { const remove = document . createElement ( 'button' ) ; remove . type = 'button' ; remove . className = 'delete-custom' ; remove . textContent = 'Supprimer' ; remove . onclick = ( ) => removeCustom ( item ) ; card . querySelector ( '.card-actions' ) . append ( remove ) ; }
return card ;
} ) ) ;
$ ( 'more' ) . hidden = entries . length <= state . shown ;
$ ( 'more' ) . hidden = entries . length <= state . shown ; persistFilterUrl ( ) ;
}
function renderSelection ( ) {
const box = $ ( 'selection' ) , locked = selectedDiscipline ( ) ; box . replaceChildren ( ) ;
$ ( 'recipe-discipline' ) . disabled = Boolean ( locked ) ; if ( locked ) $ ( 'recipe-discipline' ) . value = locked ;
if ( ! state . selected . length ) { const p = document . createElement ( 'p' ) ; p . className = 'empty' ; p . textContent = 'Aucun composant sélectionné.' ; box . append ( p ) ; }
state . selected . forEach ( ( item , index ) => { const row = document . createElement ( 'div' ) ; row . className = 'selected-item' ; row . innerHTML = '<div><b></b><small></small></div><button aria-label="Retirer">× </button>' ; row . querySelector ( 'b' ) . textContent = item . name ; row . querySelector ( 'small' ) . textContent = ` ${ item . discipline } · ${ item . stage } ${ item . custom ? ' · personnel' : '' } ` ; row . querySelector ( 'button' ) . onclick = ( ) => { state . selected . splice ( index , 1 ) ; renderSelection ( ) ; } ; box . append ( row ) ; } ) ;
const sum = key => state . selected . reduce ( ( total , item ) => total + numeric ( item [ key ] ) , 0 ) ;
$ ( 'sum-tier' ) . textContent = Math . max ( 0 , ... state . selected . map ( i => numeric ( i . tier ) ) ) ;
$ ( 'sum-cost' ) . textContent = stars ( sum ( 'cost' ) ) ; $ ( 'sum-difficulty' ) . textContent = stars ( sum ( 'difficulty' ) ) ; $ ( 'sum-toxicity' ) . textContent = stars ( sum ( 'toxicity' ) ) ;
const box = $ ( 'selection' ) , locked = selectedDiscipline ( ) ;
const level = numeric ( $ ( 'intelligence' ) . value ) + numeric ( $ ( 'skill' ) . value ) + numeric ( $ ( 'excellence' ) . value ) ; $ ( 'craft-level' ) . textContent = level ;
const invalid = sum ( 'difficulty' ) > level , validity = $ ( 'validity' ) ; validity . className = ` validity ${ invalid ? 'bad' : 'good' } ` ; validity . firstElementChild . textContent = invalid ? 'Niveau dépassé' : 'Recette possible' ; validity . lastElementChild . textContent = invalid ? ` Difficulté ${ sum ( 'difficulty' ) } pour un niveau ${ level } . ` : ` ${ locked || 'Discipline libre' } · niveau ${ level } · ${ state . selected . length } composant(s). ` ;
const result = validation ( ) ; box . replaceChildren ( ) ;
$ ( 'recipe-discipline' ) . disabled = Boolean ( locked ) ; if ( locked ) $ ( 'recipe-discipline' ) . value = locked ;
if ( ! state . selected . length ) { const p = document . createElement ( 'p' ) ; p . className = 'empty' ; p . textContent = 'Aucun composant sélectionné.' ; box . append ( p ) ; }
state . selected . forEach ( ( item , index ) => { const row = document . createElement ( 'div' ) ; row . className = 'selected-item' ; row . innerHTML = '<div><b></b><small></small></div><button aria-label="Retirer">× </button>' ; row . querySelector ( 'b' ) . textContent = item . name ; row . querySelector ( 'small' ) . textContent = ` ${ item . discipline } · ${ item . stage } ${ item . custom ? ' · personnel' : '' } ` ; row . querySelector ( 'button' ) . onclick = ( ) => { state . selected . splice ( index , 1 ) ; renderSelection ( ) ; } ; box . append ( row ) ; } ) ;
$ ( 'sum-tier' ) . textContent = result . value . tier ; $ ( 'sum-cost' ) . textContent = stars ( result . value . cost ) ; $ ( 'sum-difficulty' ) . textContent = stars ( result . value . difficulty ) ; $ ( 'sum-toxicity' ) . textContent = stars ( result . value . toxicity ) ;
const validity = $ ( 'validity' ) , messages = [ ... result . errors , ... result . warnings ] ; validity . className = ` validity ${ result . errors . length ? 'bad' : result . warnings . length ? 'warn' : 'good' } ` ; validity . firstElementChild . textContent = result . errors . length ? 'Recette incomplète' : result . warnings . length ? 'À vérifier' : 'Recette valide' ; validity . lastElementChild . textContent = messages . join ( ' ' ) || ` ${ locked || 'Discipline libre' } · niveau ${ level } · ${ state . selected . length } composant(s). ` ;
renderCards ( ) ;
}
function addCustom ( event ) {
event . preventDefault ( ) ;
const name = $ ( 'custom-name' ) . value . trim ( ) , stage = $ ( 'custom-stage' ) . value . trim ( ) , status = $ ( 'custom-status' ) ;
if ( ! name || ! stage ) { status . textContent = 'Le nom et l’ étape sont requis.' ; return ; }
const optional = id => $ ( id ) . value === '' ? null : numeric ( $ ( id ) . value ) ;
const item = { id : ` custom: ${ Date . now ( ) . toString ( 36 ) } : ${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 8 ) } ` , custom : true , name , discipline : $ ( 'custom-discipline' ) . value , stage , tier : optional ( 'custom-tier' ) , cost : optional ( 'custom-cost' ) , difficulty : optional ( 'custom-difficulty' ) , toxicity : optional ( 'custom-toxicity' ) , finishing : null , source : 'local' , fields : { Effet : $ ( 'custom-effect' ) . value . trim ( ) || 'Ingrédient personnel' } } ;
state . custom . unshift ( item ) ;
if ( ! writeLocal ( CUSTOM _KEY , state . custom ) ) { state . custom . shift ( ) ; status . textContent = 'Stockage local indisponible : ingrédient non enregistré.' ; return ; }
catalog = [ ... data . entries , ... state . custom ] ; event . target . reset ( ) ; $ ( 'custom-discipline' ) . value = item . discipline ; status . textContent = ` « ${ name } » est enregistré sur cet appareil. ` ; refreshCatalog ( ) ;
event . preventDefault ( ) ; const name = $ ( 'custom-name' ) . value . trim ( ) , stage = $ ( 'custom-stage' ) . value . trim ( ) , message = $ ( 'custom-status' ) ;
if ( ! name || ! stage ) { message . textContent = 'Le nom et l’ étape sont requis.' ; return ; }
const item = { id : uid ( 'custom' ) , custom : true , name , discipline : $ ( 'custom-discipline' ) . value , stage , tier : optionalNumber ( 'custom-tier' ) , cost : optionalNumber ( 'custom-cost' ) , difficulty : optionalNumber ( 'custom-difficulty' ) , toxicity : optionalNumber ( 'custom-toxicity' ) , finishing : null , source : 'local' , fields : { Effet : $ ( 'custom-effect' ) . value . trim ( ) || 'Ingrédient personnel' } } ;
state . custom . unshift ( item ) ; if ( ! writeLocal ( CUSTOM _KEY , state . custom ) ) { state . custom . shift ( ) ; message . textContent = 'Ingrédient non enregistré.' ; return ; }
catalog = [ ... data . entries , ... state . custom ] ; event . target . reset ( ) ; $ ( 'custom-discipline' ) . value = item . discipline ; message . textContent = ` « ${ name } » est enregistré sur cet appareil. ` ; refreshCatalog ( ) ;
}
function recipeDocument ( ) {
return { format : 'ravenous-workshop-recipe' , version : 1 , catalog : CATALOG _HASH , id : state . editingId || uid ( 'recipe' ) , name : ( $ ( 'recipe-name' ) . value || 'Sans nom' ) . trim ( ) . slice ( 0 , 150 ) , discipline : selectedDiscipline ( ) || $ ( 'recipe-discipline' ) . value , notes : $ ( 'recipe-notes' ) . value . slice ( 0 , 4000 ) , output _type : $ ( 'output-type' ) . value , artisan : { intelligence : numeric ( $ ( 'intelligence' ) . value ) , skill : numeric ( $ ( 'skill' ) . value ) , excellence : numeric ( $ ( 'excellence' ) . value ) , mastery : numeric ( $ ( 'mastery' ) . value ) , finishing : numeric ( $ ( 'finishing-limit' ) . value ) } , components : state . selected . map ( item => item . id ) , custom _items : state . selected . filter ( item => item . custom ) } ;
}
function validateRecipeDocument ( doc ) {
if ( ! doc || doc . format !== 'ravenous-workshop-recipe' || doc . version !== 1 ) throw new Error ( 'Format de recette non reconnu.' ) ;
if ( ! Array . isArray ( doc . components ) || doc . components . length > 200 ) throw new Error ( 'Liste de composants invalide.' ) ;
if ( typeof doc . name !== 'string' || ! doc . name . trim ( ) || doc . name . length > 150 ) throw new Error ( 'Nom de recette invalide.' ) ;
if ( ! [ 'Alchimie' , 'Brasserie' , 'Forge' , 'Restauration' , 'Pièges' ] . includes ( doc . discipline ) ) throw new Error ( 'Discipline inconnue.' ) ;
return doc ;
}
function refreshCatalog ( ) { $ ( 'catalog-count' ) . textContent = catalog . length ; renderStages ( ) ; renderCards ( ) ; }
function saveRecipe ( ) {
const recipes = readLocal ( RECIPES _KEY , [ ] ) ; recipes . unshift ( { name : $ ( 'recipe-name' ) . value || 'Sans nom' , discipline : $ ( 'recipe-discipline' ) . value , saved : new Date ( ) . toISOString ( ) , items : state . selected . map ( i => i . id ) } ) ; writeLocal ( RECIPES _KEY , recipes . slice ( 0 , 50 ) ) ; renderLibrary ( ) ;
const doc = recipeDocument ( ) , recipes = readLocal ( RECIPES _KEY , [ ] ) , now = new Date ( ) . toISOString ( ) , index = recipes . findIndex ( item => item . id === doc . id ) ; doc . updated = now ; doc . created = index >= 0 ? recipes [ index ] . created || now : now ;
if ( index >= 0 ) recipes [ index ] = doc ; else recipes . unshift ( doc ) ; if ( writeLocal ( RECIPES _KEY , recipes . slice ( 0 , 100 ) ) ) { state . editingId = doc . id ; status ( 'Recette sauvegardée localement.' ) ; renderLibrary ( ) ; }
}
function loadRecipe ( doc ) {
try { validateRecipeDocument ( doc ) ; ( doc . custom _items || [ ] ) . forEach ( item => { if ( item && item . custom && ! catalog . some ( entry => entry . id === item . id ) ) { state . custom . push ( item ) ; catalog . push ( item ) ; } } ) ; writeLocal ( CUSTOM _KEY , state . custom ) ;
const missing = [ ] ; state . selected = doc . components . map ( id => catalog . find ( item => item . id === id ) || ( missing . push ( id ) , null ) ) . filter ( Boolean ) ; state . editingId = doc . id || uid ( 'recipe' ) ; $ ( 'recipe-name' ) . value = doc . name ; $ ( 'summary-name' ) . textContent = doc . name ; $ ( 'recipe-discipline' ) . value = doc . discipline ; $ ( 'recipe-notes' ) . value = String ( doc . notes || '' ) . slice ( 0 , 4000 ) ; $ ( 'output-type' ) . value = [ 'attack' , 'protection' , 'consumable' , 'modifier' ] . includes ( doc . output _type ) ? doc . output _type : 'consumable' ;
const artisan = doc . artisan || { } ; [ [ 'intelligence' , 3 ] , [ 'skill' , 4 ] , [ 'excellence' , 1 ] , [ 'mastery' , 3 ] , [ 'finishing-limit' , 3 ] ] . forEach ( ( [ id , fallback ] ) => $ ( id ) . value = numeric ( artisan [ id === 'finishing-limit' ? 'finishing' : id ] ? ? fallback ) ) ; renderSelection ( ) ; refreshCatalog ( ) ; location . hash = 'atelier' ; status ( ` ${ missing . length ? ` ${ missing . length } composant(s) introuvable(s). ` : '' } Recette chargée ${ doc . catalog && doc . catalog !== CATALOG _HASH ? ' depuis une autre version du catalogue.' : '' } ` , Boolean ( missing . length ) ) ;
} catch ( error ) { status ( error . message , true ) ; }
}
function renderLibrary ( ) {
const box = $ ( 'saved-recipes' ) , recipes = readLocal ( RECIPES _KEY , [ ] ) ; box . replaceChildren ( ) ; if ( ! recipes . length ) { const p = document . createElement ( 'p' ) ; p . className = 'empty' ; p . textContent = 'Aucune recette sauvegardée pour le moment.' ; box . append ( p ) ; return ; } recipes . forEach ( recipe => { const card = document . createElement ( 'article' ) ; card . className = 'saved-card' ; card . innerHTML = '<h3></h3><p></p>' ; card . querySelector ( 'h3' ) . textContent = recipe . name ; card . querySelector ( 'p' ) . textContent = ` ${ recipe . discipline } · ${ recipe . items . length } composant(s) · sauvegarde locale ` ; box . append ( card ) ; } ) ;
const box = $ ( 'saved-recipes' ) , query = fold ( $ ( 'library-search' ) . value ) , recipes = readLocal ( RECIPES _KEY , [ ] ) . filter ( recipe => ! query || fold ( ` ${ recipe . name } ${ recipe . discipline } ${ recipe . notes || '' } ` ) . includes ( query ) ) ; box . replaceChildren ( ) ;
if ( ! recipes . length ) { const p = document . createElement ( 'p' ) ; p . className = 'empty' ; p . textContent = query ? 'Aucune recette ne correspond.' : 'Aucune recette sauvegardée pour le moment.' ; box . append ( p ) ; return ; }
recipes . forEach ( recipe => { const card = document . createElement ( 'article' ) ; card . className = 'saved-card' ; const title = document . createElement ( 'h3' ) , meta = document . createElement ( 'p' ) , actions = document . createElement ( 'div' ) ; title . textContent = recipe . name ; meta . textContent = ` ${ recipe . discipline } · ${ recipe . components ? . length || 0 } composant(s) · ${ new Date ( recipe . updated || recipe . created ) . toLocaleDateString ( 'fr-FR' ) } ` ; actions . className = 'saved-actions' ;
[ [ 'Ouvrir' , ( ) => loadRecipe ( recipe ) ] , [ 'Dupliquer' , ( ) => { const clone = { ... recipe , id : uid ( 'recipe' ) , name : ` ${ recipe . name } — copie ` , created : new Date ( ) . toISOString ( ) , updated : new Date ( ) . toISOString ( ) } ; const all = readLocal ( RECIPES _KEY , [ ] ) ; all . unshift ( clone ) ; writeLocal ( RECIPES _KEY , all ) ; renderLibrary ( ) ; } ] , [ 'Supprimer' , ( ) => { if ( confirm ( ` Supprimer « ${ recipe . name } » ? ` ) ) { writeLocal ( RECIPES _KEY , readLocal ( RECIPES _KEY , [ ] ) . filter ( item => item . id !== recipe . id ) ) ; renderLibrary ( ) ; } } ] ] . forEach ( ( [ label , handler ] ) => { const button = document . createElement ( 'button' ) ; button . type = 'button' ; button . textContent = label ; button . onclick = handler ; actions . append ( button ) ; } ) ; card . append ( title , meta , actions ) ; box . append ( card ) ; } ) ;
}
function exportJson ( ) {
const payload = { format : 'ravenous-roll20-craft' , version : 1 , craft : { name : $ ( 'recipe-name' ) . value , discipline : $ ( 'recipe-discipline' ) . value , recipe : state . selected . map ( i => i . name ) . join ( '; ' ) , level : numeric ( $ ( 'craft-level' ) . textContent ) , difficulty : state . selected . reduce ( ( n , i ) => n + numeric ( i . difficulty ) , 0 ) } , items : [ { type : 'consumable' , name : $ ( 'recipe-name' ) . value || 'Création artisanale' , kind : 'Autre' , quantity : 1 , description : state . selected . map ( i => ` ${ i . name } : ${ effect ( i ) } ` ) . join ( '\n' ) } ] } ;
const blob = new Blob ( [ JSON . stringify ( payload , null , 2 ) + '\n' ] , { type : 'application/json' } ) , link = document . createElement ( 'a' ) ; link . href = URL . createObjectURL ( blob ) ; link . download = 'ravenous-craft.json' ; link . click ( ) ; URL . revokeObjectURL ( link . href ) ;
function roll20Payload ( ) {
const doc = recipeDocument ( ) , result = validation ( ) , description = [ doc . notes , ... state . selected . map ( item => ` ${ item . name } : ${ effect ( item ) } ` ) , ... result . warnings . map ( value => ` À vérifier : ${ value } ` ) ] . filter ( Boolean ) . join ( '\n' ) ; let item = { type : doc . output _type , name : doc . name } ;
if ( doc . output _type === 'consumable' ) item = { ... item , kind : doc . discipline === 'Restauration' ? 'Nourriture' : doc . discipline === 'Alchimie' ? 'Potion' : 'Autre' , quantity : 1 , description } ;
else if ( doc . output _type === 'modifier' ) item = { ... item , description } ; else if ( doc . output _type === 'protection' ) item = { ... item , effects : description } ; else item = { ... item , notes : description } ;
return { format : 'ravenous-roll20-craft' , version : 1 , craft : { name : doc . name , discipline : doc . discipline , recipe : state . selected . map ( i => i . name ) . join ( '; ' ) , level : numeric ( $ ( 'craft-level' ) . textContent ) , difficulty : result . value . difficulty } , items : [ item ] } ;
}
function setTheme ( value ) { document . documentElement . dataset . theme = value ; localStorage . setItem ( 'ravenous-craft-theme' , value ) ; $ ( 'theme' ) . value = value ; }
setTheme ( localStorage . getItem ( 'ravenous-craft-theme' ) || 'warm' ) ;
[ 'search' , 'stage' , 'tier' , 'difficulty' , 'toxicity' , 'sort' ] . forEach ( id => $ ( id ) . addEventListener ( 'input' , ( ) => { state . shown = 30 ; renderCards ( ) ; } ) ) ;
$ ( 'discipline' ) . addEventListener ( 'change' , ( ) => { renderStages ( ) ; renderCards ( ) ; document . querySelectorAll ( '#discipline-tabs button' ) . forEach ( b => b . classList . toggle ( 'active' , b . dataset . value === $ ( 'discipline' ) . value ) ) ; } ) ;
$ ( 'discipline-tabs' ) . addEventListener ( 'click' , event => { if ( ! event . target . dataset . value && event . target . tagName !== 'BUTTON' ) return ; $ ( 'discipline' ) . value = event . target . dataset . value ; renderStages ( ) ; renderCards ( ) ; document . querySelectorAll ( '#discipline-tabs button' ) . forEach ( b => b . classList . toggle ( 'active' , b === event . target ) ) ; } ) ;
$ ( 'clear' ) . onclick = ( ) => { [ 'search' , 'discipline' , 'stage' , 'tier' , 'difficulty' , 'toxicity' ] . forEach ( id => $ ( id ) . value = '' ) ; renderStages ( ) ; renderCards ( ) ; } ; $ ( 'more' ) . onclick = ( ) => { state . shown += 30 ; renderCards ( ) ; } ; $ ( 'theme' ) . onchange = event => setTheme ( event . target . value ) ;
$ ( 'custom-form' ) . addEventListener ( 'submit' , addCustom ) ;
[ 'recipe-name' , 'intelligence' , 'skill' , 'excellence' ] . forEach ( id => $ ( id ) . addEventListener ( 'input' , ( ) => { $ ( 'summary-name' ) . textContent = $ ( 'recipe-name' ) . value || 'Sans nom' ; renderSelection ( ) ; } ) ) ; $ ( 'save' ) . onclick = saveRecipe ; $ ( 'export-json' ) . onclick = exportJson ;
refreshCatalog ( ) ; renderSelection ( ) ; renderLibrary ( ) ;
function download ( value , filename ) { const blob = new Blob ( [ JSON . stringify ( value , null , 2 ) + '\n' ] , { type : 'application/json' } ) , link = document . createElement ( 'a' ) ; link . href = URL . createObjectURL ( blob ) ; link . download = filename ; link . click ( ) ; setTimeout ( ( ) => URL . revokeObjectURL ( link . href ) , 0 ) ; }
function safeFilename ( value , suffix ) { const base = fold ( value ) . replace ( /[^a-z0-9]+/g , '-' ) . replace ( /^-|-$/g , '' ) . slice ( 0 , 60 ) || 'recette' ; return ` ${ base } . ${ suffix } ` ; }
function exportWorkshop ( ) { const doc = recipeDocument ( ) ; download ( doc , safeFilename ( doc . name , 'ravenous.json' ) ) ; status ( 'Recette éditable téléchargée.' ) ; }
function exportRoll20 ( ) { const check = validation ( ) ; if ( check . errors . length ) { status ( check . errors . join ( ' ' ) , true ) ; return ; } const value = roll20Payload ( ) ; download ( value , safeFilename ( value . craft . name , 'roll20.json' ) ) ; status ( 'Export Roll20 téléchargé.' ) ; }
async function copyRoll20 ( ) { const check = validation ( ) ; if ( check . errors . length ) { status ( check . errors . join ( ' ' ) , true ) ; return ; } const value = JSON . stringify ( roll20Payload ( ) , null , 2 ) ; try { await navigator . clipboard . writeText ( value ) ; status ( 'JSON Roll20 copié.' ) ; } catch ( _ ) { const area = document . createElement ( 'textarea' ) ; area . value = value ; document . body . append ( area ) ; area . select ( ) ; const ok = document . execCommand ( 'copy' ) ; area . remove ( ) ; status ( ok ? 'JSON Roll20 copié.' : 'Copie impossible : utilisez le téléchargement.' , ! ok ) ; } }
function chooseFile ( mode ) { state . fileMode = mode ; $ ( 'file-input' ) . value = '' ; $ ( 'file-input' ) . click ( ) ; }
function importFile ( event ) { const file = event . target . files [ 0 ] ; if ( ! file ) return ; if ( file . size > 2_000_000 ) { status ( 'Fichier trop volumineux.' , true ) ; return ; } const reader = new FileReader ( ) ; reader . onload = ( ) => { try { const value = JSON . parse ( reader . result ) ; if ( state . fileMode === 'recipe' ) loadRecipe ( validateRecipeDocument ( value ) ) ; else { if ( ! value || value . format !== 'ravenous-workshop-library' || value . version !== 1 || ! Array . isArray ( value . recipes ) || value . recipes . length > 100 ) throw new Error ( 'Sauvegarde de bibliothèque invalide.' ) ; value . recipes . forEach ( validateRecipeDocument ) ; if ( writeLocal ( RECIPES _KEY , value . recipes ) ) { renderLibrary ( ) ; status ( ` ${ value . recipes . length } recette(s) restaurée(s). ` ) ; } } } catch ( error ) { status ( ` Import impossible : ${ error . message } ` , true ) ; } } ; reader . onerror = ( ) => status ( 'Lecture du fichier impossible.' , true ) ; reader . readAsText ( file , 'utf-8' ) ; }
function backupLibrary ( ) { const recipes = readLocal ( RECIPES _KEY , [ ] ) ; download ( { format : 'ravenous-workshop-library' , version : 1 , catalog : CATALOG _HASH , exported : new Date ( ) . toISOString ( ) , recipes } , 'ravenous-bibliotheque.json' ) ; status ( ` ${ recipes . length } recette(s) sauvegardée(s). ` ) ; }
function refreshCatalog ( ) { $ ( 'catalog-count' ) . textContent = catalog . length ; renderStages ( ) ; renderCards ( ) ; }
function setTheme ( value ) { document . documentElement . dataset . theme = value ; try { localStorage . setItem ( THEME _KEY , value ) ; } catch ( _ ) { } $ ( 'theme' ) . value = value ; }
const wantedStage = restoreFilterUrl ( ) ; let initialTheme = 'warm' ; try { initialTheme = localStorage . getItem ( THEME _KEY ) || 'warm' ; } catch ( _ ) { } setTheme ( initialTheme ) ; renderStages ( wantedStage ) ;
[ 'search' , 'stage' , 'tier' , 'cost' , 'difficulty' , 'toxicity' , 'finishing' , 'sort' ] . forEach ( id => $ ( id ) . addEventListener ( 'input' , ( ) => { state . shown = 30 ; renderCards ( ) ; } ) ) ;
$ ( 'discipline' ) . onchange = ( ) => { renderStages ( ) ; renderCards ( ) ; document . querySelectorAll ( '#discipline-tabs button' ) . forEach ( button => button . classList . toggle ( 'active' , button . dataset . value === $ ( 'discipline' ) . value ) ) ; } ;
$ ( 'discipline-tabs' ) . onclick = event => { if ( ! event . target . dataset . value && event . target . tagName !== 'BUTTON' ) return ; $ ( 'discipline' ) . value = event . target . dataset . value ; renderStages ( ) ; renderCards ( ) ; document . querySelectorAll ( '#discipline-tabs button' ) . forEach ( button => button . classList . toggle ( 'active' , button === event . target ) ) ; } ;
$ ( 'clear' ) . onclick = ( ) => { [ 'search' , 'discipline' , 'stage' , 'tier' , 'cost' , 'difficulty' , 'toxicity' , 'finishing' ] . forEach ( id => $ ( id ) . value = '' ) ; renderStages ( ) ; renderCards ( ) ; } ; $ ( 'more' ) . onclick = ( ) => { state . shown += 30 ; renderCards ( ) ; } ; $ ( 'theme' ) . onchange = event => setTheme ( event . target . value ) ; $ ( 'custom-form' ) . onsubmit = addCustom ;
[ 'recipe-name' , 'intelligence' , 'skill' , 'excellence' , 'mastery' , 'finishing-limit' , 'output-type' , 'recipe-notes' ] . forEach ( id => $ ( id ) . addEventListener ( 'input' , ( ) => { $ ( 'summary-name' ) . textContent = $ ( 'recipe-name' ) . value || 'Sans nom' ; renderSelection ( ) ; } ) ) ;
$ ( 'save' ) . onclick = saveRecipe ; $ ( 'export-workshop' ) . onclick = exportWorkshop ; $ ( 'export-json' ) . onclick = exportRoll20 ; $ ( 'copy-json' ) . onclick = copyRoll20 ; $ ( 'print-recipe' ) . onclick = ( ) => window . print ( ) ; $ ( 'library-search' ) . oninput = renderLibrary ; $ ( 'import-recipe' ) . onclick = ( ) => chooseFile ( 'recipe' ) ; $ ( 'restore-library' ) . onclick = ( ) => chooseFile ( 'library' ) ; $ ( 'backup-library' ) . onclick = backupLibrary ; $ ( 'file-input' ) . onchange = importFile ;
refreshCatalog ( ) ; renderSelection ( ) ; renderLibrary ( ) ;
} ) ( ) ;