diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 9b9fffb..ba1b0d4 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -11,5 +11,5 @@ jobs: with: node-version: '20.x' - - run: npm + - run: npm install - run: npm run lint diff --git a/.github/workflows/publish-experimental.yml b/.github/workflows/publish-experimental.yml index fc09ca0..60fc536 100644 --- a/.github/workflows/publish-experimental.yml +++ b/.github/workflows/publish-experimental.yml @@ -14,7 +14,7 @@ jobs: registry-url: 'https://registry.npmjs.org' scope: '@liarco' - - run: npm + - run: npm install - run: npm run lint && npm run build - run: npm version --no-git-tag-version --no-commit-hooks "0.0.0-experimental.${{ github.sha }}" && npm publish env: diff --git a/.github/workflows/publish-releases.yml b/.github/workflows/publish-releases.yml index 1912f5e..b4276fe 100644 --- a/.github/workflows/publish-releases.yml +++ b/.github/workflows/publish-releases.yml @@ -15,7 +15,7 @@ jobs: registry-url: 'https://registry.npmjs.org' scope: '@liarco' - - run: npm + - run: npm install - run: npm run lint && npm run build - run: npm publish env: diff --git a/src/index.ts b/src/index.ts index d48a5b8..5e63ed5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +// TODO: Narrow down the exports to only the ones that are actually needed export * from './lib/rarities'; export * from './lib/tags'; export * from './lib/nftTags'; diff --git a/src/lib/customWeightingRules.ts b/src/lib/customWeightingRules.ts index d6b5c14..525a908 100644 --- a/src/lib/customWeightingRules.ts +++ b/src/lib/customWeightingRules.ts @@ -1,7 +1,11 @@ import { Item } from '../data/items'; +export type CustomWeightingRulesOptions = { + space_theme__egyptian_theme_weight?: number; +}; + type CustomWeightingRules = { - [name: string]: (item: Item) => number | null; + [name: string]: (item: Item, options?: CustomWeightingRulesOptions) => number | null; }; export const customWeightingRules: CustomWeightingRules = { @@ -26,12 +30,14 @@ export const customWeightingRules: CustomWeightingRules = { // ------------------------------------------------------ // Space theme // ------------------------------------------------------ - space_theme: (item: Item) => { + space_theme: (item: Item, customSettings?: CustomWeightingRulesOptions) => { if (!item.metadata.tags.includes('space')) { return null; } - // TODO: This is just a proof of concept to show how to give a custom weight to the "Evermore" theme of a space - return item.extra?.attributes?.find((attribute) => attribute.name === 'theme')?.value === 'Evermore' ? 15 : 0; + // TODO: This is just a proof of concept to show how to give a custom weight to the theme of a space + return item.extra?.attributes?.find((attribute) => attribute.name === 'theme')?.value === 'Evermore' + ? 15 + : customSettings?.space_theme__egyptian_theme_weight ?? 0; }, } as const; diff --git a/src/lib/items.ts b/src/lib/items.ts index 91e5c73..972c90e 100644 --- a/src/lib/items.ts +++ b/src/lib/items.ts @@ -1,15 +1,15 @@ import { Item } from '../data/items'; import { Rarity } from '../data/rarities'; -import { customWeightingRules } from './customWeightingRules'; -import { getNftTagRanking } from './nftTags'; -import { getRarityRanking } from './rarities'; -import { getTagRanking } from './tags'; +import { CustomWeightingRulesOptions, customWeightingRules } from './customWeightingRules'; +import { NftTagRankingOptions, getNftTagRanking } from './nftTags'; +import { RarityRankingOptions, getRarityRanking } from './rarities'; +import { TagRankingOptions, getTagRanking } from './tags'; import { roundRankingValue } from './utils'; -const getCustomWeightRankings = (item: Item): [string, number | null][] => { +const getCustomWeightRankings = (item: Item, options?: CustomWeightingRulesOptions): [string, number | null][] => { return Object.entries(customWeightingRules).map(([ruleName, rule]) => { try { - const ranking = rule(item); + const ranking = rule(item, options); return [ruleName, ranking === null ? null : roundRankingValue(ranking)]; } catch (error) { @@ -29,17 +29,36 @@ type ItemRankingResult = [ }, ]; -export const getItemRanking = (item: Item): ItemRankingResult => { +type ItemRankingOptions = RarityRankingOptions & + TagRankingOptions & + NftTagRankingOptions & { customWeightingRulesOptions?: CustomWeightingRulesOptions }; + +export const getItemRanking = (item: Item, options?: ItemRankingOptions): ItemRankingResult => { const maxSupply = item.metadata.maxIssuance; - const rarityRanking = getRarityRanking(item.metadata.rarity); - const tagRankingsData = item.metadata.tags.map((tag) => [tag, getTagRanking(tag)] as [string, number]); + const rarityRanking = getRarityRanking(item.metadata.rarity, { customRarityWeights: options?.customRarityWeights }); + const tagRankingsData = item.metadata.tags.map( + (tag) => + [tag, getTagRanking(tag, { customTagWeights: options?.customTagWeights, failIfUnknown: options?.failIfUnknown })] as [ + string, + number, + ], + ); const tagsRanking = tagRankingsData.reduce((ranking, [, currentTagRanking]) => ranking + (currentTagRanking ?? 0), 1); - const nftTagRankingsData = item.metadata.nftTags.map((nftTag) => [nftTag, getNftTagRanking(nftTag)] as [string, number]); + const nftTagRankingsData = item.metadata.nftTags.map( + (nftTag) => + [ + nftTag, + getNftTagRanking(nftTag, { + customNftTagWeights: options?.customNftTagWeights, + failIfUnknown: options?.failIfUnknown, + }), + ] as [string, number], + ); const nftTagsRanking = nftTagRankingsData.reduce( (ranking, [, currentNftTagRanking]) => ranking + (currentNftTagRanking ?? 0), 1, ); - const customWeightRankingsData = getCustomWeightRankings(item); + const customWeightRankingsData = getCustomWeightRankings(item, options?.customWeightingRulesOptions); const customWeightRanking = customWeightRankingsData.reduce( (ranking, [, currentRanking]) => ranking + (currentRanking ?? 0), 1, diff --git a/src/lib/nftTags.ts b/src/lib/nftTags.ts index 7c2c3a8..678b26c 100644 --- a/src/lib/nftTags.ts +++ b/src/lib/nftTags.ts @@ -1,7 +1,7 @@ import { nftTagWeights } from '../data/nftTags'; import { roundRankingValue } from './utils'; -type NftTagRankingOptions = { +export type NftTagRankingOptions = { customNftTagWeights?: { [name: string]: number; }; diff --git a/src/lib/rarities.ts b/src/lib/rarities.ts index 1ae9af1..450f965 100644 --- a/src/lib/rarities.ts +++ b/src/lib/rarities.ts @@ -1,7 +1,7 @@ import { Rarity, rarityWeights } from '../data/rarities'; import { roundRankingValue } from './utils'; -type RarityRankingOptions = { +export type RarityRankingOptions = { customRarityWeights?: { [name: string]: number; }; diff --git a/src/lib/tags.ts b/src/lib/tags.ts index e784aa5..026a9c1 100644 --- a/src/lib/tags.ts +++ b/src/lib/tags.ts @@ -1,7 +1,7 @@ import { tagWeights } from '../data/tags'; import { roundRankingValue } from './utils'; -type TagRankingOptions = { +export type TagRankingOptions = { customTagWeights?: { [name: string]: number; };