예제 #1
0
 private void UpdatePrimeItem(PrimeItem source, PrimeItem dest)
 {
     dest.Name    = source.Name;
     dest.WikiUrl = source.WikiUrl;
     dest.ImageId = source.ImageId;
     dest.Image   = source.Image;
 }
예제 #2
0
 public ItemSaveData GetDataForItem(PrimeItem item)
 {
     if (!SavedData.TryGetValue(item.Name, out ItemSaveData data))
     {
         data = new ItemSaveData();
         SavedData[item.Name] = data;
     }
     return(data);
 }
        internal static async Task AddNewPrime(PrimeItem metricsRequest)
        {
            var metricsRequestAsJsonStr = JsonConvert.SerializeObject(metricsRequest);

            using (var request = new HttpRequestMessage(HttpMethod.Post, $"{BaseUri}/prime"))
                using (var content = new StringContent(metricsRequestAsJsonStr, Encoding.UTF8, "application/json"))
                {
                    request.Content = content;
                    var response = await _client.SendAsync(request);

                    response.EnsureSuccessStatusCode();
                }
        }
예제 #4
0
        public ActionResult ComputePrime([FromBody] PrimeItem request)
        {
            var prime = request.Prime;

            _prime = Convert.ToInt64(prime);
            for (long i = 2; i < _prime / 2; i++)
            {
                if (_prime % i == 0)
                {
                    _answer[0] = i;
                    _answer[1] = _prime / i;
                    break;
                }
            }

            // TODO 此处添加对_answer和 _prime加密的功能
            var plainprime = new Plaintext($"{_prime.ToString("X")}");

            Console.WriteLine(_prime);
            var ciphertextprime = new Ciphertext();

            _encryptor.Encrypt(plainprime, ciphertextprime);
            var base64Prime = SEALUtils.CiphertextToBase64String(ciphertextprime);

            var plainfactor1      = new Plaintext($"{_answer[0].ToString("X")}");
            var ciphertextfactor1 = new Ciphertext();

            _encryptor.Encrypt(plainfactor1, ciphertextfactor1);
            var base64Factor1 = SEALUtils.CiphertextToBase64String(ciphertextfactor1);

            var plainfactor2      = new Plaintext($"{_answer[1].ToString("X")}");
            var ciphertextfactor2 = new Ciphertext();

            _encryptor.Encrypt(plainfactor2, ciphertextfactor2);
            var base64Factor2 = SEALUtils.CiphertextToBase64String(ciphertextfactor2);

            var publicKey = SEALUtils.PublicKeyToBase64String(_keyGenerator.PublicKey);
            var secretKey = SEALUtils.SecretKeyToBase64String(_keyGenerator.SecretKey);

            _base64Prime   = base64Prime;
            _base64Factor1 = base64Factor1;
            _base64Factor2 = base64Factor2;
            _publicKey     = publicKey;
            _secretKey     = secretKey;


            return(Ok());
        }
        public ActionResult ComputePrime([FromBody] PrimeItem request)
        {
            var prime = request.Prime;

            _prime = Convert.ToInt64(prime);
            for (int i = 2; i < _prime / 2; i++)
            {
                if (_prime % i == 0)
                {
                    _answer[0] = i;
                    _answer[1] = _prime / i;
                    break;
                }
            }


            return(Ok());
        }
        static async Task SendPrime()
        {
            Console.WriteLine("Enter the the number u want to compute: ");
            var newPrime = Convert.ToInt64(Console.ReadLine());

            if (newPrime < 0)
            {
                Console.WriteLine("The number u want to check must be > 0.");
                return;
            }

            var PrimeString    = newPrime.ToString();
            var metricsRequest = new PrimeItem
            {
                Prime = PrimeString
            };
            await FitnessTrackerClient.AddNewPrime(metricsRequest);
        }
