Skip to content

Commit

Permalink
Merge pull request #133 from uzaiiirahmed/main
Browse files Browse the repository at this point in the history
More Playroom Commands Added
  • Loading branch information
momintlh authored Feb 17, 2025
2 parents a60701a + e0d9c94 commit 216e5ee
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 16 deletions.
202 changes: 188 additions & 14 deletions Assets/PlayroomKit/modules/Helpers/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,26 @@ private void RegisterCommands()
{
var commands = new List<CustomCommand>
{
new() { Command = "InsertCoin", Description = "Starts multiplayer", Callback = InsertCoinCommand },
new() { Command = "SetState", Description = "Sets State", Callback = SetStateCommand },
new() { Command = "GetState", Description = "Gets State", Callback = GetStateCommand },
new() { Command = "GetRoomCode", Description = "Gets Room Code", Callback = GetRoomCodeCommand },
new()
{
Command = "InsertCoin",
Description =
"Starts multiplayer -skipLobby (true/false) -roomCode (roomCode for Current room(string)) -maxPlayers (Max number of players per room (int) -discord (discord mode (true/false)) -gameId (game id from dev portal(string)) -streamMode(true/false) -allowGamepads (allows gamepads (true/false)) -baseUrl (provides base URL (string) -avatars (Array of string(image.png) -reconnectGracePeriod (int) -matchmaking (matchmaking field) ) ",
Callback = InsertCoinCommand
},
new()
{
Command = "SetState",
Description =
"Sets State -key(Maps a value to key (string) -value (value assigned to a key (of type T)) -reliable (value assigned to a key (of type T)) ",
Callback = SetStateCommand
},
new()
{
Command = "GetState", Description = "Gets State -key (Gets the value using a key (string))",
Callback = GetStateCommand
},
new() { Command = "GetRoomCode", Description = "Gets Room Code - ", Callback = GetRoomCodeCommand },
new()
{
Command = "StartMatchMaking", Description = "Starts matchmaking", Callback = StartMatchMakingCommand
Expand All @@ -61,13 +77,69 @@ private void RegisterCommands()
},
new()
{
Command = "WaitForState", Description = "Waits for state to finish", Callback = WaitForStateCommand
Command = "WaitForState", Description = "Waits for state to finish -statekey(string)",
Callback = WaitForStateCommand
},
new()
{
Command = "OnDisconnect", Description = "Callback on player disconnect", Callback = OnDisconnectCommand
},
new() { Command = "ResetState", Description = "Reset state to default", Callback = ResetStateCommand }
new()
{
Command = "ResetState", Description = "Reset state to default -keysToExclude (string)",
Callback = ResetStateCommand
},

new()
{
Command = "Player.GetProfile", Description = "Returns player profile",
Callback = PlayerGetProfileCommand
},

new()
{
Command = "Player.GetState", Description = "Returns Playrooms Player State -key(string)",
Callback = PlayerGetStateCommand
},

new()
{
Command = "Player.SetState",
Description = "Sets Playrooms Player State -key(string) -value(string) -reliable(true/false)",
Callback = PlayerSetStateCommand
},

new()
{
Command = "Player.WaitForState ",
Description = "Waits for state of Playrooms Player -stateKey(string) ",
Callback = PlayerWaitForStateCommand
},

new()
{
Command = "Player.Kick", Description = "Kicks PLayer -playerId(string)", Callback = KickPlayerCommand
},
new()
{
Command = "Player.OnQuit", Description = "Registers a Call back when a player quits the room",
Callback = PlayerOnQuitCommand
},
new()
{
Command = "RpcRegister", Description = "Registers a Remote Procedural Call -name(string)",
Callback = RpcRegisterCommand
},
new()
{
Command = "RpcCall", Description = "Calls a Remote Procedural Call -name(string) -data(string) ",
Callback = RpcCallCommand
},
new()
{
Command = "ResetPlayerState", Description = "Resets MyPlayer states -excludeKeys(string) ",
Callback = ResetPlayerStatesCommand
}
};

foreach (var command in commands)
Expand All @@ -90,8 +162,10 @@ private void OnPlayerJoinCommand(CommandCallback cmd)
private void ResetStateCommand(CommandCallback cmd)
{
LogCommand("ResetState");
string[] keyToExclude = cmd.Args["-keyToExclude"].ToString().Split(',');
_prk.ResetStates(keyToExclude);
string[] keysToExclude = cmd.Args.ContainsKey("-keysToExclude")
? cmd.Args["-keysToExclude"].Split(',')
: new string[] { "defaultKey" };
_prk.ResetStates(keysToExclude);
}

private void OnDisconnectCommand(CommandCallback cmd)
Expand All @@ -113,7 +187,7 @@ private void SetStateCommand(CommandCallback cmd)

string key = Convert.ToString(cmd.Args["-key"]);
string value = Convert.ToString(cmd.Args["-value"]);
bool reliable = Convert.ToBoolean(cmd.Args["-reliable"]);
bool reliable = Convert.ToBoolean(cmd.Args["-reliable"]);

_prk.SetState(key, value, reliable);
}
Expand Down Expand Up @@ -156,16 +230,112 @@ private void IsStreamScreenCommand(CommandCallback cmd)
private void MyPlayerCommand(CommandCallback cmd)
{
LogCommand("MyPlayer");
PowerConsole.Log(LogLevel.Information, $"My Player's Name: {_prk.MyPlayer().GetProfile().name}");
PowerConsole.Log(LogLevel.Information,
$"Player Id {_prk.MyPlayer().id}, My Player's Name: {_prk.MyPlayer().GetProfile().name}");
}

