Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Fix player sync issue using lobby system #367 #369

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private void FactoryObjectCreated(NetworkObject obj)
private void SetupService(NetworkObject obj)
{
LobbyService.Instance.Initialize(obj);
LobbyService.Instance.SendSyncAllPlayers();
SetupComplete();
}

Expand Down Expand Up @@ -432,7 +433,7 @@ public void OnFNLobbyMasterKnowledgeTransfer(ILobbyMaster previousLobbyMaster)

private void SetupComplete()
{
LobbyService.Instance.SetLobbyMaster(this);
LobbyService.Instance.SetLobbyMaster(this);
LobbyService.Instance.Initialize(NetworkManager.Instance.Networker);

//If I am the host, then I should show the kick button for all players here
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using BeardedManStudios.Forge.Networking.Frame;
using BeardedManStudios.Forge.Networking.Unity;
using BeardedManStudios.Forge.Networking.Unity.Lobby;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace BeardedManStudios.Forge.Networking.Lobby
{
Expand All @@ -18,9 +21,10 @@ public class LobbyService : INetworkBehavior
public const byte RPC_PLAYER_JOINED = 8;
public const byte RPC_PLAYER_LEFT = 9;
public const byte RPC_PLAYER_SYNC = 10;
public const byte RPC_SYNC_ALL_PLAYERS = 11;

#region Private Data
private LobbyServiceNetworkObject networkObject = null;
#region Private Data
private LobbyServiceNetworkObject networkObject = null;
private bool _initialized;
#endregion

Expand Down Expand Up @@ -479,7 +483,8 @@ public void Initialize(NetworkObject obj)
networkObject.RegisterRpc("PlayerJoined", PlayerJoined, typeof(uint));
networkObject.RegisterRpc("PlayerLeft", PlayerLeft, typeof(uint));
networkObject.RegisterRpc("SyncPlayer", SyncPlayer, typeof(uint), typeof(string), typeof(int), typeof(int));
networkObject.RegistrationComplete();
networkObject.RegisterRpc("SyncAllPlayers", SyncAllPlayers);
networkObject.RegistrationComplete();
_initialized = true;

//Logging.BMSLog.Log("SERVICE ID: " + networkObject.NetworkId);
Expand Down Expand Up @@ -571,7 +576,7 @@ private void AssignAvatar(RpcArgs args)

player.AvatarID = avatarId;
MasterLobby.OnFNAvatarIDChanged(player);
}
}
/// <summary>
/// Arguments:
/// uint playerid
Expand Down Expand Up @@ -632,7 +637,32 @@ private void SyncPlayer(RpcArgs args)
MasterLobby.OnFNPlayerSync(player);
}

private IClientMockPlayer CreateClientMockPlayer(uint playerId, string playerName)
public void SendSyncAllPlayers()
{
networkObject.SendRpc(RPC_SYNC_ALL_PLAYERS, Receivers.All);
}

private void SyncAllPlayers(RpcArgs args)
{
if (networkObject.IsServer)
{
for (int i = 0; i < MasterLobby.LobbyPlayers.Count; i++)
{
networkObject.Networker.IteratePlayers((player) =>
{
IClientMockPlayer lobbyPlayer = MasterLobby.LobbyPlayers.First(x => x.NetworkId == player.NetworkId);

if (player == MasterLobby.LobbyPlayers[i])
return;

networkObject.SendRpc(networkObject.Networker.Players[i], RPC_PLAYER_JOINED, player.NetworkId);
networkObject.SendRpc(networkObject.Networker.Players[i], RPC_PLAYER_SYNC, player.NetworkId, lobbyPlayer.Name, lobbyPlayer.TeamID, lobbyPlayer.AvatarID);
});
}
}
}

private IClientMockPlayer CreateClientMockPlayer(uint playerId, string playerName)
{
var player = new DummyPlayer(playerId, playerName, 0, 0);
return player;
Expand Down