示例#1
0
        /// <summary>
        /// Adds the attribut.
        /// </summary>
        /// <returns>The attribut.</returns>
        /// <param name="petID">Pet identifier.</param>
        /// <param name="petAttribut">Pet attribut.</param>
        public static ServerPacketConfirmation AddAttribut(int petID, PetAttribute petAttribut)
        {
            //create new client packet download attribut
            ClientPacketAddAttribut clientPacketAddAttribut = new ClientPacketAddAttribut(petID, petAttribut);

            //send packet to server
            ServerPacketConfirmation serverPacketConfirmation = TCPClient.SendPacket(clientPacketAddAttribut) as ServerPacketConfirmation;

            //if no answer
            if (serverPacketConfirmation == null)
            {
                return(new ServerPacketConfirmation(false, NetworkError.SERVER_UNAVAILABLE));
            }

            //return packet
            return(serverPacketConfirmation);
        }
        public override Packet OnPacketReceive(Packet receivedPacket)
        {
            //get received packet
            ClientPacketAddAttribut clientPacketAddAttribut = receivedPacket as ClientPacketAddAttribut;

            ConsoleHelper.Write("Receive - ClientPacketAddAttribut");

            //read packet infos
            int          petID        = clientPacketAddAttribut.PetID;
            PetAttribute petAttribute = clientPacketAddAttribut.PetAttribute;

            //download pet
            MySqlCommand downloadPet = new MySqlCommand();

            downloadPet.Connection  = Program.mySQLManager.MySQLConnection.MysqlConnection;
            downloadPet.CommandText = $@"SELECT * FROM `T_Pet` WHERE `petID` IN ({petID})";


            MySqlDataReader mysqlDataReader = null;

            List <PetAttribute> petAttributes = new List <PetAttribute>();

            try
            {
                //execute reader
                mysqlDataReader = downloadPet.ExecuteReader();

                //open reader
                while (mysqlDataReader.Read())
                {
                    petAttributes = JsonConvert.DeserializeObject <List <PetAttribute> >(mysqlDataReader.GetString(3));
                }

                //close reader
                mysqlDataReader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(new ServerPacketConfirmation(false, NetworkError.GLOBAL_UNKNOWN));
            }

            //close reader
            if (mysqlDataReader != null && !mysqlDataReader.IsClosed)
            {
                mysqlDataReader.Close();
            }

            petAttributes.Add(petAttribute);

            try
            {
                //update pet attribut list
                MySqlCommand updatePetAttributeCommand = new MySqlCommand();
                updatePetAttributeCommand.Connection  = Program.mySQLManager.MySQLConnection.MysqlConnection;
                updatePetAttributeCommand.CommandText = $@"UPDATE `T_Pet` SET `petAttributs`='{JsonConvert.SerializeObject(petAttributes).ToSQL()}' WHERE `petID`='{petID}'";
                updatePetAttributeCommand.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(new ServerPacketConfirmation(false, NetworkError.GLOBAL_UNKNOWN));
            }

            ConsoleHelper.Write("Send - ServerPacketConfirmation");

            return(new ServerPacketConfirmation(true, NetworkError.NONE));
        }