Skip to content

Commit

Permalink
feat: numeric formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ewrogers committed Jun 16, 2023
1 parent 4f5edc8 commit 73adc64
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this library will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.7.0] - Unreleased
## [4.7.0] - 2023-06-16

### Added

Expand All @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `Spell Rotation Mode` renamed `Default Spell Queue Rotation` to better describe it can be overriden
- `UserSettings` are now version `1.5`
- Now format health/mana using `k` and `m` suffixes for thousands/millions (ex: `256k`, `1.2m`)

### Fixed

Expand Down
1 change: 1 addition & 0 deletions SleepHunter/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<converters:GreaterThanOrEqualConverter x:Key="GreaterThanOrEqualConverter"/>
<converters:LessThanConverter x:Key="LessThanConverter"/>
<converters:LessThanOrEqualConverter x:Key="LessThanOrEqualConverter"/>
<converters:NumericConverter x:Key="NumericConverter"/>
<converters:PlayerClassConverter x:Key="PlayerClassConverter"/>
<converters:TimeSpanConverter x:Key="TimeSpanConverter"/>
<converters:VisibilityConverter x:Key="VisibilityConverter"/>
Expand Down
49 changes: 44 additions & 5 deletions SleepHunter/Converters/NumericConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,43 @@ namespace SleepHunter.Converters
{
public sealed class NumericConverter : IValueConverter
{
private const int ThousandsThreshold = 1_000;
private const int MillionsThreshold = 1_000_000;

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var isHexadecimal = string.Equals("Hexadecimal", parameter as string, StringComparison.OrdinalIgnoreCase);
var isThousands = string.Equals("Thousands", parameter as string, StringComparison.OrdinalIgnoreCase);

var integerValue = System.Convert.ToInt64(value, CultureInfo.InvariantCulture);

if (isHexadecimal)
{
var hexValue = (uint)value;
return hexValue.ToString("X");
return integerValue.ToString("X");
}

if (isThousands && integerValue > ThousandsThreshold)
{
if (integerValue >= MillionsThreshold)
{
var fractionalMillions = integerValue / (double)MillionsThreshold;
return $"{fractionalMillions:0.0}m";
}
else if (integerValue >= ThousandsThreshold)
{
var fractionalThousands = integerValue / (double)ThousandsThreshold;
return $"{fractionalThousands:0}k";
}
}

var decValue = (double)value;
return decValue.ToString();
return value.ToString();
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var isHexadecimal = string.Equals("Hexadecimal", parameter as string, StringComparison.OrdinalIgnoreCase);
var isThousands = string.Equals("Thousands", parameter as string, StringComparison.OrdinalIgnoreCase);

var valueString = value as string;

if (isHexadecimal)
Expand All @@ -33,11 +53,30 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
return hexValue;
}

if (isThousands)
{
if (valueString.EndsWith("m"))
{
var trimmedString = valueString.TrimEnd('m');
if (!double.TryParse(trimmedString, NumberStyles.Float, null, out var doubleValue))
return 0;
else
return (int)Math.Round(doubleValue * 1_000_000.0);
}
else if (valueString.EndsWith("k"))
{
var trimmedString = valueString.TrimEnd('m');
if (!double.TryParse(trimmedString, NumberStyles.Float, null, out var doubleValue))
return 0;
else
return (int)Math.Round(doubleValue * 1_000.0);
}
}

if (!double.TryParse(valueString, out var decValue))
return 0;
else
return decValue;

}
}
}
2 changes: 1 addition & 1 deletion SleepHunter/Macro/PlayerMacroState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ private bool SwitchToBestStaff(SpellQueueItem item, out int? numberOfLines, out
if (item == null)
throw new ArgumentNullException(nameof(item));

client.Update(PlayerFieldFlags.Inventory | PlayerFieldFlags.Equipment | PlayerFieldFlags.Stats);
client.Update( PlayerFieldFlags.Inventory | PlayerFieldFlags.Equipment | PlayerFieldFlags.Stats | PlayerFieldFlags.Spellbook);

var equippedStaff = client.Equipment.GetSlot(EquipmentSlot.Weapon);
var availableList = new List<string>(client.Inventory.ItemNames);
Expand Down
4 changes: 2 additions & 2 deletions SleepHunter/SleepHunter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/ewrogers/SleepHunter4</RepositoryUrl>
<PackageProjectUrl>https://github.com/ewrogers/SleepHunter4</PackageProjectUrl>
<AssemblyVersion>4.6.1.0</AssemblyVersion>
<AssemblyVersion>4.7.0.0</AssemblyVersion>
<Copyright>2023 Erik 'SiLo' Rogers</Copyright>
<Description>Dark Ages Automation Tool</Description>
<Title>SleepHunter</Title>
<FileVersion>4.6.1.0</FileVersion>
<FileVersion>4.7.0.0</FileVersion>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions SleepHunter/Templates/PlayerDataTemplate.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@
Visibility="{Binding ElementName=HealthBar, Path=Visibility}">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} / {1}">
<Binding Path="Stats.CurrentHealth"/>
<Binding Path="Stats.MaximumHealth"/>
<Binding Path="Stats.CurrentHealth" Converter="{StaticResource NumericConverter}" ConverterParameter="Thousands"/>
<Binding Path="Stats.MaximumHealth" Converter="{StaticResource NumericConverter}" ConverterParameter="Thousands"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Expand Down Expand Up @@ -200,8 +200,8 @@
Visibility="{Binding ElementName=ManaBar, Path=Visibility}">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} / {1}">
<Binding Path="Stats.CurrentMana"/>
<Binding Path="Stats.MaximumMana"/>
<Binding Path="Stats.CurrentMana" Converter="{StaticResource NumericConverter}" ConverterParameter="Thousands"/>
<Binding Path="Stats.MaximumMana" Converter="{StaticResource NumericConverter}" ConverterParameter="Thousands"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Expand Down

0 comments on commit 73adc64

Please sign in to comment.