From 6c811a3c1bd9f266cc27b91f5b438933c96cada7 Mon Sep 17 00:00:00 2001 From: Kalakoi Date: Wed, 18 Jun 2025 14:13:20 -0400 Subject: [PATCH] Added a dice roller leveraging true randomness from random.org API. --- osrs-toolbox/Models/DiceRollerModel.cs | 53 +++++++++++++++++++ osrs-toolbox/Models/HomePageModel.cs | 6 +++ .../ViewModels/DiceRollerViewModel.cs | 45 ++++++++++++++++ osrs-toolbox/ViewModels/HomePageViewModel.cs | 15 ++++++ osrs-toolbox/Views/DiceRollerView.xaml | 36 +++++++++++++ osrs-toolbox/Views/DiceRollerView.xaml.cs | 34 ++++++++++++ osrs-toolbox/Views/HomePageView.xaml | 4 +- 7 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 osrs-toolbox/Models/DiceRollerModel.cs create mode 100644 osrs-toolbox/ViewModels/DiceRollerViewModel.cs create mode 100644 osrs-toolbox/Views/DiceRollerView.xaml create mode 100644 osrs-toolbox/Views/DiceRollerView.xaml.cs diff --git a/osrs-toolbox/Models/DiceRollerModel.cs b/osrs-toolbox/Models/DiceRollerModel.cs new file mode 100644 index 0000000..99a0a1e --- /dev/null +++ b/osrs-toolbox/Models/DiceRollerModel.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; + +namespace osrs_toolbox +{ + public abstract class DiceRollerModel : ModelBase + { + private int _minValue; + private int _maxValue; + private int _numRolls; + private bool _allowDuplicates; + private string _rollResults; + + private ICommand _rollDice; + + public int MinValue + { + get { return _minValue; } + set { SetProperty(ref _minValue, value, nameof(MinValue)); } + } + public int MaxValue + { + get { return _maxValue; } + set { SetProperty(ref _maxValue, value, nameof(MaxValue)); } + } + public int NumRolls + { + get { return _numRolls; } + set { SetProperty(ref _numRolls, value, nameof(NumRolls)); } + } + public bool AllowDuplicates + { + get { return _allowDuplicates; } + set { SetProperty(ref _allowDuplicates, value, nameof(AllowDuplicates)); } + } + public string RollResults + { + get { return _rollResults; } + set { SetProperty(ref _rollResults, value, nameof(RollResults)); } + } + + public ICommand RollDice + { + get { return _rollDice; } + set { SetProperty(ref _rollDice, value, nameof(RollDice)); } + } + } +} diff --git a/osrs-toolbox/Models/HomePageModel.cs b/osrs-toolbox/Models/HomePageModel.cs index 4262926..18561e5 100644 --- a/osrs-toolbox/Models/HomePageModel.cs +++ b/osrs-toolbox/Models/HomePageModel.cs @@ -16,6 +16,7 @@ namespace osrs_toolbox private ICommand _openCombatLevelCalculator; private ICommand _openShopBuyoutCalculator; private ICommand _openAPITest; + private ICommand _openDiceRoller; public ICommand OpenCompetitionOverlaySettings { @@ -44,5 +45,10 @@ namespace osrs_toolbox get { return _openAPITest; } set { SetProperty(ref _openAPITest, value, nameof(OpenAPITest)); } } + public ICommand OpenDiceRoller + { + get { return _openDiceRoller; } + set { SetProperty(ref _openDiceRoller, value, nameof(OpenDiceRoller)); } + } } } diff --git a/osrs-toolbox/ViewModels/DiceRollerViewModel.cs b/osrs-toolbox/ViewModels/DiceRollerViewModel.cs new file mode 100644 index 0000000..7baf300 --- /dev/null +++ b/osrs-toolbox/ViewModels/DiceRollerViewModel.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace osrs_toolbox +{ + public class DiceRollerViewModel : DiceRollerModel + { + public DiceRollerViewModel() + { + InitializeVariables(); + InitializeCommands(); + } + + private void InitializeVariables() + { + MinValue = 0; + MaxValue = 100; + NumRolls = 1; + AllowDuplicates = true; + RollResults = string.Empty; + } + + private void InitializeCommands() + { + RollDice = new RelayCommand(DoRollDice); + } + + private async void DoRollDice(object obj) + { + RollResults = "Rolling Dice..."; + await Task.Delay(10); + int[] Rolls = await Random.GetRandomIntegersAsync(MinValue, MaxValue, NumRolls, AllowDuplicates).ConfigureAwait(false); + bool First = true; + RollResults = string.Empty; + foreach (int Roll in Rolls) + { + RollResults += string.Format("{0}{1}", First ? "" : ", ", Roll.ToString()); + First = false; + } + } + } +} diff --git a/osrs-toolbox/ViewModels/HomePageViewModel.cs b/osrs-toolbox/ViewModels/HomePageViewModel.cs index 6cb544c..a2ae5fe 100644 --- a/osrs-toolbox/ViewModels/HomePageViewModel.cs +++ b/osrs-toolbox/ViewModels/HomePageViewModel.cs @@ -27,6 +27,7 @@ namespace osrs_toolbox OpenCombatLevelCalculator = new RelayCommand(DoOpenCombatLevelCalculator); OpenShopBuyoutCalculator = new RelayCommand(DoOpenShopBuyoutCalculator); OpenAPITest = new RelayCommand(DoOpenAPITest); + OpenDiceRoller = new RelayCommand(DoOpenDiceRoller); } private void DoOpenCompetitionOverlaySettings(object obj) @@ -98,5 +99,19 @@ namespace osrs_toolbox atv.Show(); } } + + private void DoOpenDiceRoller(object obj) + { + if (DiceRollerView.Current != null) + { + DiceRollerView.Current.Close(); + DiceRollerView.Current = null; + } + else + { + DiceRollerView drv = new DiceRollerView(); + drv.Show(); + } + } } } diff --git a/osrs-toolbox/Views/DiceRollerView.xaml b/osrs-toolbox/Views/DiceRollerView.xaml new file mode 100644 index 0000000..520944a --- /dev/null +++ b/osrs-toolbox/Views/DiceRollerView.xaml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + -