using System.Collections.Concurrent; using System.Collections.Generic; using System.Text.Json; namespace BubbleSocketCore.Input { public class InputFrame { public int id; //frame id public ConcurrentDictionary> inputData = new ConcurrentDictionary>(); //entityId public InputFrame(int id) { this.id = id; } override public string ToString() { return JsonSerializer.Serialize(Serialize()); } public Dictionary Serialize() { var data = new Dictionary(); foreach (KeyValuePair> item in inputData) { var inputs = new List>(); item.Value.ForEach(input => { inputs.Add(input.Serialize()); }); data.Add($"{item.Key}", inputs); } return data; } public void Add(int entityId, List input) { inputData.TryAdd(entityId, new List { }); } public InputFrame Clone() { var clonedFrame = new InputFrame(this.id); var data = new Dictionary(); foreach (KeyValuePair> item in inputData) { var inputs = new List(); item.Value.ForEach(input => { inputs.Add(input.Clone()); }); clonedFrame.Add(item.Key, inputs); } return clonedFrame; } public List GetInputForEntity(int key) { List possibleInputs; inputData.TryGetValue(key, out possibleInputs); if (possibleInputs == null) { return new List(); } return possibleInputs; } } }