sorarebuddy/sorare_utils/update_service.go

190 lines
5.1 KiB
Go
Raw Permalink Normal View History

package sorare_utils
import (
"context"
2024-05-23 04:18:54 +00:00
"time"
"git.lehouerou.net/laurent/sorare"
"git.lehouerou.net/laurent/sorare/football"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
2024-05-23 04:18:54 +00:00
"git.lehouerou.net/laurent/sorarebuddy/db"
"git.lehouerou.net/laurent/sorarebuddy/model"
)
type UpdateService struct {
s *sorare.Sorare
2024-05-23 04:18:54 +00:00
db *db.Client
2024-05-23 04:18:54 +00:00
updater *Updater
}
2024-05-23 04:18:54 +00:00
func NewUpdateService(s *sorare.Sorare, db *db.Client) *UpdateService {
return &UpdateService{s: s, db: db, updater: NewUpdater(s, db)}
}
2024-05-23 04:18:54 +00:00
func (u *UpdateService) updateAllFixtures(ctx context.Context) error {
log.Debug().Msg("### updating all fixtures ###")
fixtures, err := u.s.Football.So5.So5Fixtures.Get(ctx, football.So5FixturesParams{})
if err != nil {
return err
}
2024-05-23 04:18:54 +00:00
log.Debug().Msgf("found %d fixtures in sorare", len(fixtures))
2024-05-23 04:18:54 +00:00
err = u.db.Fixtures.CreateOrUpdateMany(
ctx,
2024-05-23 04:18:54 +00:00
lo.Map(fixtures, func(fixture football.So5Fixture, index int) model.Fixture {
return model.Fixture{
Slug: fixture.Slug,
DisplayName: fixture.DisplayName,
State: fixture.AasmState,
2024-05-23 04:18:54 +00:00
StartDate: fixture.StartDate,
EndDate: fixture.EndDate,
GameWeek: fixture.GameWeek,
}
}),
)
2024-05-23 04:18:54 +00:00
if err != nil {
return errors.Wrap(err, "inserting fixtures")
}
2024-05-23 04:18:54 +00:00
log.Debug().Msgf("created %d fixtures", len(fixtures))
return nil
}
2024-05-23 04:18:54 +00:00
func (u *UpdateService) UpdatePlayers(ctx context.Context, playerSlugs []string, opts ...UpdaterOption) error {
u.updater.Reset()
u.updater.AddPlayersToRead(playerSlugs...)
err := u.updater.Update(ctx, opts...)
if err != nil {
2024-05-23 04:18:54 +00:00
return errors.Wrap(err, "updating data")
}
2024-05-23 04:18:54 +00:00
return nil
}
2024-05-23 04:18:54 +00:00
func (u *UpdateService) UpdateAllPlayers(ctx context.Context, opts ...UpdaterOption) error {
dbplayers, err := u.db.Players.GetAll(ctx)
if err != nil {
return errors.Wrap(err, "getting players from db")
}
2024-05-23 04:18:54 +00:00
playerSlugs := lo.Map(dbplayers, func(player model.Player, index int) string {
return player.Slug
})
return u.UpdatePlayers(ctx, playerSlugs, opts...)
}
2024-05-23 04:18:54 +00:00
func (u *UpdateService) SyncStartedFixture(ctx context.Context, opts ...UpdaterOption) error {
f, err := u.db.Fixtures.GetStarted(ctx)
if err != nil {
return errors.Wrap(err, "getting started fixtures")
}
2024-05-23 04:18:54 +00:00
return u.SyncDatabaseForFixtures(ctx, f, opts...)
}
2024-05-23 04:18:54 +00:00
func (u *UpdateService) SyncDatabaseForFixtures(
ctx context.Context,
fixtures []model.Fixture,
opts ...UpdaterOption,
) error {
2024-05-23 04:18:54 +00:00
u.updater.Reset()
u.updater.AddGamesFromFixtureToRead(lo.Map(fixtures, func(fixture model.Fixture, index int) string {
return fixture.Slug
})...)
2024-05-23 04:18:54 +00:00
err := u.updater.Update(ctx, opts...)
if err != nil {
return errors.Wrap(err, "updating data")
}
2024-05-23 04:18:54 +00:00
return nil
}
2024-05-23 04:18:54 +00:00
func (u *UpdateService) SyncDatabaseForAllFixtures(ctx context.Context) error {
err := u.updateAllFixtures(ctx)
if err != nil {
return errors.Wrap(err, "updating fixtures")
2024-03-23 19:46:17 +00:00
}
2024-05-23 04:18:54 +00:00
fixtures, err := u.db.Fixtures.GetAll(ctx)
if err != nil {
return err
}
2024-05-23 04:18:54 +00:00
return u.SyncDatabaseForFixtures(ctx, fixtures)
}
2024-05-23 04:18:54 +00:00
func (u *UpdateService) UpdateLastClosedStartedAndOpenedFixtures(ctx context.Context) error {
err := u.updateAllFixtures(ctx)
if err != nil {
return errors.Wrap(err, "updating fixtures")
}
2024-05-23 04:18:54 +00:00
var fixtures []model.Fixture
lastClosedFixture, err := u.db.Fixtures.GetLastClosed(ctx)
if err != nil {
return err
}
2024-05-23 04:18:54 +00:00
fixtures = append(fixtures, lastClosedFixture)
openedFixtures, err := u.db.Fixtures.GetOpened(ctx)
if err != nil {
return err
}
2024-05-23 04:18:54 +00:00
fixtures = append(fixtures, openedFixtures...)
startedFixtures, err := u.db.Fixtures.GetStarted(ctx)
if err != nil {
return err
}
2024-05-23 04:18:54 +00:00
fixtures = append(fixtures, startedFixtures...)
return u.SyncDatabaseForFixtures(ctx, fixtures)
}
2024-05-23 04:18:54 +00:00
func (u *UpdateService) GetGamePlayersForUpcomingGamesWithoutFormation(ctx context.Context) error {
log.Debug().Msg("updating formations for games in the next 3 hours")
gameIds, err := u.db.Games.GetFutureGameIdsWithoutFormation(ctx, 3*time.Hour, 1)
if err != nil {
return errors.Wrap(err, "getting future games")
}
2024-05-23 04:18:54 +00:00
log.Debug().Msgf("%d future games without formations found", len(gameIds))
u.updater.Reset()
u.updater.AddGameFormationsToRead(gameIds...)
err = u.updater.Update(ctx)
if err != nil {
return errors.Wrap(err, "updating data")
}
2024-03-23 19:46:17 +00:00
return nil
}
2024-05-23 04:18:54 +00:00
func (u *UpdateService) UpdateCurrentlyPlayingGames(ctx context.Context) error {
games, err := u.db.Games.CurrentlyPlayingGames(ctx)
if err != nil {
return errors.Wrap(err, "getting currently playing games")
}
u.updater.Reset()
u.updater.AddGamesToRead(lo.Map(games, func(game model.Game, index int) string {
return game.Id
})...)
2024-05-23 04:18:54 +00:00
err = u.updater.Update(ctx,
WithUpdateOnlyMissingPlayers(true),
WithUpdateOnlyMissingTeams(true),
WithUpdateOnlyMissingCompetitions(true),
WithUpdateOnlyMissingCountries(true),
)
if err != nil {
return errors.Wrap(err, "updating data")
}
return nil
}
2024-05-23 04:18:54 +00:00
func (u *UpdateService) LightUpdateStartedFixture(ctx context.Context) error {
fixture, err := u.db.Fixtures.GetStarted(ctx)
if err != nil {
return errors.Wrap(err, "getting started fixtures")
}
return u.SyncDatabaseForFixtures(ctx, fixture,
WithUpdateOnlyMissingCompetitions(true),
WithUpdateOnlyMissingCountries(true),
WithUpdateOnlyMissingPlayers(true),
WithUpdateOnlyMissingTeams(true))
}