/// <summary> /// Compares a marker /// </summary> /// <param name="mapId">Id of the map</param> /// <param name="markerId">Id of the marker</param> /// <param name="markerType">Type of the marker</param> /// <param name="currentMarker">Current marker, if null the marker will be loaded</param> /// <returns>Compare Result</returns> public async Task <CompareResult> CompareMarker(string mapId, string markerId, MarkerType markerType, IImplementationComparable currentMarker = null) { KartaMap loadingMap = null; if (currentMarker == null) { loadingMap = await _mapDbAccess.GetMapById(mapId); } IImplementationComparable oldMarker = null; if (markerType == MarkerType.Npc) { oldMarker = await _markerSnapshotDbAccess.GetNpcMarkerSnapshotById(markerId); if (loadingMap != null && loadingMap.NpcMarker != null) { currentMarker = loadingMap.NpcMarker.First(m => m.Id == markerId); } } else if (markerType == MarkerType.Item) { oldMarker = await _markerSnapshotDbAccess.GetItemMarkerSnapshotById(markerId); if (loadingMap != null && loadingMap.ItemMarker != null) { currentMarker = loadingMap.ItemMarker.First(m => m.Id == markerId); } } else if (markerType == MarkerType.MapChange) { oldMarker = await _markerSnapshotDbAccess.GetMapChangeMarkerSnapshotById(markerId); if (loadingMap != null && loadingMap.MapChangeMarker != null) { currentMarker = loadingMap.MapChangeMarker.First(m => m.Id == markerId); } } else if (markerType == MarkerType.Quest) { oldMarker = await _markerSnapshotDbAccess.GetQuestMarkerSnapshotById(markerId); if (loadingMap != null && loadingMap.QuestMarker != null) { currentMarker = loadingMap.QuestMarker.First(m => m.Id == markerId); } } else if (markerType == MarkerType.Note) { oldMarker = await _markerSnapshotDbAccess.GetNoteMarkerSnapshotById(markerId); if (loadingMap != null && loadingMap.NoteMarker != null) { currentMarker = loadingMap.NoteMarker.First(m => m.Id == markerId); } } return(CompareObjects(currentMarker, oldMarker)); }
/// <summary> /// Compares the values of two objects /// </summary> /// <param name="currentState">Current State of the object</param> /// <param name="oldState">Old State of the object</param> /// <returns>Compare results</returns> private List <CompareDifference> CompareValues(IImplementationComparable currentState, IImplementationComparable oldState) { List <CompareDifference> compareResults = new List <CompareDifference>(); List <PropertyInfo> props = currentState.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(ValueCompareAttribute))).ToList(); foreach (PropertyInfo curProperty in props) { object newValue = curProperty.GetValue(currentState); object oldValue = curProperty.GetValue(oldState); if (newValue == null && oldValue == null) { continue; } ValueCompareAttribute compareAttribute = curProperty.GetCustomAttribute <ValueCompareAttribute>(); if ((newValue != null && oldValue == null) || (newValue == null && oldValue != null)) { compareResults.Add(BuildCompareDifference(curProperty.Name, newValue, oldValue, compareAttribute.LabelKey, compareAttribute.TextKey)); } else if (newValue is IImplementationComparable && oldValue is IImplementationComparable) { // Compare complex subobject List <CompareDifference> differences = CompareValues((IImplementationComparable)newValue, (IImplementationComparable)oldValue); differences.AddRange(CompareLists((IImplementationComparable)newValue, (IImplementationComparable)oldValue)); if (differences.Any()) { CompareDifference compareDifference = BuildCompareDifference(curProperty.Name, null, null, compareAttribute.LabelKey, compareAttribute.TextKey); compareDifference.SubDifferences = differences; compareResults.Add(compareDifference); } } else if (!newValue.Equals(oldValue)) { compareResults.Add(BuildCompareDifference(curProperty.Name, newValue, oldValue, compareAttribute.LabelKey, compareAttribute.TextKey)); } } return(compareResults); }
/// <summary> /// Compares the lists of two objects /// </summary> /// <param name="currentState">Current State of the object</param> /// <param name="oldState">Old State of the object</param> /// <returns>Compare results</returns> private List <CompareDifference> CompareLists(IImplementationComparable currentState, IImplementationComparable oldState) { List <CompareDifference> compareResults = new List <CompareDifference>(); List <PropertyInfo> props = currentState.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(ListCompareAttribute))).ToList(); foreach (PropertyInfo curProperty in props) { object newValue = curProperty.GetValue(currentState); object oldValue = curProperty.GetValue(oldState); if (newValue == null || oldValue == null) { continue; } ListCompareAttribute compareAttribute = curProperty.GetCustomAttribute <ListCompareAttribute>(); if ((newValue != null && oldValue == null) || (newValue == null && oldValue != null)) { compareResults.Add(BuildCompareDifference(curProperty.Name, newValue, oldValue, compareAttribute.LabelKey, string.Empty)); } else { // Compare lists IEnumerable newValueEnumerable = (IEnumerable)newValue; IEnumerable oldValueEnumerable = (IEnumerable)oldValue; if (curProperty.PropertyType.GenericTypeArguments.Any() && typeof(IImplementationListComparable).IsAssignableFrom(curProperty.PropertyType.GenericTypeArguments[0])) { CompareDifference difference = BuildCompareDifference(curProperty.Name, null, null, compareAttribute.LabelKey, string.Empty); difference.SubDifferences.AddRange(CompareImplementationComparableList(newValueEnumerable, oldValueEnumerable)); if (difference.SubDifferences.Count > 0) { compareResults.Add(difference); } } } } return(compareResults); }
/// <summary> /// Compares two objects /// </summary> /// <param name="currentState">Current State of the object</param> /// <param name="oldState">Old State of the object</param> /// <returns>Compare results</returns> private CompareResult CompareObjects(IImplementationComparable currentState, IImplementationComparable oldState) { CompareResult result = new CompareResult(); if (oldState == null) { result.DoesSnapshotExist = false; return(result); } result.DoesSnapshotExist = true; // Check Data if (currentState.GetType() != oldState.GetType()) { throw new ArgumentException(); } result.CompareDifference.AddRange(CompareValues(currentState, oldState)); result.CompareDifference.AddRange(CompareLists(currentState, oldState)); return(result); }