From b466c351da268a138d611d9d13e1cdc4e1844ed1 Mon Sep 17 00:00:00 2001 From: H3ALY Date: Sun, 30 Mar 2025 23:51:27 +0100 Subject: [PATCH] Fixzed display of prize tiers on tickets --- handlers/ticket_handler.go | 13 +++- rules/thunderball.go | 16 ++--- services/tickets/ticketmatching.go | 80 ++++++++++++++--------- templates/account/tickets/my_tickets.html | 14 ++-- 4 files changed, 73 insertions(+), 50 deletions(-) diff --git a/handlers/ticket_handler.go b/handlers/ticket_handler.go index 6c335eb..acb0bab 100644 --- a/handlers/ticket_handler.go +++ b/handlers/ticket_handler.go @@ -280,7 +280,7 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc { ball1, ball2, ball3, ball4, ball5, ball6, bonus1, bonus2, purchase_method, purchase_date, image_path, duplicate, - matched_main, matched_bonus, prize_tier, is_winner + matched_main, matched_bonus, prize_tier, is_winner, prize_label, prize_amount FROM my_tickets WHERE userid = ? ORDER BY draw_date DESC, created_at DESC @@ -300,13 +300,15 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc { var matchedMain, matchedBonus sql.NullInt64 var prizeTier sql.NullString var isWinner sql.NullBool + var prizeLabel sql.NullString + var prizeAmount sql.NullFloat64 err := rows.Scan( &t.Id, &t.GameType, &t.DrawDate, &b1, &b2, &b3, &b4, &b5, &b6, &bo1, &bo2, &t.PurchaseMethod, &t.PurchaseDate, &t.ImagePath, &t.Duplicate, - &matchedMain, &matchedBonus, &prizeTier, &isWinner, + &matchedMain, &matchedBonus, &prizeTier, &isWinner, &prizeLabel, &prizeAmount, ) if err != nil { log.Println("⚠️ Failed to scan ticket row:", err) @@ -335,7 +337,12 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc { if isWinner.Valid { t.IsWinner = isWinner.Bool } - + if prizeLabel.Valid { + t.PrizeLabel = prizeLabel.String + } + if prizeAmount.Valid { + t.PrizeAmount = prizeAmount.Float64 + } // Build balls slices (for template use) t.Balls = helpers.BuildBallsSlice(t) t.BonusBalls = helpers.BuildBonusSlice(t) diff --git a/rules/thunderball.go b/rules/thunderball.go index 69fdcd5..cd077f6 100644 --- a/rules/thunderball.go +++ b/rules/thunderball.go @@ -22,23 +22,23 @@ var ThunderballPrizeRules = []models.PrizeRule{ func GetThunderballPrizeIndex(main, bonus int) (int, bool) { switch { - case main == 5 && bonus == 1: + case main == 0 && bonus == 1: return 9, true - case main == 5 && bonus == 0: + case main == 1 && bonus == 1: return 8, true - case main == 4 && bonus == 1: + case main == 2 && bonus == 1: return 7, true - case main == 4 && bonus == 0: + case main == 3 && bonus == 0: return 6, true case main == 3 && bonus == 1: return 5, true - case main == 3 && bonus == 0: + case main == 4 && bonus == 0: return 4, true - case main == 2 && bonus == 1: + case main == 4 && bonus == 1: return 3, true - case main == 1 && bonus == 1: + case main == 5 && bonus == 0: return 2, true - case main == 0 && bonus == 1: + case main == 5 && bonus == 1: return 1, true default: return 0, false diff --git a/services/tickets/ticketmatching.go b/services/tickets/ticketmatching.go index 0bec50e..f871aee 100644 --- a/services/tickets/ticketmatching.go +++ b/services/tickets/ticketmatching.go @@ -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 diff --git a/templates/account/tickets/my_tickets.html b/templates/account/tickets/my_tickets.html index c68d957..f4f570f 100644 --- a/templates/account/tickets/my_tickets.html +++ b/templates/account/tickets/my_tickets.html @@ -73,18 +73,16 @@ {{ if $ticket.IsWinner }} - {{ if $ticket.PrizeLabel }} - {{ if eq $ticket.PrizeLabel "Free Ticket" }} - 🎟️ {{ $ticket.PrizeLabel }} - {{ else }} - 💷 {{ $ticket.PrizeLabel }} - {{ end }} + {{ if eq $ticket.PrizeLabel "" }} + pending + {{ else if eq $ticket.PrizeLabel "Free Ticket" }} + 🎟️ {{ $ticket.PrizeLabel }} {{ else }} - pending + 💷 {{ $ticket.PrizeLabel }} {{ end }} {{ else }} – - {{ end }} + {{ end }} {{ end }}