Skip to content

Commit

Permalink
added algorithim
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Campos de Teba authored and Ruben Campos de Teba committed Dec 14, 2023
1 parent f71b81f commit f93977a
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"eslint.validate": ["javascript"]
}
7 changes: 5 additions & 2 deletions src/components/Leaderboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Spinner minHeight="65px" />

const sideItem =
Expand All @@ -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 = (
<div>
Expand Down
1 change: 1 addition & 0 deletions src/components/RankAndArtifactCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface IRankAndArtifactCount {
seasonIsActive?: boolean
totalSales: number
matchFundPooled: number
totalBase?: number
}

const RankAndArtifactCount = ({ rank, count, seasonIsActive, totalSales, matchFundPooled }: IRankAndArtifactCount) => {
Expand Down
4 changes: 3 additions & 1 deletion src/lib/SeasonSubcriptionContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ interface ISeasonSubcriptionContext {
seasonIsActive?: boolean
totalSales?: number
totalPrizePooled?: number
totalBase?: number
}

export const SeasonSubcriptionContext = createContext<ISeasonSubcriptionContext>({})

export const SeasonSubcriptionProvider = ({ children }: SimpleComponentProps) => {
const { season, loading, arrangedSeasonList, seasonIsActive, totalSales, totalPrizePooled } =
const { season, loading, arrangedSeasonList, seasonIsActive, totalSales, totalPrizePooled, totalBase } =
useSeasonSubscriptionData()

return (
Expand All @@ -26,6 +27,7 @@ export const SeasonSubcriptionProvider = ({ children }: SimpleComponentProps) =>
seasonIsActive,
totalSales,
totalPrizePooled,
totalBase
}}
>
{children}
Expand Down
44 changes: 42 additions & 2 deletions src/lib/calculateSales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
38 changes: 37 additions & 1 deletion src/lib/useSeasonSubscriptionData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISubmissionFragment[] | null>(null)
const [totalSales, setTotalSales] = useState<number>(0)
const [totalPrizePooled, setTotalPrizePooled] = useState<number>(0)
// const { isSeasonActive } = useDateHelpers()
const [totalBase, setTotalBase] = useState<number>(0)

const { data, loading, error } = useSubscription<ISubscribeSeasonsSubscription>(SUBSCRIBE_SEASONS, {
skip: !seasonId,
Expand All @@ -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
Expand All @@ -72,6 +107,7 @@ export function useSeasonSubscriptionData() {
return {
arrangedSeasonList,
totalSales,
totalBase,
season: data?.Seasons[0],
loading,
seasonIsActive,
Expand Down
8 changes: 6 additions & 2 deletions src/pages/project/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const ProjectPage = () => {
totalSales,
loading: loadingSeason,
arrangedSeasonList,
totalBase,
} = useContext(SeasonSubcriptionContext)
const { setVisibleModalWithAttrs } = useContext(LayoutContext)

Expand Down Expand Up @@ -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'
Expand All @@ -121,6 +123,7 @@ const ProjectPage = () => {
seasonIsActive={seasonIsActive}
totalSales={totalSales ? totalSales : 0}
matchFundPooled={seasonData?.matchFundPooled}
totalBase={totalBase}
/>
)}
</div>
Expand Down Expand Up @@ -159,6 +162,7 @@ const ProjectPage = () => {
count={count}
totalSales={totalSales ? totalSales : 0}
matchFundPooled={seasonData?.matchFundPooled}
totalBase={totalBase ? totalBase : 0}
/>
<ProjectSponsors projectId={project.id} />
</>
Expand Down

0 comments on commit f93977a

Please sign in to comment.