Skip to content

Commit

Permalink
fixes card sorting issues (HearthSim#1406)
Browse files Browse the repository at this point in the history
  • Loading branch information
Epix37 authored and chucklu committed Sep 10, 2015
1 parent 8e239e5 commit 981a223
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 40 deletions.
2 changes: 0 additions & 2 deletions Hearthstone Deck Tracker/Hearthstone/GameV2.cs
Expand Up @@ -108,8 +108,6 @@ public void SetPremadeDeck(Deck deck)
for(var i = 0; i < card.Count; i++)
Player.RevealDeckCard(card.Id, -1);
}
Helper.UpdatePlayerCards();
Helper.UpdateOpponentCards();
IsUsingPremade = true;
}

Expand Down
42 changes: 23 additions & 19 deletions Hearthstone Deck Tracker/Hearthstone/Player.cs
Expand Up @@ -59,7 +59,7 @@ public List<Card> DrawnCards

public const int DeckSize = 30;

private readonly Queue<string> _hightlightedCards = new Queue<string>();
private readonly Queue<string> _hightlightedCards = new Queue<string>();

public List<Card> DisplayCards
{
Expand All @@ -75,22 +75,18 @@ public List<Card> DisplayCards
}).ToList() : new List<Card>();

if(DeckList.Instance.ActiveDeck == null)
return DrawnCards.Concat(createdInHand).ToList();
return DrawnCards.Concat(createdInHand).ToSortedCardList();

var stillInDeck =
Deck.Where(ce => !string.IsNullOrEmpty(ce.CardId))
.GroupBy(ce => new {ce.CardId, ce.CardMark, ce.Discarded})
.Select(
g =>
new Card()
{
Id = g.Key.CardId,
Count = g.Count(),
IsCreated = g.Key.CardMark == CardMark.Created,
HighlightDraw = _hightlightedCards.Contains(g.Key.CardId),
HighlightInHand = Hand.Any(ce => ce.CardId == g.Key.CardId)
})
.ToList();
Deck.Where(ce => !string.IsNullOrEmpty(ce.CardId)).GroupBy(ce => new {ce.CardId, ce.CardMark, ce.Discarded}).Select(g =>
{
var card = Database.GetCardFromId(g.Key.CardId);
card.Count = g.Count();
card.IsCreated = g.Key.CardMark == CardMark.Created;
card.HighlightDraw = _hightlightedCards.Contains(g.Key.CardId);
card.HighlightInHand = Hand.Any(ce => ce.CardId == g.Key.CardId);
return card;
}).ToList();
if(Config.Instance.RemoveCardsFromDeck)
{
if(Config.Instance.HighlightLastDrawn)
Expand Down Expand Up @@ -120,7 +116,7 @@ public List<Card> DisplayCards
;
stillInDeck = stillInDeck.Concat(inHand).ToList();
}
return stillInDeck.Concat(createdInHand).ToList();
return stillInDeck.Concat(createdInHand).ToSortedCardList();
}
var notInDeck = DeckList.Instance.ActiveDeckVersion.Cards.Where(c => Deck.All(ce => ce.CardId != c.Id)).Select(c =>
{
Expand All @@ -130,7 +126,7 @@ public List<Card> DisplayCards
card.HighlightInHand = Hand.Any(ce => ce.CardId == c.Id);
return card;
});
return stillInDeck.Concat(notInDeck).Concat(createdInHand).ToList();
return stillInDeck.Concat(notInDeck).Concat(createdInHand).ToSortedCardList();
}
}

Expand All @@ -142,8 +138,16 @@ public List<Card> DisplayRevealedCards
return
RevealedCards.Where(ce => !string.IsNullOrEmpty(ce.CardId))
.GroupBy(ce => new {ce.CardId, Hidden = (ce.InHand || ce.InDeck), ce.Created, Discarded = ce.Discarded && Config.Instance.HighlightDiscarded})
.Select(g => new Card() {Id = g.Key.CardId, Count = g.Count(), Jousted = g.Key.Hidden, IsCreated = g.Key.Created, WasDiscarded = g.Key.Discarded })
.ToList();
.Select(g =>
{
var card = Database.GetCardFromId(g.Key.CardId);
card.Count = g.Count();
card.Jousted = g.Key.Hidden;
card.IsCreated = g.Key.Created;
card.WasDiscarded = g.Key.Discarded;
return card;
})
.ToSortedCardList();
}
}

