using UnityEngine; using System.Collections; using System.Collections.Generic; using Sfs2X.Entities.Data; public class BAServer { public enum ConnectionType { Local, Remote, } private MessageManager.MessageCallBackDelegate msgManCallBack; private BARoom currentRoom; private List messageStack; private ResponseWaiter resWaiter; private IConnection connection; private IConnection localConnection; private IConnection remoteConnection; public BAServer () { remoteConnection = new SFSConnection(); localConnection = new LocalConnection(); messageStack = new List(); resWaiter = new ResponseWaiter(); SetConnection (ConnectionType.Remote); } public void SetMsgManCallBack(MessageManager.MessageCallBackDelegate callBack) { this.msgManCallBack = callBack; } public void SetConnection(ConnectionType type) { IConnection newConnection = null; switch(type) { case ConnectionType.Local: newConnection = localConnection; break; case ConnectionType.Remote: newConnection = remoteConnection; break; } if (connection != null) { connection.SetResponseMessageCallBack (null); // if(connection.IsConnected && connection != newConnection) // { // connection.Disconnect(); // } } connection = newConnection; connection.SetResponseMessageCallBack (OnRemoteCallBack); } public void Connect() { if(!connection.IsConnected) connection.Connect (); } public bool IsConnected() { return connection.IsConnected; } public void EnterBase() { resWaiter.StartWait(); // SetConnection (ConnectionType.Local); Message msg = new Message(Command.EnterBase, new SFSObject()); Debuger.Log ("Send : ["+msg.cmd+"]"+msg.data.GetDump()); // connection.SendMessage(msg); DealEnterBase(msg); } private void DealEnterBase(Message msg) { string mapName = MapData.MapID.Base.ToString (); ISFSArray arr = new SFSArray (); ISFSObject myObj = new SFSObject (); myObj.PutUtfString (UserSFSObjectLabel.ID, Session.myUserId.ToString()); myObj.PutInt (UserSFSObjectLabel.TEAM_ID, TeamUtil.Team.Blue.GetHashCode()); myObj.PutUtfString (UserSFSObjectLabel.NICK, WWW.EscapeURL(Session.GetInstance().myUserData.nick)); arr.AddSFSObject(myObj); // ISFSObject oppObj = new SFSObject (); // oppObj.PutInt ("u", -1); // oppObj.PutInt ("t", TeamUtil.Team.Red.GetHashCode()); // oppObj.PutUtfString ("i", "-1"); // oppObj.PutUtfString ("n", RandomNPCNick()); // arr.AddSFSObject (oppObj); ISFSObject data = new SFSObject(); data.PutUtfString("m", mapName); data.PutUtfString ("c", Session.myUserId.ToString()); data.PutBool ("s", true); data.PutSFSArray("a", arr); Message callBackMsg = new Message(Command.SearchRoom, data); OnRemoteCallBack(callBackMsg.GetMessagePackage()); } public void SearchRoom() { resWaiter.StartWait(); // SetConnection (ConnectionType.Local); ISFSObject data = new SFSObject(); Message msg = new Message(Command.SearchRoom, data); Debuger.Log ("Send : ["+msg.cmd+"]"+msg.data.GetDump()); connection.SendMessage(msg); } private void HandleReplay() { ISFSObject msg = ReplayManager.GetInstance ().GetRecord (); if (msg != null) RoomHandleResponse (msg); } private void SetRoom(MapData.MapID mapId, ISFSArray arr, int myUserId) { if (currentRoom != null) currentRoom.Dispose (); switch(mapId) { case MapData.MapID.Base: currentRoom = new BaseRoom(this); break; case MapData.MapID.Challenge: currentRoom = new ChallengeRoom(this); break; case MapData.MapID.Defense: currentRoom = new DefenceRoom (this); break; case MapData.MapID.Flag: currentRoom = new FlagRoom (this); break; } if(currentRoom == null) throw new System.Exception("Illegal Map id["+mapId.ToString()+"]"); currentRoom.AddUser(arr); currentRoom.handlerCallBack = RoomHandleResponse; if (currentRoom.isRecord) ReplayManager.GetInstance ().StartRecord (); } private void UpdateRoom() { if(currentRoom != null) currentRoom.Update(); } public void RoomHandleResponse(ISFSObject msg) { if (currentRoom != null && currentRoom.isRecord) { //Did not ignore zone message yet ReplayManager.GetInstance ().CheckRecord (msg); } Debuger.Log ("Recieve : "+msg.GetDump()); if(msgManCallBack != null) msgManCallBack(msg); } public void Send(ISFSObject msg) { Message message = new Message (msg); Send (message); } public void Send(Message msg) { if(currentRoom == null) return; //TODO add to message trace Debuger.Log ("Send : ["+msg.cmd+"]"+msg.data.GetDump()); for(int i=0; i