using System; using System.Collections.Generic; using System.Collections.ObjectModel; namespace game.PlayerDataStorage { // Only two approved in-memory normalizations. Never a persistence receipt or retry queue. internal sealed class PlayerKVInitializationIntents { readonly Dictionary values=new Dictionary(StringComparer.Ordinal); readonly string channel; internal long Epoch { get; } internal bool Discarded { get; private set; } internal int DiscardedCount { get; private set; } internal int Count => values.Count; internal IReadOnlyDictionary Snapshot => new ReadOnlyDictionary(new Dictionary(values)); internal PlayerKVInitializationIntents(long epoch,PlayerKVIdentityChannel channel) { if(!Enum.IsDefined(typeof(PlayerKVIdentityChannel),channel)) throw new ArgumentException("Unknown intent channel."); Epoch=epoch; this.channel=channel==PlayerKVIdentityChannel.hwGuest || channel==PlayerKVIdentityChannel.none ? null : channel.ToString(); } internal void Add(long epoch,string key,string value) { if(Discarded || epoch!=Epoch || key==null || (key!="MMTest" && key!=channel) || value==null) throw new InvalidOperationException("Initialization intent rejected."); values[key]=value; } internal void Discard() { if(Discarded) return; DiscardedCount=values.Count; values.Clear(); Discarded=true; } } }