Пример #1
0
        public bool ToggleAlert(ref Item item)
        {
            try
            {
                if (!IsAlertInCollection(item.UniqueName) && !IsSpaceInAlertsCollection())
                {
                    return(false);
                }

                if (IsAlertInCollection(item.UniqueName))
                {
                    DeactivateAlert(item.UniqueName);
                    return(false);
                }

                ActivateAlert(item.UniqueName, item.AlertModeMinSellPriceIsUndercutPrice);
                return(true);
            }
            catch (Exception e)
            {
                ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
                Log.Error(MethodBase.GetCurrentMethod().DeclaringType, e);
                return(false);
            }
        }
        public static async Task <List <GoldResponseModel> > GetGoldPricesFromJsonAsync(DateTime?dateTime, int count, int timeout = 30)
        {
            var checkedDateTime = dateTime != null?dateTime.ToString() : string.Empty;

            var url = $"{Settings.Default.GoldStatsApiUrl ?? Settings.Default.GoldStatsApiUrlDefault}?date={checkedDateTime}&count={count}";

            using (var client = new HttpClient())
            {
                client.Timeout = TimeSpan.FromSeconds(timeout);
                try
                {
                    using (var response = await client.GetAsync(url))
                    {
                        using (var content = response.Content)
                        {
                            var contentString = await content.ReadAsStringAsync();

                            return(string.IsNullOrEmpty(contentString) ? new List <GoldResponseModel>() : JsonConvert.DeserializeObject <List <GoldResponseModel> >(contentString));
                        }
                    }
                }
                catch (Exception e)
                {
                    ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
                    Log.Error(MethodBase.GetCurrentMethod().DeclaringType, e);
                    return(new List <GoldResponseModel>());
                }
            }
        }
        public static BitmapImage SetImage(string webPath, int pixelHeight, int pixelWidth, bool freeze)
        {
            if (webPath == null)
            {
                return(null);
            }

            try
            {
                var request   = WebRequest.Create(new Uri(webPath));
                var userImage = new BitmapImage();
                userImage.BeginInit();
                userImage.CacheOption       = BitmapCacheOption.OnDemand;
                userImage.DecodePixelHeight = pixelHeight;
                userImage.DecodePixelWidth  = pixelWidth;
                userImage.UriSource         = request.RequestUri;
                userImage.EndInit();
                if (freeze)
                {
                    userImage.Freeze();
                }

                return(userImage);
            }
            catch (Exception e)
            {
                ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
                Log.Error($"{MethodBase.GetCurrentMethod().DeclaringType} - SetImage: {e.Message}");
                return(null);
            }
        }
        public static async Task <GameInfoGuildsResponse> GetGameInfoGuildsFromJsonAsync(string guildId)
        {
            var url = $"https://gameinfo.albiononline.com/api/gameinfo/guilds/{guildId}";

            using (var client = new HttpClient())
            {
                client.Timeout = TimeSpan.FromSeconds(30);
                try
                {
                    using (var response = await client.GetAsync(url))
                    {
                        using (var content = response.Content)
                        {
                            return(JsonConvert.DeserializeObject <GameInfoGuildsResponse>(await content.ReadAsStringAsync()));
                        }
                    }
                }
                catch (Exception e)
                {
                    ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
                    Log.Error(MethodBase.GetCurrentMethod().DeclaringType, e);
                    return(null);
                }
            }
        }
        public static BitmapImage GetImageFromResource(string resourcePath, int pixelHeight, int pixelWidth, bool freeze)
        {
            try
            {
                var bmp = new BitmapImage();
                bmp.BeginInit();
                bmp.CacheOption       = BitmapCacheOption.OnDemand;
                bmp.DecodePixelHeight = pixelHeight;
                bmp.DecodePixelWidth  = pixelWidth;
                bmp.UriSource         = new Uri(resourcePath);
                bmp.EndInit();
                if (freeze)
                {
                    bmp.Freeze();
                }

                return(bmp);
            }
            catch (Exception e)
            {
                ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
                Log.Error($"{MethodBase.GetCurrentMethod().DeclaringType} - SetImage: {e.Message}");
                return(null);
            }
        }
