Files

134 lines
6.8 KiB
C#
Raw Permalink Normal View History

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