Skip to content

Commit

Permalink
Added price sorting and fixed RPC
Browse files Browse the repository at this point in the history
- Discord RPC now changes correctly according to current language and state
- Before search starts, skins are sorted by price to get more relevant crafts
  • Loading branch information
Prevter committed Aug 9, 2022
1 parent 020fa75 commit 800d23f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 23 deletions.
51 changes: 51 additions & 0 deletions FloatTool/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace FloatTool
{
Expand Down Expand Up @@ -151,6 +152,21 @@ public static string ShortCpuName(string cpu)

return cpu.Trim();
}

public static string EscapeLocalization(string input)
{
string regex = @"%(m_[^%]{1,})%";
var matches = Regex.Matches(input, regex);

foreach (Match m in matches.Cast<Match>())
{
string key = m.Groups[1].Value;
string localization = Application.Current.Resources[key] as string;
input = input.Replace(m.Value, localization);
}

return input;
}
}

public class UpdateResult
Expand Down Expand Up @@ -210,4 +226,39 @@ public bool IsOverlapped(FloatRange other)
public float Min { get { return min; } }
public float Max { get { return max; } }
}

/// <summary>
/// Used to store current Discord Presense and update language if needed
/// </summary>
public class RPCSettingsPersist
{
public string Details { get; set; }
public string State { get; set; }

public DiscordRPC.Timestamps Timestamp { get; set; }
public bool ShowTime { get; set; }

public DiscordRPC.RichPresence GetPresense()
{
string details = Utils.EscapeLocalization(Details);
string state = Utils.EscapeLocalization(State);

var rpc = new DiscordRPC.RichPresence()
{
Details = details,
State = state,
Assets = new DiscordRPC.Assets()
{
LargeImageKey = "icon_new",
LargeImageText = $"FloatTool {AppHelpers.VersionCode}",
},
};

if (ShowTime)
rpc.Timestamps = Timestamp;

return rpc;
}
}

}
48 changes: 25 additions & 23 deletions FloatTool/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,26 @@ public partial class MainWindow : Window
{
public MainViewModel ViewModel;
public Settings Settings;
private RPCSettingsPersist RPCSettings = new();

private static long PassedCombinations;
private static List<Task> ThreadPool;
private static CancellationTokenSource TokenSource = new();
public CancellationToken CancellationToken;
private static SoundPlayer CombinationFoundSound;

public void UpdateRichPresence()
public void UpdateRichPresence(bool clear=false)
{
if (clear)
{
RPCSettings.Details = "%m_SettingUpSearch%";
RPCSettings.State = "";
RPCSettings.ShowTime = false;
}

if (Settings.DiscordRPC)
{
AppHelpers.DiscordClient.SetPresence(new DiscordRPC.RichPresence()
{
Details = Application.Current.Resources["m_SettingUpSearch"] as string,
Assets = new DiscordRPC.Assets()
{
LargeImageKey = "icon_new",
LargeImageText = $"FloatTool {AppHelpers.VersionCode}",
}
});
AppHelpers.DiscordClient.SetPresence(RPCSettings.GetPresense());
}
}

Expand All @@ -85,7 +86,7 @@ public MainWindow()
MaxHeight = SystemParameters.WorkArea.Height + 12;
MaxWidth = SystemParameters.WorkArea.Width + 12;

UpdateRichPresence();
UpdateRichPresence(true);
DataContext = ViewModel;

Logger.Log.Info("Main window started");
Expand Down Expand Up @@ -167,7 +168,7 @@ private void WindowButton_Click(object sender, RoutedEventArgs e)
break;
}

// Update RPC after closing any window
// This will return rich presense to last state and update the language
UpdateRichPresence();
}

Expand Down Expand Up @@ -262,17 +263,12 @@ private void StartSearchButton_Click(object sender, RoutedEventArgs e)

if (Settings.DiscordRPC)
{
AppHelpers.DiscordClient.SetPresence(new DiscordRPC.RichPresence()
{
Details = $"{Application.Current.Resources["m_Searching"] as string} {ViewModel.FullSkinName}",
State = $"{Application.Current.Resources["m_DesiredFloat"] as string} {ViewModel.SearchFilter}",
Assets = new DiscordRPC.Assets()
{
LargeImageKey = "icon_new",
LargeImageText = $"FloatTool {AppHelpers.VersionCode}",
},
Timestamps = DiscordRPC.Timestamps.Now,
});
RPCSettings.Details = $"%m_Searching% {ViewModel.FullSkinName}";
RPCSettings.Details = $"%m_DesiredFloat% {ViewModel.SearchFilter}";
RPCSettings.Timestamp = DiscordRPC.Timestamps.Now;
RPCSettings.ShowTime = true;

UpdateRichPresence();
}

new Thread(() =>
Expand Down Expand Up @@ -396,6 +392,10 @@ private void StartSearchButton_Click(object sender, RoutedEventArgs e)
}

List<InputSkin> inputSkinList = inputSkinBag.ToList();

// sort skins by price in ascending order
inputSkinList.Sort((a, b) => a.Price.CompareTo(b.Price));

if (ViewModel.Sort)
{
if (ViewModel.SortDescending)
Expand Down Expand Up @@ -487,6 +487,7 @@ private void StartSearchButton_Click(object sender, RoutedEventArgs e)
Thread.Sleep(100);
}

UpdateRichPresence(true);
Logger.Log.Info("Finished searching");
Dispatcher.Invoke(
new Action(() =>
Expand All @@ -503,6 +504,7 @@ private void StartSearchButton_Click(object sender, RoutedEventArgs e)
{
Logger.Log.Info("Canceling task");
TokenSource.Cancel();
UpdateRichPresence(true);
}
}
}
Expand Down

0 comments on commit 800d23f

Please sign in to comment.