private void MeCommand(CommandCallback cmd)
{
LogCommand("Me");
MyPlayerCommand(cmd);
PowerConsole.Log(LogLevel.Information, $"My Player's Name: {_prk.MyPlayer().GetProfile().name}");
PowerConsole.Log(LogLevel.Information, $"My Player's Name: {_prk.Me().GetProfile().name}");
}

private void PlayerGetProfileCommand(CommandCallback cmd)
{
PowerConsole.Log(LogLevel.Information, $"Name is : {_prk.MyPlayer().GetProfile().name}");
PowerConsole.Log(LogLevel.Information, $"Color is :{_prk.MyPlayer().GetProfile().color}");
}

private void RpcRegisterCommand(CommandCallback cmd)
{
LogCommand("RpcRegister");
string name = Convert.ToString(cmd.Args["-name"]);
_prk.RpcRegister(name, (data, sender) =>
PowerConsole.Log(LogLevel.Information, $"Received RPC from {sender}: {data}"));
}

private void RpcCallCommand(CommandCallback cmd)
{
LogCommand("RpcCall");

string name = Convert.ToString(cmd.Args["-name"]);
string data = Convert.ToString(cmd.Args["-data"]);

_prk.RpcCall(name, data, () => PowerConsole.Log(LogLevel.Information, $"RPC Call '{name}' completed."));
}


private void PlayerGetStateCommand(CommandCallback cmd)
{
LogCommand("Player.GetState");
string key = Convert.ToString(cmd.Args["-key"]);
string value = _prk.MyPlayer().GetState<string>(key);
PowerConsole.Log(LogLevel.Information,
!string.IsNullOrEmpty(value)
? $"State value for key '{key}': {value}"
: $"No value found for key '{key}'.");
}

private void PlayerSetStateCommand(CommandCallback cmd)
{
LogCommand("Player.SetState");

string key = cmd.Args.ContainsKey("-key") ? Convert.ToString(cmd.Args["-key"]) : "defaultKey";
string value = cmd.Args.ContainsKey("-value") ? Convert.ToString(cmd.Args["-value"]) : "defaultValue";
bool reliable = cmd.Args.ContainsKey("-reliable") ? Convert.ToBoolean(cmd.Args["-reliable"]) : false;

_prk.MyPlayer().SetState(key, value, reliable);
}

private void PlayerWaitForStateCommand(CommandCallback cmd)
{
LogCommand("Player.WaitForState");
string key = Convert.ToString(cmd.Args["-stateKey"]);
_prk.MyPlayer().WaitForState(key, data => PowerConsole.Log(LogLevel.Information, data.ToString()));
}

