MC/website/control-panel/public/kitset-model.mjs
2026-11-27 23:43:05 +00:00

20 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {validateItemDesign} from './item-design-model.mjs';
// A kit is a collection of the same designs used by the single-item editor.
// Saving this data does not issue individual item keys or inventory.
export const KITSET_MAX_LINES=36;
export const KITSET_LIMITS=Object.freeze({office:3,clan:10,company:10,politics:10});
export function kitsetOwner(value){
if(!value||typeof value!=='object'||Array.isArray(value)||Object.keys(value).length!==2||!Object.hasOwn(KITSET_LIMITS,value.kind)||typeof value.id!=='string'||!(/^[a-z0-9_-]{1,64}$/i.test(value.id)))throw TypeError('Choose an office, clan, company or POL organisation. Personal kits are not supported.');
return {kind:value.kind,id:value.id.toLowerCase()};
}
export function validateKitset(input){
const exact=(value,keys)=>value&&typeof value==='object'&&!Array.isArray(value)&&Object.keys(value).length===keys.length&&keys.every(key=>Object.hasOwn(value,key));
if(!exact(input,['version','name','owner','items'])||input.version!==1||typeof input.name!=='string'||!input.name.trim()||input.name.length>64||/[<>§\x00-\x1f\x7f]/.test(input.name)||!Array.isArray(input.items)||input.items.length>KITSET_MAX_LINES)throw TypeError('Use a kit owner, name and up to 36 item designs.');
const seen=new Set();
const items=input.items.map(row=>{
if(!exact(row,['id','quantity','design'])||typeof row.id!=='string'||!/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(row.id)||seen.has(row.id.toLowerCase())||!Number.isSafeInteger(row.quantity)||row.quantity<1||row.quantity>64)throw TypeError('Each kit entry needs a unique identity and a quantity of 1–64.');
seen.add(row.id.toLowerCase());return {id:row.id.toLowerCase(),quantity:row.quantity,design:validateItemDesign(row.design)};
});
return {version:1,name:input.name.trim(),owner:kitsetOwner(input.owner),items};
}