Skip to content

Commit 68538b9

Browse files
authored
Re-add Stomped Connection Input Unit Test (#3543)
1 parent 3efe8ef commit 68538b9

2 files changed

Lines changed: 96 additions & 6 deletions

File tree

engine/Sandbox.Engine/Systems/Networking/System/Channel/Connection.Input.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ public void Clear()
113113
/// </summary>
114114
public bool Down( [InputAction] string action )
115115
{
116-
// If we're the host, just use our local input instead.
117-
if ( IsHost )
116+
// If this connection is us, just use our local input instead.
117+
if ( Local == this )
118118
return Sandbox.Input.Down( action );
119119

120120
if ( string.IsNullOrWhiteSpace( action ) )
@@ -133,8 +133,8 @@ public bool Down( [InputAction] string action )
133133
/// </summary>
134134
public bool Pressed( [InputAction] string action )
135135
{
136-
// If we're the host, just use our local input instead.
137-
if ( IsHost )
136+
// If this connection is us, just use our local input instead.
137+
if ( Local == this )
138138
return Sandbox.Input.Pressed( action );
139139

140140
if ( string.IsNullOrWhiteSpace( action ) )
@@ -155,8 +155,8 @@ public bool Pressed( [InputAction] string action )
155155
/// </summary>
156156
public bool Released( [InputAction] string action )
157157
{
158-
// If we're the host, just use our local input instead.
159-
if ( IsHost )
158+
// If this connection is us, just use our local input instead.
159+
if ( Local == this )
160160
return Sandbox.Input.Released( action );
161161

162162
if ( string.IsNullOrWhiteSpace( action ) )

engine/Sandbox.Test/Scene/GameObjects/Network.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,96 @@ public void TestCleanup()
3131
Game.TypeLibrary = _oldTypeLibrary;
3232
}
3333

34+
[TestMethod]
35+
public void NetworkedInput()
36+
{
37+
Assert.IsNotNull( TypeLibrary.GetType<ModelRenderer>(), "TypeLibrary hasn't been given the game assembly" );
38+
39+
using var scope = new Scene().Push();
40+
41+
var clientAndHost = new ClientAndHost( TypeLibrary );
42+
43+
// Become the client
44+
clientAndHost.BecomeClient();
45+
46+
var inputSettings = new InputSettings();
47+
inputSettings.InitDefault();
48+
49+
Input.InputSettings = inputSettings;
50+
Input.SetAction( "Jump", true );
51+
52+
// Send a client tick - this will build a user command as well
53+
Game.ActiveScene.SendClientTick( SceneNetworkSystem.Instance );
54+
55+
// Become the host
56+
clientAndHost.BecomeHost();
57+
58+
clientAndHost.Host.ProcessMessages( InternalMessageType.ClientTick, bs =>
59+
{
60+
Networking.System.OnReceiveClientTick( bs, clientAndHost.Client );
61+
} );
62+
63+
clientAndHost.Client.Messages.Clear();
64+
65+
Assert.AreEqual( true, clientAndHost.Client.Pressed( "Jump" ) );
66+
Assert.AreEqual( true, clientAndHost.Client.Down( "Jump" ) );
67+
68+
// Become the client
69+
clientAndHost.BecomeClient();
70+
71+
Input.ClearActions();
72+
73+
// Send a client tick - this will build a user command as well
74+
Game.ActiveScene.SendClientTick( SceneNetworkSystem.Instance );
75+
76+
// Become the host
77+
clientAndHost.BecomeHost();
78+
79+
clientAndHost.Host.ProcessMessages( InternalMessageType.ClientTick, bs =>
80+
{
81+
Networking.System.OnReceiveClientTick( bs, clientAndHost.Client );
82+
} );
83+
84+
clientAndHost.Host.Messages.Clear();
85+
86+
Assert.AreEqual( true, clientAndHost.Client.Released( "Jump" ) );
87+
Assert.AreEqual( false, clientAndHost.Client.Down( "Jump" ) );
88+
89+
// Let's test wrap-aware command number processing
90+
var userCommand = new UserCommand( uint.MaxValue );
91+
92+
clientAndHost.Client.Input.ApplyUserCommand( userCommand );
93+
94+
// Become the client
95+
clientAndHost.BecomeClient();
96+
97+
Input.SetAction( "Jump", true );
98+
99+
Assert.AreEqual( true, Connection.Local.Pressed( "Jump" ) );
100+
Assert.AreEqual( true, Connection.Local.Down( "Jump" ) );
101+
102+
// Send a client tick - this will build a user command as well
103+
Game.ActiveScene.SendClientTick( SceneNetworkSystem.Instance );
104+
105+
// Become the host
106+
clientAndHost.BecomeHost();
107+
108+
clientAndHost.Host.ProcessMessages( InternalMessageType.ClientTick, bs =>
109+
{
110+
Networking.System.OnReceiveClientTick( bs, clientAndHost.Client );
111+
} );
112+
113+
Assert.AreEqual( false, clientAndHost.Client.Pressed( "Forward" ) );
114+
Assert.AreEqual( true, clientAndHost.Client.Pressed( "Jump" ) );
115+
Assert.AreEqual( true, clientAndHost.Client.Down( "Jump" ) );
116+
117+
Input.ClearActions();
118+
Input.SetAction( "Forward", true );
119+
120+
Assert.AreEqual( true, Connection.Local.Pressed( "Forward" ) );
121+
Assert.AreEqual( true, Connection.Local.Down( "Forward" ) );
122+
}
123+
34124
[TestMethod]
35125
public void RegisterSyncProps()
36126
{

0 commit comments

Comments
 (0)