< Summary

Information
Class: Lattice.ECS.Session.InputBuffer
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Session/SessionInputStore.cs
Line coverage
92%
Covered lines: 23
Uncovered lines: 2
Coverable lines: 25
Total lines: 134
Line coverage: 92%
Branch coverage
81%
Covered branches: 13
Total branches: 16
Branch coverage: 81.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
SetInput(...)75%4487.5%
GetInput(...)100%66100%
GetMinTick()50%2266.66%
ToIndex(...)50%22100%

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    {
 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;
 3476        private int _latestTick = int.MinValue;
 77
 3478        public InputBuffer(int capacity)
 79        {
 3480            _capacity = System.Math.Max(1, capacity);
 3481            _inputs = new IPlayerInput[_capacity];
 3482            _ticks = new int[_capacity];
 83
 827284            for (int i = 0; i < _ticks.Length; i++)
 85            {
 410286                _ticks[i] = int.MinValue;
 87            }
 3488        }
 89
 90        public void SetInput(int tick, IPlayerInput input)
 91        {
 33592            if (tick > _latestTick)
 93            {
 31294                _latestTick = tick;
 95            }
 96
 33597            if (tick < GetMinTick())
 98            {
 099                return;
 100            }
 101
 335102            int index = ToIndex(tick);
 335103            _inputs[index] = input;
 335104            _ticks[index] = tick;
 335105        }
 106
 107        public IPlayerInput? GetInput(int tick)
 108        {
 462109            if (_latestTick == int.MinValue || tick < GetMinTick())
 110            {
 1111                return null;
 112            }
 113
 461114            int index = ToIndex(tick);
 461115            return _ticks[index] == tick ? _inputs[index] : null;
 116        }
 117
 118        private int GetMinTick()
 119        {
 797120            if (_latestTick == int.MinValue)
 121            {
 0122                return int.MaxValue;
 123            }
 124
 797125            return _latestTick - _capacity + 1;
 126        }
 127
 128        private int ToIndex(int tick)
 129        {
 796130            int index = tick % _capacity;
 796131            return index < 0 ? index + _capacity : index;
 132        }
 133    }
 134}