Skip to content

Commit a3a1d7f

Browse files
committed
feat: leave room added
1 parent c53834a commit a3a1d7f

8 files changed

Lines changed: 81 additions & 23 deletions

File tree

Assets/PlayroomKit/Examples/2d-platformer/scripts/GameManager2d.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ private void Initialize()
6464
}, () =>
6565
{
6666
_playroomKit.OnPlayerJoin(AddPlayer);
67-
print($"[Unity Log] isHost: {_playroomKit.IsHost()}");
67+
Debug.Log($"[Unity Log] isHost: {_playroomKit.IsHost()}");
68+
}, () =>
69+
{
70+
Debug.LogWarning("OnDisconnect Callback Called");
6871
});
6972
}
7073

Assets/PlayroomKit/modules/Headers.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ private static extern void SetPlayerStateStringById(string playerID, string key,
133133
[DllImport("__Internal")]
134134
private static extern string GetProfileByPlayerId(string playerID);
135135

136+
[DllImport("__Internal")]
137+
private static extern void LeaveRoomInternal(Action callback = null);
138+
136139
#region Persistence
137140

138141
[DllImport("__Internal")]

Assets/PlayroomKit/modules/Interfaces/IPlayerBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ public interface IPlayerBase
1818
public Profile GetProfile();
1919
public Action OnQuit(Action<string> callback);
2020
public void Kick(Action onKickCallBack = null);
21+
public void LeaveRoom(Action onLeaveCallback = null);
2122

2223
public void WaitForState(string stateKey, Action<string> onStateSetCallback = null);
2324

2425
protected static Action onKickCallBack = null;
26+
protected static Action onLeaveCallback = null;
2527
}
2628
}
2729
}

Assets/PlayroomKit/modules/MockMode/BrowserMode/BrowserMockPlayerService.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void SetState(string key, object value, bool reliable = false)
6767
_ubb.CallJs("SetPlayerStateByPlayerId", null, null, false, _id, key, json,
6868
reliable.ToString().ToLower());
6969
}
70-
70+
7171
public T GetState<T>(string key)
7272
{
7373
string rawValue = _ubb.CallJs<string>("GetPlayerStateByPlayerId", null, null, false, _id, key);
@@ -81,7 +81,7 @@ public T GetState<T>(string key)
8181
try
8282
{
8383
var jsonNode = JSON.Parse(rawValue);
84-
84+
8585
if (typeof(T) == typeof(string))
8686
{
8787
return (T)(object)jsonNode.Value;
@@ -108,7 +108,7 @@ public T GetState<T>(string key)
108108
catch (ArgumentException)
109109
{
110110
Debug.LogError($"Failed to parse '{rawValue}' to Enum of type {typeof(T)}");
111-
return default;
111+
return default;
112112
}
113113
}
114114

@@ -119,8 +119,8 @@ public T GetState<T>(string key)
119119
Debug.LogError($"Failed to parse state for key '{key}': {e.Message}\nReceived value: {rawValue}");
120120
return default;
121121
}
122-
123-
122+
123+
124124
}
125125

126126
#endregion
@@ -140,6 +140,11 @@ public void Kick(Action onKickCallBack = null)
140140
_ubb.CallJs("Kick", null, null, true, _id);
141141
}
142142

