import { Item } from "../../items/types/Item" ; import { layout , LayoutResult } from "../layout" ; import { PlugyStash } from "../../plugy-stash/types" ; import { makeIndex } from "../../plugy-stash/makeIndex" ; import { Set , SET_ITEMS , SetItem , SETS } from "../../../game-data" ; import { EQUIPMENT_TYPES } from "../list/uniquesOrder" ; import { SETS_ORDER } from "../list/setsOrder" ; import { fillTemplate } from "./fillTemplate" ; import { getBase } from "../../items/getBase" ; import { groupBySet } from "../list/groupSets" ; import { addPage } from "../../plugy-stash/addPage" ; import { moveItem } from "../../items/moving/safeMove" ; function createTemplates ( ) { const allItems = groupBySet ( SET_ITEMS ) ; const templates = new Map < Set , LayoutResult < SetItem > > ( ) ; for ( const [ set , items ] of allItems ) { templates . set ( set , layout ( "set" , [ items ] ) ) ; } return templates ; } function extrasOrder ( a , b ) { return ( EQUIPMENT_TYPES . indexOf ( getBase ( a ) . type ) - EQUIPMENT_TYPES . indexOf ( getBase ( b ) . type ) ) ; } export function organizeSets ( stash , items ) { const bySet = groupBySet ( items ) ; const templates = createTemplates ( ) ; let offset = stash . pages . length ; for ( const category of SETS_ORDER ) { category . forEach ( ( { name , shortName , set } , i ) => { const mainPage = addPage ( stash , name ) ; makeIndex ( mainPage , i === 0 ) ; const remaining = fillTemplate ( stash , bySet . get ( SETS [ set ] ) ! , templates . get ( SETS [ set ] ) ! , offset ) ; offset ++ ; if ( remaining . length > 0 ) { remaining . sort ( extrasOrder ) ; const { nbPages , positions } = layout ( "lines" , [ remaining ] ) ; for ( let j = 0 ; j < nbPages ; j ++ ) { addPage ( stash , `Extra ${ shortName }` ) ; } for ( const [ item , { page , rows , cols } ] of positions . entries ( ) ) { moveItem ( stash , item , offset + page , rows [ 0 ] , cols [ 0 ] ) ; } offset += nbPages ; } } ) ; } }