Skip to content

Commit

Permalink
start/end lobby game server signal
Browse files Browse the repository at this point in the history
  • Loading branch information
Zer0xTJ committed Jun 10, 2024
1 parent a6676dc commit 59aed6e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
6 changes: 6 additions & 0 deletions addons/mattoha_lobby_system/core/nodes/MattohaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ public partial class MattohaClient : Node
/// <param name="cause">The cause of the failure.</param>
[Signal] public delegate void SpawnNodeFailedEventHandler(string cause);

/// <summary>
/// Emitted when spawning lobby nodes finishes.
/// </summary>
[Signal] public delegate void SpawnLobbyNodesSucceedEventHandler();

/// <summary>
/// Emitted when spawning lobby nodes fails.
/// </summary>
Expand Down Expand Up @@ -713,6 +718,7 @@ private void RpcSpawnLobbyNodes(Dictionary<string, Variant> payload)
}
// why?: because to notify other players to replicate their data, and we are sure that all nodes have been spawned
SetPlayerData(new Dictionary<string, Variant> { { MattohaPlayerKeys.IsInGame, true } });
EmitSignal(SignalName.SpawnLobbyNodesSucceed);
#endif
}

Expand Down
17 changes: 16 additions & 1 deletion addons/mattoha_lobby_system/core/nodes/MattohaServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ public partial class MattohaServer : Node
/// </summary>
[Signal] public delegate void PlayerLeftLobbyEventHandler(long playerId, int lobbyId);


/// <summary>
/// Emmited when a lobby game started.
/// </summary>
[Signal] public delegate void LobbyGameStartedEventHandler(int lobbyId);


/// <summary>
/// Emmited when a lobby game ended.
/// </summary>
[Signal] public delegate void LobbyGameEndedEventHandler(int lobbyId);

/// <summary>
/// Server Middleware for executing logic before and after almost every event that client do,
/// This node should have "MattohaMiddleware" or an inherited class node script attached to it.
Expand All @@ -39,7 +51,7 @@ public partial class MattohaServer : Node
/// <summary>
/// A dictionary that contains spawned nodes for each lobby, where the key is the lobby id.
/// </summary>
public Dictionary<int, Array<Dictionary<string, Variant>>> SpawnedNodes { get; private set; } = new();
[Export] public Dictionary<int, Array<Dictionary<string, Variant>>> SpawnedNodes { get; private set; } = new();

/// <summary>
/// A dictionary that contains removed scene nodes that has been despawned during game play for each lobby,
Expand Down Expand Up @@ -508,6 +520,7 @@ private void RpcJoinLobby(Dictionary<string, Variant> payload, long sender)

RefreshAvailableLobbiesForAll();
SendRpcForPlayersInLobby(lobbyId, nameof(ClientRpc.NewPlayerJoined), player, true);
SendRpcForPlayersInLobby(lobbyId, nameof(ClientRpc.SetLobbyData), lobby, true);
EmitSignal(SignalName.PlayerJoinedLobby, sender, lobbyId);
#endif
}
Expand Down Expand Up @@ -536,6 +549,7 @@ private void RpcStartGame(long sender)

MiddlewareNode.AfterStartGame(lobby, sender);
lobby[MattohaLobbyKeys.IsGameStarted] = true;
EmitSignal(SignalName.LobbyGameStarted, lobby[MattohaLobbyKeys.Id].AsInt32());
SendRpcForPlayersInLobby(lobby[MattohaLobbyKeys.Id].AsInt32(), nameof(ClientRpc.StartGame), null);
RefreshAvailableLobbiesForAll();

Expand Down Expand Up @@ -566,6 +580,7 @@ private void RpcEndGame(long sender)

MiddlewareNode.AfterEndGame(lobby, sender);
lobby[MattohaLobbyKeys.IsGameStarted] = false;
EmitSignal(SignalName.LobbyGameEnded, lobby[MattohaLobbyKeys.Id].AsInt32());
SendRpcForPlayersInLobby(lobby[MattohaLobbyKeys.Id].AsInt32(), nameof(ClientRpc.EndGame), null);
RefreshAvailableLobbiesForAll();

