Skip to content

Commit

Permalink
sync current objective with new clients
Browse files Browse the repository at this point in the history
  • Loading branch information
sonodima committed Apr 7, 2024
1 parent 26780d8 commit 3c58a80
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
10 changes: 2 additions & 8 deletions src/LobbyHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@ namespace BetterLobby;

internal static class LobbyHelpers
{
private static readonly int s_dNumPlayers = 1;
private static readonly int s_dMaxPlayers = 4;

internal static bool IsMasterClient
=> MainMenuHandler.SteamLobbyHandler?.MasterClient ?? false;

internal static int? NumPlayers => PlayerHandler.instance?.players.Count;

internal static bool IsFull => (NumPlayers ?? s_dNumPlayers)
internal static bool IsFull => PhotonNetwork.CountOfPlayers
>= (MaxPlayers ?? s_dMaxPlayers);

internal static int? MaxPlayers =>
Expand Down Expand Up @@ -75,7 +69,7 @@ private static bool RunChecks(out CSteamID? id)
return false;
}

if (!IsMasterClient)
if (!PhotonNetwork.IsMasterClient)
{
Plugin.CurLogger?.LogWarning("You can't perform this operation because you are not the master client!");
return false;
Expand Down
11 changes: 7 additions & 4 deletions src/PauseMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ namespace BetterLobby;

internal sealed class PauseMenu : MonoBehaviour
{
private static GameObject UIButtonList =>
Resources.FindObjectsOfTypeAll<Transform>()
private static GameObject UIMenu
=> Resources.FindObjectsOfTypeAll<Transform>()
.FirstOrDefault(obj => obj.name == "EscapeMenu")?
.Find("MainPage/LIST")?.gameObject;
.Find("MainPage")?.gameObject;

internal static bool CreateButton(string name, string text, UnityAction action)
private static GameObject UIButtonList
=> UIMenu?.transform.Find("LIST")?.gameObject;

internal static bool AddMainButton(string name, string text, UnityAction action)
{
var uiButtonList = UIButtonList;
if (uiButtonList == null)
Expand Down
23 changes: 14 additions & 9 deletions src/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ namespace BetterLobby;
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
public sealed class Plugin : BaseUnityPlugin
{
internal static bool IsOnSurface
=> FindObjectOfType<DivingBell>()?.onSurface ?? false;

public static ManualLogSource CurLogger { get; private set; } = null;

private void Awake()
Expand All @@ -35,7 +32,7 @@ private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
// Only allow the plugin to run when the player is the master client
// and on the surface.
if (!IsOnSurface || !LobbyHelpers.IsMasterClient)
if (!PhotonGameLobbyHandler.IsSurface || !PhotonNetwork.IsMasterClient)
{
return;
}
Expand All @@ -49,7 +46,7 @@ private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
+= (player) => StartCoroutine(OnPlayerJoined(player));
}

PauseMenu.CreateButton("FILL", "FILL LOBBY", OnFillPress);
PauseMenu.AddMainButton("FILL", "FILL LOBBY", OnFillPress);
}

private IEnumerator OnPlayerJoined(Player player)
Expand All @@ -67,13 +64,21 @@ private IEnumerator OnPlayerJoined(Player player)
// If we execute the RPC immediatly, bad things happen :(
yield return new WaitForSeconds(2f);

// Send the current objective another time, so that the newly connected client
// is on par with the progress.
if (PhotonGameLobbyHandler.CurrentObjective != null)
{
PhotonGameLobbyHandler.Instance.SetCurrentObjective(
PhotonGameLobbyHandler.CurrentObjective);
}

// If the game has started, open the remote door for the player that just
// joined.
if (SurfaceNetworkHandler.HasStarted)
if (SurfaceNetworkHandler.Instance != null && SurfaceNetworkHandler.HasStarted)
{
Logger.LogInfo("Game has already started, sending RPCA_OpenDoor to the late-joiner...");
SurfaceNetworkHandler.Instance?.photonView?
.RPC("RPCA_OpenDoor", RpcTarget.All, []);
SurfaceNetworkHandler.Instance.photonView.RPC(
"RPCA_OpenDoor", RpcTarget.All, []);
}

yield break;
Expand All @@ -87,7 +92,7 @@ private void OnFillPress()
return;
}

if (!IsOnSurface)
if (!PhotonGameLobbyHandler.IsSurface)
{
Logger.LogWarning("You need to be on surface to fill the lobby!");
return;
Expand Down

0 comments on commit 3c58a80

Please sign in to comment.