Пример #6
0
        public static void SetFavoriteItemsFromLocalFile()
        {
            var localFilePath = $"{AppDomain.CurrentDomain.BaseDirectory}{Settings.Default.FavoriteItemsFileName}";

            if (File.Exists(localFilePath))
            {
                try
                {
                    var localItemString = File.ReadAllText(localFilePath, Encoding.UTF8);
                    foreach (var uniqueName in JsonConvert.DeserializeObject <List <string> >(localItemString))
                    {
                        var item = Items.FirstOrDefault(i => i.UniqueName == uniqueName);
                        if (item != null)
                        {
                            item.IsFavorite = true;
                        }
                    }
                }
                catch (Exception e)
                {
                    ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
                    Log.Error(MethodBase.GetCurrentMethod().Name, e);
                }
            }
        }
        public static async Task <List <MarketHistoriesResponse> > GetHistoryItemPricesFromJsonAsync(string uniqueName, IList <string> locations,
                                                                                                     DateTime?date, IList <int> qualities, int timeScale = 24)
        {
            var locationsString = "";
            var qualitiesString = "";

            if (locations?.Count > 0)
            {
                locationsString = string.Join(",", locations);
            }

            if (qualities?.Count > 0)
            {
                qualitiesString = string.Join(",", qualities);
            }

            var url = Settings.Default.CityPricesHistoryApiUrl ?? Settings.Default.CityPricesHistoryApiUrlDefault;

            url += uniqueName;
            url += $"?locations={locationsString}";
            url += $"&date={date:M-d-yy}";
            url += $"&qualities={qualitiesString}";
            url += $"&time-scale={timeScale}";

            using (var client = new HttpClient())
            {
                client.Timeout = TimeSpan.FromSeconds(30);
                try
                {
                    using (var response = await client.GetAsync(url))
                    {
                        using (var content = response.Content)
                        {
                            if (response.StatusCode == (HttpStatusCode)429)
                            {
                                throw new TooManyRequestsException();
                            }

                            return(JsonConvert.DeserializeObject <List <MarketHistoriesResponse> >(await content.ReadAsStringAsync()));
                        }
                    }
                }
                catch (TooManyRequestsException)
                {
                    ConsoleManager.WriteLineForWarning(MethodBase.GetCurrentMethod().DeclaringType, new TooManyRequestsException());
                    throw new TooManyRequestsException();
                }
                catch (Exception e)
                {
                    ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
                    Log.Error(MethodBase.GetCurrentMethod().DeclaringType, e);
                    return(null);
                }
            }
        }
        public static async Task <ItemInformation> GetItemInfoFromJsonAsync(string uniqueName)
        {
            var url = $"https://gameinfo.albiononline.com/api/gameinfo/items/{uniqueName}/data";

            using (var client = new HttpClient())
            {
                client.Timeout = TimeSpan.FromSeconds(60);
                try
                {
                    using (var response = await client.GetAsync(url))
                    {
                        using (var content = response.Content)
                        {
                            var emptyItemInfo = new ItemInformation
                            {
                                UniqueName = uniqueName,
                                LastUpdate = DateTime.Now
                            };

                            if (response.StatusCode == HttpStatusCode.NotFound)
                            {
                                emptyItemInfo.HttpStatus = HttpStatusCode.NotFound;
                                return(emptyItemInfo);
                            }

                            if (response.IsSuccessStatusCode)
                            {
                                var itemInfo = JsonConvert.DeserializeObject <ItemInformation>(await content.ReadAsStringAsync());
                                itemInfo.HttpStatus = HttpStatusCode.OK;
                                return(itemInfo);
                            }

                            emptyItemInfo.HttpStatus = response.StatusCode;
                            return(emptyItemInfo);
                        }
                    }
                }
                catch (TaskCanceledException ex)
                {
                    ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, ex);
                    Log.Error(MethodBase.GetCurrentMethod().DeclaringType, ex);
                    return(null);
                }
                catch (Exception e)
                {
                    ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
                    Log.Error(MethodBase.GetCurrentMethod().DeclaringType, e);
                    return(null);
                }
            }
        }
 private static string GetCurrentSound()
 {
     try
     {
         var currentSound = AlertSounds.FirstOrDefault(s => s.FileName == Settings.Default.SelectedAlertSound);
         return(currentSound?.FilePath ?? string.Empty);
     }
     catch (Exception e) when(e is ArgumentException)
     {
         ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
         Log.Error(MethodBase.GetCurrentMethod().DeclaringType, e);
         return(string.Empty);
     }
 }
        /// <summary>
        ///     Returns city item prices bye uniqueName, locations and qualities.
        /// </summary>
        /// <exception cref="TooManyRequestsException"></exception>
        public static async Task <List <MarketResponse> > GetCityItemPricesFromJsonAsync(string uniqueName, List <string> locations, List <int> qualities)
        {
            var url = Settings.Default.CityPricesApiUrl ?? Settings.Default.CityPricesApiUrlDefault;

            url += uniqueName;

            if (locations?.Count >= 1)
            {
                url += "?locations=";
                url  = locations.Aggregate(url, (current, location) => current + $"{location},");
            }

            if (qualities?.Count >= 1)
            {
                url += "&qualities=";
                url  = qualities.Aggregate(url, (current, quality) => current + $"{quality},");
            }

            using (var client = new HttpClient())
            {
                try
                {
                    client.Timeout = TimeSpan.FromSeconds(30);

                    using (var response = await client.GetAsync(url))
                    {
                        if (response.StatusCode == (HttpStatusCode)429)
                        {
                            throw new TooManyRequestsException();
                        }

                        using (var content = response.Content)
                        {
                            return(JsonConvert.DeserializeObject <List <MarketResponse> >(await content.ReadAsStringAsync()));
                        }
                    }
                }
                catch (TooManyRequestsException)
                {
                    ConsoleManager.WriteLineForWarning(MethodBase.GetCurrentMethod().DeclaringType, new TooManyRequestsException());
                    throw new TooManyRequestsException();
                }
                catch (Exception e)
                {
                    ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
                    Log.Error(MethodBase.GetCurrentMethod().DeclaringType, e);
                    return(null);
                }
            }
        }
 public static void PlayAlertSound()
 {
     try
     {
         var player = new SoundPlayer(GetCurrentSound());
         player.Load();
         player.Play();
         player.Dispose();
     }
     catch (Exception e) when(e is InvalidOperationException || e is UriFormatException || e is FileNotFoundException ||
                              e is ArgumentException)
     {
         ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
         Log.Error(MethodBase.GetCurrentMethod().DeclaringType, e);
     }
 }
