InputFrame.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Collections.Concurrent;
  2. using System.Collections.Generic;
  3. using System.Text.Json;
  4. namespace BubbleSocketCore.Input
  5. {
  6. public class InputFrame
  7. {
  8. public int id; //frame id
  9. public ConcurrentDictionary<int, List<PlayerInput>> inputData = new ConcurrentDictionary<int, List<PlayerInput>>(); //entityId
  10. public InputFrame(int id)
  11. {
  12. this.id = id;
  13. }
  14. override public string ToString()
  15. {
  16. return JsonSerializer.Serialize(Serialize());
  17. }
  18. public Dictionary<string, object> Serialize()
  19. {
  20. var data = new Dictionary<string, object>();
  21. foreach (KeyValuePair<int, List<PlayerInput>> item in inputData)
  22. {
  23. var inputs = new List<List<string>>();
  24. item.Value.ForEach(input =>
  25. {
  26. inputs.Add(input.Serialize());
  27. });
  28. data.Add($"{item.Key}", inputs);
  29. }
  30. return data;
  31. }
  32. public void Add(int entityId, List<PlayerInput> input)
  33. {
  34. inputData.TryAdd(entityId, new List<PlayerInput> { });
  35. }
  36. public InputFrame Clone()
  37. {
  38. var clonedFrame = new InputFrame(this.id);
  39. var data = new Dictionary<int, object>();
  40. foreach (KeyValuePair<int, List<PlayerInput>> item in inputData)
  41. {
  42. var inputs = new List<PlayerInput>();
  43. item.Value.ForEach(input =>
  44. {
  45. inputs.Add(input.Clone());
  46. });
  47. clonedFrame.Add(item.Key, inputs);
  48. }
  49. return clonedFrame;
  50. }
  51. public List<PlayerInput> GetInputForEntity(int key)
  52. {
  53. List<PlayerInput> possibleInputs;
  54. inputData.TryGetValue(key, out possibleInputs);
  55. if (possibleInputs == null)
  56. {
  57. return new List<PlayerInput>();
  58. }
  59. return possibleInputs;
  60. }
  61. }
  62. }