From 2c85257a29de974ed5d143ea614b3740dd83d96e Mon Sep 17 00:00:00 2001 From: CalebRose Date: Wed, 3 Jan 2024 11:00:42 -0800 Subject: [PATCH] Fixing bugs within FA and gameplan --- managers/FreeAgencyManager.go | 76 +++++++++++++++++++++++++++++++++-- structs/BaseGameplan.go | 16 ++++++++ 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/managers/FreeAgencyManager.go b/managers/FreeAgencyManager.go index 913dc32..4deca49 100644 --- a/managers/FreeAgencyManager.go +++ b/managers/FreeAgencyManager.go @@ -7,6 +7,7 @@ import ( "sort" "strconv" "strings" + "sync" "github.com/CalebRose/SimFBA/dbprovider" "github.com/CalebRose/SimFBA/models" @@ -244,6 +245,8 @@ func SyncFreeAgencyOffers() { db.Save(&ts) FreeAgents := GetAllFreeAgents() + capsheetMap := getCapsheetMap() + for _, FA := range FreeAgents { // If the Free Agent is not available in off-season free agency anymore if ts.IsNFLOffSeason && !FA.IsNegotiating && !FA.IsAcceptingOffers { @@ -275,6 +278,17 @@ func SyncFreeAgencyOffers() { WinningOffer := structs.FreeAgencyOffer{} for _, Offer := range Offers { + // Calculate to see if team can afford to pay for contract in Y1 + capsheet := capsheetMap[Offer.TeamID] + if capsheet.ID == 0 { + // Invalid!! + continue + } + y1CapSpace := ts.Y1Capspace - capsheet.Y1Bonus - capsheet.Y1Salary - capsheet.Y1CapHit + y1Remaining := y1CapSpace - Offer.Y1BaseSalary - Offer.Y1Bonus + if y1CapSpace < 0 || y1Remaining < 0 { + continue + } // Get the Contract with the best value for the FA if Offer.IsActive && WinningOffer.ID == 0 { WinningOffer = Offer @@ -304,11 +318,28 @@ func SyncFreeAgencyOffers() { contract.DeactivateContract() db.Delete(&contract) } else { + var winningOffer structs.NFLWaiverOffer offers := GetWaiverOffersByPlayerID(strconv.Itoa(int(w.ID))) - winningOffer := offers[0] - w.SignPlayer(int(winningOffer.TeamID), winningOffer.Team) - contract := GetContractByPlayerID(strconv.Itoa(int(w.ID))) + for _, Offer := range offers { + // Calculate to see if team can afford to pay for contract in Y1 + capsheet := capsheetMap[Offer.TeamID] + if capsheet.ID == 0 { + // Invalid!! + continue + } + y1CapSpace := ts.Y1Capspace - capsheet.Y1Bonus - capsheet.Y1Salary - capsheet.Y1CapHit + y1Remaining := y1CapSpace - contract.Y1BaseSalary - contract.Y1Bonus + if y1CapSpace < 0 || y1Remaining < 0 { + continue + } + winningOffer = Offer + break + } + if winningOffer.ID == 0 { + continue + } + w.SignPlayer(int(winningOffer.TeamID), winningOffer.Team) contract.ReassignTeam(winningOffer.TeamID, winningOffer.Team) db.Save(&contract) @@ -339,7 +370,7 @@ func SyncFreeAgencyOffers() { for _, p := range practiceSquad { Offers := GetFreeAgentOffersByPlayerID(strconv.Itoa(int(p.ID))) - + contract := GetContractByPlayerID(strconv.Itoa(int(p.ID))) if len(Offers) == 0 { continue } @@ -361,6 +392,17 @@ func SyncFreeAgencyOffers() { WinningOffer := structs.FreeAgencyOffer{} for _, Offer := range Offers { + // Calculate to see if team can afford to pay for contract in Y1 + capsheet := capsheetMap[Offer.TeamID] + if capsheet.ID == 0 { + // Invalid!! + continue + } + y1CapSpace := ts.Y1Capspace - capsheet.Y1Bonus - capsheet.Y1Salary - capsheet.Y1CapHit + y1Remaining := y1CapSpace - contract.Y1BaseSalary - contract.Y1Bonus + if y1CapSpace < 0 || y1Remaining < 0 { + continue + } // Get the Contract with the best value for the FA if Offer.IsActive && WinningOffer.ID == 0 { WinningOffer = Offer @@ -693,3 +735,29 @@ func GetRetiredContracts() []structs.NFLContract { return contracts } + +func getCapsheetMap() map[uint]structs.NFLCapsheet { + capsheetMap := make(map[uint]structs.NFLCapsheet) + var mu sync.Mutex // to safely update the map + var wg sync.WaitGroup // to wait for all goroutines to finish + semaphore := make(chan struct{}, 10) + nflTeams := GetAllNFLTeams() + + for _, team := range nflTeams { + semaphore <- struct{}{} + wg.Add(1) + go func(t structs.NFLTeam) { + defer wg.Done() + capsheet := GetCapsheetByTeamID(strconv.Itoa(int(t.ID))) + mu.Lock() + capsheetMap[t.ID] = capsheet + mu.Unlock() + + <-semaphore + }(team) + } + + wg.Wait() + close(semaphore) + return capsheetMap +} diff --git a/structs/BaseGameplan.go b/structs/BaseGameplan.go index bf03f19..9acb504 100644 --- a/structs/BaseGameplan.go +++ b/structs/BaseGameplan.go @@ -165,26 +165,31 @@ func (bg *BaseGameplan) UpdateCollegeGameplan(dto CollegeGameplan) { bg.OffForm1TraditionalRun = dto.OffForm1TraditionalRun bg.OffForm1OptionRun = dto.OffForm1OptionRun bg.OffForm1RPO = dto.OffForm1RPO + bg.OffForm1Pass = dto.OffForm1Pass bg.OffFormation2Name = dto.OffFormation2Name bg.OffForm2Weight = dto.OffForm2Weight bg.OffForm2TraditionalRun = dto.OffForm2TraditionalRun bg.OffForm2OptionRun = dto.OffForm2OptionRun bg.OffForm2RPO = dto.OffForm2RPO + bg.OffForm2Pass = dto.OffForm2Pass bg.OffFormation3Name = dto.OffFormation3Name bg.OffForm3Weight = dto.OffForm3Weight bg.OffForm3TraditionalRun = dto.OffForm3TraditionalRun bg.OffForm3OptionRun = dto.OffForm3OptionRun bg.OffForm3RPO = dto.OffForm3RPO + bg.OffForm3Pass = dto.OffForm3Pass bg.OffFormation4Name = dto.OffFormation4Name bg.OffForm4Weight = dto.OffForm4Weight bg.OffForm4TraditionalRun = dto.OffForm4TraditionalRun bg.OffForm4OptionRun = dto.OffForm4OptionRun bg.OffForm4RPO = dto.OffForm4RPO + bg.OffForm4Pass = dto.OffForm4Pass bg.OffFormation5Name = dto.OffFormation5Name bg.OffForm5Weight = dto.OffForm5Weight bg.OffForm5TraditionalRun = dto.OffForm5TraditionalRun bg.OffForm5OptionRun = dto.OffForm5OptionRun bg.OffForm5RPO = dto.OffForm5RPO + bg.OffForm5Pass = dto.OffForm5Pass bg.RunnerDistributionQB = dto.RunnerDistributionQB bg.RunnerDistributionRB1 = dto.RunnerDistributionRB1 bg.RunnerDistributionRB2 = dto.RunnerDistributionRB2 @@ -219,6 +224,9 @@ func (bg *BaseGameplan) UpdateCollegeGameplan(dto CollegeGameplan) { bg.ChoiceOutside = dto.ChoiceOutside bg.ChoiceInside = dto.ChoiceInside bg.ChoicePower = dto.ChoicePower + bg.PeekOutside = dto.PeekOutside + bg.PeekInside = dto.PeekInside + bg.PeekPower = dto.PeekPower bg.TargetingWR1 = dto.TargetingWR1 bg.TargetDepthWR1 = dto.TargetDepthWR1 bg.TargetingWR2 = dto.TargetingWR2 @@ -284,26 +292,31 @@ func (bg *BaseGameplan) UpdateNFLGameplan(dto NFLGameplan) { bg.OffForm1TraditionalRun = dto.OffForm1TraditionalRun bg.OffForm1OptionRun = dto.OffForm1OptionRun bg.OffForm1RPO = dto.OffForm1RPO + bg.OffForm1Pass = dto.OffForm1Pass bg.OffFormation2Name = dto.OffFormation2Name bg.OffForm2Weight = dto.OffForm2Weight bg.OffForm2TraditionalRun = dto.OffForm2TraditionalRun bg.OffForm2OptionRun = dto.OffForm2OptionRun bg.OffForm2RPO = dto.OffForm2RPO + bg.OffForm2Pass = dto.OffForm2Pass bg.OffFormation3Name = dto.OffFormation3Name bg.OffForm3Weight = dto.OffForm3Weight bg.OffForm3TraditionalRun = dto.OffForm3TraditionalRun bg.OffForm3OptionRun = dto.OffForm3OptionRun bg.OffForm3RPO = dto.OffForm3RPO + bg.OffForm3Pass = dto.OffForm3Pass bg.OffFormation4Name = dto.OffFormation4Name bg.OffForm4Weight = dto.OffForm4Weight bg.OffForm4TraditionalRun = dto.OffForm4TraditionalRun bg.OffForm4OptionRun = dto.OffForm4OptionRun bg.OffForm4RPO = dto.OffForm4RPO + bg.OffForm4Pass = dto.OffForm4Pass bg.OffFormation5Name = dto.OffFormation5Name bg.OffForm5Weight = dto.OffForm5Weight bg.OffForm5TraditionalRun = dto.OffForm5TraditionalRun bg.OffForm5OptionRun = dto.OffForm5OptionRun bg.OffForm5RPO = dto.OffForm5RPO + bg.OffForm5Pass = dto.OffForm5Pass bg.RunnerDistributionQB = dto.RunnerDistributionQB bg.RunnerDistributionRB1 = dto.RunnerDistributionRB1 bg.RunnerDistributionRB2 = dto.RunnerDistributionRB2 @@ -338,6 +351,9 @@ func (bg *BaseGameplan) UpdateNFLGameplan(dto NFLGameplan) { bg.ChoiceOutside = dto.ChoiceOutside bg.ChoiceInside = dto.ChoiceInside bg.ChoicePower = dto.ChoicePower + bg.PeekOutside = dto.PeekOutside + bg.PeekInside = dto.PeekInside + bg.PeekPower = dto.PeekPower bg.TargetingWR1 = dto.TargetingWR1 bg.TargetDepthWR1 = dto.TargetDepthWR1 bg.TargetingWR2 = dto.TargetingWR2