123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 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<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)
- {
- //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);
- }
- }
|