Esempio n. 1
0
        /// <summary>
        /// Deletes the attribut.
        /// </summary>
        /// <returns>The attribut.</returns>
        /// <param name="petID">Pet identifier.</param>
        /// <param name="attributID">Attribut identifier.</param>
        public static ServerPacketConfirmation DeleteAttribut(int petID, int attributID)
        {
            //create new client packet delete attribut
            ClientPacketDeleteAttribut clientPacketDeleteAttribut = new ClientPacketDeleteAttribut(petID, attributID);

            //send packet to server
            ServerPacketConfirmation serverPacketConfirmation = TCPClient.SendPacket(clientPacketDeleteAttribut) 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
            ClientPacketDeleteAttribut clientPacketDeleteAttribut = receivedPacket as ClientPacketDeleteAttribut;

            ConsoleHelper.Write("Receive - DeleteAttributPacketReceiveHandler");

            //read packet infos
            int petID      = clientPacketDeleteAttribut.PetID;
            int attributID = clientPacketDeleteAttribut.AttributID;

            //download pet command
            MySqlCommand downloadPetsCommand = new MySqlCommand();

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

            MySqlDataReader mysqlDataReader = null;

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

            try
            {
                //execute reader
                mysqlDataReader = downloadPetsCommand.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);
            }

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


            if (petAttributes.Count <= attributID)
            {
                Console.WriteLine("Warn too much pet attributs S: " + petAttributes.Count + " C: " + attributID);
                return(new ServerPacketConfirmation(false, NetworkError.GLOBAL_UNKNOWN));
            }

            //Get attribute with id
            PetAttribute attribute = petAttributes[attributID];

            //Remove attribute from the list
            petAttributes.Remove(attribute);

            try
            {
                //update attributes command
                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 {}

            ConsoleHelper.Write("Send - ServerPacketConfirmation");

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