using UnityEngine; using System; using System.Collections.Generic; using Sfs2X.Core; using Sfs2X.Entities; using Sfs2X.Requests; using Sfs2X.Entities.Data; public class PlazaRoomExtension { #region Variable public short PlazaRoomCapacity = 10; public string BasePlazaRoomName = "PlazaRoom"; public string PlazaRoomGroupID = "default"; public Action onExitPlazaRoom; public Action onJoinPlazaRoom; public Action onUserExitCurrentPlazaRoom; public Action onUserEnterCurrentPlazaRoom; 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) { //Debug.Log("A"); //Room room = new SFSRoom(999, "123"); //BaseSFSExtension.GardenSmartFox.SmartFox.Send(new JoinRoomRequest(room)); //return; string roomName = BasePlazaRoomName + index; RoomSettings roomSettings = new RoomSettings(roomName); roomSettings.GroupId = PlazaRoomGroupID; roomSettings.MaxUsers = PlazaRoomCapacity; roomSettings.MaxVariables = 30; BaseSFSExtension.GardenSmartFox.SmartFox.Send(new CreateRoomRequest(roomSettings, true)); } public void SendPublicMessage(string message) { SFSObject parameter = new SFSObject(); parameter.PutInt("RoomID", CurrentPlazaRoom.Id); parameter.PutText("Message", message); BaseSFSExtension.SendRequest(Request.SendPlazaRoomPublicMessage.GetHashCode(), parameter); } public void ReceivePlazaRoomPublicMessage(BaseEvent baseEvent) { //DebugManager.PrintKeysAndValuesOfBaseEvent("ReceivePlazaRoomPublicMessage", baseEvent); SFSObject parameter = (SFSObject) baseEvent.Params["params"]; int senderID = parameter.GetInt("SenderID"); string message = parameter.GetText("Message"); User sender = CurrentPlazaRoom.GetUserById(senderID); Debug.LogWarning(sender.IsItMe + " : " + message); } 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); } }