- Added evaluation functions for NFL money line, spread, and total points. - Implemented evaluation functions for Rugby money line, spread, and total points. - Created evaluation functions for Baseball money line, spread, total runs, first inning, and first 5 innings. - Developed unit tests for all sports markets to ensure correct evaluation outcomes. - Introduced ResultCheckerService to handle game result checking for NFL, Rugby, and Baseball. - Updated routes to include new functionality for virtual game handling.
190 lines
5.8 KiB
Go
190 lines
5.8 KiB
Go
package services
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
)
|
|
|
|
// ResultCheckerService handles the checking of game results
|
|
type ResultCheckerService struct {
|
|
// Add any dependencies here (e.g., repositories, external APIs)
|
|
}
|
|
|
|
// NewResultCheckerService creates a new instance of ResultCheckerService
|
|
func NewResultCheckerService() *ResultCheckerService {
|
|
return &ResultCheckerService{}
|
|
}
|
|
|
|
// CheckNFLResult checks the result of an NFL game
|
|
func (s *ResultCheckerService) CheckNFLResult(data json.RawMessage) (*domain.Result, error) {
|
|
nflResult, err := domain.ParseNFLResult(data)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse NFL result: %w", err)
|
|
}
|
|
|
|
winner, err := domain.GetNFLWinner(nflResult)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to determine NFL winner: %w", err)
|
|
}
|
|
|
|
score := domain.FormatNFLScore(nflResult)
|
|
|
|
return &domain.Result{
|
|
Status: determineOutcomeStatus(winner, nflResult.Home.Name, nflResult.Away.Name),
|
|
Score: score,
|
|
FullTimeScore: score,
|
|
SS: nflResult.SS,
|
|
Scores: map[string]domain.Score{
|
|
"1": nflResult.Scores.FirstQuarter,
|
|
"2": nflResult.Scores.SecondQuarter,
|
|
"3": nflResult.Scores.ThirdQuarter,
|
|
"4": nflResult.Scores.FourthQuarter,
|
|
"5": nflResult.Scores.Overtime,
|
|
"7": nflResult.Scores.TotalScore,
|
|
},
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}, nil
|
|
}
|
|
|
|
// determineOutcomeStatus determines the outcome status based on the winner and teams
|
|
func determineOutcomeStatus(winner, homeTeam, awayTeam string) domain.OutcomeStatus {
|
|
if winner == "Draw" {
|
|
return domain.OUTCOME_STATUS_VOID
|
|
}
|
|
if winner == homeTeam {
|
|
return domain.OUTCOME_STATUS_WIN
|
|
}
|
|
if winner == awayTeam {
|
|
return domain.OUTCOME_STATUS_LOSS
|
|
}
|
|
return domain.OUTCOME_STATUS_PENDING
|
|
}
|
|
|
|
// CheckRugbyResult checks the result of a Rugby game
|
|
func (s *ResultCheckerService) CheckRugbyResult(data json.RawMessage) (*domain.Result, error) {
|
|
rugbyResult, err := domain.ParseRugbyResult(data)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse Rugby result: %w", err)
|
|
}
|
|
|
|
winner, err := domain.GetRugbyWinner(rugbyResult)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to determine Rugby winner: %w", err)
|
|
}
|
|
|
|
score := domain.FormatRugbyScore(rugbyResult)
|
|
|
|
return &domain.Result{
|
|
Status: determineOutcomeStatus(winner, rugbyResult.Home.Name, rugbyResult.Away.Name),
|
|
Score: score,
|
|
FullTimeScore: score,
|
|
SS: rugbyResult.SS,
|
|
Scores: map[string]domain.Score{
|
|
"1": rugbyResult.Scores.FirstHalf,
|
|
"2": rugbyResult.Scores.SecondHalf,
|
|
"7": rugbyResult.Scores.TotalScore,
|
|
},
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}, nil
|
|
}
|
|
|
|
// CheckBaseballResult checks the result of a Baseball game
|
|
func (s *ResultCheckerService) CheckBaseballResult(data json.RawMessage) (*domain.Result, error) {
|
|
baseballResult, err := domain.ParseBaseballResult(data)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse Baseball result: %w", err)
|
|
}
|
|
|
|
winner, err := domain.GetBaseballWinner(baseballResult)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to determine Baseball winner: %w", err)
|
|
}
|
|
|
|
score := domain.FormatBaseballScore(baseballResult)
|
|
|
|
return &domain.Result{
|
|
Status: determineOutcomeStatus(winner, baseballResult.Home.Name, baseballResult.Away.Name),
|
|
Score: score,
|
|
FullTimeScore: score,
|
|
SS: baseballResult.SS,
|
|
Scores: map[string]domain.Score{
|
|
"1": baseballResult.Scores.FirstInning,
|
|
"2": baseballResult.Scores.SecondInning,
|
|
"3": baseballResult.Scores.ThirdInning,
|
|
"4": baseballResult.Scores.FourthInning,
|
|
"5": baseballResult.Scores.FifthInning,
|
|
"6": baseballResult.Scores.SixthInning,
|
|
"7": baseballResult.Scores.SeventhInning,
|
|
"8": baseballResult.Scores.EighthInning,
|
|
"9": baseballResult.Scores.NinthInning,
|
|
"10": baseballResult.Scores.ExtraInnings,
|
|
"T": baseballResult.Scores.TotalScore,
|
|
},
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}, nil
|
|
}
|
|
|
|
// CheckRugbyUnionResult checks the result of a Rugby Union game
|
|
func (s *ResultCheckerService) CheckRugbyUnionResult(data json.RawMessage) (*domain.Result, error) {
|
|
rugbyResult, err := domain.ParseRugbyUnionResult(data)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse Rugby Union result: %w", err)
|
|
}
|
|
|
|
winner, err := domain.GetRugbyWinner(rugbyResult)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to determine Rugby Union winner: %w", err)
|
|
}
|
|
|
|
score := domain.FormatRugbyScore(rugbyResult)
|
|
|
|
return &domain.Result{
|
|
Status: determineOutcomeStatus(winner, rugbyResult.Home.Name, rugbyResult.Away.Name),
|
|
Score: score,
|
|
FullTimeScore: score,
|
|
SS: rugbyResult.SS,
|
|
Scores: map[string]domain.Score{
|
|
"1": rugbyResult.Scores.FirstHalf,
|
|
"2": rugbyResult.Scores.SecondHalf,
|
|
"7": rugbyResult.Scores.TotalScore,
|
|
},
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}, nil
|
|
}
|
|
|
|
// CheckRugbyLeagueResult checks the result of a Rugby League game
|
|
func (s *ResultCheckerService) CheckRugbyLeagueResult(data json.RawMessage) (*domain.Result, error) {
|
|
rugbyResult, err := domain.ParseRugbyLeagueResult(data)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to parse Rugby League result: %w", err)
|
|
}
|
|
|
|
winner, err := domain.GetRugbyWinner(rugbyResult)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to determine Rugby League winner: %w", err)
|
|
}
|
|
|
|
score := domain.FormatRugbyScore(rugbyResult)
|
|
|
|
return &domain.Result{
|
|
Status: determineOutcomeStatus(winner, rugbyResult.Home.Name, rugbyResult.Away.Name),
|
|
Score: score,
|
|
FullTimeScore: score,
|
|
SS: rugbyResult.SS,
|
|
Scores: map[string]domain.Score{
|
|
"1": rugbyResult.Scores.FirstHalf,
|
|
"2": rugbyResult.Scores.SecondHalf,
|
|
"7": rugbyResult.Scores.TotalScore,
|
|
},
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}, nil
|
|
}
|