Working through issue where prizes are incorrect and need updating.
This commit is contained in:
@@ -6,9 +6,10 @@ import (
|
||||
"log"
|
||||
"synlotto-website/handlers"
|
||||
"synlotto-website/helpers"
|
||||
"synlotto-website/matcher"
|
||||
"synlotto-website/models"
|
||||
"synlotto-website/rules"
|
||||
draws "synlotto-website/services/draws"
|
||||
thunderballrules "synlotto-website/rules"
|
||||
services "synlotto-website/services/draws"
|
||||
)
|
||||
|
||||
func RunTicketMatching(db *sql.DB, triggeredBy string) (models.MatchRunStats, error) {
|
||||
@@ -62,8 +63,8 @@ func RunTicketMatching(db *sql.DB, triggeredBy string) (models.MatchRunStats, er
|
||||
BonusBalls: helpers.BuildBonusSlice(t),
|
||||
}
|
||||
|
||||
draw := draws.GetDrawResultForTicket(db, t.GameType, t.DrawDate)
|
||||
result := handlers.MatchTicketToDraw(matchTicket, draw, rules.ThunderballPrizeRules)
|
||||
draw := services.GetDrawResultForTicket(db, t.GameType, t.DrawDate)
|
||||
result := handlers.MatchTicketToDraw(matchTicket, draw, thunderballrules.ThunderballPrizeRules)
|
||||
|
||||
if result.MatchedDrawID == 0 {
|
||||
continue
|
||||
@@ -130,7 +131,7 @@ func UpdateMissingPrizes(db *sql.DB) error {
|
||||
continue
|
||||
}
|
||||
|
||||
idx, ok := rules.GetThunderballPrizeIndex(t.Main, t.Bonus)
|
||||
idx, ok := thunderballrules.GetThunderballPrizeIndex(t.Main, t.Bonus)
|
||||
if !ok {
|
||||
log.Printf("❌ No index for %d main, %d bonus", t.Main, t.Bonus)
|
||||
continue
|
||||
@@ -163,3 +164,78 @@ func UpdateMissingPrizes(db *sql.DB) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RefreshTicketPrizes(db *sql.DB) error {
|
||||
rows, err := db.Query(`
|
||||
SELECT id, game_type, draw_date, ball1, ball2, ball3, ball4, ball5, ball6,
|
||||
bonus1, bonus2
|
||||
FROM my_tickets
|
||||
`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var t models.MatchTicket
|
||||
var ticketID int
|
||||
|
||||
var b1, b2, b3, b4, b5, b6 sql.NullInt64
|
||||
var bonus1, bonus2 sql.NullInt64
|
||||
|
||||
if err := rows.Scan(
|
||||
&ticketID, &t.GameType, &t.DrawDate, &b1, &b2, &b3, &b4, &b5, &b6, &bonus1, &bonus2,
|
||||
); err != nil {
|
||||
log.Println("⚠️ Failed to scan ticket:", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Build balls
|
||||
t.Balls = helpers.BuildBallsFromNulls(b1, b2, b3, b4, b5, b6)
|
||||
t.BonusBalls = helpers.BuildBonusFromNulls(bonus1, bonus2)
|
||||
|
||||
draw := services.GetDrawResultForTicket(db, t.GameType, t.DrawDate)
|
||||
if draw.DrawID == 0 {
|
||||
log.Printf("❌ No draw result for %s (%s)", t.DrawDate, t.GameType)
|
||||
continue
|
||||
}
|
||||
|
||||
mainMatches := helpers.CountMatches(t.Balls, draw.Balls)
|
||||
bonusMatches := helpers.CountMatches(t.BonusBalls, draw.BonusBalls)
|
||||
prizeTier := matcher.GetPrizeTier(t.GameType, mainMatches, bonusMatches, thunderballrules.ThunderballPrizeRules)
|
||||
isWinner := prizeTier != ""
|
||||
|
||||
var label string
|
||||
var amount float64
|
||||
if t.GameType == "Thunderball" {
|
||||
idx, ok := thunderballrules.GetThunderballPrizeIndex(mainMatches, bonusMatches)
|
||||
if ok {
|
||||
query := fmt.Sprintf(`SELECT prize%d_per_winner FROM prizes_thunderball WHERE draw_date = ?`, idx)
|
||||
var val int
|
||||
err := db.QueryRow(query, t.DrawDate).Scan(&val)
|
||||
if err == nil {
|
||||
amount = float64(val)
|
||||
if val > 0 {
|
||||
label = fmt.Sprintf("£%.2f", amount)
|
||||
} else {
|
||||
label = "Free Ticket"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, err = db.Exec(`
|
||||
UPDATE my_tickets
|
||||
SET matched_main = ?, matched_bonus = ?, prize_tier = ?, is_winner = ?, prize_amount = ?, prize_label = ?
|
||||
WHERE id = ?
|
||||
`, mainMatches, bonusMatches, prizeTier, isWinner, amount, label, ticketID)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("❌ Failed to update ticket %d: %v", ticketID, err)
|
||||
} else {
|
||||
log.Printf("✅ Updated ticket %d → Tier: %s, Label: %s, Matches: %d+%d", ticketID, prizeTier, label, mainMatches, bonusMatches)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user