Expand Down
18 changes: 9 additions & 9 deletions addons/mattoha_lobby_system/core/utils/MattohaLobbyKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ namespace Mattoha.Core.Utils;

public static class MattohaLobbyKeys
{
public static string Id { get; set; } = "Id";
public static string OwnerId { get; set; } = "OwnerId";
public static string Name { get; set; } = "Name";
public static string MaxPlayers { get; set; } = "MaxPlayers";
public static string PlayersCount { get; set; } = "PlayersCount";
public static string IsGameStarted { get; set; } = "IsGameStarted";
public static string PrivateProps { get; set; } = "PrivateProps";
public static string LobbySceneFile { get; set; } = "LobbySceneFile";
public static string Id { get; private set; } = "Id";
public static string OwnerId { get; private set; } = "OwnerId";
public static string Name { get; private set; } = "Name";
public static string MaxPlayers { get; private set; } = "MaxPlayers";
public static string PlayersCount { get; private set; } = "PlayersCount";
public static string IsGameStarted { get; private set; } = "IsGameStarted";
public static string PrivateProps { get; private set; } = "PrivateProps";
public static string LobbySceneFile { get; private set; } = "LobbySceneFile";

public static List<string> FreezedProperties = new() { Id, OwnerId, PlayersCount, IsGameStarted };
public static List<string> FreezedProperties { get; private set; } = new() { Id, OwnerId, PlayersCount, IsGameStarted };

}
16 changes: 8 additions & 8 deletions addons/mattoha_lobby_system/core/utils/MattohaPlayerKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ namespace Mattoha.Core.Utils;

static class MattohaPlayerKeys
{
public static string Id { get; set; } = "Id";
public static string Username { get; set; } = "Username";
public static string JoinedLobbyId { get; set; } = "JoinedLobbyId";
public static string TeamId { get; set; } = "TeamId";
public static string IsInGame { get; set; } = "IsInGame";
public static string PrivateProps { get; set; } = "PrivateProps";
public static string ChatProps { get; set; } = "ChatProps";
public static string Id { get; private set; } = "Id";
public static string Username { get; private set; } = "Username";
public static string JoinedLobbyId { get; private set; } = "JoinedLobbyId";
public static string TeamId { get; private set; } = "TeamId";
public static string IsInGame { get; private set; } = "IsInGame";
public static string PrivateProps { get; private set; } = "PrivateProps";
public static string ChatProps { get; private set; } = "ChatProps";

public static List<string> FreezedProperties = new() { Id, JoinedLobbyId };
public static List<string> FreezedProperties { get; private set; } = new() { Id, JoinedLobbyId };

}
18 changes: 9 additions & 9 deletions addons/mattoha_lobby_system/core/utils/MattohaSpawnKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

public class MattohaSpawnKeys
{
public static string SceneFile { get; set; } = "SceneFile";
public static string NodeName { get; set; } = "NodeName";
public static string ParentPath { get; set; } = "ParentPath";
public static string Position { get; set; } = "Position";
public static string Rotation { get; set; } = "Rotation";
public static string Owner { get; set; } = "Owner";
public static string TeamOnly { get; set; } = "TeamOnly";
public static string TeamId { get; set; } = "TeamId";
public static string AdditionalProps { get; set; } = "AdditionalProps";
public static string SceneFile { get; private set; } = "SceneFile";
public static string NodeName { get; private set; } = "NodeName";
public static string ParentPath { get; private set; } = "ParentPath";
public static string Position { get; private set; } = "Position";
public static string Rotation { get; private set; } = "Rotation";
public static string Owner { get; private set; } = "Owner";
public static string TeamOnly { get; private set; } = "TeamOnly";
public static string TeamId { get; private set; } = "TeamId";
public static string AdditionalProps { get; private set; } = "AdditionalProps";
}

0 comments on commit 59aed6e

Please sign in to comment.