Lots of changes, still more to do
This commit is contained in:
44
handlers/ticket_matcher.go
Normal file
44
handlers/ticket_matcher.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"synlotto-website/models"
|
||||
)
|
||||
|
||||
func MatchTicketToDraw(ticket models.Ticket, 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 ""
|
||||
}
|
||||
Reference in New Issue
Block a user