Created
October 28, 2019 02:29
-
-
Save theWill/79ce4c48e70fadc274eac97ce44f918b to your computer and use it in GitHub Desktop.
Forge Remastered custom disconnect detection manager
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
using BeardedManStudios.Forge.Networking; | |
using BeardedManStudios.Forge.Networking.Generated; | |
using BeardedManStudios.Forge.Networking.Unity; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class ConnectionManager : ConnectionStatusBehavior | |
{ | |
public uint ownerId; //for inspector visibility | |
[Header("Cooldowns")] | |
public PlayerController.ConnectionStatus fromServerStatus; | |
public float fromServerStatusCooldown; | |
public PlayerController.ConnectionStatus fromClientStatus; | |
public float fromClientStatusCooldown; | |
private float slowUpdateCooldown; | |
public void Start() | |
{ | |
fromServerStatusCooldown = 10.0f; | |
fromClientStatusCooldown = 10.0f; | |
} | |
protected override void NetworkStart() | |
{ | |
base.NetworkStart(); | |
ownerId = networkObject.Owner.NetworkId; | |
fromServerStatusCooldown = 10.0f; | |
fromClientStatusCooldown = 10.0f; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
if (NetworkManager.Instance == null) | |
return; | |
if (MatchSetupCommon.instance == null || MatchSetupCommon.instance.MAIN_PLAYER == null) | |
return; | |
if (!NetworkManager.Instance.IsServer && !networkObject.IsOwner) | |
return; | |
if (LauncherController.instance.isShowingAd == true) | |
return; | |
//update cooldowns | |
slowUpdateCooldown -= Time.deltaTime; | |
if (NetworkManager.Instance.IsServer) | |
fromClientStatusCooldown -= Time.deltaTime; | |
if (networkObject.IsOwner) | |
fromServerStatusCooldown -= Time.deltaTime; | |
slowUpdate(); | |
} | |
public void slowUpdate() | |
{ | |
if (slowUpdateCooldown < 0) | |
{ | |
slowUpdateCooldown = 1.0f + Random.Range(0.0f, 0.5f); | |
try | |
{ | |
//server | |
if (NetworkManager.Instance.IsServer) | |
{ | |
networkObject.SendRpc(RPC_PING_CLIENTS, Receivers.Owner); | |
fromClientStatus = getConnectionStatus(fromClientStatusCooldown); | |
if (fromClientStatus == PlayerController.ConnectionStatus.disconnected) | |
{ | |
MatchCleanup.kickPlayer(networkObject.Owner, NetworkManager.Instance.Networker); | |
} | |
} | |
//client | |
if (networkObject.IsOwner) | |
{ | |
networkObject.SendRpc(RPC_PING_SERVER, Receivers.Server); | |
fromServerStatus = getConnectionStatus(fromServerStatusCooldown); | |
setMainPlayerConnectionStatus(fromServerStatus); | |
if (fromServerStatus == PlayerController.ConnectionStatus.disconnected) | |
{ | |
setMainPlayerConnectionStatus(PlayerController.ConnectionStatus.disconnected); | |
LauncherController.instance.multiplayerDisconnect = true; | |
} | |
} | |
} catch(System.Exception e) | |
{ | |
Debug.Log("Exception " + e); | |
} | |
} | |
} | |
private PlayerController.ConnectionStatus getConnectionStatus(float cooldown) | |
{ | |
if (cooldown < 0) | |
return PlayerController.ConnectionStatus.disconnected; | |
if (cooldown < 6.5) | |
return PlayerController.ConnectionStatus.warning; | |
return PlayerController.ConnectionStatus.normal; | |
} | |
//clients hear that the Server is still up. | |
public override void pingClients(RpcArgs args) | |
{ | |
fromServerStatusCooldown = 10.0f; | |
setMainPlayerConnectionStatus(PlayerController.ConnectionStatus.normal); | |
} | |
//Server hears that a client is still up | |
//TODO | |
public override void pingServer(RpcArgs args) | |
{ | |
fromClientStatusCooldown = 10.0f; | |
} | |
private void setMainPlayerConnectionStatus(PlayerController.ConnectionStatus newStatus) | |
{ | |
if (MatchSetupCommon.instance == null || MatchSetupCommon.instance.MAIN_PLAYER == null) | |
return; | |
MatchSetupCommon.instance.MAIN_PLAYER.connectionStatus = newStatus; | |
//Debug.Log("setMainPlayerConnectionStatus to " + newStatus.ToString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment