Files
ft/Client/Assets/Scripts/Services/PlayerData/PlayerKVInitializationIntents.cs

31 lines
1.5 KiB
C#

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<string,string> values=new Dictionary<string,string>(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<string,string> Snapshot => new ReadOnlyDictionary<string,string>(new Dictionary<string,string>(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; }
}
}