Expand Down
1 change: 0 additions & 1 deletion Hearthstone Deck Tracker/MainWindow/MainWindow.xaml.cs
Expand Up @@ -65,7 +65,6 @@ public void UseDeck(Deck selected)
//needs to be true for automatic deck detection to work
HsLogReaderV2.Instance.Reset(true);
Overlay.Update(false);
Overlay.SortViews();
Overlay.UpdatePlayerCards();
PlayerWindow.UpdatePlayerCards();
}
Expand Down
6 changes: 6 additions & 0 deletions Hearthstone Deck Tracker/Utility/Helper.cs
Expand Up @@ -24,6 +24,7 @@
using Hearthstone_Deck_Tracker.Hearthstone;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.Win32;
using Card = Hearthstone_Deck_Tracker.Hearthstone.Card;
using Color = System.Drawing.Color;
using PixelFormat = System.Drawing.Imaging.PixelFormat;
using Point = System.Drawing.Point;
Expand Down Expand Up @@ -221,6 +222,11 @@ public static void SortCardCollection(IEnumerable collection, bool classFirst)
view1.SortDescriptions.Add(new SortDescription("LocalizedName", ListSortDirection.Ascending));
}

public static List<Card> ToSortedCardList(this IEnumerable<Card> cards)
{
return cards.OrderBy(x => x.Cost).ThenByDescending(x => x.Type).ThenBy(x => x.LocalizedName).ToArray().ToList();
}

public static string DeckToIdString(Deck deck)
{
return deck.GetSelectedDeckVersion().Cards.Aggregate("", (current, card) => current + (card.Id + ":" + card.Count + ";"));
Expand Down
1 change: 0 additions & 1 deletion Hearthstone Deck Tracker/Windows/OpponentWindow.xaml.cs
Expand Up @@ -222,7 +222,6 @@ public async void UpdateOpponentCards()
if((DateTime.Now - _lastOpponentUpdateReqest).Milliseconds < 50)
return;
OnPropertyChanged("OpponentDeck");
Helper.SortCardCollection(ListViewOpponent.ItemsSource, false);
}

public event PropertyChangedEventHandler PropertyChanged;
Expand Down
14 changes: 0 additions & 14 deletions Hearthstone Deck Tracker/Windows/OverlayWindow.xaml.cs
Expand Up @@ -406,17 +406,8 @@ private void GrayOutSecrets(Point mousePos)
ShowSecrets();
}

public void SortViews()
{
Helper.SortCardCollection(ListViewPlayer.ItemsSource, Config.Instance.CardSortingClassFirst);
Helper.SortCardCollection(ListViewOpponent.ItemsSource, Config.Instance.CardSortingClassFirst);
}

private void SetOpponentCardCount(int cardCount, int cardsLeftInDeck)
{
//previous cardcout > current -> opponent played -> resort list
if (_opponentCardCount > cardCount)
Helper.SortCardCollection(ListViewOpponent.ItemsSource, Config.Instance.CardSortingClassFirst);
_opponentCardCount = cardCount;

LblOpponentCardCount.Text = cardCount.ToString();
Expand Down Expand Up @@ -449,9 +440,6 @@ private void SetOpponentCardCount(int cardCount, int cardsLeftInDeck)

private void SetCardCount(int cardCount, int cardsLeftInDeck)
{
//previous < current -> draw
if (_cardCount < cardCount)
Helper.SortCardCollection(ListViewPlayer.ItemsSource, Config.Instance.CardSortingClassFirst);
_cardCount = cardCount;

LblCardCount.Text = cardCount.ToString();
Expand Down Expand Up @@ -1300,7 +1288,6 @@ public async void UpdatePlayerCards()
if((DateTime.Now - _lastPlayerUpdateReqest).Milliseconds < 50)
return;
OnPropertyChanged("PlayerDeck");
Helper.SortCardCollection(ListViewPlayer.ItemsSource, false);
}

private DateTime _lastOpponentUpdateReqest = DateTime.MinValue;
Expand All @@ -1311,7 +1298,6 @@ public async void UpdateOpponentCards()
if((DateTime.Now - _lastOpponentUpdateReqest).Milliseconds < 50)
return;
OnPropertyChanged("OpponentDeck");
Helper.SortCardCollection(ListViewOpponent.ItemsSource, false);
}

public event PropertyChangedEventHandler PropertyChanged;
Expand Down
5 changes: 2 additions & 3 deletions Hearthstone Deck Tracker/Windows/PlayerWindow.xaml.cs
Expand Up @@ -234,11 +234,10 @@ public void SetTextLocation(bool top)
public async void UpdatePlayerCards()
{
_lastPlayerUpdateReqest = DateTime.Now;
await Task.Delay(50);
if((DateTime.Now - _lastPlayerUpdateReqest).Milliseconds < 50)
await Task.Delay(100);
if((DateTime.Now - _lastPlayerUpdateReqest).Milliseconds < 100)
return;
OnPropertyChanged("PlayerDeck");
Helper.SortCardCollection(ListViewPlayer.ItemsSource, false);
}

public event PropertyChangedEventHandler PropertyChanged;
Expand Down

0 comments on commit 981a223

Please sign in to comment.