예제 #7
0
        /// <inheritdoc />
        public async Task <PrimeItem> AddOrUpdatePrimeItem(PrimeItem primeItem, PrimeItemData itemData)
        {
            // Create a base group for Ingredients
            foreach (var ingredientGroupData in itemData.PartsData)
            {
                // Create the ingredients group
                var groupName = ingredientGroupData.Key;
                if (string.IsNullOrEmpty(groupName))
                {
                    groupName = null;
                }
                var group = await AddOrUpdatePeristentItem(
                    new IngredientsGroup()
                {
                    Name        = groupName,
                    PrimeItemId = primeItem.Id,
                    PrimeItem   = primeItem,
                },
                    (dest, source, _) => {
                    if (dest.Name != source.Name)
                    {
                        dest.Name = source.Name;
                    }
                    if (dest.PrimeItemId != source.PrimeItemId)
                    {
                        dest.PrimeItemId = source.PrimeItemId;
                        dest.PrimeItem   = source.PrimeItem;
                    }
                    // Always completely clear the ingredients list
                    if (dest.ResourceIngredients.Count > 0)
                    {
                        dest.ResourceIngredients.Clear();
                    }
                });

                // Add all ingredients to it
                foreach (var ingredientData in ingredientGroupData.Value)
                {
                    // Get the image
                    var data = await StreamToByteArray(ingredientData.Image);

                    // If part was found, update count and image
                    var foundPart = primeItem.PrimePartIngredients.FirstOrDefault(x => x.PrimePart.Name.StartsWith(ingredientData.Name));
                    if (foundPart != null)
                    {
                        foundPart.Count = ingredientData.Count;
                        // Add image
                        var partImage = await AddOrUpdatePeristentItem(
                            new Image()
                        {
                            Data = data
                        },
                            (dest, source, _) => dest.Data = source.Data
                            );

                        foundPart.PrimePart.ImageId = partImage.Id;
                        foundPart.PrimePart.Image   = partImage;
                    }

                    // It's also a resource, so create or update it also as a resource
                    var resourceImage = await AddOrUpdatePeristentItem(
                        new Image()
                    {
                        Data = data
                    },
                        (dest, source, _) => dest.Data = source.Data
                        );

                    var resource = await AddOrUpdatePeristentItem(
                        new Resource()
                    {
                        Name    = ingredientData.Name,
                        ImageId = resourceImage.Id,
                        Image   = resourceImage,
                    },
                        (dest, source, _) => {
                        if (dest.Name != source.Name)
                        {
                            dest.Name = source.Name;
                        }
                        if (dest.ImageId != source.ImageId)
                        {
                            dest.ImageId = source.ImageId;
                            dest.Image   = source.Image;
                        }
                    });

                    var resourceIngredient = await AddOrUpdateItem(
                        new ResourceIngredient()
                    {
                        ResourceId         = resource.Id,
                        Resource           = resource,
                        IngredientsGroupId = group.Id,
                        IngredientsGroup   = group,
                        Count = ingredientData.Count,
                    },
                        i => new object[] { i.IngredientsGroupId, i.ResourceId },
                        (dest, source, entry) => {
                        if (dest.ResourceId != source.ResourceId)
                        {
                            dest.ResourceId = source.ResourceId;
                            dest.Resource   = source.Resource;
                        }
                        if (dest.IngredientsGroupId != source.IngredientsGroupId)
                        {
                            dest.IngredientsGroupId = source.IngredientsGroupId;
                            dest.IngredientsGroup   = source.IngredientsGroup;
                        }
                        if (dest.Count != source.Count)
                        {
                            dest.Count = source.Count;
                        }
                    }
                        );
                }
            }

            // Add the image
            var image = await AddOrUpdatePeristentItem(
                new Image()
            {
                Data = await StreamToByteArray(itemData.Image),
            },
                (dest, source, _) => dest.Data = source.Data
                );

            primeItem.ImageId = image.Id;
            primeItem.Image   = image;

            return(primeItem);
        }