123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- 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<Room> onJoinPlazaRoom;
- public Action<User> onUserExitCurrentPlazaRoom;
- public Action<User> onUserEnterCurrentPlazaRoom;
- public Action<int, User> onReceiveExpression;
- public Action<string, User> onReceivePublicMessage;
- public Action<User, SFSObject> onSynchronize;
- public Action<User, SFSObject> 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<Room> 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<int> 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);
- }
- }
|