123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- 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 = 10;
- public string BasePlazaRoomName = "PlazaRoom";
- public string PlazaRoomGroupID = "default";
- public Action onExitPlazaRoom;
- public Action<Room> onJoinPlazaRoom;
- public Action<User> onUserExitCurrentPlazaRoom;
- public Action<User> 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<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 SendSynchronize(Vector3 destination)
- {
- JsonData jsonData = new JsonData();
- jsonData[DataID.Destination.GetHashCode().ToString()] = destination.VectorToString();
-
- SFSObject sfsObject = new SFSObject();
- sfsObject.PutInt(DataID.SenderID.GetHashCode().ToString(), BaseSFSExtension.GardenSmartFox.User.Id);
- sfsObject.PutText(DataID.JsonString.GetHashCode().ToString(), jsonData.ToJson());
- SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.Synchronize.GetHashCode(), sfsObject, -1);
- BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.BatchAndOverride, CommandID.Synchronize.ToString());
- }
- public void SendPublicMessage(string message)
- {
- SFSObject sfsObject = new SFSObject();
- sfsObject.PutInt(DataID.SenderID.GetHashCode().ToString(), BaseSFSExtension.GardenSmartFox.User.Id);
- sfsObject.PutText(DataID.Message.GetHashCode().ToString(), message);
- SFSObject parameter = GardenSmartFox.ConstructCommandParameter(CommandID.ReceivePublicMessage.GetHashCode(), sfsObject, -1);
- BaseSFSExtension.GardenSmartFox.AddRequest(parameter, RequestType.Immediate);
- }
- public void Synchronize(BaseEvent baseEvent)
- {
- //DebugManager.PrintKeysAndValuesOfBaseEvent("Synchronize", baseEvent);
- SFSObject parameter = (SFSObject)baseEvent.Params["params"];
-
- int senderID = parameter.GetInt(DataID.SenderID.GetHashCode().ToString());
- string jsonString = parameter.GetText(DataID.JsonString.GetHashCode().ToString());
- User sender = CurrentPlazaRoom.GetUserById(senderID);
- JsonData jsonData = JsonMapper.ToObject(jsonString);
- BaseSFSExtension.GardenSmartFox.PlazaRoomManager.Synchronize(jsonData, sender);
- }
- public void ReceivePublicMessage(BaseEvent baseEvent)
- {
- //DebugManager.PrintKeysAndValuesOfBaseEvent("ReceivePlazaRoomPublicMessage", baseEvent);
- SFSObject parameter = (SFSObject)baseEvent.Params["params"];
- int senderID = parameter.GetInt(DataID.SenderID.GetHashCode().ToString());
- string message = parameter.GetText(DataID.Message.GetHashCode().ToString());
- User sender = CurrentPlazaRoom.GetUserById(senderID);
- BaseSFSExtension.GardenSmartFox.PlazaRoomManager.ReceivePublicMessage(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);
- }
- }
|