using System.Collections; using System.Collections.Generic; using System.Security.Policy; using System.Xml; using Sfs2X; using Sfs2X.Core; using Sfs2X.Entities.Data; using Sfs2X.Requests; using Sfs2X.Util; using UnityEngine; public class Robot { #region Config private static bool Initialized; private static int MinChestValue; private static int MaxChestValue; private static float MinLifetime; private static float MaxLifetime; private static float CreateChestRate; private static float ColorChestRate; private static float DigitChestRate; private static float MinMoveTime; private static float MaxMoveTime; private static float MinRobotCreateTime; private static float MaxRobotCreateTime; private static string MaxRobotAmtFml; private static string ConfigName = "robot_config"; private static string RootNodeName = "data"; private static string DataNodeName = "item"; private SmartFox SmartFox; #endregion public static void Initialize() { if (Initialized) { Initialized = true; return; } XmlDocument document = ManaData.GetXmlDocument(ConfigName); XmlNode dataNode = document.SelectSingleNode(RootNodeName).SelectSingleNode(DataNodeName); XmlAttributeCollection attributes = dataNode.Attributes; int index = 1; List ints = Auxiliary.IntListParse(',', attributes[index++].Value, null); MinRobotCreateTime = ints[0]; MaxRobotCreateTime = ints[1]; MaxRobotAmtFml = attributes[index++].Value; DigitChestRate = float.Parse(attributes[index++].Value); ColorChestRate = float.Parse(attributes[index++].Value); ints = Auxiliary.IntListParse(',', attributes[index++].Value, null); MinMoveTime = ints[0]; MaxMoveTime = ints[1]; CreateChestRate = float.Parse(attributes[index++].Value); ints = Auxiliary.IntListParse(',', attributes[index++].Value, null); MinChestValue = ints[0]; MaxChestValue = ints[1]; ints = Auxiliary.IntListParse(',', attributes[index++].Value, null); MinLifetime = ints[0]; MaxLifetime = ints[1]; //Debug.Log($"RobotConfig {MinRobotCreateTime}"); //Debug.Log($"RobotConfig {MaxRobotCreateTime}"); //Debug.Log($"RobotConfig {MaxRobotAmtFml}"); //Debug.Log($"RobotConfig {DigitChestRate}"); //Debug.Log($"RobotConfig {ColorChestRate}"); //Debug.Log($"RobotConfig {MinMoveTime}"); //Debug.Log($"RobotConfig {MaxMoveTime}"); //Debug.Log($"RobotConfig {CreateChestRate}"); //Debug.Log($"RobotConfig {MinChestValue}"); //Debug.Log($"RobotConfig {MaxChestValue}"); //Debug.Log($"RobotConfig {MinLifetime}"); //Debug.Log($"RobotConfig {MaxLifetime}"); } public void Connect(int sfsRoomID, int maxRobot) { Debug.LogWarning("Robot connect"); SmartFox = new SmartFox(); SmartFox.AddEventListener(SFSEvent.CONNECTION, OnConnectReturn); SmartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnActivateError); SmartFox.AddEventListener(SFSEvent.LOGIN, evt => { Mark(sfsRoomID, maxRobot); }); SmartFox.AddEventListener(SFSEvent.LOGIN_ERROR, OnActivateError); SmartFox.AddEventListener(SFSEvent.LOGOUT, OnActivateError); SmartFox.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse); ConfigData configData = new ConfigData { Host = BaseConnector.Host, Port = BaseConnector.TcpPort, Zone = BaseConnector.Zone, }; SmartFox.Connect(configData); } private void Login() { Debug.LogWarning("Robot login"); string userName = Random.Range(0f, 1000f).ToString(); SmartFox.Send(new LoginRequest(userName, "")); } private void Mark(int sfsRoomID, int maxRobot) { Debug.LogWarning("Robot mark"); SFSObject arg = new SFSObject(); arg.PutInt(Label.CommandID, PlazaRoomReq.MarkAsRobot.GetHashCode()); arg.PutInt(Label.RoomID, sfsRoomID); arg.PutInt(Label.Data, maxRobot); SendRequest(arg); } private void SendRequest(SFSObject arg) { arg = GardenSmartFox.WrapIntoArray(new List { arg }); SmartFox.Send(new ExtensionRequest(HandlerID.PlazaRoom.GetHashString(), arg)); } private void OnConnectReturn(BaseEvent evt) { if ((bool)evt.Params["success"]) { Debug.LogWarning("Robot connect succeed"); Login(); } else { Debug.LogWarning("Robot connect failed"); OnActivateError(null); } } private void OnExtensionResponse(BaseEvent evt) { string cmd = evt.Params["cmd"].ToString(); if (cmd == PlazaRoomRep.JoinRoomSucceed.GetHashString()) { Activate(); } else if (cmd == PlazaRoomRep.MarkRobotError.GetHashString()) { OnActivateError(evt); } } public bool Update() { if (SmartFox != null) { SmartFox.ProcessEvents(); } return Behaviour(); } private bool Activated; private void Activate() { Debug.LogWarning("Robot Activate"); Activated = true; MoveTime = Random.Range(MinMoveTime, MaxMoveTime); LifetimeTime = Random.Range(MinLifetime, MaxLifetime); CreateChestTime = Random.Range(0, LifetimeTime); } private bool Behaviour() { if (!Activated) { return false; } if (LifetimeBehaviour()) { return true; } MoveBehaviour(); CreateChestBehaviour(); GetChestAwardBehaviour(); return false; } private float MoveTime; private float MoveTimer; private void MoveBehaviour() { MoveTimer += Time.deltaTime; if (MoveTimer >= MoveTime) { MoveTimer = 0; Debug.LogWarning("Robot move"); } } private float LifetimeTime; private float LifetimeTimer; private bool LifetimeBehaviour() { LifetimeTimer += Time.deltaTime; if (LifetimeTimer >= LifetimeTime) { Debug.LogWarning("Robot lifetime over"); return true; } else { return false; } } private bool CreateChestLock; private float CreateChestTime; private float CreateChestTimer; private void CreateChestBehaviour() { if (CreateChestLock) { return; } CreateChestTimer += Time.deltaTime; if (CreateChestTimer >= CreateChestTime) { Debug.LogWarning("Robot create chest"); CreateChestLock = true; } } private void GetChestAwardBehaviour() { //Debug.LogWarning("robot get a chest"); } public void Deactivate() { Debug.LogWarning("Robot disconnect"); SmartFox.Disconnect(); Activated = false; } private void OnActivateError(BaseEvent evt) { Debug.LogWarning("Robot activate error"); RobotManager.Robots.Remove(this); } }