public RcfProfile SearchRcfProfile(int rcfId) { using (var db = new Ri2Context()) { return(db.RcfProfiles.Include(x => x.FideProfile).FirstOrDefault(x => x.RcfId == rcfId)); } }
public IEnumerable <Group> GetGroups() { using (var db = new Ri2Context()) { return(db.Groups.ToList()); } }
public IEnumerable <FideProfile> GetFideProfiles() { using (var db = new Ri2Context()) { return(db.FideProfiles.ToList()); } }
public FideProfile SearchFideProfile(int fideId) { using (var db = new Ri2Context()) { return(db.FideProfiles.FirstOrDefault(x => x.FideId == fideId)); } }
public IEnumerable <Profile> GetProfiles(string needle = null) { var res = new List <Profile>(); using (var db = new Ri2Context()) { if (string.IsNullOrEmpty(needle)) { res.AddRange(db.Profiles.Include(x => x.RcfProfile).Include(x => x.FideProfile) .Include(x => x.Group)); } else { var tmp = db.Profiles .Where(x => x.RcfProfile.Name.StartsWith(needle) || x.FideProfile.Name.StartsWith(needle) || x.RcfProfile.RcfId.ToString() == needle || x.FideProfile.FideId.ToString() == needle) .Include(x => x.RcfProfile) .Include(x => x.FideProfile) .Include(x => x.Group); res.AddRange(tmp); } } return(res); }
public void MergeProfiles(Profile targetProfile, RcfProfile rcfProfile, FideProfile fideProfile) { using (var db = new Ri2Context()) { targetProfile = db.Profiles.FirstOrDefault(x => x.Id == targetProfile.Id); if (targetProfile == null) { return; } if (rcfProfile != null) { rcfProfile = db.RcfProfiles.FirstOrDefault(x => x.Id == rcfProfile.Id); } if (fideProfile != null) { fideProfile = db.FideProfiles.FirstOrDefault(x => x.Id == fideProfile.Id); } if (rcfProfile != null) { targetProfile.RcfProfile = rcfProfile; } if (fideProfile != null) { targetProfile.FideProfile = fideProfile; if (rcfProfile != null) { rcfProfile.FideProfile = fideProfile; } } db.SaveChanges(); } }
public async Task MergeGroups(Group from, Group to) { using (var db = new Ri2Context()) { var origin = db.Groups.FirstOrDefault(x => x.Id == from.Id); if (origin == null) { return; } var target = db.Groups.FirstOrDefault(x => x.Id == to.Id); if (target == null) { return; } await Task.Run(() => { foreach (var profile in origin.Profiles) { profile.Group = target; } }).ConfigureAwait(false); db.SaveChanges(); } }
public IEnumerable <RcfProfile> GetRcfProfiles() { using (var db = new Ri2Context()) { return(db.RcfProfiles.Include(x => x.FideProfile).ToList()); } }
public void ImportAsync(IEnumerable <int> ids, Group group, ProfileType profileType) { var profiles = new List <Profile>(); using (var db = new Ri2Context()) { var gr = db.Groups.FirstOrDefault(x => x.Id == group.Id); if (gr == null) { return; } foreach (var ind in ids) { if (profileType == ProfileType.Rcf) { var pr = db.RcfProfiles.FirstOrDefault(x => x.RcfId == ind); if (pr == null) { File.AppendAllText("err.log", DateTime.Now.ToShortTimeString() + "|ImportService|RcfId" + ind + "not found" + Environment.NewLine); continue; } if (db.Profiles.Any(x => x.RcfProfileId == pr.Id)) { continue; } profiles.Add(new Profile { RcfProfile = pr, FideProfile = pr.FideProfile, Group = gr }); } else { var pr = db.FideProfiles.FirstOrDefault(profile => profile.FideId == ind); if (pr == null) { File.AppendAllText("err.log", DateTime.Now.ToShortTimeString() + "|ImportService|FideId " + ind + "not found" + Environment.NewLine); continue; } var rcf = db.RcfProfiles.FirstOrDefault(profile => profile.FideProfileId == pr.Id); if (db.Profiles.Any(x => x.FideProfileId == pr.Id)) { continue; } profiles.Add(new Profile { FideProfile = pr, RcfProfile = rcf, Group = gr }); } } db.Profiles.AddRange(profiles); db.SaveChanges(); //await db.SaveChangesAsync().ConfigureAwait(false); } }
public void DeleteProfile(int id) { using (var db = new Ri2Context()) { db.Profiles.Remove( db.Profiles.SingleOrDefault(x => x.Id == id) ?? throw new InvalidOperationException()); db.SaveChanges(); } }
public List <Group> GetGroups(bool fullData = false) { using (var db = new Ri2Context()) { return(fullData ? db.Groups.Include(x => x.Profiles).Include(x => x.Profiles.Select(y => y.FideProfile)) .Include(x => x.Profiles.Select(y => y.RcfProfile)).ToList() : db.Groups.ToList()); } }
public void CreateGroup(Group gr) { using (var db = new Ri2Context()) { gr.Id = 0; db.Groups.Attach(gr); db.Entry(gr).State = EntityState.Added; db.SaveChanges(); } }
public void DeleteGroup(Group gr) { using (var db = new Ri2Context()) { var tmp = db.Groups.Include(x => x.Profiles).FirstOrDefault(x => x.Id == gr.Id); if (tmp == null || tmp.Profiles.Count != 0) { return; } db.Groups.Remove(tmp); db.SaveChanges(); } }
public void UpdateGroup(Group gr) { using (var db = new Ri2Context()) { var tmp = db.Groups.FirstOrDefault(x => x.Id == gr.Id); if (tmp == null) { return; } tmp.Name = gr.Name; db.SaveChanges(); } }
public void SaveProfile(Profile profile) { using (var db = new Ri2Context()) { var pr = db.Profiles.FirstOrDefault(x => x.Id == profile.Id); if (pr == null) { throw new Exception("profile"); } var gr = db.Groups.FirstOrDefault(x => x.Id == profile.Group.Id); pr.Group = gr ?? throw new Exception("group"); db.SaveChanges(); } }
public void DeleteGroupWithProfiles(int id) { using (var db = new Ri2Context()) { var gr = db.Groups.Include(x => x.Profiles).FirstOrDefault(x => x.Id == id); if (gr == null) { return; } db.Profiles.RemoveRange(gr.Profiles); db.Groups.Remove(gr); db.SaveChanges(); } }
public Task ExportAsync(IEnumerable <Group> groups, ExportSettings settings, Guid pluginGuid) { var t = new List <Profile>(); using (var db = new Ri2Context()) { foreach (var g in groups) { var gr = db.Groups.Include(x => x.Profiles).Include(x => x.Profiles.Select(p => p.FideProfile)) .Include(x => x.Profiles.Select(p => p.RcfProfile).Select(r => r.FideProfile)) .Single(x => x.Id == g.Id); t.AddRange(gr.Profiles); } } return(ExportAsync(t, settings, pluginGuid)); }
private static void CheckProfiles() { using (var ri2 = new Ri2Context()) { foreach (var profile in ri2.Profiles.Include("RcfProfile").Include("FideProfile")) { if (profile.FideProfileId.HasValue && profile.RcfProfileId.HasValue) { continue; } if (profile.RcfProfileId.HasValue) { if (profile.RcfProfile.FideProfileId.HasValue) { profile.FideProfile = profile.RcfProfile.FideProfile; } continue; } if (!profile.FideProfileId.HasValue) { continue; } var id = profile.FideProfileId.Value; var rcf = ri2.RcfProfiles.FirstOrDefault(x => x.FideProfileId.Value == id); if (rcf == null) { continue; } profile.RcfProfile = rcf; } ri2.SaveChanges(); } }
private static void ProcessRcf() { var fi = new FileInfo(RcfFilePath); using (var pkg = new ExcelPackage(fi)) { var ws = pkg.Workbook.Worksheets[1]; var start = ws.Dimension.Start; var end = ws.Dimension.End; var add = new List <RcfProfile>(75000); var mod = new List <RcfProfile>(75000); Dictionary <int, int> tf; Dictionary <int, int> t; using (var db = new Ri2Context { Configuration = { AutoDetectChangesEnabled = false } }) { tf = db.FideProfiles.Select(x => new { x.FideId, x.Id }) .ToDictionary(o => o.FideId, o => o.Id); t = db.RcfProfiles.Select(x => new { x.RcfId, x.Id }) .ToDictionary(o => o.RcfId, o => o.Id); } for (var i = start.Row + 1; i <= end.Row; i++) { var pr = new RcfProfile { RcfId = ws.Cells[i, RcfColumns.RcfId].GetValue <int>(), Name = ws.Cells[i, RcfColumns.Name].GetValue <string>(), Birth = ws.Cells[i, RcfColumns.Birth].GetValue <string>() != "" ? ws.Cells[i, RcfColumns.Birth].GetValue <int>() : 0, Std = ws.Cells[i, RcfColumns.Std].GetValue <string>() != "" ? ws.Cells[i, RcfColumns.Std].GetValue <int>() : 0, Rpd = ws.Cells[i, RcfColumns.Rpd].GetValue <string>() != "" ? ws.Cells[i, RcfColumns.Rpd].GetValue <int>() : 0, Blz = ws.Cells[i, RcfColumns.Blz].GetValue <string>() != "" ? ws.Cells[i, RcfColumns.Blz].GetValue <int>() : 0 }; if (pr.Birth < Settings.Current.BirthCutoff) { continue; } var fideId = ws.Cells[i, RcfColumns.FideId].GetValue <int?>(); //using (var ri2 = new Ri2Context {Configuration = {AutoDetectChangesEnabled = false}}) { if (fideId.HasValue) { if (tf.ContainsKey(fideId.Value)) { pr.FideProfileId = tf[fideId.Value]; } } //var t = ri2.RcfProfiles.FirstOrDefault(cp => cp.RcfId == pr.RcfId); if (t.ContainsKey(pr.RcfId)) { pr.Id = t[pr.RcfId]; mod.Add(pr); } else { add.Add(pr); } } } using (var ri2 = new Ri2Context()) { EFBatchOperation.For(ri2, ri2.RcfProfiles).InsertAll(add); EFBatchOperation.For(ri2, ri2.RcfProfiles).UpdateAll(mod, x => x.ColumnsToUpdate(c => c.Name, c => c.Birth, c => c.Std, c => c.Rpd, c => c.Blz, c => c.FideProfileId)); ri2.SaveChanges(); } } }
private static void ProcessFide() { var query = SimpleXmlStream.SimpleStreamAxis(FideFilePath, FideXmlElements.Player); var add = new List <FideProfile>(100000); var mod = new List <FideProfile>(100000); Dictionary <int, int> t; using (var db = new Ri2Context { Configuration = { AutoDetectChangesEnabled = false } }) { t = db.FideProfiles.Select(x => new { x.FideId, x.Id }) .ToDictionary(o => o.FideId, o => o.Id); } foreach (var profile in query) { if (!Settings.Current.Filter.Contains(profile.Element(FideXmlElements.Country)?.Value)) { continue; } if (profile.Element(FideXmlElements.Name)?.Value == "") { continue; } var pr = new FideProfile { Name = (profile.Element(FideXmlElements.Name)?.Value == "" ? "_" : profile.Element(FideXmlElements.Name)?.Value) ?? "_", FideId = Convert.ToInt32((profile.Element(FideXmlElements.FideId)?.Value == "" ? "0" : profile.Element(FideXmlElements.FideId)?.Value) ?? "0"), Std = Convert.ToInt32((profile.Element(FideXmlElements.Std)?.Value == "" ? "0" : profile.Element(FideXmlElements.Std)?.Value) ?? "0"), Rpd = Convert.ToInt32((profile.Element(FideXmlElements.Rpd)?.Value == "" ? "0" : profile.Element(FideXmlElements.Rpd)?.Value) ?? "0"), Blz = Convert.ToInt32((profile.Element(FideXmlElements.Blz)?.Value == "" ? "0" : profile.Element(FideXmlElements.Blz)?.Value) ?? "0"), Birth = Convert.ToInt32((profile.Element(FideXmlElements.Birth)?.Value == "" ? "0" : profile.Element(FideXmlElements.Birth)?.Value) ?? "0") }; if (pr.Birth < Settings.Current.BirthCutoff) { continue; } //File.AppendAllText("log.txt",pr.FideId+Environment.NewLine); //using (var ri2 = new Ri2Context {Configuration = {AutoDetectChangesEnabled = false}}) { //var t = ri2.FideProfiles.FirstOrDefault(cp => cp.FideId == pr.FideId); //if (t != null) if (t.ContainsKey(pr.FideId)) { //pr.Id = t.Id; pr.Id = t[pr.FideId]; mod.Add(pr); } else { add.Add(pr); } } } using (var db = new Ri2Context()) { EFBatchOperation.For(db, db.FideProfiles).InsertAll(add); db.SaveChanges(); } using (var db = new Ri2Context()) { EFBatchOperation.For(db, db.FideProfiles).UpdateAll(mod, x => x.ColumnsToUpdate(c => c.Name, c => c.Birth, c => c.Std, c => c.Rpd, c => c.Blz)); db.SaveChanges(); } }
public async Task UpdateAsync(IProgress <string> progress = null) { if (File.Exists(FideZipPath)) { File.Delete(FideZipPath); } if (File.Exists(RcfFilePath)) { File.Delete(RcfFilePath); } if (File.Exists(FideFilePath)) { File.Delete(FideFilePath); } using (var client = new WebClient()) { progress?.Report(FideZip); await client.DownloadFileTaskAsync(new Uri(Settings.Current.FideUrl), FideZipPath) .ConfigureAwait(false); progress?.Report(FideZipComplete); progress?.Report(RcfXsl); await client.DownloadFileTaskAsync(new Uri(Settings.Current.RcfUrl), RcfFilePath) .ConfigureAwait(false); progress?.Report(RcfXslComplete); } progress?.Report(FideUnzip); ZipFile.ExtractToDirectory(FideZipPath, TmpPath); progress?.Report(FideProcess); /* * <fideid>419214</fideid> * <name></name> * <country>ENG</country> * <sex>M</sex> * <title></title> * <w_title></w_title> * <o_title></o_title> * <foa_title></foa_title> * <rating>1736</rating> * <games>0</games> * <k>20</k> * <rapid_rating>1587</rapid_rating> * <rapid_games>10</rapid_games> * <rapid_k>20</rapid_k> * <blitz_rating></blitz_rating> * <blitz_games></blitz_games> * <blitz_k></blitz_k> * <birthday></birthday> * <flag></flag> */ await Task.Run(() => ProcessFide()).ConfigureAwait(false); progress?.Report(CompleteMsg); progress?.Report(RcfProcess); await Task.Run(() => ProcessRcf()).ConfigureAwait(false); progress?.Report(Chk); CheckProfiles(); using (var db = new Ri2Context()) { var t = db.Info.FirstOrDefault(x => x.Name == InfoKeys.LastUpdate); if (t == null) { t = new InfoValue { Name = InfoKeys.LastUpdate, Value = DateTime.Now.ToShortDateString() }; db.Info.Add(t); } else { t.Value = DateTime.Now.ToShortDateString(); } await db.SaveChangesAsync().ConfigureAwait(false); } progress?.Report(CompleteMsg); }