Add bulk import feature for futures odds with fuzzy matching (#3)

* Add bulk import to futures odds page

Adds a textarea where admins can paste odds from FanDuel, DraftKings,
OddsChecker, or any site. A client-side parser extracts American odds
from each line and fuzzy-matches team names to participants (exact,
substring, and word-overlap matching). Shows matched/unmatched results
before applying to the individual input fields.

https://claude.ai/code/session_01JvgcqKSJZ36bEztfg6CkWP

* Code review fixes for futures-odds page

- Remove unused upsertParticipantEVWithNormalization import
- Remove debug console.log statements left in save action
- Move normalizeName to module level (pure function, no component deps)
- Pre-compute normalized participant names once in findParticipantMatch
  instead of re-normalizing across three separate passes
- Fix array index key on unmatched list to use composite string key
- Drop unused result variable from upsertParticipantEV call

https://claude.ai/code/session_01JvgcqKSJZ36bEztfg6CkWP

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Chris Parsons 2026-02-19 14:34:45 -08:00 committed by GitHub
parent 740ffbb411
commit eb260649b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,6 +6,7 @@ import { getAllParticipantEVsForSeason } from '~/models/participant-expected-val
import { Button } from '~/components/ui/button';
import { Input } from '~/components/ui/input';
import { Label } from '~/components/ui/label';
import { Textarea } from '~/components/ui/textarea';
import {
Card,
CardContent,
@ -18,11 +19,8 @@ import {
calculateICMFromOdds,
icmResultToArray,
} from '~/services/icm-calculator';
import {
upsertParticipantEV,
upsertParticipantEVWithNormalization,
} from '~/models/participant-expected-value';
import { Loader2, Info } from 'lucide-react';
import { upsertParticipantEV } from '~/models/participant-expected-value';
import { Loader2, Info, CheckCircle2, AlertCircle } from 'lucide-react';
export async function loader({ params }: Route.LoaderArgs) {
const sportsSeasonId = params.id;
@ -132,13 +130,6 @@ export async function action({ request, params }: Route.ActionArgs) {
// Save probabilities from preview (passed through form)
// This ensures saved values match exactly what user saw in preview
for (const { participantId, odds } of futuresOdds) {
// Debug: check what's in form data
const probFirstRaw = formData.get(`prob_first_${participantId}`);
const probSecondRaw = formData.get(`prob_second_${participantId}`);
console.log(`[Futures Odds Save DEBUG] Looking for participant ${participantId}`);
console.log(` prob_first_${participantId} = ${probFirstRaw} (type: ${typeof probFirstRaw})`);
console.log(` prob_second_${participantId} = ${probSecondRaw} (type: ${typeof probSecondRaw})`);
// Read probabilities from form data (preview results)
const probFirst = parseFloat(formData.get(`prob_first_${participantId}`) as string) || 0;
const probSecond = parseFloat(formData.get(`prob_second_${participantId}`) as string) || 0;
@ -149,11 +140,9 @@ export async function action({ request, params }: Route.ActionArgs) {
const probSeventh = parseFloat(formData.get(`prob_seventh_${participantId}`) as string) || 0;
const probEighth = parseFloat(formData.get(`prob_eighth_${participantId}`) as string) || 0;
console.log(`[Futures Odds Save] ${participantId}: P(1st)=${probFirst}, P(2nd)=${probSecond}, P(3rd)=${probThird}, P(4th)=${probFourth}, P(5th)=${probFifth}, P(6th)=${probSixth}, P(7th)=${probSeventh}, P(8th)=${probEighth}, sum=${probFirst+probSecond+probThird+probFourth+probFifth+probSixth+probSeventh+probEighth}`);
// Use upsertParticipantEV (not WithNormalization) because ICM probabilities
// are already correct - they sum to <1.0 for teams unlikely to finish top 8
const result = await upsertParticipantEV({
await upsertParticipantEV({
participantId,
sportsSeasonId: sportsSeasonId,
probabilities: {
@ -170,11 +159,8 @@ export async function action({ request, params }: Route.ActionArgs) {
source: 'futures_odds',
sourceOdds: odds,
});
console.log(`[Futures Odds Save] Saved ${participantId}: DB probFirst=${result.probFirst}, EV=${result.expectedValue}`);
}
console.log('[Futures Odds Save] All probabilities saved, redirecting...');
return redirect(`/admin/sports-seasons/${sportsSeasonId}/expected-values`);
}
@ -188,6 +174,10 @@ export async function action({ request, params }: Route.ActionArgs) {
}
}
function normalizeName(name: string): string {
return name.toLowerCase().replace(/[^a-z0-9\s]/g, '').replace(/\s+/g, ' ').trim();
}
export default function AdminSportsSeasonFuturesOdds() {
const { sportsSeason, participants, existingOdds } = useLoaderData<typeof loader>();
const actionData = useActionData<ActionData>();
@ -205,6 +195,80 @@ export default function AdminSportsSeasonFuturesOdds() {
return initial;
});
// Bulk import state
const [bulkText, setBulkText] = useState('');
const [parseResults, setParseResults] = useState<{
matched: Array<{ participantId: string; name: string; odds: number; inputName: string }>;
unmatched: Array<{ inputName: string; odds: number }>;
} | null>(null);
function findParticipantMatch(inputName: string) {
const normalizedInput = normalizeName(inputName);
// Pre-compute normalized names once for all three passes
const normalized = participants.map(p => ({ p, n: normalizeName(p.name) }));
// Exact match
const exact = normalized.find(({ n }) => n === normalizedInput);
if (exact) return exact.p;
// Contains match (one contains the other)
const contains = normalized.find(({ n }) => n.includes(normalizedInput) || normalizedInput.includes(n));
if (contains) return contains.p;
// Word-overlap match (at least half the significant words overlap)
const inputWords = normalizedInput.split(' ').filter(w => w.length > 2);
const overlap = normalized.find(({ n }) => {
const pWords = n.split(' ').filter(w => w.length > 2);
const shared = inputWords.filter(w => pWords.includes(w));
return shared.length > 0 && shared.length >= Math.min(inputWords.length, pWords.length) * 0.5;
});
return overlap?.p ?? null;
}
function parseBulkText() {
const lines = bulkText.split('\n');
// Match lines that end with an American-style odds number (+/- then 26 digits)
const oddsPattern = /^(.+?)\s+([+-]\d{2,6})\s*$/;
const matched: Array<{ participantId: string; name: string; odds: number; inputName: string }> = [];
const unmatched: Array<{ inputName: string; odds: number }> = [];
const seenParticipants = new Set<string>();
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.length < 3) continue;
const match = oddsPattern.exec(trimmed);
if (!match) continue;
const inputName = match[1].trim();
const odds = parseInt(match[2], 10);
if (isNaN(odds)) continue;
const participant = findParticipantMatch(inputName);
if (participant && !seenParticipants.has(participant.id)) {
seenParticipants.add(participant.id);
matched.push({ participantId: participant.id, name: participant.name, odds, inputName });
} else if (!participant) {
unmatched.push({ inputName, odds });
}
}
setParseResults({ matched, unmatched });
}
function applyMatches() {
if (!parseResults) return;
const newOdds = { ...oddsValues };
for (const m of parseResults.matched) {
newOdds[m.participantId] = m.odds.toString();
}
setOddsValues(newOdds);
setParseResults(null);
setBulkText('');
}
const isSubmitting = navigation.state === 'submitting';
const isGenerating = isSubmitting && navigation.formData?.get('intent') === 'preview';
const isSaving = isSubmitting && navigation.formData?.get('intent') === 'save';
@ -218,6 +282,80 @@ export default function AdminSportsSeasonFuturesOdds() {
</p>
</div>
{/* Bulk Import */}
<Card className="mb-6">
<CardHeader>
<CardTitle>Bulk Import</CardTitle>
<CardDescription>
Paste odds from FanDuel, DraftKings, OddsChecker, or any site. Each line should end with
American odds (e.g. <code>Kansas City Chiefs +450</code>). Team names are fuzzy-matched to
participants.
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<Textarea
placeholder={`Paste odds here, one team per line:\nKansas City Chiefs +450\nSan Francisco 49ers +600\nBaltimore Ravens +700`}
value={bulkText}
onChange={e => { setBulkText(e.target.value); setParseResults(null); }}
rows={8}
className="font-mono text-sm"
/>
<Button type="button" variant="outline" onClick={parseBulkText} disabled={!bulkText.trim()}>
Parse Odds
</Button>
{parseResults && (
<div className="space-y-3">
{parseResults.matched.length > 0 && (
<div>
<div className="flex items-center gap-2 text-sm font-medium text-green-700 mb-2">
<CheckCircle2 className="h-4 w-4" />
Matched ({parseResults.matched.length})
</div>
<div className="rounded-md border border-green-200 bg-green-50 divide-y divide-green-100 text-sm">
{parseResults.matched.map(m => (
<div key={m.participantId} className="flex justify-between px-3 py-1.5">
<span className="text-muted-foreground">{m.inputName}</span>
<span className="font-medium">
{m.name} &rarr; {m.odds > 0 ? '+' : ''}{m.odds}
</span>
</div>
))}
</div>
</div>
)}
{parseResults.unmatched.length > 0 && (
<div>
<div className="flex items-center gap-2 text-sm font-medium text-amber-700 mb-2">
<AlertCircle className="h-4 w-4" />
Not matched ({parseResults.unmatched.length}) enter manually below
</div>
<div className="rounded-md border border-amber-200 bg-amber-50 divide-y divide-amber-100 text-sm">
{parseResults.unmatched.map((u, i) => (
<div key={`${u.inputName}-${i}`} className="flex justify-between px-3 py-1.5">
<span>{u.inputName}</span>
<span className="font-medium">{u.odds > 0 ? '+' : ''}{u.odds}</span>
</div>
))}
</div>
</div>
)}
{parseResults.matched.length === 0 && parseResults.unmatched.length === 0 && (
<p className="text-sm text-muted-foreground">No odds found. Make sure each line ends with a value like +450 or -200.</p>
)}
{parseResults.matched.length > 0 && (
<Button type="button" onClick={applyMatches}>
Apply {parseResults.matched.length} matched odds to form
</Button>
)}
</div>
)}
</CardContent>
</Card>
<div className="grid gap-6 lg:grid-cols-2">
<div>
<Card>