Fixzed display of prize tiers on tickets
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -166,8 +166,19 @@ 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,
|
||||
SELECT id, game_type, draw_date,
|
||||
ball1, ball2, ball3, ball4, ball5, ball6,
|
||||
bonus1, bonus2
|
||||
FROM my_tickets
|
||||
`)
|
||||
@@ -177,42 +188,43 @@ func RefreshTicketPrizes(db *sql.DB) error {
|
||||
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(`
|
||||
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, ticketID)
|
||||
`, 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
|
||||
|
||||
@@ -73,15 +73,13 @@
|
||||
</td>
|
||||
<td>
|
||||
{{ if $ticket.IsWinner }}
|
||||
{{ if $ticket.PrizeLabel }}
|
||||
{{ if eq $ticket.PrizeLabel "Free Ticket" }}
|
||||
{{ if eq $ticket.PrizeLabel "" }}
|
||||
<span class="text-yellow-500 italic">pending</span>
|
||||
{{ else if eq $ticket.PrizeLabel "Free Ticket" }}
|
||||
🎟️ {{ $ticket.PrizeLabel }}
|
||||
{{ else }}
|
||||
💷 {{ $ticket.PrizeLabel }}
|
||||
{{ end }}
|
||||
{{ else }}
|
||||
<span class="text-gray-400 italic">pending</span>
|
||||
{{ end }}
|
||||
{{ else }}
|
||||
–
|
||||
{{ end }}
|
||||
|
||||
Reference in New Issue
Block a user