| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | |
| | | 4 | | namespace Lattice.ECS.Session |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// SessionRuntime 的输入保留与聚合存储。 |
| | | 8 | | /// 负责 `(playerId, tick)` 输入写入、固定窗口保留以及 `SessionInputSet` 聚合。 |
| | | 9 | | /// </summary> |
| | | 10 | | internal sealed class SessionInputStore |
| | | 11 | | { |
| | | 12 | | private readonly Dictionary<int, InputBuffer> _inputBuffers = new(); |
| | | 13 | | private readonly List<SessionPlayerInput> _inputSetScratch = new(); |
| | | 14 | | private readonly int _retentionCapacity; |
| | | 15 | | |
| | | 16 | | public SessionInputStore(int retentionCapacity) |
| | | 17 | | { |
| | | 18 | | _retentionCapacity = System.Math.Max(1, retentionCapacity); |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | public void SetPlayerInput(int playerId, int tick, IPlayerInput input) |
| | | 22 | | { |
| | | 23 | | if (!_inputBuffers.TryGetValue(playerId, out InputBuffer? buffer)) |
| | | 24 | | { |
| | | 25 | | buffer = new InputBuffer(_retentionCapacity); |
| | | 26 | | _inputBuffers[playerId] = buffer; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | buffer.SetInput(tick, input); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public IPlayerInput? GetPlayerInput(int playerId, int tick) |
| | | 33 | | { |
| | | 34 | | if (!_inputBuffers.TryGetValue(playerId, out InputBuffer? buffer)) |
| | | 35 | | { |
| | | 36 | | return null; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | return buffer.GetInput(tick); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public SessionInputSet CollectInputSet(int tick) |
| | | 43 | | { |
| | | 44 | | _inputSetScratch.Clear(); |
| | | 45 | | |
| | | 46 | | foreach (KeyValuePair<int, InputBuffer> pair in _inputBuffers) |
| | | 47 | | { |
| | | 48 | | IPlayerInput? input = pair.Value.GetInput(tick); |
| | | 49 | | if (input == null) |
| | | 50 | | { |
| | | 51 | | continue; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | _inputSetScratch.Add(new SessionPlayerInput(input)); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | _inputSetScratch.Sort(static (left, right) => left.PlayerId.CompareTo(right.PlayerId)); |
| | | 58 | | return new SessionInputSet(tick, _inputSetScratch); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public void Clear() |
| | | 62 | | { |
| | | 63 | | _inputBuffers.Clear(); |
| | | 64 | | _inputSetScratch.Clear(); |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// 按固定窗口保留玩家输入的 ring buffer。 |
| | | 70 | | /// </summary> |
| | | 71 | | internal sealed class InputBuffer |
| | | 72 | | { |
| | | 73 | | private readonly IPlayerInput?[] _inputs; |
| | | 74 | | private readonly int[] _ticks; |
| | | 75 | | private readonly int _capacity; |
| | 34 | 76 | | private int _latestTick = int.MinValue; |
| | | 77 | | |
| | 34 | 78 | | public InputBuffer(int capacity) |
| | | 79 | | { |
| | 34 | 80 | | _capacity = System.Math.Max(1, capacity); |
| | 34 | 81 | | _inputs = new IPlayerInput[_capacity]; |
| | 34 | 82 | | _ticks = new int[_capacity]; |
| | | 83 | | |
| | 8272 | 84 | | for (int i = 0; i < _ticks.Length; i++) |
| | | 85 | | { |
| | 4102 | 86 | | _ticks[i] = int.MinValue; |
| | | 87 | | } |
| | 34 | 88 | | } |
| | | 89 | | |
| | | 90 | | public void SetInput(int tick, IPlayerInput input) |
| | | 91 | | { |
| | 335 | 92 | | if (tick > _latestTick) |
| | | 93 | | { |
| | 312 | 94 | | _latestTick = tick; |
| | | 95 | | } |
| | | 96 | | |
| | 335 | 97 | | if (tick < GetMinTick()) |
| | | 98 | | { |
| | 0 | 99 | | return; |
| | | 100 | | } |
| | | 101 | | |
| | 335 | 102 | | int index = ToIndex(tick); |
| | 335 | 103 | | _inputs[index] = input; |
| | 335 | 104 | | _ticks[index] = tick; |
| | 335 | 105 | | } |
| | | 106 | | |
| | | 107 | | public IPlayerInput? GetInput(int tick) |
| | | 108 | | { |
| | 462 | 109 | | if (_latestTick == int.MinValue || tick < GetMinTick()) |
| | | 110 | | { |
| | 1 | 111 | | return null; |
| | | 112 | | } |
| | | 113 | | |
| | 461 | 114 | | int index = ToIndex(tick); |
| | 461 | 115 | | return _ticks[index] == tick ? _inputs[index] : null; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | private int GetMinTick() |
| | | 119 | | { |
| | 797 | 120 | | if (_latestTick == int.MinValue) |
| | | 121 | | { |
| | 0 | 122 | | return int.MaxValue; |
| | | 123 | | } |
| | | 124 | | |
| | 797 | 125 | | return _latestTick - _capacity + 1; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | private int ToIndex(int tick) |
| | | 129 | | { |
| | 796 | 130 | | int index = tick % _capacity; |
| | 796 | 131 | | return index < 0 ? index + _capacity : index; |
| | | 132 | | } |
| | | 133 | | } |
| | | 134 | | } |