Пример #12
0
        public static void SaveFavoriteItemsToLocalFile()
        {
            var localFilePath       = $"{AppDomain.CurrentDomain.BaseDirectory}{Settings.Default.FavoriteItemsFileName}";
            var favoriteItems       = Items.Where(x => x.IsFavorite);
            var toSaveFavoriteItems = favoriteItems.Select(x => x.UniqueName);
            var fileString          = JsonConvert.SerializeObject(toSaveFavoriteItems);

            try
            {
                File.WriteAllText(localFilePath, fileString, Encoding.UTF8);
            }
            catch (Exception e)
            {
                ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
                Log.Error(MethodBase.GetCurrentMethod().Name, e);
            }
        }
Пример #13
0
        private void SaveActiveAlertsToLocalFile()
        {
            var localFilePath    = $"{AppDomain.CurrentDomain.BaseDirectory}{Settings.Default.ActiveAlertsFileName}";
            var activeItemAlerts = _alerts.Select(alert => new AlertSaveObject
            {
                UniqueName = alert.Item.UniqueName, MinSellUndercutPrice = alert.AlertModeMinSellPriceIsUndercutPrice
            }).ToList();
            var fileString = JsonConvert.SerializeObject(activeItemAlerts);

            try
            {
                File.WriteAllText(localFilePath, fileString, Encoding.UTF8);
            }
            catch (Exception e)
            {
                ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
                Log.Error(MethodBase.GetCurrentMethod().DeclaringType, e);
            }
        }
Пример #14
0
        private void SetActiveAlertsFromLocalFile()
        {
            var localFilePath = $"{AppDomain.CurrentDomain.BaseDirectory}{Settings.Default.ActiveAlertsFileName}";

            if (File.Exists(localFilePath))
            {
                try
                {
                    var localItemString = File.ReadAllText(localFilePath, Encoding.UTF8);

                    foreach (var alert in JsonConvert.DeserializeObject <List <AlertSaveObject> >(localItemString))
                    {
                        ActivateAlert(alert.UniqueName, alert.MinSellUndercutPrice);
                    }
                }
                catch (Exception e)
                {
                    ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
                    Log.Error(MethodBase.GetCurrentMethod().DeclaringType, e);
                }
            }
        }
Пример #15
0
        public void DeactivateAlert(string uniqueName)
        {
            try
            {
                var itemCollection = (ObservableCollection <Item>)_itemsView.SourceCollection;
                var item           = itemCollection.FirstOrDefault(i => i.UniqueName == uniqueName);

                if (item == null)
                {
                    return;
                }

                item.IsAlertActive = false;
                Remove(item.UniqueName);

                _mainWindow.Dispatcher?.Invoke(() => { _itemsView.Refresh(); });
            }
            catch (Exception e)
            {
                ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
                Log.Error(MethodBase.GetCurrentMethod().DeclaringType, e);
            }
        }
Пример #16
0
        private void ActivateAlert(string uniqueName, int minSellUndercutPrice)
        {
            try
            {
                var itemCollection = (ObservableCollection <Item>)_itemsView.SourceCollection;
                var item           = itemCollection.FirstOrDefault(i => i.UniqueName == uniqueName);

                if (item == null)
                {
                    return;
                }

                item.IsAlertActive = true;
                item.AlertModeMinSellPriceIsUndercutPrice = minSellUndercutPrice;
                Add(item, item.AlertModeMinSellPriceIsUndercutPrice);

                _mainWindow.Dispatcher?.Invoke(() => { _itemsView.Refresh(); });
            }
            catch (Exception e)
            {
                ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod().DeclaringType, e);
                Log.Error(MethodBase.GetCurrentMethod().DeclaringType, e);
            }
        }