forked from Unity-Technologies/ECS-Network-Racing-Sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResetServerSystem.cs
More file actions
126 lines (111 loc) · 4.22 KB
/
Copy pathResetServerSystem.cs
File metadata and controls
126 lines (111 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using Unity.Entities.Racing.Common;
using Unity.Burst;
using Unity.NetCode;
using Unity.Scenes;
using UnityEngine;
using static Unity.Entities.SystemAPI;
namespace Unity.Entities.Racing.Gameplay
{
/// <summary>
/// Restarts the server when all players got disconnected.
/// </summary>
[BurstCompile]
public partial struct ResetServerSystem : ISystem
{
private EntityQuery m_query;
private EntityQuery m_networkStreamQuery;
private EntityQuery m_subSceneQuery;
public void OnCreate(ref SystemState state)
{
m_query = state.GetEntityQuery(ComponentType.ReadOnly<ResetServerOnDisconnect>());
m_subSceneQuery = state.GetEntityQuery(ComponentType.ReadOnly<SceneReference>());
m_networkStreamQuery = state.GetEntityQuery(ComponentType.ReadOnly<NetworkStreamConnection>());
state.RequireForUpdate(m_query);
}
public void OnDestroy(ref SystemState state) { }
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var connections = m_networkStreamQuery.CalculateEntityCount();
if (connections > 0)
return;
// All players disconnected
// We reset the race scene
Debug.Log("Resetting server");
var subScenes = m_subSceneQuery.ToEntityArray(state.WorldUpdateAllocator);
foreach (var subScene in subScenes)
{
SceneSystem.UnloadScene(state.WorldUnmanaged, subScene);
SceneSystem.LoadSceneAsync(state.WorldUnmanaged, subScene);
}
state.EntityManager.DestroyEntity(m_query);
}
}
[BurstCompile]
public partial struct ConnectionValidationSystem : ISystem, ISystemStartStop
{
private EntityQuery m_LocalUserQuery;
private double TimeOutCachedValue;
public void OnCreate(ref SystemState state)
{
TimeOutCachedValue = 0;
state.RequireForUpdate<TimeOutServer>();
m_LocalUserQuery = state.GetEntityQuery(ComponentType.ReadOnly<LocalUser>());
}
public void OnDestroy(ref SystemState state)
{
}
public void OnStartRunning(ref SystemState state)
{
ResetTimeout();
Debug.Log($"initializing connection at: {state.WorldUnmanaged.Time.ElapsedTime}");
}
private void ResetTimeout()
{
var timeout = GetSingleton<TimeOutServer>();
if (TimeOutCachedValue <= 0)
{
TimeOutCachedValue = timeout.Value;
}
else
{
timeout.Value = TimeOutCachedValue;
SetSingleton(timeout);
}
}
public void OnStopRunning(ref SystemState state)
{
LoadingScreen.Instance.ShowLoadingScreen(false);
Debug.Log($"connection completed at: {state.WorldUnmanaged.Time.ElapsedTime}");
}
public void OnUpdate(ref SystemState state)
{
if (!ServerConnectionUtils.IsTryingToConnect)
return;
LoadingScreen.Instance.ShowLoadingScreen(true, "CONNECTING...");
var timeout = GetSingleton<TimeOutServer>();
if (timeout.Value > 0)
{
timeout.Value -= state.WorldUnmanaged.Time.DeltaTime;
SetSingleton(timeout);
if (m_LocalUserQuery.CalculateEntityCount() > 0)
{
ServerConnectionUtils.IsTryingToConnect = false;
ResetTimeout();
state.Enabled = false;
}
}
else
{
ServerConnectionUtils.IsTryingToConnect = false;
ResetTimeout();
Popup.Instance.Show("Connection Error",
"The server could not be found.",
"Restart",
() => SceneLoader.Instance.LoadScene(SceneType.MainMenu));
Debug.Log($"Server connection timeout: {state.WorldUnmanaged.Time.ElapsedTime}");
state.Enabled = false;
}
}
}
}