Skip to content

Commit

Permalink
Fixing bugs within FA and gameplan
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebRose committed Jan 3, 2024
1 parent 4196997 commit 2c85257
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
76 changes: 72 additions & 4 deletions managers/FreeAgencyManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"
"strconv"
"strings"
"sync"

"github.com/CalebRose/SimFBA/dbprovider"
"github.com/CalebRose/SimFBA/models"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
16 changes: 16 additions & 0 deletions structs/BaseGameplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2c85257

Please sign in to comment.