Example #1
0
 public Recipe(API.ANet.Recipe recipe, bool is_checked, bool is_saved, long session_id)
 {
     this._recipe     = recipe;
     this._is_checked = is_checked;
     this._is_saved   = is_saved;
     this._session_id = session_id;
 }
Example #2
0
        public static List <Recipe> GetRecipesInError(string dbname)
        {
            try
            {
                SQLite    db = new SQLite(dbname);
                DataTable recipe;
                String    query = String.Format("select * FROM Recipe WHERE json LIKE '%\"error\":\"{0}\"%';", Config.DEFAULT_ERROR_CODE);
                recipe = db.GetDataTable(query);

                List <Recipe> recipes = new List <Recipe>();

                foreach (DataRow r in recipe.Rows)
                {
                    string          json        = (string)r["json"];
                    bool            is_checked  = (bool)r["is_checked"];
                    long            session_id  = (long)r["session_id"];
                    API.ANet.Recipe anet_recipe = Serializer <API.ANet.Recipe> .Deserialize(json);

                    recipes.Add(new Recipe(anet_recipe, is_checked, true, session_id));
                }

                return(recipes);
            }
            catch
            {
                throw;
            }
        }
Example #3
0
        public static List <Recipe> GetRecipes(string dbname)
        {
            try
            {
                SQLite    db = new SQLite(dbname);
                DataTable recipe;
                String    query = "select * FROM Recipe;";
                recipe = db.GetDataTable(query);

                List <Recipe> recipes = new List <Recipe>();

                foreach (DataRow r in recipe.Rows)
                {
                    string          json        = (string)r["json"];
                    bool            is_checked  = (bool)r["is_checked"];
                    long            session_id  = (long)r["session_id"];
                    API.ANet.Recipe anet_recipe = Serializer <API.ANet.Recipe> .Deserialize(json);

                    recipes.Add(new Recipe(anet_recipe, is_checked, true, session_id));
                }

                return(recipes);
            }
            catch
            {
                throw;
            }
        }
