/// <summary> /// Used by above initialize methods to create any type of cloud preference. /// </summary> /// <param name="key">Must be a unique identifier for this specific value.</param> /// <param name="item">The <see cref="SyncableItem"/> you want to store in the local <see cref="GameData"/>.</param> private static void CreateItem(string key, SyncableItem item) { SyncableItem localItem; // If the item already exists, update it instead of creating a new item if (s_localGameData.SyncableItems.TryGetValue(key, out localItem)) { // Ignore if the new item doesn't have the same persistence type as the current item if (localItem.Metadata.PersistenceType == item.Metadata.PersistenceType) { // Only set item if the value is different from the current value if (!localItem.Equals(item)) { s_localGameData.SyncableItems[key] = ConflictResolver.ResolveConflict(localItem, item); IsLocalDataDirty = true; } } } else { // Create new item s_localGameData.SyncableItems.Add(key, item); IsLocalDataDirty = true; } }
/// <summary> /// Merges this <see cref="GameData"/> with another. /// </summary> /// <param name="otherData">The <see cref="GameData"/> to merge into this one.</param> /// <returns>A <see cref="string"/> array of the changed keys. Will be empty if no data changed.</returns> public string[] MergeWith(GameData otherData) { var changedKeys = new List <string>(); foreach (var otherItem in otherData.SyncableItems) { SyncableItem localItem; if (SyncableItems.TryGetValue(otherItem.Key, out localItem)) { var selectedItem = ConflictResolver.ResolveConflict(localItem, otherItem.Value); if (!selectedItem.Equals(localItem)) { SyncableItems[otherItem.Key] = selectedItem; changedKeys.Add(otherItem.Key); } } else { SyncableItems.Add(otherItem.Key, otherItem.Value); changedKeys.Add(otherItem.Key); } } foreach (var otherCurrency in otherData.SyncableCurrencies) { SyncableCurrency localCurrency; if (SyncableCurrencies.TryGetValue(otherCurrency.Key, out localCurrency)) { var mergeResult = localCurrency.MergeWith(otherCurrency.Value); if (mergeResult) { changedKeys.Add(otherCurrency.Key); } } else { SyncableCurrencies.Add(otherCurrency.Key, otherCurrency.Value); changedKeys.Add(otherCurrency.Key); } } return(changedKeys.ToArray()); }