123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Sfs2X;
- using Sfs2X.Core;
- using Sfs2X.Entities;
- using Sfs2X.Entities.Data;
- using Sfs2X.Requests;
- using UnityEngine;
- public enum BatchOption
- {
- Always,
- TryNot,
- }
- public enum RequestType
- {
- Batch,
- BatchAndOverride,
- Immediate,
- }
- public enum RequestStatus
- {
- Failed,
- Succeed,
- Pending,
- }
- public class GardenSmartFox
- {
- #region Variable
- public const string CommandArrayLabel = "A";
- public const string CommandLabel = "C";
- public const string RoomIDLabel = "I";
- public const string ParameterLabel = "P";
- public const string TargetUserLabel = "U";
- public User User;
- public SmartFox SmartFox;
- public BaseLoggor Loggor;
- public BaseConnector Connector;
- public DatabaseManager DatabaseManager;
- public SFSExtensionManager ExtensionManager;
- public SFSPlazaRoomManager SFSPlazaRoomManager;
- public float RequestTime = 3f;
- public float RequestTimer = 0f;
- public List<SFSObject> BatchRequestList = new List<SFSObject>();
- public List<string> StaleRequests = new List<string>();
- public Dictionary<string, SFSObject> BatchAndOverrideRequestDictionary = new Dictionary<string, SFSObject>();
- #endregion
- public GardenSmartFox(BaseLoggor baseLoggor, BaseConnector baseConnector)
- {
- SmartFox = new SmartFox();
- Loggor = baseLoggor.Init(this);
- Connector = baseConnector.Init(this);
- ExtensionManager = new SFSExtensionManager(this);
- DatabaseManager = new DatabaseManager(this);
- SFSPlazaRoomManager = new SFSPlazaRoomManager(this);
- }
- public void ExecuteAfterCheckConection(Action execute, Action<bool, BaseEvent> onConnectResult = null, Action<bool, BaseEvent> onLoginResult = null)
- {
- if (!SmartFox.IsConnected)
- {
- Reset();
- Connector.Connect();
- if (onConnectResult != null)
- {
- new CounterAction<bool, BaseEvent>
- (
- (succeed, baseEvent) =>
- {
- onConnectResult.Invoke(succeed, baseEvent);
- }
- ).Bind(ref Connector.onConnectResult);
- }
- if (onLoginResult != null)
- {
- new CounterAction<bool, BaseEvent>
- (
- (succeed, baseEvent) =>
- {
- onLoginResult.Invoke(succeed, baseEvent);
- }
- ).Bind(ref Loggor.onLoginResult);
- }
- new CounterAction<BaseEvent>
- (
- baseEven =>
- {
- execute.Invoke();
- }
- ).Bind(ref Loggor.onLogin);
- }
- else
- {
- execute.Invoke();
- }
- }
- public void Reset()
- {
- SmartFox.RemoveAllEventListeners();
- SmartFox = new SmartFox();
-
- Loggor.Init(this);
- Connector.Init(this);
- ExtensionManager.Init(this);
- }
- public void ProcessRequest()
- {
- RequestTimer += Time.deltaTime;
- if (RequestTimer >= RequestTime)
- {
- RequestTimer = 0;
- StaleRequests = new List<string>();
- if (BatchRequestList.Count > 0 || BatchAndOverrideRequestDictionary.Count > 0)
- {
- BatchRequestList.AddRange(BatchAndOverrideRequestDictionary.Values.ToList());
- SendRequest(BatchRequestList);
- BatchRequestList = new List<SFSObject>();
- BatchAndOverrideRequestDictionary = new Dictionary<string, SFSObject>();
- }
- }
- }
- public void AddRequest(SFSObject sfsObject, RequestType requestType, string requestName = null, BatchOption batchOption = BatchOption.Always)
- {
- if (requestType == RequestType.Immediate)
- {
- SendRequest(sfsObject);
- //Debug.Log("Send request immediately");
- }
- else if (requestType == RequestType.Batch)
- {
- if (CheckBatchOption(sfsObject, requestName, batchOption))
- {
- return;
- }
- else
- {
- BatchRequestList.Add(sfsObject);
- }
- //Debug.Log("Add batch request");
- }
- else if (requestType == RequestType.BatchAndOverride)
- {
- //Debug.Log("Override batch request");
- if (CheckBatchOption(sfsObject, requestName, batchOption))
- {
- return;
- }
- else
- {
- if (BatchAndOverrideRequestDictionary.ContainsKey(requestName))
- {
- BatchAndOverrideRequestDictionary[requestName] = sfsObject;
- }
- else
- {
- BatchAndOverrideRequestDictionary.Add(requestName, sfsObject);
- }
- }
- }
- else
- {
- throw new Exception();
- }
- }
- public bool CheckBatchOption(SFSObject sfsObject, string requestName, BatchOption batchOption)
- {
- if (batchOption == BatchOption.Always)
- {
- return false;
- }
- else if (batchOption == BatchOption.TryNot)
- {
- if (!StaleRequests.Contains(requestName))
- {
- SendRequest(sfsObject);
- StaleRequests.Add(requestName);
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- throw new Exception();
- }
- }
- public void SendRequest(SFSObject parameter)
- {
- parameter = ConstructRequestParameter(new List<SFSObject>() { parameter });
- SmartFox.Send(new ExtensionRequest(RequestID.Command.GetHashCode().ToString(), parameter));
- }
- public void SendRequest(List<SFSObject> parameters)
- {
- SFSObject parameter = ConstructRequestParameter(parameters);
- SmartFox.Send(new ExtensionRequest(RequestID.Command.GetHashCode().ToString(), parameter));
- }
- public static SFSObject ConstructRequestParameter(List<SFSObject> sfsObjects)
- {
- SFSObject parameter = new SFSObject();
- SFSArray sfsArray = new SFSArray();
- foreach (var sfsObject in sfsObjects)
- {
- sfsArray.AddSFSObject(sfsObject);
- }
- parameter.PutSFSArray(CommandArrayLabel, sfsArray);
- return parameter;
- }
- public static SFSObject ConstructCommandParameter(int commandID, SFSObject sfsObject, int targetUserID, int roomID)
- {
- SFSObject parameter = new SFSObject();
- parameter.PutInt(CommandLabel, commandID);
- parameter.PutSFSObject(ParameterLabel, sfsObject);
- parameter.PutInt(TargetUserLabel, targetUserID);
- parameter.PutInt(RoomIDLabel, roomID);
- return parameter;
- }
- }
|