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
|
* Get all draft picks for a season with participant and sport information
|
||||||
* Used for draft eligibility calculations
|
* Used for draft eligibility calculations
|
||||||
*/
|
*/
|
||||||
export async function getDraftPicksWithSports(seasonId: string) {
|
export async function getDraftPicksWithSports(seasonId: string, providedDb?: ReturnType<typeof database>) {
|
||||||
const db = database();
|
const db = providedDb || database();
|
||||||
const results = await db
|
const results = await db
|
||||||
.select({
|
.select({
|
||||||
id: schema.draftPicks.id,
|
id: schema.draftPicks.id,
|
||||||
|
|
@ -119,8 +119,8 @@ export async function getDraftPicksWithSports(seasonId: string) {
|
||||||
/**
|
/**
|
||||||
* Get team's draft picks with participant and sport information
|
* Get team's draft picks with participant and sport information
|
||||||
*/
|
*/
|
||||||
export async function getTeamDraftPicksWithSports(teamId: string, seasonId: string) {
|
export async function getTeamDraftPicksWithSports(teamId: string, seasonId: string, providedDb?: ReturnType<typeof database>) {
|
||||||
const db = database();
|
const db = providedDb || database();
|
||||||
const results = await db
|
const results = await db
|
||||||
.select({
|
.select({
|
||||||
id: schema.draftPicks.id,
|
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));
|
await db.delete(schema.draftQueue).where(eq(schema.draftQueue.id, queueId));
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getTeamQueue(teamId: string) {
|
export async function getTeamQueue(teamId: string, providedDb?: ReturnType<typeof database>) {
|
||||||
const db = database();
|
const db = providedDb || database();
|
||||||
return await db
|
return await db
|
||||||
.select()
|
.select()
|
||||||
.from(schema.draftQueue)
|
.from(schema.draftQueue)
|
||||||
|
|
|
||||||
|
|
@ -76,15 +76,16 @@ export async function autoPickForTeam(
|
||||||
seasonId: string,
|
seasonId: string,
|
||||||
teamId: string,
|
teamId: string,
|
||||||
draftRounds: number,
|
draftRounds: number,
|
||||||
allTeamIds: string[]
|
allTeamIds: string[],
|
||||||
|
providedDb?: ReturnType<typeof database>
|
||||||
) {
|
) {
|
||||||
const db = database();
|
const db = providedDb || database();
|
||||||
|
|
||||||
// Calculate eligibility for this team
|
// Calculate eligibility for this team
|
||||||
const allPicks = await getDraftPicksWithSports(seasonId);
|
const allPicks = await getDraftPicksWithSports(seasonId, db);
|
||||||
const teamPicks = await getTeamDraftPicksWithSports(teamId, seasonId);
|
const teamPicks = await getTeamDraftPicksWithSports(teamId, seasonId, db);
|
||||||
const allParticipants = await getParticipantsForSeasonWithSports(seasonId);
|
const allParticipants = await getParticipantsForSeasonWithSports(seasonId, db);
|
||||||
const seasonSports = await getSeasonSportsSimple(seasonId);
|
const seasonSports = await getSeasonSportsSimple(seasonId, db);
|
||||||
const allTeams = allTeamIds.map((id) => ({ id }));
|
const allTeams = allTeamIds.map((id) => ({ id }));
|
||||||
|
|
||||||
const eligibility = calculateDraftEligibility(
|
const eligibility = calculateDraftEligibility(
|
||||||
|
|
@ -103,7 +104,7 @@ export async function autoPickForTeam(
|
||||||
);
|
);
|
||||||
|
|
||||||
// Check queue first - filter by eligible sports
|
// Check queue first - filter by eligible sports
|
||||||
const queue = await getTeamQueue(teamId);
|
const queue = await getTeamQueue(teamId, db);
|
||||||
|
|
||||||
if (queue.length > 0) {
|
if (queue.length > 0) {
|
||||||
console.log(`[AutoPick] Team ${teamId} has ${queue.length} items in queue`);
|
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
|
// Queue is empty or all queued players drafted/ineligible
|
||||||
// Pick highest EV available from eligible sports
|
// Pick highest EV available from eligible sports
|
||||||
console.log(`[AutoPick] No valid queue items, selecting highest EV 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(
|
export async function getTopAvailableParticipant(
|
||||||
seasonId: string,
|
seasonId: string,
|
||||||
eligibleSportIds?: Set<string>
|
eligibleSportIds?: Set<string>,
|
||||||
|
providedDb?: ReturnType<typeof database>
|
||||||
) {
|
) {
|
||||||
const db = database();
|
const db = providedDb || database();
|
||||||
|
|
||||||
// Get all drafted participant IDs
|
// Get all drafted participant IDs
|
||||||
const draftedPicks = await db
|
const draftedPicks = await db
|
||||||
|
|
@ -443,7 +445,8 @@ export async function executeAutoPick(params: {
|
||||||
seasonId,
|
seasonId,
|
||||||
teamId,
|
teamId,
|
||||||
season.draftRounds,
|
season.draftRounds,
|
||||||
allTeamIds
|
allTeamIds,
|
||||||
|
db
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!participantId) {
|
if (!participantId) {
|
||||||
|
|
|
||||||
|
|
@ -105,8 +105,8 @@ export async function copyParticipantsFromSeason(
|
||||||
* Get all participants for a season with sport information
|
* Get all participants for a season with sport information
|
||||||
* Used for draft eligibility calculations
|
* Used for draft eligibility calculations
|
||||||
*/
|
*/
|
||||||
export async function getParticipantsForSeasonWithSports(seasonId: string) {
|
export async function getParticipantsForSeasonWithSports(seasonId: string, providedDb?: ReturnType<typeof database>) {
|
||||||
const db = database();
|
const db = providedDb || database();
|
||||||
|
|
||||||
// First get all sports seasons for this season
|
// First get all sports seasons for this season
|
||||||
const seasonSports = await db.query.seasonSports.findMany({
|
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
|
* Get sports for a season in a simple format for eligibility calculations
|
||||||
*/
|
*/
|
||||||
export async function getSeasonSportsSimple(seasonId: string) {
|
export async function getSeasonSportsSimple(seasonId: string, providedDb?: ReturnType<typeof database>) {
|
||||||
const db = database();
|
const db = providedDb || database();
|
||||||
|
|
||||||
const seasonSports = await db.query.seasonSports.findMany({
|
const seasonSports = await db.query.seasonSports.findMany({
|
||||||
where: eq(schema.seasonSports.seasonId, seasonId),
|
where: eq(schema.seasonSports.seasonId, seasonId),
|
||||||
|
|
|
||||||
|
|
@ -118,7 +118,7 @@ async function updateDraftTimers(): Promise<void> {
|
||||||
// If timer is at 0 or below, check autodraft settings and trigger auto-pick
|
// If timer is at 0 or below, check autodraft settings and trigger auto-pick
|
||||||
if (timer.timeRemaining <= 0) {
|
if (timer.timeRemaining <= 0) {
|
||||||
console.log(
|
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
|
// Check if team has autodraft enabled
|
||||||
|
|
@ -130,14 +130,11 @@ async function updateDraftTimers(): Promise<void> {
|
||||||
});
|
});
|
||||||
|
|
||||||
const shouldAutodraft = autodraftSettings?.isEnabled ?? false;
|
const shouldAutodraft = autodraftSettings?.isEnabled ?? false;
|
||||||
|
console.log(
|
||||||
|
`[Timer] Team ${currentTeamId} autodraft: ${shouldAutodraft ? 'ENABLED' : 'DISABLED'}, triggering forced auto-pick`
|
||||||
|
);
|
||||||
|
|
||||||
if (shouldAutodraft) {
|
await triggerAutoPick(season.id, currentTeamId, currentPickNumber, shouldAutodraft ? autodraftSettings : null);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
continue; // Skip to next draft after triggering pick
|
continue; // Skip to next draft after triggering pick
|
||||||
}
|
}
|
||||||
|
|
@ -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);
|
await triggerAutoPick(season.id, currentTeamId, currentPickNumber, autodraftSettings || null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue