< Summary

Information
Class: Lattice.ECS.Session.SessionInputStore
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Session/SessionInputStore.cs
Line coverage
95%
Covered lines: 22
Uncovered lines: 1
Coverable lines: 23
Total lines: 134
Line coverage: 95.6%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
SetPlayerInput(...)100%22100%
GetPlayerInput(...)50%2266.66%
CollectInputSet(...)100%44100%
Clear()100%11100%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Session/SessionInputStore.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3
 4namespace Lattice.ECS.Session
 5{
 6    /// <summary>
 7    /// SessionRuntime 的输入保留与聚合存储。
 8    /// 负责 `(playerId, tick)` 输入写入、固定窗口保留以及 `SessionInputSet` 聚合。
 9    /// </summary>
 10    internal sealed class SessionInputStore
 11    {
 11312        private readonly Dictionary<int, InputBuffer> _inputBuffers = new();
 11313        private readonly List<SessionPlayerInput> _inputSetScratch = new();
 14        private readonly int _retentionCapacity;
 15
 11316        public SessionInputStore(int retentionCapacity)
 17        {
 11318            _retentionCapacity = System.Math.Max(1, retentionCapacity);
 11319        }
 20
 21        public void SetPlayerInput(int playerId, int tick, IPlayerInput input)
 22        {
 33023            if (!_inputBuffers.TryGetValue(playerId, out InputBuffer? buffer))
 24            {
 3225                buffer = new InputBuffer(_retentionCapacity);
 3226                _inputBuffers[playerId] = buffer;
 27            }
 28
 33029            buffer.SetInput(tick, input);
 33030        }
 31
 32        public IPlayerInput? GetPlayerInput(int playerId, int tick)
 33        {
 234            if (!_inputBuffers.TryGetValue(playerId, out InputBuffer? buffer))
 35            {
 036                return null;
 37            }
 38
 239            return buffer.GetInput(tick);
 40        }
 41
 42        public SessionInputSet CollectInputSet(int tick)
 43        {
 62544            _inputSetScratch.Clear();
 45
 216046            foreach (KeyValuePair<int, InputBuffer> pair in _inputBuffers)
 47            {
 45548                IPlayerInput? input = pair.Value.GetInput(tick);
 45549                if (input == null)
 50                {
 51                    continue;
 52                }
 53
 40954                _inputSetScratch.Add(new SessionPlayerInput(input));
 55            }
 56
 62657            _inputSetScratch.Sort(static (left, right) => left.PlayerId.CompareTo(right.PlayerId));
 62558            return new SessionInputSet(tick, _inputSetScratch);
 59        }
 60
 61        public void Clear()
 62        {
 11163            _inputBuffers.Clear();
 11164            _inputSetScratch.Clear();
 11165        }
 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;
 76        private int _latestTick = int.MinValue;
 77
 78        public InputBuffer(int capacity)
 79        {
 80            _capacity = System.Math.Max(1, capacity);
 81            _inputs = new IPlayerInput[_capacity];
 82            _ticks = new int[_capacity];
 83
 84            for (int i = 0; i < _ticks.Length; i++)
 85            {
 86                _ticks[i] = int.MinValue;
 87            }
 88        }
 89
 90        public void SetInput(int tick, IPlayerInput input)
 91        {
 92            if (tick > _latestTick)
 93            {
 94                _latestTick = tick;
 95            }
 96
 97            if (tick < GetMinTick())
 98            {
 99                return;
 100            }
 101
 102            int index = ToIndex(tick);
 103            _inputs[index] = input;
 104            _ticks[index] = tick;
 105        }
 106
 107        public IPlayerInput? GetInput(int tick)
 108        {
 109            if (_latestTick == int.MinValue || tick < GetMinTick())
 110            {
 111                return null;
 112            }
 113
 114            int index = ToIndex(tick);
 115            return _ticks[index] == tick ? _inputs[index] : null;
 116        }
 117
 118        private int GetMinTick()
 119        {
 120            if (_latestTick == int.MinValue)
 121            {
 122                return int.MaxValue;
 123            }
 124
 125            return _latestTick - _capacity + 1;
 126        }
 127
 128        private int ToIndex(int tick)
 129        {
 130            int index = tick % _capacity;
 131            return index < 0 ? index + _capacity : index;
 132        }
 133    }
 134}