using Pomelo.DotNetClient; using System; using pomelo.area; using XmdsBattleClient.Battle; using Pomelo.DotNetClient.NetClient; using CommonAI.Zone.ZoneEditor; using System.Diagnostics; using System.IO; namespace XmdsBattleClientBot.XmdsBot { /// /// 机器人实体对象 /// public class XmdsNetClient : BaseNetClient { public LoginHandler LoginHandler { get; private set; } public override EditorTemplates DataRoot { get { return XmdsBattleManager.DataRoot; } } public XmdsNetClient(bool auto_invoke_change_scene = false) : base(new GateSocket(new ProtoSerializer()), new GameSocket(new ProtoSerializer())) { this.LoginHandler = new LoginHandler(this); this.OnBattlePlayerReady += XmdsNetClient_OnBattlePlayerReady; if (auto_invoke_change_scene) { this.GameSocket.listen(new Action((act) => { this.LoginHandler.Invoke_ChangeScene(); })); } } public override XmdsBattleClient.Battle.XmdsBattleClient CreateBattle() { return new XmdsBattleClient.Battle.XmdsBattleClient(this); } //public override void SendBattleMessage(CommonAI.Zone.Action action) //{ // byte[] bin; // // 将战斗服协议编码成二进制 // // if (mBattleCodec.doEncodeBin(out bin, action)) // { // GameSocket.playerHandler.battleEventNotify(bin); // } //} private readonly MemoryStream writeBuffer = new MemoryStream(2048); public override void SendBattleMessage(CommonAI.Zone.Action action) { if (writeBuffer != null) { writeBuffer.Position = 0; if (mBattleCodec.doEncode(writeBuffer, action)) { var route = EventTypes.GetNotifyKey(typeof(BattleEventNotify)); GameSocket.send(route, 0, writeBuffer); } } } //----------------------------------------------------------------------------------------- #region _监视进入场景时间_ private Stopwatch entry_server_watch = new Stopwatch(); internal void StartEntryServer() { entry_server_watch.Reset(); entry_server_watch.Start(); } private void XmdsNetClient_OnBattlePlayerReady(CommonAI.ZoneClient.ZoneLayer arg1, CommonAI.ZoneClient.ZoneActor arg2) { entry_server_watch.Stop(); this.EntryServerElapsedMS = entry_server_watch.ElapsedMilliseconds; } public long EntryServerElapsedMS { get; private set; } #endregion //----------------------------------------------------------------------------------------- } public class ProtoSerializer : ISerializer { private ProtobufSerializer ser = new ProtobufSerializer(); public object Deserialize(Stream source, object value, Type type) { return ser.Deserialize(source, value, type); } public void Serialize(Stream stream, object msg) { ser.Serialize(stream, msg); } } }