143+
public void LeaveRoom(Action onLeave = null)
144+
{
145+
DebugLogger.LogWarning("[MockMode] leaveRoom doesn't work in mock mode, build test required");
146+
}
147+
143148
public void WaitForState(string stateKey, Action<string> onStateSetCallback = null)
144149
{
145150
string callbackKey = $"WaitForState_{stateKey}";
@@ -152,7 +157,7 @@ public void WaitForState(string stateKey, Action<string> onStateSetCallback = nu
152157

153158
_ubb.CallJs("WaitForPlayerState", null, null, true, _id, stateKey, callbackKey);
154159
}
155-
160+
156161
private List<Action<string>> OnQuitCallbacks = new();
157162

158163
public Action OnQuit(Action<string> callback)
@@ -166,14 +171,14 @@ void Unsubscribe()
166171

167172
return Unsubscribe;
168173
}
169-
174+
170175
public void OnQuitWrapperCallback(string id)
171176
{
172177
if (OnQuitCallbacks != null)
173178
foreach (var callback in OnQuitCallbacks)
174179
callback?.Invoke(id);
175180
}
176-
181+
177182
internal void InvokePlayerOnQuitCallback(string id)
178183
{
179184
OnQuitWrapperCallback(id);

Assets/PlayroomKit/modules/Player/LocalPlayerService.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,27 @@ public void SetState(string key, object value, bool reliable = false)
6161
{
6262
SetStateHelper(key, value, reliable);
6363
}
64-
64+
6565
public void SetState(string key, Enum value, bool reliable = false)
6666
{
6767
SetStateHelper(key, value.ToString(), reliable);
6868
}
69-
70-
69+
70+
7171

7272
public T GetState<T>(string key)
7373
{
7474
if (mockPlayerStatesDictionary.TryGetValue(key, out var value) && value is T typedValue)
7575
{
76-
try
77-
{
78-
return (T)Convert.ChangeType(typedValue, typeof(T));
79-
}
80-
catch (InvalidCastException)
81-
{
82-
Debug.LogWarning($"Failed to convert the value of key '{key}' to type {typeof(T)}.");
83-
return default;
84-
}
76+
try
77+
{
78+
return (T)Convert.ChangeType(typedValue, typeof(T));
79+
}
80+
catch (InvalidCastException)
81+
{
82+
Debug.LogWarning($"Failed to convert the value of key '{key}' to type {typeof(T)}.");
83+
return default;
84+
}
8585
}
8686
else
8787
{
@@ -123,6 +123,11 @@ public void Kick(Action onKickCallBack = null)
123123
IPlayerBase.onKickCallBack?.Invoke();
124124
}
125125

126+
public void LeaveRoom(Action onLeave = null)
127+
{
128+
DebugLogger.LogWarning("[MockMode] leaveRoom doesn't work in mock mode, build test required");
129+
}
130+
126131
public void WaitForState(string stateKey, Action<string> onStateSetCallback = null)
127132
{
128133
if (onStateSetCallback == null)

Assets/PlayroomKit/modules/Player/Player.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ public void Kick(Action OnKickCallBack = null)
126126
_playerService.Kick(OnKickCallBack);
127127
}
128128

129+
public void LeaveRoom(Action onLeaveCallback = null)
130+
{
131+
if (!IsPlayRoomInitialized)
132+
{
133+
Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin.");
134+
return;
135+
}
136+
137+
_playerService.LeaveRoom(onLeaveCallback);
138+
}
139+
129140
public void WaitForState(string StateKey, Action<string> onStateSetCallback = null)
130141
{
131142
if (!IsPlayRoomInitialized)
@@ -160,7 +171,7 @@ public class Profile
160171
{
161172
[NonSerialized]
162173
public UnityEngine.Color color;
163-
174+
164175
public PlayerProfileColor playerProfileColor;
165176
public string name;
166177
public string photo;

Assets/PlayroomKit/modules/Player/PlayerService.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,17 @@ public void Kick(Action onKickCallBack = null)
186186
_interop.KickPlayerWrapper(_id, InvokeKickCallBack);
187187
}
188188

189+
public void LeaveRoom(Action onLeave = null)
190+
{
191+
IPlayerBase.onLeaveCallback = onLeave;
192+
LeaveRoomInternal(InvokeLeaveRoom);
193+
}
194+
195+
[MonoPInvokeCallback(typeof(Action))]
196+
private static void InvokeLeaveRoom()
197+
{
198+
IPlayerBase.onKickCallBack?.Invoke();
199+
}
189200

190201
private static Action<string> onSetState;
191202

Assets/PlayroomKit/src/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ mergeInto(LibraryManager.library, {
121121
return;
122122
}
123123

124-
var myPlayerID = Playroom.myPlayer().id;
124+
const player = Playroom.myPlayer()
125+
if (!player || player.id == null) return;
126+
127+
var myPlayerID = player.id;
125128
var bufferSize = lengthBytesUTF8(myPlayerID) + 1;
126129
var buffer = _malloc(bufferSize);
127130
stringToUTF8(myPlayerID, buffer, bufferSize);
@@ -820,6 +823,21 @@ mergeInto(LibraryManager.library, {
820823
});
821824
},
822825

826+
LeaveRoomInternal: function(callback) {
827+
if (!window.Playroom) {
828+
console.error(
829+
"Playroom library is not loaded. Please make sure to call InsertCoin first."
830+
);
831+
return;
832+
}
833+
834+
Playroom.myPlayer().leaveRoom().then(() => {
835+
{{{ makeDynCall('v', 'callback') }}};
836+
}).catch((error) => {
837+
console.error("[JS]: Error calling leaveRoom: ", error);
838+
});
839+
},
840+
823841
ResetStatesInternal: function (keysToExclude, onStatesReset) {
824842
if (!window.Playroom) {
825843
console.error(

0 commit comments

Comments
 (0)