using UnityEngine; using System; using System.Collections.Generic; using LitJson; using Sfs2X.Core; using Sfs2X.Entities; using Sfs2X.Requests; using Sfs2X.Entities.Data; public class PlazaRoomExtension { #region Variable public short PlazaRoomCapacity = 50; public string BasePlazaRoomName = "PlazaRoom"; public string PlazaRoomGroupID = "default"; public Action onExitPlazaRoom; public Action onJoinPlazaRoom; public Action onUserExitCurrentPlazaRoom; public Action onUserEnterCurrentPlazaRoom; public Action onReceiveExpression; public Action onReceivePublicMessage; public Action onSynchronize; public Action onInstantiate; public BaseSFSExtension BaseSFSExtension; public Room CurrentPlazaRoom; #endregion public PlazaRoomExtension(BaseSFSExtension baseSFSExtension) { baseSFSExtension.Instances.Add(this); BaseSFSExtension = baseSFSExtension; } public void ExitPlazaRoom() { BaseSFSExtension.GardenSmartFox.SmartFox.Send(new LeaveRoomRequest(CurrentPlazaRoom)); if (onExitPlazaRoom != null) onExitPlazaRoom.Invoke(); } public void JoinPlazaRoom() { List rooms = BaseSFSExtension.GardenSmartFox.SmartFox.GetRoomListFromGroup(PlazaRoomGroupID); foreach (var room in rooms) { if (room.UserCount < room.Capacity) { BaseSFSExtension.GardenSmartFox.SmartFox.Send(new JoinRoomRequest(room)); return; } } CreateAndJoinPlazaRoom(rooms.Count); } private void CreateAndJoinPlazaRoom(int index) { string roomName = BasePlazaRoomName + index; RoomSettings roomSettings = new RoomSettings(roomName); roomSettings.GroupId = PlazaRoomGroupID; roomSettings.MaxUsers = PlazaRoomCapacity; BaseSFSExtension.GardenSmartFox.SmartFox.Send(new CreateRoomRequest(roomSettings, true)); } public void SendSynchronizeDestination(Vector3 destination) { SFSObject sfsObject = new SFSObject(); sfsObject.PutInt(DataID.SenderID.GetHashString(), BaseSFSExtension.GardenSmartFox.User.Id); sfsObject.PutUtfString(DataID.Destination.GetHashString(), destination.VectorToString()); SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.Synchronize.GetHashCode(), sfsObject, -1, BaseSFSExtension.GardenSmartFox.SFSPlazaRoomManager.CurrentPlazaRoom.Id); BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.BatchAndOverride, RequestName.SynchronizePosition.ToString(), BatchOption.TryNot); } public void SendSynchronizeClose(List closeIDs) { SFSObject sfsObject = new SFSObject(); sfsObject.PutInt(DataID.SenderID.GetHashString(), BaseSFSExtension.GardenSmartFox.User.Id); sfsObject.PutIntArray(DataID.Close.GetHashString(), closeIDs.ToArray()); SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.Synchronize.GetHashCode(), sfsObject, -1, BaseSFSExtension.GardenSmartFox.SFSPlazaRoomManager.CurrentPlazaRoom.Id); BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.BatchAndOverride, RequestName.SynchronizeClose.ToString()); } public void SendExpression(ExpressionID expressionID) { SFSObject sfsObject = new SFSObject(); sfsObject.PutInt(DataID.SenderID.GetHashString(), BaseSFSExtension.GardenSmartFox.User.Id); sfsObject.PutInt(DataID.Expression.GetHashString(), expressionID.GetHashCode()); SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.ReceiveExpression.GetHashCode(), sfsObject, -1, BaseSFSExtension.GardenSmartFox.SFSPlazaRoomManager.CurrentPlazaRoom.Id); BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.Immediate); } public void SendPublicMessage(string message) { SFSObject sfsObject = new SFSObject(); sfsObject.PutInt(DataID.SenderID.GetHashString(), BaseSFSExtension.GardenSmartFox.User.Id); sfsObject.PutUtfString(DataID.Message.GetHashString(), message); SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.ReceivePublicMessage.GetHashCode(), sfsObject, -1, BaseSFSExtension.GardenSmartFox.SFSPlazaRoomManager.CurrentPlazaRoom.Id); BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.Immediate); } public void Instantiate(BaseEvent baseEvent) { //DebugManager.PrintKeysAndValuesOfBaseEvent("Instantiate", baseEvent); SFSObject parameter = (SFSObject)baseEvent.Params["params"]; int senderID = parameter.GetInt(DataID.SenderID.GetHashString()); User sender = CurrentPlazaRoom.GetUserById(senderID); if (onInstantiate != null) onInstantiate.Invoke(sender, parameter); } public void Synchronize(BaseEvent baseEvent) { //DebugManager.PrintKeysAndValuesOfBaseEvent("Synchronize", baseEvent); SFSObject parameter = (SFSObject)baseEvent.Params["params"]; int senderID = parameter.GetInt(DataID.SenderID.GetHashString()); User sender = CurrentPlazaRoom.GetUserById(senderID); if (onSynchronize != null) onSynchronize.Invoke(sender, parameter); } public void ReceiveExpression(BaseEvent baseEvent) { //DebugManager.PrintKeysAndValuesOfBaseEvent("ReceivePlazaRoomPublicMessage", baseEvent); SFSObject parameter = (SFSObject)baseEvent.Params["params"]; int senderID = parameter.GetInt(DataID.SenderID.GetHashString()); int expressionID = parameter.GetInt(DataID.Expression.GetHashString()); User sender = CurrentPlazaRoom.GetUserById(senderID); if (onReceiveExpression != null) onReceiveExpression.Invoke(expressionID, sender); } public void ReceivePublicMessage(BaseEvent baseEvent) { //DebugManager.PrintKeysAndValuesOfBaseEvent("ReceivePlazaRoomPublicMessage", baseEvent); SFSObject parameter = (SFSObject)baseEvent.Params["params"]; int senderID = parameter.GetInt(DataID.SenderID.GetHashString()); string message = parameter.GetUtfString(DataID.Message.GetHashString()); User sender = CurrentPlazaRoom.GetUserById(senderID); if (onReceivePublicMessage != null) onReceivePublicMessage.Invoke(message, sender); } public void OnJoinPlazaRoom(Room room) { //DebugManager.PrintKeysAndValuesOfBaseEvent("OnJoinPlazaRoom", baseEvent); Debug.LogWarning("Join plaza room : " + room.Name); CurrentPlazaRoom = room; if (onJoinPlazaRoom != null) onJoinPlazaRoom.Invoke(room); } public void OnUserExitPlazaRoom(Room room, User user) { if (room.Id == CurrentPlazaRoom.Id) { OnUserExitCurrentPlazaRoom(user); } } public void OnUserExitCurrentPlazaRoom(User user) { Debug.LogWarning($"User : {user.Name} left room"); if (onUserExitCurrentPlazaRoom != null) onUserExitCurrentPlazaRoom.Invoke(user); } public void OnUserEnterPlazaRoom(Room room, User user) { if (room.Id == CurrentPlazaRoom.Id) { OnUserEnterCurrentPlazaRoom(user); } } public void OnUserEnterCurrentPlazaRoom(User user) { Debug.LogWarning($"User : {user.Name} joined room"); if (onUserEnterCurrentPlazaRoom != null) onUserEnterCurrentPlazaRoom.Invoke(user); } }