From 9b4865312238a0e69c031b6735f9257683db7543 Mon Sep 17 00:00:00 2001 From: JFronny <33260128+JFronny@users.noreply.github.com> Date: Sat, 13 Jun 2020 12:51:14 +0200 Subject: [PATCH] Save settings --- Lemonade/DollarConv.cs | 17 +++++++++++++++++ Lemonade/ResultScreen.cs | 19 ++++++++++--------- Lemonade/Settings.cs | 33 ++++++++++++++++++++++++++++++++- Lemonade/TransactionScreen.cs | 6 +++--- 4 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 Lemonade/DollarConv.cs diff --git a/Lemonade/DollarConv.cs b/Lemonade/DollarConv.cs new file mode 100644 index 0000000..efd0998 --- /dev/null +++ b/Lemonade/DollarConv.cs @@ -0,0 +1,17 @@ +using System.Globalization; + +namespace Lemonade +{ + public static class DollarConv + { + public static string ToDollar(this int cents) + { + string tmp = $"${(cents / 100f).ToString(CultureInfo.InvariantCulture)}"; + if (tmp.Length > 1 && tmp[1] == '-') + tmp = "-$" + tmp.Substring(2); + if (tmp.Contains('.') && tmp.Split('.')[1].Length < 2) + tmp += '0'; + return tmp; + } + } +} \ No newline at end of file diff --git a/Lemonade/ResultScreen.cs b/Lemonade/ResultScreen.cs index e582710..e06cfda 100644 --- a/Lemonade/ResultScreen.cs +++ b/Lemonade/ResultScreen.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Drawing; using CC_Functions.Commandline; using CC_Functions.Commandline.TUI; +using static System.Environment; namespace Lemonade { @@ -32,22 +33,22 @@ namespace Lemonade lab.Content = "Lightning struck your table while you were setting it up. Nothing was sold"; else lab.Content = $"The weather was {weather}. Nothing out of the ordinary happened"; - lab.Content += Environment.NewLine + players.ToStringTable( + lab.Content += NewLine + NewLine + players.ToStringTable( new[] { - "Player", "Glasses made", "Earnings per glass", "Glasses sold", "Signs made", - "Income", "Expenses", - "Profit", "Budget" + "Player", "Glasses made", "Earnings per glass", + "Glasses sold", "Signs made", "Income", + "Expenses", "Profit", "Budget" }, s => s.Number, s => s.Glasses, - s => s.GlassPrice, + s => s.GlassPrice.ToDollar(), s => s.Sales, s => s.Signs, - s => s.Earnings, - s => s.Expenses, - s => s.Earnings - s.Expenses, - s => s.Budget); + s => s.Earnings.ToDollar(), + s => s.Expenses.ToDollar(), + s => (s.Earnings - s.Expenses).ToDollar(), + s => s.Budget.ToDollar()); lab.Render(); ActualSize = new Size(lab.Size.Width, lab.Size.Height); } diff --git a/Lemonade/Settings.cs b/Lemonade/Settings.cs index f8935d9..94d1830 100644 --- a/Lemonade/Settings.cs +++ b/Lemonade/Settings.cs @@ -1,6 +1,10 @@ using System; +using System.Diagnostics; using System.Drawing; +using System.IO; +using System.Reflection; using System.Threading; +using System.Xml.Linq; using CC_Functions.Commandline.TUI; namespace Lemonade @@ -12,7 +16,10 @@ namespace Lemonade Configure(); } - public bool Color { get; private set; } = true; + private static string Settingsfile = + Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Settings.xml"); + + public bool Color { get; private set; } public int PlayerCount { get; private set; } public float DifficultyFactor { get; private set; } @@ -63,6 +70,15 @@ namespace Lemonade bool visible = true; okButton.Click += (screen, args) => visible = false; settingsScreen.Close += (screen, args) => visible = false; + + if (!File.Exists(Settingsfile)) GenDef(); + try { + XElement conf = XElement.Parse(File.ReadAllText(Settingsfile)); + playerSlider.Value = int.Parse(conf.Element("Players").Value); + difficulty.Value = int.Parse(conf.Element("Difficulty").Value); + colorBox.Checked = bool.Parse(conf.Element("Color").Value); + settingsScreen.Color = colorBox.Checked; + } catch { Debug.Fail("Failed to load settings"); } settingsScreen.Render(); while (visible) @@ -73,6 +89,21 @@ namespace Lemonade PlayerCount = playerSlider.Value; DifficultyFactor = difficulty.Value / 10f; Color = colorBox.Checked; + try + { + Gen(PlayerCount, difficulty.Value, Color); + } + catch {Debug.Fail("Failed to save settings");} + } + + private static XElement GenDef() => Gen(2, 5, true); + + private static XElement Gen(int players, int difficulty, bool color) + { + XElement tmp = new XElement("Settings", new XElement("Players", players), new XElement("Difficulty", difficulty), + new XElement("Color", color)); + tmp.Save(Settingsfile); + return tmp; } } } \ No newline at end of file diff --git a/Lemonade/TransactionScreen.cs b/Lemonade/TransactionScreen.cs index cf90260..3878653 100644 --- a/Lemonade/TransactionScreen.cs +++ b/Lemonade/TransactionScreen.cs @@ -63,10 +63,10 @@ namespace Lemonade Button okButton = new Button("OK"); okButton.Point = new Point(ContentPanel.Size.Width / 2 - okButton.Size.Width / 2, 16); - okButton.Click += (sender, e) => Ok?.Invoke(_glasses.Value, 10, 0); + okButton.Click += (sender, e) => Ok?.Invoke(_glasses.Value, _price.Value, _signs.Value); ContentPanel.Controls.Add(okButton); - Close += (screen, args) => Ok?.Invoke(_glasses.Value, 10, 0); + Close += (screen, args) => Ok?.Invoke(_glasses.Value, _price.Value, _signs.Value); } public event OkDelegate Ok; @@ -101,7 +101,7 @@ namespace Lemonade _glasses.MaxValue = (int) Math.Floor(leftover / (double) _lemonadeCost) + _glasses.Value; _price.MaxValue = 200; _signs.MaxValue = (int) Math.Floor(leftover / (double) _signCost) + _signs.Value; - _infoLabelBottom.Content = $"Leftover: ${leftover / 100f}/${_player.Budget / 100f}"; + _infoLabelBottom.Content = $"Leftover: {leftover.ToDollar()}/{_player.Budget.ToDollar()}"; _infoLabelBottom.Point = new Point(ContentPanel.Size.Width / 2 - _infoLabelBottom.Content.Length / 2, 14); }