Lots of changes around viewing tickets from css perspective logic changes nwe handlers and service triggers... just lots of stuff...

This commit is contained in:
2025-03-28 10:05:54 +00:00
parent e13b375af7
commit 23e0208317
22 changed files with 410 additions and 148 deletions

View File

@@ -1,21 +0,0 @@
package services
import (
"database/sql"
"net/http"
"strconv"
"synlotto-website/services"
)
var db *sql.DB
func AdminMatchHandler(w http.ResponseWriter, r *http.Request) {
stats, err := services.RunTicketMatching(db, "manual")
if err != nil {
http.Error(w, "Matching failed: "+err.Error(), 500)
return
}
http.Redirect(w, r, "/admin?flash=Matched "+
strconv.Itoa(stats.TicketsMatched)+" tickets, "+
strconv.Itoa(stats.WinnersFound)+" winners.", http.StatusSeeOther)
}

View File

@@ -0,0 +1,29 @@
package services
import (
"database/sql"
"log"
"synlotto-website/models"
)
func GetDrawResultForTicket(db *sql.DB, game string, drawDate string) models.DrawResult {
var result models.DrawResult
query := `
SELECT id, ball1, ball2, ball3, ball4, ball5, bonus1
FROM results_thunderball
WHERE draw_date = ?
`
var b1, b2, b3, b4, b5, bonus sql.NullInt64
err := db.QueryRow(query, drawDate).Scan(&result.DrawID, &b1, &b2, &b3, &b4, &b5, &bonus)
if err != nil {
log.Printf("No draw found for %s %s: %v", game, drawDate, err)
return result
}
result.GameType = game
result.DrawDate = drawDate
result.Balls = []int{int(b1.Int64), int(b2.Int64), int(b3.Int64), int(b4.Int64), int(b5.Int64)}
if bonus.Valid {
result.BonusBalls = []int{int(bonus.Int64)}
}
return result
}

View File

@@ -1,90 +0,0 @@
package services
import (
"database/sql"
"log"
"synlotto-website/handlers"
"synlotto-website/models"
"synlotto-website/rules"
)
func RunTicketMatching(db *sql.DB, triggeredBy string) (models.MatchRunStats, error) {
stats := models.MatchRunStats{}
rows, err := db.Query(`
SELECT id, game_type, draw_date, ball1, ball2, ball3, ball4, ball5, bonus1
FROM my_tickets
WHERE matched_draw_id IS NULL
`)
if err != nil {
return stats, err
}
defer rows.Close()
for rows.Next() {
var t models.Ticket
var b1, b2, b3, b4, b5, bonus sql.NullInt64
if err := rows.Scan(&t.ID, &t.GameType, &t.DrawDate, &b1, &b2, &b3, &b4, &b5, &bonus); err != nil {
continue
}
t.Balls = []int{int(b1.Int64), int(b2.Int64), int(b3.Int64), int(b4.Int64), int(b5.Int64)}
if bonus.Valid {
t.BonusBalls = []int{int(bonus.Int64)}
} else {
t.BonusBalls = nil
}
draw := GetDrawResultForTicket(db, t.GameType, t.DrawDate)
result := handlers.MatchTicketToDraw(t, draw, rules.ThunderballPrizeRules)
if result.MatchedDrawID == 0 {
continue // no draw found
}
_, err := db.Exec(`
UPDATE my_tickets SET
matched_draw_id = ?, matched_main = ?, matched_bonus = ?, prize_tier = ?, is_winner = ?
WHERE id = ?`,
result.MatchedDrawID, result.MatchedMain, result.MatchedBonus, result.PrizeTier, result.IsWinner, t.ID,
)
if err != nil {
continue
}
stats.TicketsMatched++
if result.IsWinner {
stats.WinnersFound++
}
}
_, _ = db.Exec(`
INSERT INTO matching_log (triggered_by, tickets_matched, winners_found)
VALUES (?, ?, ?)`,
triggeredBy, stats.TicketsMatched, stats.WinnersFound,
)
return stats, nil
}
func GetDrawResultForTicket(db *sql.DB, game string, drawDate string) models.DrawResult {
var result models.DrawResult
query := `
SELECT id, ball1, ball2, ball3, ball4, ball5, bonus1
FROM results_thunderball
WHERE draw_date = ?
`
var b1, b2, b3, b4, b5, bonus sql.NullInt64
err := db.QueryRow(query, drawDate).Scan(&result.DrawID, &b1, &b2, &b3, &b4, &b5, &bonus)
if err != nil {
log.Printf("No draw found for %s %s: %v", game, drawDate, err)
return result
}
result.GameType = game
result.DrawDate = drawDate
result.Balls = []int{int(b1.Int64), int(b2.Int64), int(b3.Int64), int(b4.Int64), int(b5.Int64)}
if bonus.Valid {
result.BonusBalls = []int{int(bonus.Int64)}
}
return result
}

View File

@@ -0,0 +1,85 @@
package services
import (
"database/sql"
"log"
"synlotto-website/handlers"
"synlotto-website/helpers"
"synlotto-website/models"
"synlotto-website/rules"
)
func RunTicketMatching(db *sql.DB, triggeredBy string) (models.MatchRunStats, error) {
stats := models.MatchRunStats{}
rows, err := db.Query(`
SELECT id, game_type, draw_date,
ball1, ball2, ball3, ball4, ball5, ball6,
bonus1, bonus2
FROM my_tickets
WHERE matched_main IS NULL
`)
if err != nil {
return stats, err
}
defer rows.Close()
for rows.Next() {
var t models.Ticket
var b1, b2, b3, b4, b5, b6, bo1, bo2 sql.NullInt64
if err := rows.Scan(
&t.Id, &t.GameType, &t.DrawDate,
&b1, &b2, &b3, &b4, &b5, &b6,
&bo1, &bo2,
); err != nil {
continue
}
t.Ball1 = int(b1.Int64)
t.Ball2 = int(b2.Int64)
t.Ball3 = int(b3.Int64)
t.Ball4 = int(b4.Int64)
t.Ball5 = int(b5.Int64)
t.Ball6 = int(b6.Int64)
t.Bonus1 = helpers.IntPtrIfValid(bo1)
t.Bonus2 = helpers.IntPtrIfValid(bo2)
matchTicket := models.MatchTicket{
ID: t.Id,
GameType: t.GameType,
DrawDate: t.DrawDate,
Balls: helpers.BuildBallsSlice(t),
BonusBalls: helpers.BuildBonusSlice(t),
}
draw := GetDrawResultForTicket(db, t.GameType, t.DrawDate)
result := handlers.MatchTicketToDraw(matchTicket, draw, rules.ThunderballPrizeRules)
if result.MatchedDrawID == 0 {
continue
}
_, err = db.Exec(`
UPDATE my_tickets
SET matched_main = ?, matched_bonus = ?, prize_tier = ?, is_winner = ?
WHERE id = ?
`, result.MatchedMain, result.MatchedBonus, result.PrizeTier, result.IsWinner, t.Id)
if err != nil {
log.Println("⚠️ Failed to update ticket match:", err)
continue
}
stats.TicketsMatched++
if result.IsWinner {
stats.WinnersFound++
}
}
_, _ = db.Exec(`
INSERT INTO log_ticket_matching (triggered_by, tickets_matched, winners_found)
VALUES (?, ?, ?)
`, triggeredBy, stats.TicketsMatched, stats.WinnersFound)
return stats, nil
}