diff --git a/W3C.Domain/Repositories/ISeasonal.cs b/W3C.Domain/Repositories/ISeasonal.cs new file mode 100644 index 00000000..4f4ec389 --- /dev/null +++ b/W3C.Domain/Repositories/ISeasonal.cs @@ -0,0 +1,7 @@ +namespace W3C.Domain.Repositories +{ + public interface ISeasonal + { + public int Season { get; } + } +} diff --git a/W3C.Domain/Repositories/MongoDbRepositoryBase.cs b/W3C.Domain/Repositories/MongoDbRepositoryBase.cs index 3a302aff..4f711e86 100644 --- a/W3C.Domain/Repositories/MongoDbRepositoryBase.cs +++ b/W3C.Domain/Repositories/MongoDbRepositoryBase.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Reflection; using System.Threading.Tasks; using MongoDB.Driver; @@ -23,9 +24,17 @@ protected IMongoDatabase CreateClient() return database; } - protected Task LoadFirst(Expression> expression) + protected Task LoadFirst(Expression> expression, int? season = 1) { - var mongoCollection = CreateCollection(); + IMongoCollection mongoCollection; + if (typeof(ISeasonal).IsAssignableFrom(typeof(T))) + { + mongoCollection = CreateSeasonalCollection(season ?? 1); + } + else + { + mongoCollection = CreateCollection(); + } return mongoCollection.FindSync(expression).FirstOrDefaultAsync(); } @@ -60,12 +69,27 @@ protected IMongoCollection CreateCollection(string collectionName = null) return mongoCollection; } - protected async Task Upsert(T insertObject, Expression> identityQuerry) + protected IMongoCollection CreateSeasonalCollection(int season, string collectionName = null) { var mongoDatabase = CreateClient(); - var mongoCollection = mongoDatabase.GetCollection(typeof(T).Name); + var mongoCollection = mongoDatabase.GetCollection(String.Format(collectionName ?? "{0}_{1}", typeof(T).Name, season)); + return mongoCollection; + } + + protected async Task Upsert(T insertObject, Expression> identityQuery) + { + var mongoDatabase = CreateClient(); + IMongoCollection mongoCollection; + if (insertObject is ISeasonal seasonalObj) + { + mongoCollection = mongoDatabase.GetCollection(String.Format("{0}_{1}", typeof(T).Name, seasonalObj.Season)); + } + else + { + mongoCollection = mongoDatabase.GetCollection(typeof(T).Name); + } await mongoCollection.FindOneAndReplaceAsync( - identityQuerry, + identityQuery, insertObject, new FindOneAndReplaceOptions {IsUpsert = true}); } diff --git a/W3ChampionsStatisticService/Ladder/LeagueSyncHandler.cs b/W3ChampionsStatisticService/Ladder/LeagueSyncHandler.cs index 89129141..b53785e1 100644 --- a/W3ChampionsStatisticService/Ladder/LeagueSyncHandler.cs +++ b/W3ChampionsStatisticService/Ladder/LeagueSyncHandler.cs @@ -3,7 +3,6 @@ using W3C.Domain.Repositories; using W3ChampionsStatisticService.Ports; using W3ChampionsStatisticService.ReadModelBase; -using W3C.Contracts.Matchmaking; namespace W3ChampionsStatisticService.Ladder; @@ -26,9 +25,22 @@ public async Task Update() var loadLeagueConstellation = await _matchEventRepository.LoadLeagueConstellationChanged(); var leagueConstellations = loadLeagueConstellation.Select(l => - new LeagueConstellation(l.season, l.gateway, l.gameMode, l.leagues.Select(le => - new League(le.id, le.order, le.name.Replace("League", "").Trim(), le.division) - ).OrderBy(l => l.Order).ThenBy(l => l.Division).ToList().ToList()) + new LeagueConstellation( + l.season, + l.gateway, + l.gameMode, + l.leagues.Select(le => + new League( + le.id, + le.order, + le.name + .Replace("League", "") + .Trim(), + le.division)) + .OrderBy(l => l.Order) + .ThenBy(l => l.Division) + .ToList() + .ToList()) ).ToList(); await _rankRepository.InsertLeagues(leagueConstellations); diff --git a/W3ChampionsStatisticService/Matches/MatchReadModelHandler.cs b/W3ChampionsStatisticService/Matches/MatchReadModelHandler.cs index 30438b6f..7a9f87c4 100644 --- a/W3ChampionsStatisticService/Matches/MatchReadModelHandler.cs +++ b/W3ChampionsStatisticService/Matches/MatchReadModelHandler.cs @@ -24,7 +24,6 @@ public async Task Update(MatchFinishedEvent nextEvent) { if (nextEvent.WasFakeEvent) return; var matchup = Matchup.Create(nextEvent); - await _matchRepository.Insert(matchup); await _matchRepository.DeleteOnGoingMatch(matchup.MatchId); } diff --git a/W3ChampionsStatisticService/Matches/MatchRepository.cs b/W3ChampionsStatisticService/Matches/MatchRepository.cs index 97c02b25..a38f3569 100644 --- a/W3ChampionsStatisticService/Matches/MatchRepository.cs +++ b/W3ChampionsStatisticService/Matches/MatchRepository.cs @@ -38,7 +38,7 @@ public async Task> LoadFor( int offset = 0, int season = 1) { - var mongoCollection = CreateCollection(); + var mongoCollection = CreateSeasonalCollection(season); var textSearchOpts = new TextSearchOptions(); if (string.IsNullOrEmpty(opponentId)) { @@ -47,8 +47,7 @@ public async Task> LoadFor( && (gameMode == GameMode.Undefined || m.GameMode == gameMode) && (gateWay == GateWay.Undefined || m.GateWay == gateWay) && (playerRace == Race.Total || m.Teams.Any(team => team.Players[0].Race == playerRace && playerId == team.Players[0].BattleTag)) - && (opponentRace == Race.Total || m.Teams.Any(team => team.Players[0].Race == opponentRace && playerId != team.Players[0].BattleTag)) - && (m.Season == season)) + && (opponentRace == Race.Total || m.Teams.Any(team => team.Players[0].Race == opponentRace && playerId != team.Players[0].BattleTag))) .SortByDescending(s => s.Id) .Skip(offset) .Limit(pageSize) @@ -59,8 +58,7 @@ public async Task> LoadFor( .Find(m => Builders.Filter.Text($"\"{playerId}\" \"{opponentId}\"", textSearchOpts).Inject() && (gameMode == GameMode.Undefined || m.GameMode == gameMode) - && (gateWay == GateWay.Undefined || m.GateWay == gateWay) - && (m.Season == season)) + && (gateWay == GateWay.Undefined || m.GateWay == gateWay)) .SortByDescending(s => s.Id) .Skip(offset) .Limit(pageSize) @@ -77,7 +75,7 @@ public Task CountFor( int season = 1) { var textSearchOpts = new TextSearchOptions(); - var mongoCollection = CreateCollection(); + var mongoCollection = CreateSeasonalCollection(season); if (string.IsNullOrEmpty(opponentId)) { return mongoCollection.CountDocumentsAsync(m => @@ -85,21 +83,19 @@ public Task CountFor( && (gameMode == GameMode.Undefined || m.GameMode == gameMode) && (gateWay == GateWay.Undefined || m.GateWay == gateWay) && (playerRace == Race.Total || m.Teams.Any(team => team.Players[0].Race == playerRace && playerId == team.Players[0].BattleTag)) - && (opponentRace == Race.Total || m.Teams.Any(team => team.Players[0].Race == opponentRace && playerId != team.Players[0].BattleTag)) - && (m.Season == season)); + && (opponentRace == Race.Total || m.Teams.Any(team => team.Players[0].Race == opponentRace && playerId != team.Players[0].BattleTag))); } return mongoCollection.CountDocumentsAsync(m => Builders.Filter.Text($"\"{playerId}\" \"{opponentId}\"", textSearchOpts).Inject() && (gameMode == GameMode.Undefined || m.GameMode == gameMode) - && (gateWay == GateWay.Undefined || m.GateWay == gateWay) - && (m.Season == season)); + && (gateWay == GateWay.Undefined || m.GateWay == gateWay)); } public async Task LoadDetails(ObjectId id) { var originalMatch = await LoadFirst(t => t.Id == id); - var match = await LoadFirst(t => t.Id == id); + var match = await LoadFirst(t => t.Id == id, originalMatch?.match?.season); return new MatchupDetail { @@ -163,9 +159,9 @@ public Task> Load( int offset = 0, int pageSize = 100) { - var mongoCollection = CreateCollection(); + var mongoCollection = CreateSeasonalCollection(season); return mongoCollection - .Find(m => gameMode == m.GameMode && m.Season == season) + .Find(m => gameMode == m.GameMode) .SortByDescending(s => s.EndTime) .Skip(offset) .Limit(pageSize) @@ -179,12 +175,9 @@ public async Task GetFloIdFromId(string gameId) return (match == null || match.FloMatchId == null) ? 0 : match.FloMatchId.Value; } - public Task Count( - int season, - GameMode gameMode) + public Task Count(int season, GameMode gameMode) { - return CreateCollection().CountDocumentsAsync(m => - gameMode == m.GameMode && m.Season == season); + return CreateSeasonalCollection(season).CountDocumentsAsync(m => gameMode == m.GameMode); } public Task InsertOnGoingMatch(OnGoingMatchup matchup) diff --git a/W3ChampionsStatisticService/Matches/Matchup.cs b/W3ChampionsStatisticService/Matches/Matchup.cs index ddf0a996..b7106554 100644 --- a/W3ChampionsStatisticService/Matches/Matchup.cs +++ b/W3ChampionsStatisticService/Matches/Matchup.cs @@ -8,10 +8,11 @@ using W3C.Domain.MatchmakingService; using W3C.Contracts.Matchmaking; using W3C.Domain.GameModes; +using W3C.Domain.Repositories; namespace W3ChampionsStatisticService.Matches; -public class Matchup +public class Matchup : ISeasonal { public string Map { get; set; } public string MapName { get; set; } diff --git a/WC3ChampionsStatisticService.UnitTests/Helpers/TestDtoHelper.cs b/WC3ChampionsStatisticService.UnitTests/Helpers/TestDtoHelper.cs index ecfa8480..e88fc063 100644 --- a/WC3ChampionsStatisticService.UnitTests/Helpers/TestDtoHelper.cs +++ b/WC3ChampionsStatisticService.UnitTests/Helpers/TestDtoHelper.cs @@ -27,7 +27,7 @@ public static MatchFinishedEvent CreateFakeEvent() fakeEvent.match.gateway = GateWay.Europe; fakeEvent.match.gameMode = GameMode.GM_1v1; - fakeEvent.match.season = 0; + fakeEvent.match.season = 1; fakeEvent.match.players.First().battleTag = name1; fakeEvent.match.players.First().won = true; diff --git a/WC3ChampionsStatisticService.UnitTests/IntegrationTestBase.cs b/WC3ChampionsStatisticService.UnitTests/IntegrationTestBase.cs index 8fadc2f0..25e3206e 100644 --- a/WC3ChampionsStatisticService.UnitTests/IntegrationTestBase.cs +++ b/WC3ChampionsStatisticService.UnitTests/IntegrationTestBase.cs @@ -15,8 +15,8 @@ namespace WC3ChampionsStatisticService.Tests; public class IntegrationTestBase { - //protected readonly MongoClient MongoClient = new MongoClient("mongodb://localhost:27017/"); - protected readonly MongoClient MongoClient = new MongoClient("mongodb://157.90.1.251:3512/"); + protected readonly MongoClient MongoClient = new MongoClient("mongodb://localhost:27017/"); + //protected readonly MongoClient MongoClient = new MongoClient("mongodb://157.90.1.251:3512/"); protected PersonalSettingsProvider personalSettingsProvider; diff --git a/WC3ChampionsStatisticService.UnitTests/Matchups/MatchupDetailTest.cs b/WC3ChampionsStatisticService.UnitTests/Matchups/MatchupDetailTest.cs index 3b4e6d1d..6431d2b2 100644 --- a/WC3ChampionsStatisticService.UnitTests/Matchups/MatchupDetailTest.cs +++ b/WC3ChampionsStatisticService.UnitTests/Matchups/MatchupDetailTest.cs @@ -16,6 +16,7 @@ public async Task LoadDetails_NotDetailsAvailable() var matchFinishedEvent = TestDtoHelper.CreateFakeEvent(); matchFinishedEvent.match.id = "nmhcCLaRc7"; matchFinishedEvent.Id = ObjectId.GenerateNewId(); + matchFinishedEvent.match.season = 1; var matchRepository = new MatchRepository(MongoClient, new OngoingMatchesCache(MongoClient)); await matchRepository.Insert(Matchup.Create(matchFinishedEvent)); @@ -103,6 +104,7 @@ public async Task LoadDetails_RandomRaceSetToRandomWhenNullResult() matchFinishedEvent.match.id = "nmhcCLaRc7"; matchFinishedEvent.Id = ObjectId.GenerateNewId(); matchFinishedEvent.result = null; + matchFinishedEvent.match.season = 1; var player = matchFinishedEvent.match.players[0]; diff --git a/WC3ChampionsStatisticService.UnitTests/Matchups/MatchupRepoTests.cs b/WC3ChampionsStatisticService.UnitTests/Matchups/MatchupRepoTests.cs index 44fa1ac0..5b428afd 100644 --- a/WC3ChampionsStatisticService.UnitTests/Matchups/MatchupRepoTests.cs +++ b/WC3ChampionsStatisticService.UnitTests/Matchups/MatchupRepoTests.cs @@ -487,6 +487,4 @@ public async Task Cache_ByPlayerId_OneTeamEmpty() Assert.AreEqual(storedEvent.match.id, result.MatchId); Assert.AreEqual(notCachedEvent.match.id, result2.MatchId); } - - }