import { fixedSizeArray, FixedSizeArray, staticImplements, } from '@staratlas/data-source'; import { PermissionType, PermissionTypeStatic, } from '@staratlas/player-profile'; type NotCraftingPermissions = | 'and' | 'contains' | 'eq' | 'or' | 'getPermissions'; @staticImplements>() export class CraftingPermissions implements PermissionType { constructor( public manageDomain: boolean, public createCraftableItem: boolean, public manageCraftableItem: boolean, public manageCraftingFacility: boolean, public createRecipeCategory: boolean, public manageRecipeCategory: boolean, public createRecipe: boolean, public manageRecipe: boolean, ) {} getPermissions(): FixedSizeArray { const out: FixedSizeArray = fixedSizeArray(8, 0); out[0] = (this.manageDomain ? 1 << 0 : 0) | (this.createCraftableItem ? 1 << 1 : 0) | (this.manageCraftableItem ? 1 << 2 : 0) | (this.manageCraftingFacility ? 1 << 3 : 0) | (this.createRecipeCategory ? 1 << 4 : 0) | (this.manageRecipeCategory ? 1 << 5 : 0) | (this.createRecipe ? 1 << 6 : 0) | (this.manageRecipe ? 1 << 7 : 0); return out; } static empty(): CraftingPermissions { return new CraftingPermissions( false, false, false, false, false, false, false, false, ); } static all(): CraftingPermissions { return new CraftingPermissions( true, true, true, true, true, true, true, true, ); } static manageDomain(): CraftingPermissions { return CraftingPermissions.permissionsBuilder('manageDomain'); } static createCraftableItem(): CraftingPermissions { return CraftingPermissions.permissionsBuilder('createCraftableItem'); } static manageCraftableItem(): CraftingPermissions { return CraftingPermissions.permissionsBuilder('manageCraftableItem'); } static manageCraftingFacility(): CraftingPermissions { return CraftingPermissions.permissionsBuilder('manageCraftingFacility'); } static createRecipeCategory(): CraftingPermissions { return CraftingPermissions.permissionsBuilder('createRecipeCategory'); } static manageRecipeCategory(): CraftingPermissions { return CraftingPermissions.permissionsBuilder('manageRecipeCategory'); } static createRecipe(): CraftingPermissions { return CraftingPermissions.permissionsBuilder('createRecipe'); } static manageRecipe(): CraftingPermissions { return CraftingPermissions.permissionsBuilder('manageRecipe'); } static craftableItemPermissions(): CraftingPermissions { return CraftingPermissions.permissionsBuilder([ 'createCraftableItem', 'manageCraftableItem', ]); } static recipeCategoryPermissions(): CraftingPermissions { return CraftingPermissions.permissionsBuilder([ 'createRecipeCategory', 'manageRecipeCategory', ]); } static recipePermissions(): CraftingPermissions { return CraftingPermissions.permissionsBuilder([ 'createRecipe', 'manageRecipe', ]); } static fromPermissions( permissions: FixedSizeArray, ): CraftingPermissions { return new CraftingPermissions( (permissions[0] & (1 << 0)) !== 0, (permissions[0] & (1 << 1)) !== 0, (permissions[0] & (1 << 2)) !== 0, (permissions[0] & (1 << 3)) !== 0, (permissions[0] & (1 << 4)) !== 0, (permissions[0] & (1 << 5)) !== 0, (permissions[0] & (1 << 6)) !== 0, (permissions[0] & (1 << 7)) !== 0, ); } and(other: CraftingPermissions): CraftingPermissions { return new CraftingPermissions( this.manageDomain && other.manageDomain, this.createCraftableItem && other.createCraftableItem, this.manageCraftableItem && other.manageCraftableItem, this.manageCraftingFacility && other.manageCraftingFacility, this.createRecipeCategory && other.createRecipeCategory, this.manageRecipeCategory && other.manageRecipeCategory, this.createRecipe && other.createRecipe, this.manageRecipe && other.manageRecipe, ); } or(other: CraftingPermissions): CraftingPermissions { return new CraftingPermissions( this.manageDomain || other.manageDomain, this.createCraftableItem || other.createCraftableItem, this.manageCraftableItem || other.manageCraftableItem, this.manageCraftingFacility || other.manageCraftingFacility, this.createRecipeCategory || other.createRecipeCategory, this.manageRecipeCategory || other.manageRecipeCategory, this.createRecipe || other.createRecipe, this.manageRecipe || other.manageRecipe, ); } eq(other: CraftingPermissions): boolean { return ( this.manageDomain === other.manageDomain && this.createCraftableItem === other.createCraftableItem && this.manageCraftableItem === other.manageCraftableItem && this.manageCraftingFacility === other.manageCraftingFacility && this.createRecipeCategory === other.createRecipeCategory && this.manageRecipeCategory === other.manageRecipeCategory && this.createRecipe === other.createRecipe && this.manageRecipe === other.manageRecipe ); } contains(other: CraftingPermissions): boolean { return this.and(other).eq(other); } /** * Dynamically builds an instance of CraftingPermissions * @param input - the desired permission or array of permissions * @returns CraftingPermissions */ static permissionsBuilder( input: | Exclude | Array>, ): CraftingPermissions { const inputPerms = Array.isArray(input) ? input : [input]; const params = Object.keys(CraftingPermissions.empty()).map((key) => (inputPerms).includes(key), ) as [ boolean, boolean, boolean, boolean, boolean, boolean, boolean, boolean, ]; if (params.length != CraftingPermissions.length) { console.log(params.length, CraftingPermissions.length); throw 'Incorrect permissions array'; } return new CraftingPermissions(...params); } }