예제 #1
0
        async public Task <bool> StartGame()
        {
            if (Game.HasStarted)
            {
                return(true);
            }

            if (Game.Treasures.Count == 0)
            {
                Hud.Instance.ShowToast("Please ensure you have added at least one treasure to hunt for.");
                return(false);
            }

            if (!Game.HasMinimumPlayers())
            {
                Hud.Instance.ShowToast("Please ensure there are at least at least 2 teams with players.");
                return(false);
            }

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

                var task = new Task <Game>(() => { return(SaveGameSafe(action, GameUpdateAction.StartGame).Result); });
                await task.RunProtected();

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

                SetGame(task.Result);
                return(true);
            }
        }
예제 #2
0
        public async Task <bool> RemoveTreasure()
        {
            using (var busy = new Busy(this, "Removing treasure from game"))
            {
                Func <Game, Game> action = (game) =>
                {
                    game = game ?? Game;
                    var clone = game.Clone();
                    var tr    = clone.Treasures.SingleOrDefault(t => t.Id == Treasure.Id);
                    if (tr != null)
                    {
                        clone.Treasures.Remove(tr);
                        return(clone);
                    }
                    return(null);
                };

                var success = await SaveGameSafe(action, GameUpdateAction.RemoveTreasure);

                return(success != null);
            }
        }
예제 #3
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);
            }
        }
예제 #4
0
        async public Task <Game> JoinTeam(string teamId)
        {
            var toJoin = Game.Teams.SingleOrDefault(t => t.Id == teamId);

            if (toJoin == null)
            {
                return(null);
            }

            var fullMsg = $"{toJoin.Name} is full.\nPlease choose another team.";

            if (toJoin.Players.Count >= Game.PlayerCountPerTeam)
            {
                Hud.Instance.ShowToast(fullMsg, NoticationType.Error);
                return(null);
            }

            using (var busy = new Busy(this, "Joining team"))
            {
                await Task.Delay(500);

                Team team = null;
                Func <Game, Game> gameUpdateLogic = (game) =>
                {
                    game = game ?? Game;
                    var clone = game.Clone();
                    if (clone.JoinTeam(teamId))
                    {
                        team = clone.GetTeam();
                        return(clone);
                    }

                    Game = game;
                    InvokeRefreshedGame();
                    Hud.Instance.ShowToast(fullMsg, NoticationType.Error);
                    return(null);
                };

                var savedGame = await SaveGameSafe(gameUpdateLogic, GameUpdateAction.JoinTeam,
                                                   new Dictionary <string, string> {
                    { "teamId", teamId }
                });

                if (savedGame == null)
                {
                    return(null);
                }

                var savedTeam = savedGame.Teams.SingleOrDefault(t => t.Id == teamId);
                if (savedTeam == null)
                {
                    return(null);
                }

                Game = savedGame;
                InvokeRefreshedGame();
                var playerExists = savedTeam.Players.Exists(p => p.Email == App.Instance.Player.Email);

                //if(playerExists)
                //{
                //	Hud.Instance.ShowToast($"You are now part of {savedTeam.Name}", NoticationType.Success);
                //}

                return(playerExists ? savedGame : null);
            }
        }
예제 #5
0
        public async Task <bool> AnalyzePhotoForAcquisition()
        {
            if (Treasure == null)
            {
                throw new Exception("Please specify a treasure");
            }

            using (var busy = new Busy(this, "Uploading photo"))
            {
                var url = await App.Instance.StorageService.SaveImage(Photo, Game.Id);

                Log.Instance.WriteLine(url);

                if (url == null)
                {
                    throw new Exception("There was an issue uploading the image. Please try again.");
                }

                url = url.ToUrlCDN();
                Hud.Instance.HudMessage = "Analyzing photo";

                bool success = false;
                if (Treasure.Attributes[0].ServiceType == CognitiveServiceType.CustomVision)
                {
                    var task = new Task <bool>(() => App.Instance.DataService.AnalyzeCustomImage(Game, url, Treasure.Id).Result);
                    await task.RunProtected();

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

                    _treasureImageUrl = url;
                    success           = true;
                }
                else
                {
                    var task = new Task <string[]>(() => App.Instance.DataService.AnalyseImage(new string[] { url }).Result);
                    await task.RunProtected();

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

                    AttributeResults = task.Result;
                    foreach (var a in AttributeResults)
                    {
                        Log.Instance.WriteLine(a);
                    }

                    _treasureImageUrl = url;
                    success           = GetMatchCount() >= 1;
                }

                if (success)
                {
                    await AquireTreasureAndSaveGame();
                }

                NotifyPropertiesChanged();
                return(success);
            }
        }