using System.Collections.Generic; using System.Threading.Tasks; using MongoDB.Driver; using WotBlitzStatisticsPro.Common; using WotBlitzStatisticsPro.DataAccess.Model.Accounts; namespace WotBlitzStatisticsPro.DataAccess { public class WargamingAccountDataAccessor : IWargamingAccountDataAccessor { private const string AccountInfoCollectionName = "account-info"; private const string AccountInfoHistoryCollectionName = "account-info-history"; private const string TankInfoCollectionName = "tank-info"; private const string TankInfoHistoryCollectionName = "tank-info-history"; private readonly IMongoDatabase _database; public WargamingAccountDataAccessor(IMongoSettings mongoSettings) { var client = new MongoClient(mongoSettings.ConnectionString); _database = client.GetDatabase(mongoSettings.DatabaseName); CreateAccountIndexesIfNotExists(); } private void CreateAccountIndexesIfNotExists() { // Account Info var accountInfoBuilder = Builders.IndexKeys; var accountInfoRealmIndexModel = new CreateIndexModel(accountInfoBuilder.Ascending(a => a.Realm)); var accountInfoLastBattleTimeIndexModel = new CreateIndexModel(accountInfoBuilder.Descending(a => a.LastBattleTime)); _database.GetCollection(AccountInfoCollectionName).Indexes .CreateMany(new[] {accountInfoRealmIndexModel, accountInfoLastBattleTimeIndexModel}); //// AccountInfoHistory AccountId+LastBattleTime //var accountInfoHistoryBuilder = Builders.IndexKeys; //var accountInfoHistoryIndexModel = new CreateIndexModel( // accountInfoHistoryBuilder.Combine( // Builders.IndexKeys.Ascending(h => h.AccountId), // Builders.IndexKeys.Ascending(h => h.LastBattleTime))); //_database.GetCollection(AccountInfoHistoryCollectionName).Indexes // .CreateOne(accountInfoHistoryIndexModel); //// TankInfo AccountId+TankId, LastBattleTime //var tankInfoBuilder = Builders.IndexKeys; //var tankInfoIndexModel = new CreateIndexModel( // tankInfoBuilder.Combine( // Builders.IndexKeys.Ascending(h => h.AccountId), // Builders.IndexKeys.Ascending(h => h.TankId))); //var tankInfoLastBattleTimeIndexModel = // new CreateIndexModel(tankInfoBuilder.Ascending(t => t.LastBattleTime)); //_database.GetCollection(TankInfoCollectionName).Indexes // .CreateMany(new[] {tankInfoIndexModel, tankInfoLastBattleTimeIndexModel}); //// TankInfoHistory AccountId+TankId+LastBattleTime //var tankInfoHistoryBuilder = Builders.IndexKeys; //var tankInfoHistoryIndexModel = new CreateIndexModel( // tankInfoHistoryBuilder.Combine( // Builders.IndexKeys.Ascending(h => h.AccountId), // Builders.IndexKeys.Ascending(h => h.TankId), // Builders.IndexKeys.Ascending(h => h.LastBattleTime))); //_database.GetCollection(TankInfoHistoryCollectionName).Indexes // .CreateOne(tankInfoHistoryIndexModel); } public async Task ReadAccountInfo(long accountId) { var account = await _database.GetCollection(AccountInfoCollectionName) .FindAsync(Builders.Filter.Where(a => a.AccountId == accountId)); return account.FirstOrDefault(); } public async Task ReadTankInfo(long accountId, long tankId) { var tank = await _database.GetCollection(TankInfoCollectionName) .FindAsync(Builders.Filter.Where(t => t.TankInfoId == new TankInfoKey(accountId, tankId))); return tank.FirstOrDefault(); } public Task AddOurUpdateAccountInfo(AccountInfo accountInfo) { return _database.GetCollection(AccountInfoCollectionName) .ReplaceOneAsync( Builders.Filter.Where(a => a.AccountId == accountInfo.AccountId), accountInfo, new ReplaceOptions {IsUpsert = true} ); } public Task AddAccountInfoHistory(AccountInfoHistory accountInfoHistory) { return _database.GetCollection(AccountInfoHistoryCollectionName) .InsertOneAsync(accountInfoHistory); } public Task AddOrUpdateTankInfo(TankInfo tankInfo) { return _database.GetCollection(TankInfoCollectionName) .ReplaceOneAsync( Builders.Filter.Where(t => t.TankInfoId == new TankInfoKey(tankInfo.AccountId, tankInfo.TankId)), tankInfo, new ReplaceOptions { IsUpsert = true } ); } public Task AddTankInfoHistory(TankInfoHistory tankInfoHistory) { return _database.GetCollection(TankInfoHistoryCollectionName) .InsertOneAsync(tankInfoHistory); } public async Task> GetAccountHistory(long accountId, int lastBattleSince) { var history = await _database.GetCollection(AccountInfoHistoryCollectionName) .FindAsync(Builders.Filter.Where(a => a.AccountInfoHistoryId.AccountId == accountId && a.AccountInfoHistoryId.LastBattleTime >= lastBattleSince), new FindOptions { Sort = Builders.Sort.Descending(h => h.AccountInfoHistoryId.LastBattleTime)}); return history.ToList(); } public async Task> GetTankHistory(long accountId, long tankId, int lastBattleSince) { var history = await _database.GetCollection(TankInfoHistoryCollectionName) .FindAsync( Builders.Filter.Where(t => t.TankInfoHistoryId.AccountId == accountId && t.TankInfoHistoryId.TankId == tankId && t.TankInfoHistoryId.LastBattleTime >= lastBattleSince), new FindOptions {Sort = Builders.Sort.Descending(h => h.TankInfoHistoryId.LastBattleTime)}); return history.ToList(); } } }