This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from proepkes/develop
Develop
- Loading branch information
Showing
70 changed files
with
1,450 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,68 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Lockstep.Core.Data; | ||
using Lockstep.Core.Interfaces; | ||
|
||
namespace Lockstep.Client.Implementations | ||
{ | ||
public class CommandBuffer : ICommandBuffer | ||
{ | ||
private readonly Dictionary<long, List<ICommand>> _commands = new Dictionary<long, List<ICommand>>(); | ||
/// <summary> | ||
/// Mapping: FrameNumber -> Commands per player(Id) | ||
/// </summary> | ||
public Dictionary<uint, Dictionary<byte, List<ICommand>>> Buffer { get; } = new Dictionary<uint, Dictionary<byte, List<ICommand>>>(5000); | ||
|
||
public event Action<long, ICommand> Inserted; | ||
public uint LastInsertedFrame { get; private set; } | ||
|
||
public long Count | ||
public virtual void Insert(uint frameNumber, byte commanderId, ICommand[] commands) | ||
{ | ||
get | ||
{ | ||
lock (_commands) | ||
lock (Buffer) | ||
{ | ||
if (!Buffer.ContainsKey(frameNumber)) | ||
{ | ||
return _commands.LongCount(); | ||
Buffer.Add(frameNumber, new Dictionary<byte, List<ICommand>>(10)); //Initial size for 10 players | ||
} | ||
} | ||
} | ||
|
||
public long ItemIndex { get; private set; } | ||
if (!Buffer[frameNumber].ContainsKey(commanderId)) | ||
{ | ||
Buffer[frameNumber].Add(commanderId, new List<ICommand>(5)); //Initial size of 5 commands per frame | ||
} | ||
|
||
//TODO: order by timestamp in case of multiple commands in the same frame => if commands intersect, the first one should win, requires !serverside! timestamp | ||
//ordering is enough, validation should take place in the simulation(core) | ||
Buffer[frameNumber][commanderId].AddRange(commands); | ||
|
||
public long Remaining => Count - ItemIndex; | ||
LastInsertedFrame = frameNumber; | ||
} | ||
} | ||
|
||
public virtual void Insert(long frameNumber, ICommand command) | ||
public Dictionary<byte, List<ICommand>> Get(uint frame) | ||
{ | ||
lock (_commands) | ||
lock (Buffer) | ||
{ | ||
if (!_commands.ContainsKey(frameNumber)) | ||
//If no commands were inserted then return an empty list | ||
if (!Buffer.ContainsKey(frame)) | ||
{ | ||
_commands.Add(frameNumber, new List<ICommand>(10)); | ||
Buffer.Add(frame, new Dictionary<byte, List<ICommand>>()); | ||
} | ||
|
||
_commands[frameNumber].Add(command); | ||
|
||
Inserted?.Invoke(frameNumber, command); | ||
} | ||
return Buffer[frame]; | ||
} | ||
} | ||
|
||
public ICommand[] GetNext() | ||
{ | ||
lock (_commands) | ||
public ICommand[] GetMany(uint frame) | ||
{ | ||
lock (Buffer) | ||
{ | ||
//If no commands were inserted then return an empty list | ||
if (!_commands.ContainsKey(ItemIndex)) | ||
if (!Buffer.ContainsKey(frame)) | ||
{ | ||
_commands[ItemIndex] = new List<ICommand>(); | ||
Buffer.Add(frame, new Dictionary<byte, List<ICommand>>()); | ||
} | ||
|
||
return _commands[ItemIndex++].ToArray(); | ||
|
||
} | ||
return Buffer[frame].SelectMany(pair => pair.Value).ToArray(); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.