Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[W3C-232] Seasonal Matchup collections - WIP #302

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions W3C.Domain/Repositories/ISeasonal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace W3C.Domain.Repositories
{
public interface ISeasonal
{
public int Season { get; }
}
}
34 changes: 29 additions & 5 deletions W3C.Domain/Repositories/MongoDbRepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,9 +24,17 @@ protected IMongoDatabase CreateClient()
return database;
}

protected Task<T> LoadFirst<T>(Expression<Func<T, bool>> expression)
protected Task<T> LoadFirst<T>(Expression<Func<T, bool>> expression, int? season = 1)
{
var mongoCollection = CreateCollection<T>();
IMongoCollection<T> mongoCollection;
if (typeof(ISeasonal).IsAssignableFrom(typeof(T)))
{
mongoCollection = CreateSeasonalCollection<T>(season ?? 1);
}
else
{
mongoCollection = CreateCollection<T>();
}
return mongoCollection.FindSync(expression).FirstOrDefaultAsync();
}

Expand Down Expand Up @@ -60,12 +69,27 @@ protected IMongoCollection<T> CreateCollection<T>(string collectionName = null)
return mongoCollection;
}

protected async Task Upsert<T>(T insertObject, Expression<Func<T, bool>> identityQuerry)
protected IMongoCollection<T> CreateSeasonalCollection<T>(int season, string collectionName = null)
{
var mongoDatabase = CreateClient();
var mongoCollection = mongoDatabase.GetCollection<T>(typeof(T).Name);
var mongoCollection = mongoDatabase.GetCollection<T>(String.Format(collectionName ?? "{0}_{1}", typeof(T).Name, season));
return mongoCollection;
}

protected async Task Upsert<T>(T insertObject, Expression<Func<T, bool>> identityQuery)
{
var mongoDatabase = CreateClient();
IMongoCollection<T> mongoCollection;
if (insertObject is ISeasonal seasonalObj)
{
mongoCollection = mongoDatabase.GetCollection<T>(String.Format("{0}_{1}", typeof(T).Name, seasonalObj.Season));
}
else
{
mongoCollection = mongoDatabase.GetCollection<T>(typeof(T).Name);
}
await mongoCollection.FindOneAndReplaceAsync(
identityQuerry,
identityQuery,
insertObject,
new FindOneAndReplaceOptions<T> {IsUpsert = true});
}
Expand Down
20 changes: 16 additions & 4 deletions W3ChampionsStatisticService/Ladder/LeagueSyncHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using W3C.Domain.Repositories;
using W3ChampionsStatisticService.Ports;
using W3ChampionsStatisticService.ReadModelBase;
using W3C.Contracts.Matchmaking;

namespace W3ChampionsStatisticService.Ladder;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
29 changes: 11 additions & 18 deletions W3ChampionsStatisticService/Matches/MatchRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task<List<Matchup>> LoadFor(
int offset = 0,
int season = 1)
{
var mongoCollection = CreateCollection<Matchup>();
var mongoCollection = CreateSeasonalCollection<Matchup>(season);
var textSearchOpts = new TextSearchOptions();
if (string.IsNullOrEmpty(opponentId))
{
Expand All @@ -47,8 +47,7 @@ public async Task<List<Matchup>> 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)
Expand All @@ -59,8 +58,7 @@ public async Task<List<Matchup>> LoadFor(
.Find(m =>
Builders<Matchup>.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)
Expand All @@ -77,29 +75,27 @@ public Task<long> CountFor(
int season = 1)
{
var textSearchOpts = new TextSearchOptions();
var mongoCollection = CreateCollection<Matchup>();
var mongoCollection = CreateSeasonalCollection<Matchup>(season);
if (string.IsNullOrEmpty(opponentId))
{
return mongoCollection.CountDocumentsAsync(m =>
Builders<Matchup>.Filter.Text($"\"{playerId}\"", textSearchOpts).Inject()
&& (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<Matchup>.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<MatchupDetail> LoadDetails(ObjectId id)
{
var originalMatch = await LoadFirst<MatchFinishedEvent>(t => t.Id == id);
var match = await LoadFirst<Matchup>(t => t.Id == id);
var match = await LoadFirst<Matchup>(t => t.Id == id, originalMatch?.match?.season);

return new MatchupDetail
{
Expand Down Expand Up @@ -163,9 +159,9 @@ public Task<List<Matchup>> Load(
int offset = 0,
int pageSize = 100)
{
var mongoCollection = CreateCollection<Matchup>();
var mongoCollection = CreateSeasonalCollection<Matchup>(season);
return mongoCollection
.Find(m => gameMode == m.GameMode && m.Season == season)
.Find(m => gameMode == m.GameMode)
.SortByDescending(s => s.EndTime)
.Skip(offset)
.Limit(pageSize)
Expand All @@ -179,12 +175,9 @@ public async Task<int> GetFloIdFromId(string gameId)
return (match == null || match.FloMatchId == null) ? 0 : match.FloMatchId.Value;
}

public Task<long> Count(
int season,
GameMode gameMode)
public Task<long> Count(int season, GameMode gameMode)
{
return CreateCollection<Matchup>().CountDocumentsAsync(m =>
gameMode == m.GameMode && m.Season == season);
return CreateSeasonalCollection<Matchup>(season).CountDocumentsAsync(m => gameMode == m.GameMode);
}

public Task InsertOnGoingMatch(OnGoingMatchup matchup)
Expand Down
3 changes: 2 additions & 1 deletion W3ChampionsStatisticService/Matches/Matchup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions WC3ChampionsStatisticService.UnitTests/IntegrationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,4 @@ public async Task Cache_ByPlayerId_OneTeamEmpty()
Assert.AreEqual(storedEvent.match.id, result.MatchId);
Assert.AreEqual(notCachedEvent.match.id, result2.MatchId);
}


}
Loading