Exemplo n.º 1
0
        public async Task <bool> SaveTreasure()
        {
            var treasure = new Treasure
            {
                ImageSource = _treasureImageUrl,
                IsRequired  = true,
                Points      = Constants.PointsPerAttribute / SelectedAttributes.Count,
                Hint        = Hint,
            };

            foreach (var attributeString in SelectedAttributes)
            {
                var attribute = new Hunt.Common.Attribute
                {
                    Name        = attributeString,
                    ServiceType = CognitiveServiceType.Vision
                };

                treasure.Attributes.Add(attribute);
            }

            using (var busy = new Busy(this, "Adding treasure"))
            {
                Func <Game, Game> action = (refreshedGame) =>
                {
                    refreshedGame = refreshedGame ?? Game;
                    var clone = refreshedGame.Clone();
                    clone.Treasures.Add(treasure);
                    return(clone);
                };

                var game = await SaveGameSafe(action, GameUpdateAction.AddTreasure, new Dictionary <string, string> {
                    { "treasureId", treasure.Id }
                });

                if (game != null)
                {
                    SetGame(game);
                    OnTreasureAdded?.Invoke(Game);
                    Hud.Instance.ShowToast("Treasure successfully added.");
                }

                return(game != null);
            }
        }
Exemplo n.º 2
0
        public async Task <bool> SaveTreasure()
        {
            var imageUrls = new List <string>();

            using (var busy = new Busy(this, "Uploading photo 1"))
            {
                int i = 1;
                foreach (var photo in Photos)
                {
                    Hud.Instance.HudMessage = $"Uploading photo {i}";
                    var url = await UploadPhotoToAzureStorage(photo);

                    if (url == null)
                    {
                        Hud.Instance.ShowToast("Unable to upload all the photos.", NoticationType.Error);
                        return(false);
                    }

                    imageUrls.Add(url);
                    i++;
                }

                var tags = new List <string>(AssignedTags.Split(',').ToList());
                while (tags.Count < 2)
                {
                    tags.Add("random_tag");
                }

                for (int j = 0; j < tags.Count; j++)
                {
                    tags[j] = $"{tags[j].Trim()}{Guid.NewGuid().ToString().Split('-')[0]}";
                }

                Hud.Instance.HudMessage = $"Training the classifier";
                var task = new Task <bool>(() => App.Instance.DataService.TrainClassifier(Game, imageUrls, tags.ToArray()).Result);
                await task.RunProtected(NotifyMode.Throw);

                if (!task.WasSuccessful() || !task.Result)
                {
                    return(false);
                }

                Hud.Instance.HudMessage = $"Adding the treasure";

                var treasure = new Treasure
                {
                    ImageSource = imageUrls[0],
                    IsRequired  = true,
                    Points      = Constants.PointsPerAttribute,
                    Hint        = Hint,
                };

                foreach (var tag in tags)
                {
                    var attribute = new Hunt.Common.Attribute
                    {
                        Name        = tag,
                        ServiceType = CognitiveServiceType.CustomVision,
                    };

                    treasure.Attributes.Add(attribute);
                }

                Func <Game, Game> action = (refreshedGame) =>
                {
                    refreshedGame = refreshedGame ?? Game;
                    var clone = refreshedGame.Clone();
                    clone.Treasures.Add(treasure);
                    return(clone);
                };

                var game = await SaveGameSafe(action, GameUpdateAction.AddTreasure);

                if (game != null)
                {
                    SetGame(game);
                    OnTreasureAdded?.Invoke(Game);
                }

                return(game != null);
            }
        }