Skip to content

Commit bcdc287

Browse files
authored
Merge pull request #135 from RA341/main
fix autobuy
2 parents 01c8dc1 + cd09eba commit bcdc287

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

core/internal/mam/service.go

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,43 +74,46 @@ func NewMamValidator(token string) error {
7474
func (s *Service) autoBuy() {
7575
log.Info().Msg("starting autobuy")
7676

77-
var vip *BuyVIPResponse
78-
var err error
77+
var availableBonus float64 = 0
7978

8079
if s.conf().AutoBuyVip {
81-
vip, err = s.BuyVIP(0)
80+
vip, err := s.BuyVIP(0)
8281
if err != nil {
8382
log.Warn().Err(err).Msg("unable buy vip")
8483
} else {
8584
log.Info().Any("vip", vip).Msg("bought vip weeks")
85+
availableBonus = vip.SeedBonus
8686
}
8787
} else {
8888
log.Info().Msg("AutoBuy vip is disabled, skipping....")
8989
}
9090

9191
if s.conf().AutoBuyBonus {
9292
gbToBuy := 0
93-
if vip != nil {
94-
MinPointsToKeep := float64(s.conf().MinPointsToKeep)
95-
96-
if vip.SeedBonus > MinPointsToKeep {
97-
98-
gbToBuy = int((vip.SeedBonus - MinPointsToKeep) / PointsPerGB)
99-
// reduce by safeMargin incase we are at the edge
100-
safeMargin := 1
101-
if gbToBuy > safeMargin {
102-
gbToBuy -= safeMargin
103-
}
93+
if availableBonus == 0 {
94+
profile, err := s.GetProfile()
95+
if err != nil {
96+
log.Warn().Err(err).Msg("unable to get profile, available points will be 0")
97+
} else {
98+
availableBonus = float64(profile.Seedbonus)
10499
}
105100
}
106101

107-
log.Info().Int("gb", gbToBuy).Msg("buying bonus")
108-
109-
bonus, err := s.BuyBonus(uint(gbToBuy))
110-
if err != nil {
111-
log.Warn().Err(err).Msg("unable buy bonus")
102+
minPointsToKeep := float64(s.conf().MinPointsToKeep)
103+
if availableBonus > minPointsToKeep {
104+
gbToBuy = int((availableBonus - minPointsToKeep) / PointsPerGB)
105+
log.Info().Int("gb", gbToBuy).Msg("buying bonus")
106+
bonus, err := s.BuyBonus(uint(gbToBuy))
107+
if err != nil {
108+
log.Warn().Err(err).Msg("unable buy bonus")
109+
} else {
110+
log.Info().Any("bonus", bonus).Msg("bought bonus points")
111+
}
112112
} else {
113-
log.Info().Any("bonus", bonus).Msg("bought bonus points")
113+
log.Info().
114+
Float64("available", availableBonus).
115+
Float64("minimum required", minPointsToKeep).
116+
Msg("Insufficient Bonus, skipping")
114117
}
115118
} else {
116119
log.Info().Msg("AutoBuy bonus is disabled, skipping....")
@@ -150,22 +153,15 @@ func divideIntoMamGBAmounts(num uint) []uint {
150153
intervals := []uint{100, 20, 5, 1}
151154

152155
var result []uint
153-
154156
for _, interval := range intervals {
155-
// Calculate how many times the current interval fits into the remaining number
156157
count := n / interval
157158

158159
for i := uint(0); i < count; i++ {
159-
// Add the interval to the result array
160160
result = append(result, interval)
161-
// Subtract the interval from the remaining number
162161
n -= interval
163162
}
164163
}
165164

166-
// 'n' will be the final remainder, which should be 0 since 1 is the smallest interval
167-
// If you wanted to return a remainder, you'd check 'n' here.
168-
169165
return result
170166
}
171167

0 commit comments

Comments
 (0)