public void LoadConnections() { PalantirDbContext Database = new PalantirDbContext(); PalantirTethers = new List <Tether>(); foreach (PalantirEntity palantirEntity in Database.Palantiri) { Tether tether; ObservedGuild guild = JsonConvert.DeserializeObject <ObservedGuild>(palantirEntity.Palantir); if (Database.GuildSettings.Any(s => s.GuildID == guild.GuildID)) { tether = new Tether(guild, JsonConvert.DeserializeObject <GuildSettings>(Database.GuildSettings.FirstOrDefault(s => s.GuildID == guild.GuildID).Settings)); } else { tether = new Tether(guild); } PalantirTethers.Add(tether); } PalantirMembers = new List <Member>(); foreach (MemberEntity memberEntity in Database.Members) { PalantirMembers.Add(JsonConvert.DeserializeObject <Member>(memberEntity.Member)); } Database.Dispose(); }
public void SavePalantiri(ObservedGuild guild) { bool newGuild = true; PalantirDbContext Database = new PalantirDbContext(); // If guild of new palantir has already an active palantir, close tether, replace palantir and reopen tether PalantirTethers.ForEach((t) => { if (t.PalantirEndpoint.GuildID == guild.GuildID) { string oldToken = t.PalantirEndpoint.ObserveToken; // update tether t.StopDataflow(); t.SetNewPalantirEndpoint(guild); t.EstablishDataflow(); newGuild = false; // Console.WriteLine("Change token from " + oldToken + " to " + guild.ObserveToken); // update db entry Database.Palantiri.Remove(Database.Palantiri.FirstOrDefault(p => p.Token == oldToken)); PalantirEntity entity = new PalantirEntity(); entity.Palantir = JsonConvert.SerializeObject(guild); entity.Token = guild.ObserveToken; Database.Palantiri.Add(entity); Database.SaveChanges(); } }); if (newGuild) { // add tether Tether tether = new Tether(guild); tether.EstablishDataflow(); PalantirTethers.Add(tether); // Add db entry PalantirEntity entity = new PalantirEntity(); entity.Token = guild.ObserveToken; entity.Palantir = JsonConvert.SerializeObject(guild); Database.Palantiri.Add(entity); Database.SaveChanges(); } Database.Dispose(); UpdateMemberGuilds(); }
public void UpdatePalantirSettings(Tether tether) { PalantirDbContext context = new PalantirDbContext(); GuildSettingsEntity entity = context.GuildSettings.FirstOrDefault(s => s.GuildID == tether.PalantirEndpoint.GuildID); if (entity != null) { entity.Settings = JsonConvert.SerializeObject(tether.PalantirSettings); context.SaveChanges(); } else { entity = new GuildSettingsEntity(); entity.GuildID = tether.PalantirEndpoint.GuildID; entity.Settings = JsonConvert.SerializeObject(tether.PalantirSettings); context.GuildSettings.Add(entity); context.SaveChanges(); } context.SaveChanges(); context.Dispose(); }