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

@@ -280,7 +280,7 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc {
ball1, ball2, ball3, ball4, ball5, ball6, ball1, ball2, ball3, ball4, ball5, ball6,
bonus1, bonus2, bonus1, bonus2,
purchase_method, purchase_date, image_path, duplicate, 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 FROM my_tickets
WHERE userid = ? WHERE userid = ?
ORDER BY draw_date DESC, created_at DESC 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 matchedMain, matchedBonus sql.NullInt64
var prizeTier sql.NullString var prizeTier sql.NullString
var isWinner sql.NullBool var isWinner sql.NullBool
var prizeLabel sql.NullString
var prizeAmount sql.NullFloat64
err := rows.Scan( err := rows.Scan(
&t.Id, &t.GameType, &t.DrawDate, &t.Id, &t.GameType, &t.DrawDate,
&b1, &b2, &b3, &b4, &b5, &b6, &b1, &b2, &b3, &b4, &b5, &b6,
&bo1, &bo2, &bo1, &bo2,
&t.PurchaseMethod, &t.PurchaseDate, &t.ImagePath, &t.Duplicate, &t.PurchaseMethod, &t.PurchaseDate, &t.ImagePath, &t.Duplicate,
&matchedMain, &matchedBonus, &prizeTier, &isWinner, &matchedMain, &matchedBonus, &prizeTier, &isWinner, &prizeLabel, &prizeAmount,
) )
if err != nil { if err != nil {
log.Println("⚠️ Failed to scan ticket row:", err) log.Println("⚠️ Failed to scan ticket row:", err)
@@ -335,7 +337,12 @@ func GetMyTickets(db *sql.DB) http.HandlerFunc {
if isWinner.Valid { if isWinner.Valid {
t.IsWinner = isWinner.Bool t.IsWinner = isWinner.Bool
} }
if prizeLabel.Valid {
t.PrizeLabel = prizeLabel.String
}
if prizeAmount.Valid {
t.PrizeAmount = prizeAmount.Float64
}
// Build balls slices (for template use) // Build balls slices (for template use)
t.Balls = helpers.BuildBallsSlice(t) t.Balls = helpers.BuildBallsSlice(t)
t.BonusBalls = helpers.BuildBonusSlice(t) t.BonusBalls = helpers.BuildBonusSlice(t)

View File

@@ -22,23 +22,23 @@ var ThunderballPrizeRules = []models.PrizeRule{
func GetThunderballPrizeIndex(main, bonus int) (int, bool) { func GetThunderballPrizeIndex(main, bonus int) (int, bool) {
switch { switch {
case main == 5 && bonus == 1: case main == 0 && bonus == 1:
return 9, true return 9, true
case main == 5 && bonus == 0: case main == 1 && bonus == 1:
return 8, true return 8, true
case main == 4 && bonus == 1: case main == 2 && bonus == 1:
return 7, true return 7, true
case main == 4 && bonus == 0: case main == 3 && bonus == 0:
return 6, true return 6, true
case main == 3 && bonus == 1: case main == 3 && bonus == 1:
return 5, true return 5, true
case main == 3 && bonus == 0: case main == 4 && bonus == 0:
return 4, true return 4, true
case main == 2 && bonus == 1: case main == 4 && bonus == 1:
return 3, true return 3, true
case main == 1 && bonus == 1: case main == 5 && bonus == 0:
return 2, true return 2, true
case main == 0 && bonus == 1: case main == 5 && bonus == 1:
return 1, true return 1, true
default: default:
return 0, false return 0, false

View File

@@ -166,8 +166,19 @@ func UpdateMissingPrizes(db *sql.DB) error {
} }
func RefreshTicketPrizes(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(` rows, err := db.Query(`
SELECT id, game_type, draw_date, ball1, ball2, ball3, ball4, ball5, ball6, SELECT id, game_type, draw_date,
ball1, ball2, ball3, ball4, ball5, ball6,
bonus1, bonus2 bonus1, bonus2
FROM my_tickets FROM my_tickets
`) `)
@@ -177,42 +188,43 @@ func RefreshTicketPrizes(db *sql.DB) error {
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var t models.MatchTicket var t TicketRow
var ticketID int 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 {
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) log.Println("⚠️ Failed to scan ticket:", err)
continue continue
} }
tickets = append(tickets, t)
}
rows.Close() // ✅ Release read lock before updating
// Build balls for _, row := range tickets {
t.Balls = helpers.BuildBallsFromNulls(b1, b2, b3, b4, b5, b6) matchTicket := models.MatchTicket{
t.BonusBalls = helpers.BuildBonusFromNulls(bonus1, bonus2) 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 { 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 continue
} }
mainMatches := helpers.CountMatches(t.Balls, draw.Balls) mainMatches := helpers.CountMatches(matchTicket.Balls, draw.Balls)
bonusMatches := helpers.CountMatches(t.BonusBalls, draw.BonusBalls) bonusMatches := helpers.CountMatches(matchTicket.BonusBalls, draw.BonusBalls)
prizeTier := matcher.GetPrizeTier(t.GameType, mainMatches, bonusMatches, thunderballrules.ThunderballPrizeRules) prizeTier := matcher.GetPrizeTier(row.GameType, mainMatches, bonusMatches, thunderballrules.ThunderballPrizeRules)
isWinner := prizeTier != "" isWinner := prizeTier != ""
var label string var label string
var amount float64 var amount float64
if t.GameType == "Thunderball" { if row.GameType == "Thunderball" {
idx, ok := thunderballrules.GetThunderballPrizeIndex(mainMatches, bonusMatches) idx, ok := thunderballrules.GetThunderballPrizeIndex(mainMatches, bonusMatches)
if ok { if ok {
query := fmt.Sprintf(`SELECT prize%d_per_winner FROM prizes_thunderball WHERE draw_date = ?`, idx) query := fmt.Sprintf(`SELECT prize%d_per_winner FROM prizes_thunderball WHERE draw_date = ?`, idx)
var val int var val int
err := db.QueryRow(query, t.DrawDate).Scan(&val) err := db.QueryRow(query, row.DrawDate).Scan(&val)
if err == nil { if err == nil {
amount = float64(val) amount = float64(val)
if val > 0 { if val > 0 {
@@ -224,17 +236,23 @@ func RefreshTicketPrizes(db *sql.DB) error {
} }
} }
_, err = db.Exec(` 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 UPDATE my_tickets
SET matched_main = ?, matched_bonus = ?, prize_tier = ?, is_winner = ?, prize_amount = ?, prize_label = ? SET matched_main = ?, matched_bonus = ?, prize_tier = ?, is_winner = ?, prize_amount = ?, prize_label = ?
WHERE id = ? WHERE id = ?
`, mainMatches, bonusMatches, prizeTier, isWinner, amount, label, ticketID) `, mainMatches, bonusMatches, prizeTier, isWinner, amount, label, row.ID)
if err != nil { if err != nil {
log.Printf("❌ Failed to update ticket %d: %v", ticketID, err) log.Printf("❌ Failed to update ticket %d: %v", row.ID, err)
} else { continue
log.Printf("✅ Updated ticket %d → Tier: %s, Label: %s, Matches: %d+%d", ticketID, prizeTier, label, mainMatches, bonusMatches)
} }
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 return nil

View File

@@ -73,15 +73,13 @@
</td> </td>
<td> <td>
{{ if $ticket.IsWinner }} {{ if $ticket.IsWinner }}
{{ if $ticket.PrizeLabel }} {{ if eq $ticket.PrizeLabel "" }}
{{ if eq $ticket.PrizeLabel "Free Ticket" }} <span class="text-yellow-500 italic">pending</span>
{{ else if eq $ticket.PrizeLabel "Free Ticket" }}
🎟️ {{ $ticket.PrizeLabel }} 🎟️ {{ $ticket.PrizeLabel }}
{{ else }} {{ else }}
💷 {{ $ticket.PrizeLabel }} 💷 {{ $ticket.PrizeLabel }}
{{ end }} {{ end }}
{{ else }}
<span class="text-gray-400 italic">pending</span>
{{ end }}
{{ else }} {{ else }}
{{ end }} {{ end }}