package sorare_utils import ( "context" "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" "git.lehouerou.net/laurent/sorarebuddy/db" "git.lehouerou.net/laurent/sorarebuddy/model" ) type UpdateService struct { s *sorare.Sorare db *db.Client updater *Updater } func NewUpdateService(s *sorare.Sorare, db *db.Client) *UpdateService { return &UpdateService{s: s, db: db, updater: NewUpdater(s, db)} } 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 } log.Debug().Msgf("found %d fixtures in sorare", len(fixtures)) err = u.db.Fixtures.CreateOrUpdateMany( ctx, lo.Map(fixtures, func(fixture football.So5Fixture, index int) model.Fixture { return model.Fixture{ Slug: fixture.Slug, DisplayName: fixture.DisplayName, State: fixture.AasmState, StartDate: fixture.StartDate, EndDate: fixture.EndDate, GameWeek: fixture.GameWeek, } }), ) if err != nil { return errors.Wrap(err, "inserting fixtures") } log.Debug().Msgf("created %d fixtures", len(fixtures)) return nil } 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 { return errors.Wrap(err, "updating data") } return nil } 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") } playerSlugs := lo.Map(dbplayers, func(player model.Player, index int) string { return player.Slug }) return u.UpdatePlayers(ctx, playerSlugs, opts...) } 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") } return u.SyncDatabaseForFixtures(ctx, f, opts...) } func (u *UpdateService) SyncDatabaseForFixtures( ctx context.Context, fixtures []model.Fixture, opts ...UpdaterOption, ) error { u.updater.Reset() u.updater.AddGamesFromFixtureToRead(lo.Map(fixtures, func(fixture model.Fixture, index int) string { return fixture.Slug })...) err := u.updater.Update(ctx, opts...) if err != nil { return errors.Wrap(err, "updating data") } return nil } func (u *UpdateService) SyncDatabaseForAllFixtures(ctx context.Context) error { err := u.updateAllFixtures(ctx) if err != nil { return errors.Wrap(err, "updating fixtures") } fixtures, err := u.db.Fixtures.GetAll(ctx) if err != nil { return err } return u.SyncDatabaseForFixtures(ctx, fixtures) } func (u *UpdateService) UpdateLastClosedStartedAndOpenedFixtures(ctx context.Context) error { err := u.updateAllFixtures(ctx) if err != nil { return errors.Wrap(err, "updating fixtures") } var fixtures []model.Fixture lastClosedFixture, err := u.db.Fixtures.GetLastClosed(ctx) if err != nil { return err } fixtures = append(fixtures, lastClosedFixture) openedFixtures, err := u.db.Fixtures.GetOpened(ctx) if err != nil { return err } fixtures = append(fixtures, openedFixtures...) startedFixtures, err := u.db.Fixtures.GetStarted(ctx) if err != nil { return err } fixtures = append(fixtures, startedFixtures...) return u.SyncDatabaseForFixtures(ctx, fixtures) } 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") } 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") } return nil } 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 })...) err = u.updater.Update(ctx, WithUpdateOnlyMissingPlayers(true), WithUpdateOnlyMissingTeams(true), WithUpdateOnlyMissingCompetitions(true), WithUpdateOnlyMissingCountries(true), ) if err != nil { return errors.Wrap(err, "updating data") } return nil } 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)) }