public void UpdateFactoryItems(PlayerInfo aPlayerInfo) { using (var DB = new FactoryItemsContext()) { var FactoryItems = DB.FactoryItems .OrderByDescending(B => B.Timestamp) .FirstOrDefault(B => B.Id == aPlayerInfo.steamId); var IsNewBackpack = FactoryItems == null || (DateTime.Now - FactoryItems.Timestamp).TotalMinutes >= 1; var Content = JsonConvert.SerializeObject(aPlayerInfo.bpResourcesInFactory?.Select(I => new ItemStack(I.Key, (int)I.Value))); if (FactoryItems != null) { if (IsEqual(JsonConvert.DeserializeObject <ItemStack[]>(FactoryItems.Content), aPlayerInfo.bpResourcesInFactory?.Select(I => new ItemStack(I.Key, (int)I.Value)).ToArray())) { return; } } if (IsNewBackpack) { FactoryItems = new FactoryItems() { Id = aPlayerInfo.steamId, Timestamp = DateTime.Now, Content = Content, InProduction = aPlayerInfo.bpInFactory, Produced = aPlayerInfo.producedPrefabs?.Aggregate("", (S, B) => string.IsNullOrEmpty(S) ? B : S + "\t" + B), }; DB.FactoryItems.Add(FactoryItems); } else { FactoryItems.InProduction = aPlayerInfo.bpInFactory; FactoryItems.Produced = aPlayerInfo.producedPrefabs?.Aggregate("", (S, B) => string.IsNullOrEmpty(S) ? B : S + "\t" + B); } DB.SaveChanges(); FactoryItems = DB.FactoryItems.FirstOrDefault(B => B.Id == aPlayerInfo.steamId && B.Timestamp == DateTime.MinValue); IsNewBackpack = FactoryItems == null; if (IsNewBackpack) { FactoryItems = new FactoryItems() { Id = aPlayerInfo.steamId, Timestamp = DateTime.MinValue, } } ; FactoryItems.Content = Content; if (IsNewBackpack) { DB.FactoryItems.Add(FactoryItems); } var count = DB.SaveChanges(); } }
public void DeleteOldFactoryItems(int aDays) { using (var DB = new FactoryItemsContext()) { DB.FactoryItems .Where(B => B.Timestamp != DateTime.MinValue && (DateTime.Now - B.Timestamp).TotalDays > aDays) .ToList() .ForEach(B => DB.FactoryItems.Remove(B)); DB.SaveChanges(); DB.Database.ExecuteSqlCommand("VACUUM;"); } }