45 lines
1016 B
Go
45 lines
1016 B
Go
package handlers
|
|
|
|
import (
|
|
"synlotto-website/internal/models"
|
|
)
|
|
|
|
func MatchTicketToDraw(ticket models.MatchTicket, draw models.DrawResult, rules []models.PrizeRule) models.MatchResult {
|
|
mainMatches := countMatches(ticket.Balls, draw.Balls)
|
|
bonusMatches := countMatches(ticket.BonusBalls, draw.BonusBalls)
|
|
|
|
prizeTier := getPrizeTier(ticket.GameType, mainMatches, bonusMatches, rules)
|
|
isWinner := prizeTier != ""
|
|
|
|
return models.MatchResult{
|
|
MatchedDrawID: draw.DrawID,
|
|
MatchedMain: mainMatches,
|
|
MatchedBonus: bonusMatches,
|
|
PrizeTier: prizeTier,
|
|
IsWinner: isWinner,
|
|
}
|
|
}
|
|
|
|
func countMatches(a, b []int) int {
|
|
m := make(map[int]bool)
|
|
for _, n := range b {
|
|
m[n] = true
|
|
}
|
|
match := 0
|
|
for _, n := range a {
|
|
if m[n] {
|
|
match++
|
|
}
|
|
}
|
|
return match
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
return ""
|
|
}
|