From f93977a7f0c8dc623219d6994314a5627a0843a9 Mon Sep 17 00:00:00 2001 From: Ruben Campos de Teba Date: Thu, 14 Dec 2023 18:54:12 +0000 Subject: [PATCH] added algorithim --- .vscode/settings.json | 2 +- src/components/Leaderboard.tsx | 7 ++-- src/components/RankAndArtifactCount.tsx | 1 + src/lib/SeasonSubcriptionContext.tsx | 4 ++- src/lib/calculateSales.ts | 44 +++++++++++++++++++++++-- src/lib/useSeasonSubscriptionData.ts | 38 ++++++++++++++++++++- src/pages/project/[slug].tsx | 8 +++-- 7 files changed, 95 insertions(+), 9 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index b7d4426c..a8c804ec 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "editor.codeActionsOnSave": { - "source.fixAll.eslint": true + "source.fixAll.eslint": "explicit" }, "eslint.validate": ["javascript"] } diff --git a/src/components/Leaderboard.tsx b/src/components/Leaderboard.tsx index 02749869..f5d1709d 100644 --- a/src/components/Leaderboard.tsx +++ b/src/components/Leaderboard.tsx @@ -12,14 +12,17 @@ interface ILeaderboard { count: number totalSales: number matchFundPooled: number + totalBase: number } const DEFAULT_LIMIT = 3 const FIXED_PRECISION = 2 -const Leaderboard = ({ openEditions, isWinner, count, totalSales, matchFundPooled }: ILeaderboard) => { +const Leaderboard = ({ openEditions, isWinner, count, totalSales, matchFundPooled, totalBase }: ILeaderboard) => { const [limit, setLimit] = useState(DEFAULT_LIMIT) + console.log('Leaderboard totalBase', totalBase) + if (!openEditions) return const sideItem = @@ -37,7 +40,7 @@ const Leaderboard = ({ openEditions, isWinner, count, totalSales, matchFundPoole ) const { salesArtifacts, prize, totalAward, projectMatchFund, calculateMatchFundContribution, getSalesArtifacts } = - calculateSales(isWinner, matchFundPooled, count, totalSales) + calculateSales(isWinner, matchFundPooled, count, totalSales, totalBase) const title = (
diff --git a/src/components/RankAndArtifactCount.tsx b/src/components/RankAndArtifactCount.tsx index ba797e3a..1daa3b9c 100644 --- a/src/components/RankAndArtifactCount.tsx +++ b/src/components/RankAndArtifactCount.tsx @@ -11,6 +11,7 @@ interface IRankAndArtifactCount { seasonIsActive?: boolean totalSales: number matchFundPooled: number + totalBase?: number } const RankAndArtifactCount = ({ rank, count, seasonIsActive, totalSales, matchFundPooled }: IRankAndArtifactCount) => { diff --git a/src/lib/SeasonSubcriptionContext.tsx b/src/lib/SeasonSubcriptionContext.tsx index 6b18dca1..dd7e586e 100644 --- a/src/lib/SeasonSubcriptionContext.tsx +++ b/src/lib/SeasonSubcriptionContext.tsx @@ -9,12 +9,13 @@ interface ISeasonSubcriptionContext { seasonIsActive?: boolean totalSales?: number totalPrizePooled?: number + totalBase?: number } export const SeasonSubcriptionContext = createContext({}) export const SeasonSubcriptionProvider = ({ children }: SimpleComponentProps) => { - const { season, loading, arrangedSeasonList, seasonIsActive, totalSales, totalPrizePooled } = + const { season, loading, arrangedSeasonList, seasonIsActive, totalSales, totalPrizePooled, totalBase } = useSeasonSubscriptionData() return ( @@ -26,6 +27,7 @@ export const SeasonSubcriptionProvider = ({ children }: SimpleComponentProps) => seasonIsActive, totalSales, totalPrizePooled, + totalBase }} > {children} diff --git a/src/lib/calculateSales.ts b/src/lib/calculateSales.ts index 124de09a..3ffae2e5 100644 --- a/src/lib/calculateSales.ts +++ b/src/lib/calculateSales.ts @@ -10,14 +10,54 @@ const getMatchFundMoney = (totalSales: number, countH: number, spli80: number): return (spli80 * split) / 100 } -export const calculateSales = (isWinner: boolean, matchFundPooled: number, count: number, totalSales: number) => { +function getBaseLog(x:number, y:number) { + return Math.log(x)/Math.log(y); +} + + +const getMatchFundMoneyAlgor = (totalSales: number, countH: number, spli90: number, totalBase:number):number => { + // const split = totalSales > 0 ? (countH * 100) / totalSales : 0 + + // //split: 6*100/8 = 75% + // //spli80: 80*10/100 = 8 + // return (spli80 * getBaseLog(2,split)) / 100 + + console.log('totalBase ', totalBase) + + const BaseLog = 1.0001; + + console.log('countH ', countH) + + + const LogB2 = getBaseLog(countH + 1, BaseLog) + + console.log('LogB2 ', LogB2) + + const projectBasedPercent = LogB2 * 100 / totalBase + + console.log('getPercent ', projectBasedPercent) + + const getCOntribution = (spli90 * projectBasedPercent) / 100 + + console.log('getCOntribution ', getCOntribution) + + return getCOntribution +} + + + +export const calculateSales = (isWinner: boolean, matchFundPooled: number, count: number, totalSales: number, totalBase:number) => { const getSalesArtifacts = (artifactCount: number) => BASE_ARTIFACT_PRICE * artifactCount const totalProjectSales = getSalesArtifacts(count) const spli90 = (90 * matchFundPooled) / 100 const split10 = (10 * matchFundPooled) / 100 - const projectMatchFund = getMatchFundMoney(totalSales, count, spli90) + //const projectMatchFund = getMatchFundMoney(totalSales, count, spli90) + + console.log('calculateSales totalBase ', totalBase) + + const projectMatchFund = getMatchFundMoneyAlgor(totalSales, count, spli90, totalBase) const totalAward = (totalProjectSales + projectMatchFund + (isWinner ? split10 : 0)).toFixed(2) diff --git a/src/lib/useSeasonSubscriptionData.ts b/src/lib/useSeasonSubscriptionData.ts index 45bb39a1..b21739e3 100644 --- a/src/lib/useSeasonSubscriptionData.ts +++ b/src/lib/useSeasonSubscriptionData.ts @@ -40,12 +40,43 @@ const countTotalSales = (submissions: ISubmissionFragment[]): number => { return total } +function getBaseLog(x:number, y:number) { + return Math.log(x)/Math.log(y); +} + +const getBaseAllSales = (submissions: ISubmissionFragment[]): number => { + let baseTotal = 0 + const BaseLog = 1.0001; + + + + submissions.forEach(submission => { + // total += submission.project!.artifacts[0].openEditionCopies_aggregate.aggregate!.sum!.copies! + const filteredOpenEditionCopiesTotal = filteredOpenEditionCopies(submission.project!.artifacts[0].openEditionCopies) + + console.log('filteredOpenEditionCopiesTotal ', filteredOpenEditionCopiesTotal) + + const submissionsCount = count(filteredOpenEditionCopiesTotal) + console.log('submissionsCount ', submissionsCount + 1) + + const LogB2 = submissionsCount > 0 ? getBaseLog(submissionsCount + 1, BaseLog) : 0 + + console.log('LogB2 ', LogB2) + baseTotal += LogB2 + }) + + console.log('getBaseAllSales baseTotal::: ', baseTotal) + + return baseTotal +} + + export function useSeasonSubscriptionData() { const { seasonId, isSeasonActive: seasonIsActive } = useContext(SeasonContext) const [arrangedSeasonList, setArrangedSeasonList] = useState(null) const [totalSales, setTotalSales] = useState(0) const [totalPrizePooled, setTotalPrizePooled] = useState(0) - // const { isSeasonActive } = useDateHelpers() + const [totalBase, setTotalBase] = useState(0) const { data, loading, error } = useSubscription(SUBSCRIBE_SEASONS, { skip: !seasonId, @@ -61,6 +92,10 @@ export function useSeasonSubscriptionData() { const arrangedSeasonListHere = arrangeSubmissions(data?.Seasons[0].submissions) const totalSales = countTotalSales(data?.Seasons[0].submissions) + //getBaseAllSales + + setTotalBase(getBaseAllSales(data?.Seasons[0].submissions)) + setArrangedSeasonList(arrangedSeasonListHere) setTotalSales(totalSales) const totalPrizePooledL = data?.Seasons[0].matchFundPooled + totalSales * BASE_ARTIFACT_PRICE @@ -72,6 +107,7 @@ export function useSeasonSubscriptionData() { return { arrangedSeasonList, totalSales, + totalBase, season: data?.Seasons[0], loading, seasonIsActive, diff --git a/src/pages/project/[slug].tsx b/src/pages/project/[slug].tsx index a259a7a0..7fde6706 100644 --- a/src/pages/project/[slug].tsx +++ b/src/pages/project/[slug].tsx @@ -53,6 +53,7 @@ const ProjectPage = () => { totalSales, loading: loadingSeason, arrangedSeasonList, + totalBase, } = useContext(SeasonSubcriptionContext) const { setVisibleModalWithAttrs } = useContext(LayoutContext) @@ -89,12 +90,13 @@ const ProjectPage = () => { const openEditions = openEditionsSub || openEditionsQuery - console.log('openEditions', openEditions) - const lead = project?.members?.find((m: any) => m.type === 'lead')?.user const rank = arrangedSeasonList?.findIndex(submission => submission.project?.id === project.id) || 0 + // I can take arrangedSeasonList and work out the tatio + console.log('arrangedSeasonList2', arrangedSeasonList) + const arrayOfOpenEdtionClean = openEditions?.OpenEditionCopies.filter(({ status }: any) => { return status === 'confirmed' @@ -121,6 +123,7 @@ const ProjectPage = () => { seasonIsActive={seasonIsActive} totalSales={totalSales ? totalSales : 0} matchFundPooled={seasonData?.matchFundPooled} + totalBase={totalBase} /> )}
@@ -159,6 +162,7 @@ const ProjectPage = () => { count={count} totalSales={totalSales ? totalSales : 0} matchFundPooled={seasonData?.matchFundPooled} + totalBase={totalBase ? totalBase : 0} />