private void KickPlayerCommand(CommandCallback cmd)
{
LogCommand("Player.Kick");

string playerId = cmd.Args.ContainsKey("-playerId") ? cmd.Args["-playerId"] : _prk.MyPlayer().id;

var playerToKick = _prk.GetPlayer(playerId);
if (playerToKick == null)
{
PowerConsole.Log(LogLevel.Warning, $"Player with ID '{playerId}' not found.");
return;
}

playerToKick.Kick();

PowerConsole.Log(LogLevel.Information, $"{playerToKick.GetProfile().name} has been kicked.");
}

private void PlayerOnQuitCommand(CommandCallback cmd)
{
LogCommand("Player.OnQuit");

_prk.MyPlayer().OnQuit(_ =>
{
string playerName = _prk.MyPlayer().GetProfile().name;
PowerConsole.Log(LogLevel.Information, $"{playerName} has quit the match.");
});
}

private void ResetPlayerStatesCommand(CommandCallback cmd)
{
LogCommand("ResetPlayerState");

string[] keysToExclude = cmd.Args.ContainsKey("-excludeKeys")
? Convert.ToString(cmd.Args["-excludeKeys"]).Split(',')
: new string[] { "defaultKey" };

_prk.ResetPlayersStates(keysToExclude,
() => { PowerConsole.Log(LogLevel.Information, "Player states have been successfully reset."); });
}


private void InsertCoinCommand(CommandCallback cmd)
{
LogCommand("InsertCoin");
Expand All @@ -176,8 +346,12 @@ private void InsertCoinCommand(CommandCallback cmd)
bool allowGamepads = cmd.Args.ContainsKey("-allowGamepads") && Convert.ToBoolean(cmd.Args["-allowGamepads"]);
bool matchmaking = cmd.Args.ContainsKey("-matchmaking") && Convert.ToBoolean(cmd.Args["-matchmaking"]);
string baseUrl = cmd.Args.ContainsKey("-baseUrl") ? Convert.ToString(cmd.Args["-baseUrl"]) : "";
int reconnectGracePeriod = cmd.Args.ContainsKey("-reconnectGracePeriod") ? Convert.ToInt32(cmd.Args["-reconnectGracePeriod"]) : 0;
int? maxPlayersPerRoom = cmd.Args.ContainsKey("-maxPlayersPerRoom") ? Convert.ToInt32(cmd.Args["-maxPlayersPerRoom"]) : null;
int reconnectGracePeriod = cmd.Args.ContainsKey("-reconnectGracePeriod")
? Convert.ToInt32(cmd.Args["-reconnectGracePeriod"])
: 0;
int? maxPlayersPerRoom = cmd.Args.ContainsKey("-maxPlayersPerRoom")
? Convert.ToInt32(cmd.Args["-maxPlayersPerRoom"])
: null;
string[] avatars = cmd.Args.ContainsKey("-avatars") ? cmd.Args["-avatars"].ToString().Split(',') : null;
string roomCode = cmd.Args.ContainsKey("-roomCode") ? cmd.Args["-roomCode"] : "";
string gameId = cmd.Args.ContainsKey("-gameId") ? cmd.Args["-gameId"] : "";
Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/EditorBuildSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ EditorBuildSettings:
- enabled: 0
path: Assets/PlayroomKit/Examples/2d-platformer/2d-platformer.unity
guid: cf82eec4c654cf44b89cec2a91afebd5
- enabled: 0
- enabled: 1
path: Assets/PlayroomKit/Examples/discord-activity/discord-activity.unity
guid: bb7ef8e4a67f3fb46b78e5d95a6858a7
- enabled: 0
path: Assets/PlayroomKit/Examples/package-showcase/showcase.unity
guid: 85c9af77ae006408ab24500b953a9035
- enabled: 1
- enabled: 0
path: Assets/Scenes/TurnBased.unity
guid: 490247da3cf48784ab657a183ba059d1
m_configObjects: {}

0 comments on commit 216e5ee

Please sign in to comment.