protected override async Task RunInterval()
        {
            await using var conn = await _db.Obtain();

            var sim = await _updateStore.GetLatestUpdate(conn, UpdateType.Sim);

            var(season, dayStart) = (sim.Data.Value <int>("season"), sim.Data.Value <int>("day"));

            var updates = new List <GameUpdate>();

            for (var day = dayStart + 1; day < 100; day++)
            {
                var jsonStr = await _client.GetStringAsync($"https://www.blaseball.com/database/games?season={season}&day={day}");

                var timestamp = _clock.GetCurrentInstant();
                var json      = JArray.Parse(jsonStr);
                _logger.Information("Polled future games at season {Season} day {Day}", season, day);

                updates.AddRange(json.Select(game => GameUpdate.From(_sourceId, timestamp, game)));
            }

            await using (var tx = await conn.BeginTransactionAsync())
            {
                await _gameUpdateStore.SaveGameUpdates(conn, updates);

                await tx.CommitAsync();
            }

            await _gameStore.TryAddNewGameIds(conn, updates.Select(gu => gu.GameId));
        }
Example #2
0
        private async Task FetchGamesInner(int season, int day)
        {
            var gameUpdates = await FetchGamesAt(season, day);

            await using var conn = await _db.Obtain();

            await using (var tx = await conn.BeginTransactionAsync())
            {
                await _gameUpdateStore.SaveGameUpdates(conn, gameUpdates);

                await tx.CommitAsync();
            }

            await _gameStore.TryAddNewGameIds(conn, gameUpdates.Select(gu => gu.GameId));
        }
        protected override async Task ProcessFile(string filename, IAsyncEnumerable <JToken> entries)
        {
            using var hasher = new SibrHasher();
            var streamUpdates = new List <EntityUpdate>();
            var miscUpdates   = new List <EntityUpdate>();
            var gameUpdates   = new List <GameUpdate>();
            var gameIds       = new HashSet <Guid>();

            var count = 0;

            await using var conn = await _db.Obtain();

            await foreach (var entry in entries)
            {
                var timestamp = ExtractTimestamp(entry);
                if (timestamp == null)
                {
                    continue;
                }

                var root = FindStreamRoot(entry as JObject);
                streamUpdates.Add(EntityUpdate.From(UpdateType.Stream, _sourceId, timestamp.Value, root));

                var updates = TgbUtils.ExtractUpdatesFromStreamRoot(_sourceId, timestamp.Value, root, hasher);
                gameUpdates.AddRange(updates.GameUpdates);
                miscUpdates.AddRange(updates.EntityUpdates);

                gameIds.UnionWith(updates.GameUpdates.Select(g => g.GameId));
                if (count++ % 100 == 0)
                {
                    await FlushUpdates(conn, streamUpdates, gameUpdates, miscUpdates);
                }
            }

            await FlushUpdates(conn, streamUpdates, gameUpdates, miscUpdates);

            await _gameStore.TryAddNewGameIds(conn, gameUpdates.Select(gu => gu.GameId));

            await _gameUpdateStore.UpdateSearchIndex(conn);
        }