Sync game state on connect (#86)

* Sync full state on init

* Sync game state on start
This commit is contained in:
Ricardo Lopes 2020-05-19 19:31:54 +02:00 committed by GitHub
parent f3ec54f75c
commit 83e8112c0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 86 additions and 3 deletions

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using QSB.Messaging;
using UnityEngine.Networking;
namespace QSB.Events
{
class FullStateMessage : NameMessage
{
public override MessageType MessageType => MessageType.FullState;
public Dictionary<uint, string> PlayerNames;
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
PlayerNames = new Dictionary<uint, string>();
reader.ReadString().Split(',').ToList().ForEach(pair =>
{
var splitPair = pair.Split(':');
var key = Convert.ToUInt16(splitPair[0]);
var value = splitPair[1];
PlayerNames[key] = value;
});
}
public override void Serialize(NetworkWriter writer)
{
base.Serialize(writer);
var playerNamePairs = PlayerNames.ToList().Select(pair => $"{pair.Key}:{pair.Value}").ToArray();
writer.Write(string.Join(",", playerNamePairs));
}
}
}

35
QSB/Events/GameState.cs Normal file
View File

@ -0,0 +1,35 @@
using System.Collections;
using System.Collections.Generic;
using QSB.Messaging;
using QSB.TransformSync;
using UnityEngine;
using UnityEngine.Networking;
namespace QSB.Events
{
class GameState : NetworkBehaviour
{
private MessageHandler<FullStateMessage> _messageHandler;
private void Awake()
{
_messageHandler = new MessageHandler<FullStateMessage>();
_messageHandler.OnClientReceiveMessage += OnClientReceiveMessage;
}
private void OnClientReceiveMessage(FullStateMessage message)
{
PlayerJoin.PlayerNames = message.PlayerNames;
}
public void Send()
{
var message = new FullStateMessage()
{
PlayerNames = PlayerJoin.PlayerNames
};
_messageHandler.SendToAll(message);
}
}
}

View File

@ -9,7 +9,7 @@ namespace QSB.Events
{
public class PlayerJoin : NetworkBehaviour
{
public static readonly Dictionary<uint, string> PlayerNames = new Dictionary<uint, string>();
public static Dictionary<uint, string> PlayerNames = new Dictionary<uint, string>();
public static string MyName { get; private set; }
private MessageHandler<JoinMessage> _joinHandler;

View File

@ -9,7 +9,8 @@ namespace QSB.Messaging
AnimTrigger = MsgType.Highest + 3,
Join = MsgType.Highest + 4,
Death = MsgType.Highest + 5,
Leave = MsgType.Highest + 6
Leave = MsgType.Highest + 6,
FullState = MsgType.Highest + 7
// Add other message types here, incrementing the value.
}
}

View File

@ -107,6 +107,8 @@
<Compile Include="Animation\AnimTriggerMessage.cs" />
<Compile Include="Animation\AnimTrigger.cs" />
<Compile Include="DebugActions.cs" />
<Compile Include="Events\FullStateMessage.cs" />
<Compile Include="Events\GameState.cs" />
<Compile Include="Events\Necronomicon.cs" />
<Compile Include="Events\JoinMessage.cs" />
<Compile Include="Events\LeaveMessage.cs" />

View File

@ -96,19 +96,26 @@ namespace QSB
base.OnServerAddPlayer(conn, playerControllerId);
NetworkServer.SpawnWithClientAuthority(Instantiate(_shipPrefab), conn);
var gameState = gameObject.AddComponent<GameState>();
gameState.Send();
}
public override void OnClientConnect(NetworkConnection conn)
{
base.OnClientConnect(conn);
DebugLog.Screen("OnClientConnect");
gameObject.AddComponent<SectorSync>();
gameObject.AddComponent<PlayerJoin>().Join(_playerName);
gameObject.AddComponent<PlayerLeave>();
gameObject.AddComponent<RespawnOnDeath>();
gameObject.AddComponent<PreventShipDestruction>();
if (!Network.isServer)
{
gameObject.AddComponent<GameState>();
}
_canEditName = false;
OnNetworkManagerReady.Invoke();