Fixzed display of prize tiers on tickets

This commit is contained in:
2025-03-30 23:51:27 +01:00
parent f8dab97a54
commit b466c351da
4 changed files with 73 additions and 50 deletions

View File

@@ -166,53 +166,65 @@ func UpdateMissingPrizes(db *sql.DB) error {
}
func RefreshTicketPrizes(db *sql.DB) error {
type TicketRow struct {
ID int
GameType string
DrawDate string
B1, B2, B3, B4, B5, B6 sql.NullInt64
Bonus1, Bonus2 sql.NullInt64
}
var tickets []TicketRow
rows, err := db.Query(`
SELECT id, game_type, draw_date, ball1, ball2, ball3, ball4, ball5, ball6,
bonus1, bonus2
FROM my_tickets
`)
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 {
var t TicketRow
if err := rows.Scan(&t.ID, &t.GameType, &t.DrawDate,
&t.B1, &t.B2, &t.B3, &t.B4, &t.B5, &t.B6, &t.Bonus1, &t.Bonus2); err != nil {
log.Println("⚠️ Failed to scan ticket:", err)
continue
}
tickets = append(tickets, t)
}
rows.Close() // ✅ Release read lock before updating
// Build balls
t.Balls = helpers.BuildBallsFromNulls(b1, b2, b3, b4, b5, b6)
t.BonusBalls = helpers.BuildBonusFromNulls(bonus1, bonus2)
for _, row := range tickets {
matchTicket := models.MatchTicket{
GameType: row.GameType,
DrawDate: row.DrawDate,
Balls: helpers.BuildBallsFromNulls(row.B1, row.B2, row.B3, row.B4, row.B5, row.B6),
BonusBalls: helpers.BuildBonusFromNulls(row.Bonus1, row.Bonus2),
}
draw := services.GetDrawResultForTicket(db, t.GameType, t.DrawDate)
draw := services.GetDrawResultForTicket(db, row.GameType, row.DrawDate)
if draw.DrawID == 0 {
log.Printf("❌ No draw result for %s (%s)", t.DrawDate, t.GameType)
log.Printf("❌ No draw result for %s (%s)", row.DrawDate, row.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)
mainMatches := helpers.CountMatches(matchTicket.Balls, draw.Balls)
bonusMatches := helpers.CountMatches(matchTicket.BonusBalls, draw.BonusBalls)
prizeTier := matcher.GetPrizeTier(row.GameType, mainMatches, bonusMatches, thunderballrules.ThunderballPrizeRules)
isWinner := prizeTier != ""
var label string
var amount float64
if t.GameType == "Thunderball" {
if row.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)
err := db.QueryRow(query, row.DrawDate).Scan(&val)
if err == nil {
amount = float64(val)
if val > 0 {
@@ -224,17 +236,23 @@ func RefreshTicketPrizes(db *sql.DB) error {
}
}
_, 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)
log.Printf("🧪 Ticket %d → Matches: %d+%d, Tier: %s, Winner: %v, Label: %s, Amount: %.2f",
row.ID, mainMatches, bonusMatches, prizeTier, isWinner, label, amount)
res, 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, row.ID)
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)
log.Printf("❌ Failed to update ticket %d: %v", row.ID, err)
continue
}
rowsAffected, _ := res.RowsAffected()
log.Printf("✅ Ticket %d updated — rows affected: %d | Tier: %s | Label: %s | Matches: %d+%d",
row.ID, rowsAffected, prizeTier, label, mainMatches, bonusMatches)
}
return nil