Пример #1
0
        //Byte-array-based Encoding
        /// <summary>
        /// Encoding for CustomObject - Encoding format: TargetReceiverName, ObjectName, ObjectHealth, ObjectPosX, ObjectPosY, ObjectPosZ, ObjectRotX, ObjectRotY, ObjectRotZ, ObjectRotW
        /// </summary>
        public byte[] EncodeCustomObject(object obj)
        {
            CustomObject customObject = obj as CustomObject;

            if (customObject == null)
            {
                Console.WriteLine("CustomObject null");
                return(null);
            }

            byte[] returnObject;

            //Write to byte array
            using (var ms = new MemoryStream())
            {
                using (var bw = new BinaryWriter(ms))
                {
                    bw.Write(customObject.GetPacketTarget());
                    bw.Write(customObject.GetObjectName());
                    bw.Write(customObject.GetHealth());
                    bw.Write(customObject.GetPosX());
                    bw.Write(customObject.GetPosY());
                    bw.Write(customObject.GetPosZ());
                    bw.Write(customObject.GetRotX());
                    bw.Write(customObject.GetRotY());
                    bw.Write(customObject.GetRotZ());
                    bw.Write(customObject.GetRotW());

                    returnObject = ms.ToArray();
                }
            }

            return(returnObject);
        }
        public override void OnRaiseEvent(IRaiseEventCallInfo info)
        {
            try
            {
                base.OnRaiseEvent(info);
            }
            catch (Exception ex)
            {
                this.PluginHost.BroadcastErrorInfoEvent(ex.ToString(), info);
                return;
            }

            string stringData = "";

            try
            {
                stringData = Encoding.Default.GetString((byte[])info.Request.Data);
                SenderName = PacketDecoder.Instance.GetStringFromString(stringData, "Name");
            }
            catch (Exception ex)
            {
                //this.PluginHost.BroadcastErrorInfoEvent(ex.ToString(), info);
                Console.WriteLine(ex.ToString());
            }

            CustomObject packetData = null;

            try
            {
                packetData = (CustomObject)PacketDecoder.Instance.DecodeCustomObject((byte[])info.Request.Data);
            }
            catch (Exception ex)
            {
                //this.PluginHost.BroadcastErrorInfoEvent(ex.ToString(), info);
                Console.WriteLine(ex.ToString());
            }

            ///ASSIGNMENT 2 STUFF
            ///ASSIGNMENT 2 STUFF
            ///ASSIGNMENT 2 STUFF
            //TEST PACKET
            if (info.Request.EvCode == (byte)EVENT_CODES.TEST_PING)
            {
                SendListOfPlayers(info);
            }

            //Client asking for all AIs
            if (info.Request.EvCode == (byte)EVENT_CODES.EV_INITAI)
            {
                if (packetData != null)
                {
                    //Make sure AIManager thread is fully initialized and running
                    do
                    {
                        Thread.Sleep(100);
                    }while (AIManager.Instance == null);

                    //Iterate and send AI to the requesting client
                    foreach (AI enemy in AIManager.Instance.GetAIList())
                    {
                        //Set packetTarget to be the requesting client name so ONLY the requesting client will open
                        //This is to prevent duplicate AI from spawning on other clients when a new client joins the server
                        enemy.SetPacketTarget(packetData.GetPacketTarget());
                        this.PluginHost.BroadcastEvent(target: ReciverGroup.All, senderActor: 0, targetGroup: 0, evCode: (byte)EVENT_CODES.EV_SPAWNAI, data: new Dictionary <byte, object>()
                        {
                            { (byte)245, enemy }
                        }, cacheOp: 0);
                    }
                }
            }

            //Client updating position within server
            else if (info.Request.EvCode == (byte)EVENT_CODES.EV_UPDATEPLAYERPOS)
            {
                foreach (Player connectedPlayer in ConnectedPlayers)
                {
                    if (connectedPlayer.GetObjectName() == packetData.GetObjectName())
                    {
                        connectedPlayer.SetPos(packetData.GetPos());
                        return;
                    }
                }
            }

            //Client asking for List of all connected clients
            else if (info.Request.EvCode == (byte)EVENT_CODES.EV_LISTALLUSERS)
            {
                SendListOfPlayers(info);
            }

            //User joining server
            else if (info.Request.EvCode == (byte)EVENT_CODES.EV_USERJOINING)
            {
                UserJoined(info);
            }
            //User leaving server
            else if (info.Request.EvCode == (byte)EVENT_CODES.EV_USERLEAVING)
            {
                UserLeft(info);
            }

            ///ASSIGNMENT 1 STUFF
            ///ASSIGNMENT 1 STUFF
            ///ASSIGNMENT 1 STUFF

            //Search Users Data from Database from Server
            else if (info.Request.EvCode == (byte)EVENT_CODES.EV_SEARCHDB)
            {
                SearchUsersDatabase(info);
            }
            //Insert Users Data into Database from Server
            else if (info.Request.EvCode == (byte)EVENT_CODES.EV_INSERTDB)
            {
                InsertUsersDatabase(info);
            }
            //Update Users Data in Database from Server
            else if (info.Request.EvCode == (byte)EVENT_CODES.EV_UPDATEDB)
            {
                UpdateUsersDatabase(info);
            }

            //Add Friend in Database from Server
            else if (info.Request.EvCode == (byte)EVENT_CODES.EV_ADD_FRIEND)
            {
                InsertFriendsDatabase(info);
            }
            //Remove Friend in Database from Server
            else if (info.Request.EvCode == (byte)EVENT_CODES.EV_REMOVE_FRIEND)
            {
                DeleteFriendsDatabase(info);
            }
            //Accept Friend in Database from Server
            else if (info.Request.EvCode == (byte)EVENT_CODES.EV_ACCEPT_FRIEND)
            {
                UpdateFriendsDatabase(info);
            }
            //Decline Friend in Database from Server
            else if (info.Request.EvCode == (byte)EVENT_CODES.EV_DECLINE_FRIEND)
            {
                UpdateFriendsDatabase(info);
            }
            //List Friend in Database from Server
            else if (info.Request.EvCode == (byte)EVENT_CODES.EV_LIST_FRIEND)
            {
                SearchFriendsDatabase(info);
            }
        }