예제 #1
0
 public RunMetadata Clone(IRun run)
 {
     return(new RunMetadata(run)
     {
         oldGameName = oldGameName,
         oldCategoryName = oldCategoryName,
         game = game,
         category = category,
         run = this.run,
         runId = runId,
         platformName = platformName,
         regionName = regionName,
         usesEmulator = usesEmulator,
         VariableValueNames = VariableValueNames.ToDictionary(x => x.Key, x => x.Value),
         //TODO: set members instead later
         //TODO: clone PropertyChanged
     });
 }
예제 #2
0
        public void Refresh()
        {
            lock (this)
            {
                if (LiveSplitRun.GameName != oldGameName)
                {
                    GameAvailable     = false;
                    CategoryAvailable = false;

                    oldGameName = LiveSplitRun.GameName;
                    if (!string.IsNullOrEmpty(LiveSplitRun.GameName))
                    {
                        var gameTask = Task.Factory.StartNew(() =>
                        {
                            try
                            {
                                var game   = SpeedrunCom.Client.Games.SearchGameExact(oldGameName, new GameEmbeds(embedRegions: true, embedPlatforms: true));
                                gameLoaded = true;
                                if (game != null)
                                {
                                    GameAvailable = true;
                                }
                                return(game);
                            }
                            catch
                            {
                                gameLoaded = false;
                                return(null);
                            }
                        });
                        this.game = new Lazy <Game>(() => gameTask.Result);

                        var platformTask = Task.Factory.StartNew(() =>
                        {
                            var game = this.game.Value;
                            if (game != null && !game.Platforms.Any(x => x.Name == PlatformName) || gameLoaded && game == null)
                            {
                                PlatformName = string.Empty;
                            }
                        });
                        var regionTask = Task.Factory.StartNew(() =>
                        {
                            var game = this.game.Value;
                            if (game != null && !game.Regions.Any(x => x.Name == RegionName) || gameLoaded && game == null)
                            {
                                RegionName = string.Empty;
                            }
                        });
                    }
                    else
                    {
                        this.game = new Lazy <Game>(() => null);
                    }

                    oldCategoryName = null;
                }


                if (LiveSplitRun.CategoryName != oldCategoryName)
                {
                    CategoryAvailable = false;

                    oldCategoryName = LiveSplitRun.CategoryName;
                    if (!string.IsNullOrEmpty(oldCategoryName))
                    {
                        var categoryTask = Task.Factory.StartNew(() =>
                        {
                            var game = this.game.Value;
                            if (game == null)
                            {
                                return(null);
                            }

                            try
                            {
                                var category = SpeedrunCom.Client.Games.GetCategories(game.ID, embeds: new CategoryEmbeds(embedVariables: true))
                                               .FirstOrDefault(x => x.Type == CategoryType.PerGame && x.Name == oldCategoryName);
                                if (category != null)
                                {
                                    CategoryAvailable = true;
                                }
                                return(category);
                            }
                            catch
                            {
                                return(null);
                            }
                        });
                        this.category = new Lazy <Category>(() => categoryTask.Result);

                        var variableTask = Task.Factory.StartNew(() =>
                        {
                            var category   = this.category.Value;
                            var categoryId = category != null ? category.ID : null;
                            var game       = this.game.Value;
                            if (game == null && !gameLoaded)
                            {
                                return;
                            }

                            try
                            {
                                var deletions          = new List <string>();
                                var variableValueNames = VariableValueNames.ToDictionary(x => x.Key, x => x.Value);

                                if (game != null)
                                {
                                    var variables = game.FullGameVariables.Where(x => x.CategoryID == null || x.CategoryID == categoryId).ToList();

                                    foreach (var variableNamePair in variableValueNames)
                                    {
                                        var variable = variables.FirstOrDefault(x => x.Name == variableNamePair.Key);
                                        if (variable == null ||
                                            (!variable.Values.Any(x => x.Value == variableNamePair.Value) && !variable.IsUserDefined))
                                        {
                                            deletions.Add(variableNamePair.Key);
                                        }
                                    }
                                }
                                else
                                {
                                    deletions.AddRange(variableValueNames.Keys);
                                }

                                foreach (var variable in deletions)
                                {
                                    variableValueNames.Remove(variable);
                                }
                                VariableValueNames = variableValueNames;
                            }
                            catch (Exception ex)
                            {
                                Log.Error(ex);
                            }
                        });
                    }
                    else
                    {
                        this.category = new Lazy <Category>(() => null);
                    }
                }
            }
        }