Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Triky313 committed Mar 26, 2021
1 parent 3f84386 commit fedc158
Show file tree
Hide file tree
Showing 128 changed files with 3,944 additions and 4,298 deletions.
4 changes: 2 additions & 2 deletions StatisticsAnalysisTool/StatisticsAnalysisTool/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Main.xaml"/>
<ResourceDictionary Source="Tracking.xaml"/>
<ResourceDictionary Source="Main.xaml" />
<ResourceDictionary Source="Tracking.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Expand Down
4 changes: 2 additions & 2 deletions StatisticsAnalysisTool/StatisticsAnalysisTool/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace StatisticsAnalysisTool
{
/// <summary>
/// Interaktionslogik für "App.xaml"
/// Interaktionslogik für "App.xaml"
/// </summary>
public partial class App : Application
{
}
}
}
69 changes: 34 additions & 35 deletions StatisticsAnalysisTool/StatisticsAnalysisTool/Common/Alert.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using log4net;
using StatisticsAnalysisTool.Annotations;
using StatisticsAnalysisTool.Exceptions;
using StatisticsAnalysisTool.Models;
using StatisticsAnalysisTool.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using log4net;
using StatisticsAnalysisTool.Annotations;
using StatisticsAnalysisTool.Exceptions;
using StatisticsAnalysisTool.Models;
using StatisticsAnalysisTool.Views;

