Working through issue where prizes are incorrect and need updating.

This commit is contained in:
2025-03-29 15:42:14 +00:00
parent c0143df8c0
commit f8dab97a54
7 changed files with 136 additions and 17 deletions

View File

@@ -43,6 +43,14 @@ func AdminTriggersHandler(db *sql.DB) http.HandlerFunc {
} }
flashMsg = "✅ Missing prizes updated." flashMsg = "✅ Missing prizes updated."
case "refresh_prizes":
err := services.RefreshTicketPrizes(db)
if err != nil {
http.Error(w, "Refresh failed: "+err.Error(), http.StatusInternalServerError)
return
}
flashMsg = "✅ Ticket prizes refreshed."
case "run_all": case "run_all":
stats, err := services.RunTicketMatching(db, "manual") stats, err := services.RunTicketMatching(db, "manual")
if err != nil { if err != nil {

View File

@@ -1,6 +1,9 @@
package helpers package helpers
import "synlotto-website/models" import (
"database/sql"
"synlotto-website/models"
)
func BuildBallsSlice(t models.Ticket) []int { func BuildBallsSlice(t models.Ticket) []int {
balls := []int{t.Ball1, t.Ball2, t.Ball3, t.Ball4, t.Ball5} balls := []int{t.Ball1, t.Ball2, t.Ball3, t.Ball4, t.Ball5}
@@ -22,3 +25,26 @@ func BuildBonusSlice(t models.Ticket) []int {
return bonuses return bonuses
} }
// BuildBallsFromNulls builds main balls from sql.NullInt64 values
func BuildBallsFromNulls(vals ...sql.NullInt64) []int {
var result []int
for _, v := range vals {
if v.Valid {
result = append(result, int(v.Int64))
}
}
return result
}
// BuildBonusFromNulls builds bonus balls from two sql.NullInt64 values
func BuildBonusFromNulls(b1, b2 sql.NullInt64) []int {
var result []int
if b1.Valid {
result = append(result, int(b1.Int64))
}
if b2.Valid {
result = append(result, int(b2.Int64))
}
return result
}

View File

@@ -12,7 +12,7 @@ func MatchTicketToDraw(ticket models.MatchTicket, draw models.DrawResult, rules
mainMatches := helpers.CountMatches(ticket.Balls, draw.Balls) mainMatches := helpers.CountMatches(ticket.Balls, draw.Balls)
bonusMatches := helpers.CountMatches(ticket.BonusBalls, draw.BonusBalls) bonusMatches := helpers.CountMatches(ticket.BonusBalls, draw.BonusBalls)
prizeTier := getPrizeTier(ticket.GameType, mainMatches, bonusMatches, rules) prizeTier := GetPrizeTier(ticket.GameType, mainMatches, bonusMatches, rules)
isWinner := prizeTier != "" isWinner := prizeTier != ""
result := models.MatchResult{ result := models.MatchResult{
@@ -43,7 +43,7 @@ func MatchTicketToDraw(ticket models.MatchTicket, draw models.DrawResult, rules
return result return result
} }
func getPrizeTier(game string, main, bonus int, rules []models.PrizeRule) string { func GetPrizeTier(game string, main, bonus int, rules []models.PrizeRule) string {
for _, rule := range rules { for _, rule := range rules {
if rule.Game == game && rule.MainMatches == main && rule.BonusMatches == bonus { if rule.Game == game && rule.MainMatches == main && rule.BonusMatches == bonus {
return rule.Tier return rule.Tier

View File

@@ -6,9 +6,10 @@ import (
"log" "log"
"synlotto-website/handlers" "synlotto-website/handlers"
"synlotto-website/helpers" "synlotto-website/helpers"
"synlotto-website/matcher"
"synlotto-website/models" "synlotto-website/models"
"synlotto-website/rules" thunderballrules "synlotto-website/rules"
draws "synlotto-website/services/draws" services "synlotto-website/services/draws"
) )
func RunTicketMatching(db *sql.DB, triggeredBy string) (models.MatchRunStats, error) { 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), BonusBalls: helpers.BuildBonusSlice(t),
} }
draw := draws.GetDrawResultForTicket(db, t.GameType, t.DrawDate) draw := services.GetDrawResultForTicket(db, t.GameType, t.DrawDate)
result := handlers.MatchTicketToDraw(matchTicket, draw, rules.ThunderballPrizeRules) result := handlers.MatchTicketToDraw(matchTicket, draw, thunderballrules.ThunderballPrizeRules)
if result.MatchedDrawID == 0 { if result.MatchedDrawID == 0 {
continue continue
@@ -130,7 +131,7 @@ func UpdateMissingPrizes(db *sql.DB) error {
continue continue
} }
idx, ok := rules.GetThunderballPrizeIndex(t.Main, t.Bonus) idx, ok := thunderballrules.GetThunderballPrizeIndex(t.Main, t.Bonus)
if !ok { if !ok {
log.Printf("❌ No index for %d main, %d bonus", t.Main, t.Bonus) log.Printf("❌ No index for %d main, %d bonus", t.Main, t.Bonus)
continue continue
@@ -163,3 +164,78 @@ func UpdateMissingPrizes(db *sql.DB) error {
return nil 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
}

View File

@@ -1,3 +1,14 @@
body { font-family: Arial, sans-serif; margin: 40px; }
table { border-collapse: collapse; width: 100%; margin-top: 20px; }
th, td { padding: 8px 12px; border: 1px solid #ddd; text-align: center; }
th { background-color: #f5f5f5; }
.form-section { margin-bottom: 20px; }
.topbar { margin-bottom: 20px; }
.flash { padding: 10px; color: green; background: #e9ffe9; border: 1px solid #c3e6c3; margin-bottom: 15px; }
/* Ball Stuff */
.ball { .ball {
display: inline-flex; display: inline-flex;
justify-content: center; justify-content: center;

View File

@@ -19,4 +19,11 @@
<button>Run All</button> <button>Run All</button>
</form> </form>
<form method="POST" action="/admin/triggers" class="mt-4">
{{ .CSRFField }}
<input type="hidden" name="action" value="refresh_prizes">
<button class="bg-indigo-500 text-white px-4 py-2 rounded hover:bg-indigo-600">
Refresh Ticket Prizes
</button>
</form>
{{ end }} {{ end }}

View File

@@ -6,15 +6,6 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>SynLotto</title> <title>SynLotto</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
table { border-collapse: collapse; width: 100%; margin-top: 20px; }
th, td { padding: 8px 12px; border: 1px solid #ddd; text-align: center; }
th { background-color: #f5f5f5; }
.form-section { margin-bottom: 20px; }
.topbar { margin-bottom: 20px; }
.flash { padding: 10px; color: green; background: #e9ffe9; border: 1px solid #c3e6c3; margin-bottom: 15px; }
</style>
</head> </head>
<body> <body>
<div class="topbar"> <div class="topbar">