Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the custom options to all functions (override defaults) #1

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ jobs:
with:
node-version: '20.x'

- run: npm
- run: npm install
- run: npm run lint
2 changes: 1 addition & 1 deletion .github/workflows/publish-experimental.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
14 changes: 10 additions & 4 deletions src/lib/customWeightingRules.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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;
41 changes: 30 additions & 11 deletions src/lib/items.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/nftTags.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { nftTagWeights } from '../data/nftTags';
import { roundRankingValue } from './utils';

type NftTagRankingOptions = {
export type NftTagRankingOptions = {
customNftTagWeights?: {
[name: string]: number;
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/rarities.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Rarity, rarityWeights } from '../data/rarities';
import { roundRankingValue } from './utils';

type RarityRankingOptions = {
export type RarityRankingOptions = {
customRarityWeights?: {
[name: string]: number;
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/tags.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { tagWeights } from '../data/tags';
import { roundRankingValue } from './utils';

type TagRankingOptions = {
export type TagRankingOptions = {
customTagWeights?: {
[name: string]: number;
};
Expand Down