namespace StatisticsAnalysisTool.Common
{
public class Alert
{
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private readonly MainWindow _mainWindow;
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private AlertController AlertController { get; }
private Item _item;
private int _alertModeMinSellPriceIsUndercutPrice;
private bool _isEventActive;
private Item _item;

public Alert(MainWindow mainWindow, AlertController alertController, Item item, int alertModeMinSellPriceIsUndercutPrice)
{
Expand All @@ -29,13 +29,32 @@ public Alert(MainWindow mainWindow, AlertController alertController, Item item,
AlertModeMinSellPriceIsUndercutPrice = alertModeMinSellPriceIsUndercutPrice;
}

public void StartEvent()
private AlertController AlertController { get; }

public Item Item
{
get => _item;
set
{
_item = value;
OnPropertyChanged();
}
}

public int AlertModeMinSellPriceIsUndercutPrice
{
if (_isEventActive)
get => _alertModeMinSellPriceIsUndercutPrice;
set
{
return;
_alertModeMinSellPriceIsUndercutPrice = value;
OnPropertyChanged();
}

}

public void StartEvent()
{
if (_isEventActive) return;

_isEventActive = true;
AlertEventAsync(_item.UniqueName);
}
Expand All @@ -49,16 +68,14 @@ public void StopEvent()
private async void AlertEventAsync(string uniqueName)
{
while (_isEventActive)
{
try
{
var cityPrices = await ApiController.GetCityItemPricesFromJsonAsync(uniqueName, null, null).ConfigureAwait(false);

foreach (var marketResponse in cityPrices ?? new List<MarketResponse>())
{
if (Locations.GetName(marketResponse.City) != Location.BlackMarket
&& marketResponse.SellPriceMinDate >= DateTime.UtcNow.AddMinutes(-5)
&& marketResponse.SellPriceMin <= (ulong)AlertModeMinSellPriceIsUndercutPrice
if (Locations.GetName(marketResponse.City) != Location.BlackMarket
&& marketResponse.SellPriceMinDate >= DateTime.UtcNow.AddMinutes(-5)
&& marketResponse.SellPriceMin <= (ulong) AlertModeMinSellPriceIsUndercutPrice
&& AlertModeMinSellPriceIsUndercutPrice > 0)
{
SoundController.PlayAlertSound();
Expand All @@ -73,7 +90,6 @@ private async void AlertEventAsync(string uniqueName)

break;
}
}

await Task.Delay(25000);
}
Expand All @@ -86,23 +102,6 @@ private async void AlertEventAsync(string uniqueName)
Log.Warn(nameof(AlertEventAsync), e);
return;
}
}
}

public Item Item {
get => _item;
set {
_item = value;
OnPropertyChanged();
}
}

public int AlertModeMinSellPriceIsUndercutPrice {
get => _alertModeMinSellPriceIsUndercutPrice;
set {
_alertModeMinSellPriceIsUndercutPrice = value;
OnPropertyChanged();
}
}

public event PropertyChangedEventHandler PropertyChanged;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using log4net;
using Newtonsoft.Json;
using StatisticsAnalysisTool.Models;
using StatisticsAnalysisTool.Properties;
using StatisticsAnalysisTool.Views;
using System;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
Expand All @@ -12,15 +7,20 @@
using System.Linq;
using System.Reflection;
using System.Text;
using log4net;
using Newtonsoft.Json;
using StatisticsAnalysisTool.Models;
using StatisticsAnalysisTool.Properties;
using StatisticsAnalysisTool.Views;

namespace StatisticsAnalysisTool.Common
{
public class AlertController
{
private readonly MainWindow _mainWindow;
private readonly ICollectionView _itemsView;
private readonly ObservableCollection<Alert> _alerts = new ObservableCollection<Alert>();
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private readonly ObservableCollection<Alert> _alerts = new ObservableCollection<Alert>();
private readonly ICollectionView _itemsView;
private readonly MainWindow _mainWindow;

private readonly int _maxAlertsAtSameTime = 10;

Expand All @@ -34,17 +34,11 @@ public AlertController(MainWindow mainWindow, ICollectionView itemsView)

private void Add(Item item, int alertModeMinSellPriceIsUndercutPrice)
{
if (IsAlertInCollection(item.UniqueName) || !IsSpaceInAlertsCollection())
{
return;
}
if (IsAlertInCollection(item.UniqueName) || !IsSpaceInAlertsCollection()) return;

_alerts.CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e)
_alerts.CollectionChanged += delegate(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
SaveActiveAlertsToLocalFile();
}
if (e.Action == NotifyCollectionChangedAction.Add) SaveActiveAlertsToLocalFile();
};

var alertController = this;
Expand All @@ -55,12 +49,9 @@ private void Add(Item item, int alertModeMinSellPriceIsUndercutPrice)

private void Remove(string uniqueName)
{
_alerts.CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e)
_alerts.CollectionChanged += delegate(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Remove)
{
SaveActiveAlertsToLocalFile();
}
if (e.Action == NotifyCollectionChangedAction.Remove) SaveActiveAlertsToLocalFile();
};

var alert = GetAlertByUniqueName(uniqueName);
Expand All @@ -75,10 +66,7 @@ public bool ToggleAlert(ref Item item)
{
try
{
if (!IsAlertInCollection(item.UniqueName) && !IsSpaceInAlertsCollection())
{
return false;
}
if (!IsAlertInCollection(item.UniqueName) && !IsSpaceInAlertsCollection()) return false;

if (IsAlertInCollection(item.UniqueName))
{
Expand All @@ -100,21 +88,15 @@ public void DeactivateAlert(string uniqueName)
{
try
{
var itemCollection = (ObservableCollection<Item>)_itemsView.SourceCollection;
var itemCollection = (ObservableCollection<Item>) _itemsView.SourceCollection;
var item = itemCollection.FirstOrDefault(i => i.UniqueName == uniqueName);

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

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

_mainWindow.Dispatcher?.Invoke(() =>
{
_itemsView.Refresh();
});
_mainWindow.Dispatcher?.Invoke(() => { _itemsView.Refresh(); });
}
catch (Exception e)
{
Expand All @@ -126,37 +108,37 @@ private void ActivateAlert(string uniqueName, int minSellUndercutPrice)
{
try
{
var itemCollection = (ObservableCollection<Item>)_itemsView.SourceCollection;
var itemCollection = (ObservableCollection<Item>) _itemsView.SourceCollection;
var item = itemCollection.FirstOrDefault(i => i.UniqueName == uniqueName);

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

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

_mainWindow.Dispatcher?.Invoke(() =>
{
_itemsView.Refresh();
});
_mainWindow.Dispatcher?.Invoke(() => { _itemsView.Refresh(); });
}
catch (Exception e)
{
Log.Error(nameof(DeactivateAlert), e);
}
}

private bool IsAlertInCollection(string uniqueName) => _alerts.Any(alert => alert.Item.UniqueName == uniqueName);
private bool IsAlertInCollection(string uniqueName)
{
return _alerts.Any(alert => alert.Item.UniqueName == uniqueName);
}

private Alert GetAlertByUniqueName(string uniqueName)
{
return _alerts.FirstOrDefault(alert => alert.Item.UniqueName == uniqueName);
}

public bool IsSpaceInAlertsCollection() => _alerts.Count < _maxAlertsAtSameTime;
public bool IsSpaceInAlertsCollection()
{
return _alerts.Count < _maxAlertsAtSameTime;
}

#region Alert file controls

Expand All @@ -165,27 +147,24 @@ 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)
{
Log.Error(nameof(SetActiveAlertsFromLocalFile), e);
}
}
}

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 activeItemAlerts = _alerts.Select(alert => new AlertSaveObject
{UniqueName = alert.Item.UniqueName, MinSellUndercutPrice = alert.AlertModeMinSellPriceIsUndercutPrice}).ToList();
var fileString = JsonConvert.SerializeObject(activeItemAlerts);

try
Expand Down
Loading

0 comments on commit fedc158

Please sign in to comment.