Example #4
0
        public Data.Recipe AddRecipe(API.ANet.Recipe recipe)
        {
            if (this._recipes.Where(r => r.Id != null && r.Id == int.Parse(recipe.recipe_id)).Count() == 0)
            {
                Data.Recipe newrecipe = new Data.Recipe(recipe, false, false, _session_id);
                this._recipes.Add(newrecipe);
                this.SaveRecipe(newrecipe);

                return(newrecipe);
            }
            return(null);
        }
        private void DownloadNewRecipes()
        {
            BackgroundWorker downloadWorker = new BackgroundWorker();

            downloadWorker.WorkerReportsProgress = true;

            List <int> recipe_ids = null;
            MainWindow win        = this;

            downloadWorker.DoWork +=
                (object sender, DoWorkEventArgs e) =>
            {
                try
                {
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        this.tbCurrentTask.Text = Properties.Resources.TextBoxCurrentDownloadStarting;
                    }));

                    recipe_ids = Data.DataProvider.GetRecipeIDs();

                    // get only not cached recipes
                    List <int> cached_recipe_ids = Data.Cache.GetCache().Recipes.Select <Data.Recipe, int>(p => (int)p.Id).ToList();
                    recipe_ids = recipe_ids.Where(id => !cached_recipe_ids.Contains(id)).ToList();

                    Dispatcher.BeginInvoke(new Action(() => {
                        this.tbCurrentTask.Text =
                            String.Format(Properties.Resources.TextBoxCurrentDownloadNewRecipesFoundNotification, recipe_ids.Count);
                        this.pbDownloadIndicator.Minimum = 0;
                        this.pbDownloadIndicator.Maximum = recipe_ids.Count;
                    }));

                    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                    for (int i = 0; i <= recipe_ids.Count - 1; i++)
                    {
                        if (e.Cancel)
                        {
                            break;
                        }

                        int recipe_id = recipe_ids[i];

                        try
                        {
                            sw.Start();
                            Data.API.ANet.Recipe recipe = Data.DataProvider.GetANetRecipe(recipe_id, Config.Lang);
                            if (recipe == null)
                            {
                                throw new System.Net.WebException(String.Format("Something goes wrong with recipe {0}!", recipe_id));
                            }

                            sw.Stop();
                            DownloadState.AddNewTimeElapsed(sw.ElapsedMilliseconds);
                            downloadWorker.ReportProgress(i + 1, new DownloadState()
                            {
                                Current = i + 1, Total = recipe_ids.Count, Recipe = recipe
                            });
                        }
                        finally
                        {
                            sw.Reset();
                        }
                    }
                }
                catch (Exception)
                {
                    // Ignore exceptions at this point - failed to take from queue
                }
            };
            downloadWorker.ProgressChanged +=
                (object sender, ProgressChangedEventArgs e) =>
            {
                DownloadState state = (DownloadState)e.UserState;
                TimeSpan      t     = TimeSpan.FromMilliseconds(DownloadState.AverageMilliseconds * (state.Total - state.Current));
                this.pbDownloadIndicator.Value = state.Current;
                this.tbCurrentTask.Text        =
                    String.Format(
                        Properties.Resources.TextBoxCurrentDownloadInProgress,
                        state.Current,
                        state.Total,
                        state.Percent,
                        t.Minutes,
                        t.Seconds);
                // cache new recipe
                Data.Recipe recipe = Data.Cache.GetCache().AddRecipe(state.Recipe);
                // add to view
                this.RefreshDownloadResultsCount();
            };
            downloadWorker.RunWorkerCompleted +=
                (object sender, RunWorkerCompletedEventArgs e) =>
            {
                if (e.Error != null)
                {
                    this.tbCurrentTask.Text =
                        String.Format(Properties.Resources.TextBoxCurrentDownloadSomethingGoesWrong);
                }
                else
                {
                    if (recipe_ids == null)
                    {
                        this.tbCurrentTask.Text = Properties.Resources.SomethingIsWrongNoDownloadPossible;
                    }
                    else
                    {
                        if (recipe_ids != null && recipe_ids.Count == 0)
                        {
                            this.tbCurrentTask.Text = Properties.Resources.TextBoxCurrentDownloadNoNewRecipesFoundNotification;
                        }
                        else
                        {
                            this.tbCurrentTask.Text = Properties.Resources.TextBoxCurrentDownloadFinished;
                        }
                    }
                }
                this.pbDownloadIndicator.Visibility = System.Windows.Visibility.Collapsed;
            };
            downloadWorker.RunWorkerAsync();
        }
        public static API.ANet.Recipe GetANetRecipe(int recipe_id, string lang)
        {
            API.ANet.Recipe recipe = null;
            try
            {
                recipe = DataProvider <API.ANet.Recipe> .GetData(
                    String.Format("https://api.guildwars2.com/v1/recipe_details.json?recipe_id={0}&lang={1}", recipe_id, lang));
            }
            catch (Exception ex)
            {
                if (recipe == null)
                {
                    recipe           = new API.ANet.Recipe();
                    recipe.recipe_id = recipe_id.ToString();
                    recipe.error     = Config.DEFAULT_ERROR_CODE;
                    recipe.text      = ex.Message;
                }
            }
            if (recipe.error == null)
            {
                API.ANet.Item item           = null;
                string        output_item_id = recipe.output_item_id;
                try
                {
                    // get correspondig item
                    item = GetANetItem(int.Parse(output_item_id), lang);
#if DEBUG
                    if (!API.ANet.ItemType.Values.Contains(item.type))
                    {
                        Console.WriteLine("New item type \"{0}\": {1}", item.type, output_item_id);
                    }
#endif
                    foreach (API.ANet.Ingredient ingredient in recipe.ingredients)
                    {
                        API.ANet.Item item2 = null;
                        try
                        {
                            item2 = GetANetItem(int.Parse(ingredient.item_id), lang);
                        }
                        catch (Exception ex)
                        {
                            if (item2 == null)
                            {
                                item2         = new API.ANet.Item();
                                item2.item_id = ingredient.item_id;
                                item2.error   = Config.DEFAULT_ERROR_CODE;
                                item2.text    = ex.Message;
                            }
                        }
                        finally
                        {
                            if (item2 != null)
                            {
                                ingredient.Item = item2;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (item == null)
                    {
                        item         = new API.ANet.Item();
                        item.item_id = output_item_id;
                        item.error   = Config.DEFAULT_ERROR_CODE;
                        item.text    = ex.Message;
                    }
                }
                finally
                {
                    if (recipe != null && item != null)
                    {
                        recipe.Item = item;
                    }
                }
            }
            return(recipe);
        }