Skip to content

Commit

Permalink
Merge pull request #794 from hackclub/matchups-algo
Browse files Browse the repository at this point in the history
fallbacks to paid v paid in case a user has voted on all unpaid proj…
  • Loading branch information
cskartikey authored Nov 17, 2024
2 parents 498d5e7 + e6826d5 commit 05863f5
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions lib/battles/matchupGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,34 @@ export async function generateMatchup(
const unpaidProjects = availableProjects.filter(
(p) => p.doubloon_payout == null && p.project_source === 'high_seas',
)
// Chris, randomly decide if we want paid vs unpaid or unpaid vs unpaid otherwise our sample size might get too tight
const usePaidComparison = 1 // quicky dirty hack to always use paid vs unpaid

const usePaidComparison = unpaidProjects.length === 0 // if true => paid v paid otherwise unpaid v paid

let project1, project2

if (usePaidComparison) {
const shuffledPaidProjects = [...paidProjects].sort(
() => Math.random() - 0.5,
)
project1 = shuffledPaidProjects[0]
const project1Hours = project1.total_hours || 0
const hourRange = project1Hours * 0.5

const similarHourProjects = shuffledPaidProjects.slice(1).filter((p) => {
// Filter out projects with identical repos
if (project1.repo_url == p.repo_url) return false

const hours = p.total_hours || 0
return Math.abs(hours - project1Hours) <= hourRange
})

project2 =
similarHourProjects.length > 0
? similarHourProjects[
Math.floor(Math.random() * similarHourProjects.length)
]
: shuffledPaidProjects[1]
} else {
const now = Date.now()
const weightedUnpaidProjects = unpaidProjects.map((p) => ({
project: p,
Expand Down Expand Up @@ -201,27 +223,6 @@ export async function generateMatchup(
}
}
if (!project2) project2 = paidProjects[0]
} else {
const shuffled = [...unpaidProjects].sort(() => Math.random() - 0.5)
project1 = shuffled[0]

const project1Hours = project1.total_hours || 0
const hourRange = project1Hours * 0.5

const similarHourProjects = shuffled.slice(1).filter((p) => {
// Filter out projects with identical repos
if (project1.repo_url == p.repo_url) return false

const hours = p.total_hours || 0
return Math.abs(hours - project1Hours) <= hourRange
})

project2 =
similarHourProjects.length > 0
? similarHourProjects[
Math.floor(Math.random() * similarHourProjects.length)
]
: shuffled[1]
}

const uniqueVote = await ensureUniqueVote(
Expand Down

0 comments on commit 05863f5

Please sign in to comment.