From f8dab97a54e27368b3df022b0ac36fec29f68953 Mon Sep 17 00:00:00 2001 From: H3ALY Date: Sat, 29 Mar 2025 15:42:14 +0000 Subject: [PATCH] Working through issue where prizes are incorrect and need updating. --- handlers/admin/manualtriggers.go | 8 +++ helpers/ballslice.go | 28 +++++++++- matcher/engine.go | 4 +- services/tickets/ticketmatching.go | 86 ++++++++++++++++++++++++++++-- static/css/site.css | 11 ++++ templates/admin/triggers.html | 7 +++ templates/layout.html | 9 ---- 7 files changed, 136 insertions(+), 17 deletions(-) diff --git a/handlers/admin/manualtriggers.go b/handlers/admin/manualtriggers.go index 1a8e3ec..8345bcf 100644 --- a/handlers/admin/manualtriggers.go +++ b/handlers/admin/manualtriggers.go @@ -43,6 +43,14 @@ func AdminTriggersHandler(db *sql.DB) http.HandlerFunc { } 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": stats, err := services.RunTicketMatching(db, "manual") if err != nil { diff --git a/helpers/ballslice.go b/helpers/ballslice.go index a69454f..fb5cb33 100644 --- a/helpers/ballslice.go +++ b/helpers/ballslice.go @@ -1,6 +1,9 @@ package helpers -import "synlotto-website/models" +import ( + "database/sql" + "synlotto-website/models" +) func BuildBallsSlice(t models.Ticket) []int { balls := []int{t.Ball1, t.Ball2, t.Ball3, t.Ball4, t.Ball5} @@ -22,3 +25,26 @@ func BuildBonusSlice(t models.Ticket) []int { 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 +} diff --git a/matcher/engine.go b/matcher/engine.go index a66a824..5fc17e2 100644 --- a/matcher/engine.go +++ b/matcher/engine.go @@ -12,7 +12,7 @@ func MatchTicketToDraw(ticket models.MatchTicket, draw models.DrawResult, rules mainMatches := helpers.CountMatches(ticket.Balls, draw.Balls) bonusMatches := helpers.CountMatches(ticket.BonusBalls, draw.BonusBalls) - prizeTier := getPrizeTier(ticket.GameType, mainMatches, bonusMatches, rules) + prizeTier := GetPrizeTier(ticket.GameType, mainMatches, bonusMatches, rules) isWinner := prizeTier != "" result := models.MatchResult{ @@ -43,7 +43,7 @@ func MatchTicketToDraw(ticket models.MatchTicket, draw models.DrawResult, rules 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 { if rule.Game == game && rule.MainMatches == main && rule.BonusMatches == bonus { return rule.Tier diff --git a/services/tickets/ticketmatching.go b/services/tickets/ticketmatching.go index 01b3731..0bec50e 100644 --- a/services/tickets/ticketmatching.go +++ b/services/tickets/ticketmatching.go @@ -6,9 +6,10 @@ import ( "log" "synlotto-website/handlers" "synlotto-website/helpers" + "synlotto-website/matcher" "synlotto-website/models" - "synlotto-website/rules" - draws "synlotto-website/services/draws" + thunderballrules "synlotto-website/rules" + services "synlotto-website/services/draws" ) 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), } - draw := draws.GetDrawResultForTicket(db, t.GameType, t.DrawDate) - result := handlers.MatchTicketToDraw(matchTicket, draw, rules.ThunderballPrizeRules) + draw := services.GetDrawResultForTicket(db, t.GameType, t.DrawDate) + result := handlers.MatchTicketToDraw(matchTicket, draw, thunderballrules.ThunderballPrizeRules) if result.MatchedDrawID == 0 { continue @@ -130,7 +131,7 @@ func UpdateMissingPrizes(db *sql.DB) error { continue } - idx, ok := rules.GetThunderballPrizeIndex(t.Main, t.Bonus) + idx, ok := thunderballrules.GetThunderballPrizeIndex(t.Main, t.Bonus) if !ok { log.Printf("❌ No index for %d main, %d bonus", t.Main, t.Bonus) continue @@ -163,3 +164,78 @@ func UpdateMissingPrizes(db *sql.DB) error { 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 +} diff --git a/static/css/site.css b/static/css/site.css index fe76042..23cc4b9 100644 --- a/static/css/site.css +++ b/static/css/site.css @@ -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 { display: inline-flex; justify-content: center; diff --git a/templates/admin/triggers.html b/templates/admin/triggers.html index 6b7e695..c6869df 100644 --- a/templates/admin/triggers.html +++ b/templates/admin/triggers.html @@ -19,4 +19,11 @@ +
+ {{ .CSRFField }} + + +
{{ end }} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index 2e7f27d..53d2aaa 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -6,15 +6,6 @@ SynLotto -