private static void TryAddOrUpdateFactionPresence(MinorFactionPresence mfp, StarSystem ss) { DbContextOptionsBuilder <EliteDbContext> config = new DbContextOptionsBuilder <EliteDbContext>(); config.UseNpgsql(_connectionString); using (EliteDbContext context = new EliteDbContext(config.Options)) { mfp.StarSystemId = context.StarSystem.Where(s => s.EddbId == ss.EddbId).Select(s => s.Id).FirstOrDefault(); if (mfp.StarSystemId == 0) { return; } if (context.MinorFactionPresence.Where(m => m.StarSystemId == mfp.StarSystemId && m.MinorFactionId == mfp.MinorFactionId).Count() == 0) { context.MinorFactionPresence.Add(mfp); context.SaveChanges(); Console.WriteLine("Added faction presence for system #" + mfp.StarSystemId); } else { context.MinorFactionPresence.Update(mfp); context.SaveChanges(); } } }
private static void ImportPopSystemsJson(string path, bool parseFactions) { var array = JArray.Parse(File.ReadAllText(path)); foreach (JObject jo in array) { try { var ss = new StarSystem { Allegiance = jo.Value <string>("allegiance").ToNormalCase(LookupOptions.Allegiances), EddbId = jo.Value <long?>("id"), EdsmId = jo.Value <long?>("edsm_id"), Government = jo.Value <string>("government").ToNormalCase(LookupOptions.Governments), IsPopulated = true, State = jo.Value <string>("state").ToNormalCase(LookupOptions.States), PrimaryEconomy = jo.Value <string>("primary_economy").ToNormalCase(LookupOptions.Economies), Name = jo.Value <string>("name"), NameLower = jo.Value <string>("name").ToLower(), Security = jo.Value <string>("security").ToNormalCase(LookupOptions.SecurityTypes), PowerPlayLeader = jo.Value <string>("power").ToNormalCase(LookupOptions.PowerPlayLeaders), PowerPlayState = jo.Value <string>("power_state").ToNormalCase(LookupOptions.PowerEffects), Reserves = jo.Value <string>("reserve_type").ToNormalCase(LookupOptions.ReserveTypes), Population = jo.Value <long?>("population") ?? 0, X = jo.Value <double>("x"), Y = jo.Value <double>("y"), Z = jo.Value <double>("z"), NeedsPermit = jo.Value <bool?>("needs_permit") ?? false, SimbadRef = jo.Value <string>("simbad_ref"), UpdatedAt = DateTimeOffset.FromUnixTimeSeconds(jo.Value <long>("updated_at")).UtcDateTime }; if (parseFactions) { ss.ControllingMinorFactionId = jo.Value <long?>("controlling_minor_faction_id"); var presences = jo.Value <JArray>("minor_faction_presences"); foreach (JObject m in presences) { try { var mfp = new MinorFactionPresence { MinorFactionId = m.Value <long>("minor_faction_id"), Influence = m.Value <double>("influence") }; TryAddOrUpdateFactionPresence(mfp, ss); } catch { File.AppendAllText($"{_dir}/debug/debug.txt", $"eddb_mfp,{ss.Name},{m.Value<long?>("minor_faction_id")}\n"); } } } TryAddOrUpdateSystem(ss); } catch { File.AppendAllText($"{_dir}/debug/debug.txt", $"eddb_ss,{jo.Value<string>("name")},{jo.Value<long?>("id")}\n"); } } }