feat: Refactor database connection handling in draft-related functions for improved flexibility
This commit is contained in:
parent
918e9ff04a
commit
f0c289353e
6 changed files with 38 additions and 33 deletions
|
|
@ -75,8 +75,8 @@ export async function deleteAllDraftPicks(seasonId: string) {
|
|||
* Get all draft picks for a season with participant and sport information
|
||||
* Used for draft eligibility calculations
|
||||
*/
|
||||
export async function getDraftPicksWithSports(seasonId: string) {
|
||||
const db = database();
|
||||
export async function getDraftPicksWithSports(seasonId: string, providedDb?: ReturnType<typeof database>) {
|
||||
const db = providedDb || database();
|
||||
const results = await db
|
||||
.select({
|
||||
id: schema.draftPicks.id,
|
||||
|
|
@ -119,8 +119,8 @@ export async function getDraftPicksWithSports(seasonId: string) {
|
|||
/**
|
||||
* Get team's draft picks with participant and sport information
|
||||
*/
|
||||
export async function getTeamDraftPicksWithSports(teamId: string, seasonId: string) {
|
||||
const db = database();
|
||||
export async function getTeamDraftPicksWithSports(teamId: string, seasonId: string, providedDb?: ReturnType<typeof database>) {
|
||||
const db = providedDb || database();
|
||||
const results = await db
|
||||
.select({
|
||||
id: schema.draftPicks.id,
|
||||
|
|
|
|||
|
|
@ -18,8 +18,8 @@ export async function removeFromQueue(queueId: string) {
|
|||
await db.delete(schema.draftQueue).where(eq(schema.draftQueue.id, queueId));
|
||||
}
|
||||
|
||||
export async function getTeamQueue(teamId: string) {
|
||||
const db = database();
|
||||
export async function getTeamQueue(teamId: string, providedDb?: ReturnType<typeof database>) {
|
||||
const db = providedDb || database();
|
||||
return await db
|
||||
.select()
|
||||
.from(schema.draftQueue)
|
||||
|
|
|
|||
|
|
@ -76,15 +76,16 @@ export async function autoPickForTeam(
|
|||
seasonId: string,
|
||||
teamId: string,
|
||||
draftRounds: number,
|
||||
allTeamIds: string[]
|
||||
allTeamIds: string[],
|
||||
providedDb?: ReturnType<typeof database>
|
||||
) {
|
||||
const db = database();
|
||||
const db = providedDb || database();
|
||||
|
||||
// Calculate eligibility for this team
|
||||
const allPicks = await getDraftPicksWithSports(seasonId);
|
||||
const teamPicks = await getTeamDraftPicksWithSports(teamId, seasonId);
|
||||
const allParticipants = await getParticipantsForSeasonWithSports(seasonId);
|
||||
const seasonSports = await getSeasonSportsSimple(seasonId);
|
||||
const allPicks = await getDraftPicksWithSports(seasonId, db);
|
||||
const teamPicks = await getTeamDraftPicksWithSports(teamId, seasonId, db);
|
||||
const allParticipants = await getParticipantsForSeasonWithSports(seasonId, db);
|
||||
const seasonSports = await getSeasonSportsSimple(seasonId, db);
|
||||
const allTeams = allTeamIds.map((id) => ({ id }));
|
||||
|
||||
const eligibility = calculateDraftEligibility(
|
||||
|
|
@ -103,7 +104,7 @@ export async function autoPickForTeam(
|
|||
);
|
||||
|
||||
// Check queue first - filter by eligible sports
|
||||
const queue = await getTeamQueue(teamId);
|
||||
const queue = await getTeamQueue(teamId, db);
|
||||
|
||||
if (queue.length > 0) {
|
||||
console.log(`[AutoPick] Team ${teamId} has ${queue.length} items in queue`);
|
||||
|
|
@ -182,7 +183,7 @@ export async function autoPickForTeam(
|
|||
// Queue is empty or all queued players drafted/ineligible
|
||||
// Pick highest EV available from eligible sports
|
||||
console.log(`[AutoPick] No valid queue items, selecting highest EV from eligible sports`);
|
||||
return await getTopAvailableParticipant(seasonId, eligibility.eligibleSportIds);
|
||||
return await getTopAvailableParticipant(seasonId, eligibility.eligibleSportIds, db);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -191,9 +192,10 @@ export async function autoPickForTeam(
|
|||
*/
|
||||
export async function getTopAvailableParticipant(
|
||||
seasonId: string,
|
||||
eligibleSportIds?: Set<string>
|
||||
eligibleSportIds?: Set<string>,
|
||||
providedDb?: ReturnType<typeof database>
|
||||
) {
|
||||
const db = database();
|
||||
const db = providedDb || database();
|
||||
|
||||
// Get all drafted participant IDs
|
||||
const draftedPicks = await db
|
||||
|
|
@ -443,7 +445,8 @@ export async function executeAutoPick(params: {
|
|||
seasonId,
|
||||
teamId,
|
||||
season.draftRounds,
|
||||
allTeamIds
|
||||
allTeamIds,
|
||||
db
|
||||
);
|
||||
|
||||
if (!participantId) {
|
||||
|
|
|
|||
|
|
@ -105,8 +105,8 @@ export async function copyParticipantsFromSeason(
|
|||
* Get all participants for a season with sport information
|
||||
* Used for draft eligibility calculations
|
||||
*/
|
||||
export async function getParticipantsForSeasonWithSports(seasonId: string) {
|
||||
const db = database();
|
||||
export async function getParticipantsForSeasonWithSports(seasonId: string, providedDb?: ReturnType<typeof database>) {
|
||||
const db = providedDb || database();
|
||||
|
||||
// First get all sports seasons for this season
|
||||
const seasonSports = await db.query.seasonSports.findMany({
|
||||
|
|
|
|||
|
|
@ -110,8 +110,8 @@ export async function applySportsFromTemplate(
|
|||
/**
|
||||
* Get sports for a season in a simple format for eligibility calculations
|
||||
*/
|
||||
export async function getSeasonSportsSimple(seasonId: string) {
|
||||
const db = database();
|
||||
export async function getSeasonSportsSimple(seasonId: string, providedDb?: ReturnType<typeof database>) {
|
||||
const db = providedDb || database();
|
||||
|
||||
const seasonSports = await db.query.seasonSports.findMany({
|
||||
where: eq(schema.seasonSports.seasonId, seasonId),
|
||||
|
|
|
|||
|
|
@ -118,9 +118,9 @@ async function updateDraftTimers(): Promise<void> {
|
|||
// If timer is at 0 or below, check autodraft settings and trigger auto-pick
|
||||
if (timer.timeRemaining <= 0) {
|
||||
console.log(
|
||||
`[Timer] Timer at 0 for team ${currentTeamId} in season ${season.id}, checking autodraft settings`
|
||||
`[Timer] ⚠️ Timer at ${timer.timeRemaining} for team ${currentTeamId} in season ${season.id} (pick ${currentPickNumber})`
|
||||
);
|
||||
|
||||
|
||||
// Check if team has autodraft enabled
|
||||
const autodraftSettings = await db.query.autodraftSettings.findFirst({
|
||||
where: and(
|
||||
|
|
@ -130,15 +130,12 @@ async function updateDraftTimers(): Promise<void> {
|
|||
});
|
||||
|
||||
const shouldAutodraft = autodraftSettings?.isEnabled ?? false;
|
||||
|
||||
if (shouldAutodraft) {
|
||||
console.log(`[Timer] Autodraft enabled for team ${currentTeamId}, triggering auto-pick`);
|
||||
await triggerAutoPick(season.id, currentTeamId, currentPickNumber, autodraftSettings);
|
||||
} else {
|
||||
console.log(`[Timer] Autodraft disabled for team ${currentTeamId}, triggering regular auto-pick`);
|
||||
await triggerAutoPick(season.id, currentTeamId, currentPickNumber, null);
|
||||
}
|
||||
|
||||
console.log(
|
||||
`[Timer] Team ${currentTeamId} autodraft: ${shouldAutodraft ? 'ENABLED' : 'DISABLED'}, triggering forced auto-pick`
|
||||
);
|
||||
|
||||
await triggerAutoPick(season.id, currentTeamId, currentPickNumber, shouldAutodraft ? autodraftSettings : null);
|
||||
|
||||
continue; // Skip to next draft after triggering pick
|
||||
}
|
||||
|
||||
|
|
@ -171,7 +168,7 @@ async function updateDraftTimers(): Promise<void> {
|
|||
console.log(
|
||||
`[Timer] Timer expired for team ${currentTeamId} in season ${season.id}, checking autodraft settings`
|
||||
);
|
||||
|
||||
|
||||
// Check if team has autodraft enabled
|
||||
const autodraftSettings = await db.query.autodraftSettings.findFirst({
|
||||
where: and(
|
||||
|
|
@ -180,6 +177,11 @@ async function updateDraftTimers(): Promise<void> {
|
|||
),
|
||||
});
|
||||
|
||||
const shouldAutodraft = autodraftSettings?.isEnabled ?? false;
|
||||
console.log(
|
||||
`[Timer] Autodraft ${shouldAutodraft ? 'enabled' : 'disabled'} for team ${currentTeamId}, triggering auto-pick`
|
||||
);
|
||||
|
||||
await triggerAutoPick(season.id, currentTeamId, currentPickNumber, autodraftSettings || null);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue