Skip to content

Commit

Permalink
loading faster some queries
Browse files Browse the repository at this point in the history
  • Loading branch information
valdisz committed Oct 13, 2021
1 parent 42bd9a0 commit 5aebc77
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
13 changes: 5 additions & 8 deletions server/Features/SetupStudyPlans.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public async Task<Unit> Handle(SetupStudyPlans request, CancellationToken cancel
var universityId = membership.UniversityId;
var turnNumber = membership.Player.LastTurnNumber;

var turn = await db.Turns.FirstOrDefaultAsync(x => x.PlayerId == membership.PlayerId && x.Number == turnNumber);

var members = await db.UniversityMemberships
.Include(x => x.Player)
.Where(x => x.UniversityId == universityId)
Expand All @@ -43,12 +45,9 @@ public async Task<Unit> Handle(SetupStudyPlans request, CancellationToken cancel
var prevPalns = await GetTurnPlans(turnNumber - 1, universityId);

var mages = (await db.Units
.Include(x => x.Turn)
.Include(x => x.Faction)
.Include(x => x.Plan)
.Where(x => x.Turn.Number == turnNumber
&& x.Turn.PlayerId == request.PlayerId
&& x.Faction != null
.Where(x => x.TurnId == turn.Id && x.FactionId != null
&& x.Skills.Any(s => s.Code == "FORC" || s.Code == "PATT" || s.Code == "SPIR")
)
.ToListAsync())
Expand Down Expand Up @@ -79,12 +78,10 @@ public async Task<Unit> Handle(SetupStudyPlans request, CancellationToken cancel
return Unit.Value;
}

private async Task<Dictionary<int, DbStudyPlan>> GetTurnPlans(int turnNum, long universityId) {
private async Task<Dictionary<int, DbStudyPlan>> GetTurnPlans(long turnId, long universityId) {
return (await db.StudyPlans
.Include(x => x.Turn)
.Include(x => x.Unit)
.ThenInclude(x => x.Faction)
.Where(x => x.UniversityId == universityId && x.Turn.Number == turnNum)
.Where(x => x.UniversityId == universityId && x.TurnId == turnId)
.ToListAsync())
.ToDictionary(x => x.Unit.Number);
}
Expand Down
54 changes: 52 additions & 2 deletions server/Schema/UniversityClass.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace advisor
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -29,12 +30,61 @@ public UniversityClass(string id) {
public int TurnNumber { get; }

public async Task<List<DbStudyPlan>> Students([Service] Database db) {
var students = await db.StudyPlans
var query = db.StudyPlans
.AsNoTracking()
.Include(x => x.Turn)
.Include(x => x.Unit)
.ThenInclude(x => x.Faction)
.Where(x => x.UniversityId == universityId && x.Turn.Number == TurnNumber)
.ToListAsync();
.Select(x => new {
x.Id,
x.Study,
x.Target,
x.Teach,
x.TurnId,
x.UniversityId,
x.UnitId,
UnitNumber = x.Unit.Number,
UnitName = x.Unit.Name,
UnitFactionId = x.Unit.FactionId,
UnitFaction = x.Unit.Faction,
UnitSkills = x.Unit.Skills,
UnitCanStudy = x.Unit.CanStudy,
UnitRegionId = x.Unit.RegionId
});

var students = (await query.ToListAsync())
.Select(x => new DbStudyPlan {
Id = x.Id,
Study = x.Study,
Target = x.Target,
Teach = x.Teach,
TurnId = x.TurnId,
UniversityId = x.UniversityId,
UnitId = x.UnitId,
Unit = new DbUnit {
Id = x.UnitId,
Name = x.UnitName,
Number = x.UnitNumber,
FactionId = x.UnitFactionId,
Faction = x.UnitFaction,
Skills = x.UnitSkills,
CanStudy = x.UnitCanStudy,
RegionId = x.UnitRegionId
}
})
.ToList();

var regions = new Dictionary<long, DbRegion>();
foreach (var s in students) {
var regId = s.Unit.RegionId;
if (!regions.ContainsKey(regId)) {
var reg = await db.Regions.AsNoTracking().FirstOrDefaultAsync(x => x.Id == regId);
regions.Add(regId, reg);
}

s.Unit.Region = regions[regId];
}

return students;
}
Expand Down

0 comments on commit 5aebc77

Please sign in to comment.