This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
Lemonade/Lemonade/TransactionScreen.cs

112 lines
5.4 KiB
C#
Raw Normal View History

2020-06-12 20:02:39 +02:00
using System;
using System.Diagnostics.Contracts;
using System.Drawing;
using CC_Functions.Commandline.TUI;
2020-06-12 20:02:04 +02:00
namespace Lemonade
{
2020-06-12 20:02:39 +02:00
public class TransactionScreen : CenteredScreen
2020-06-12 20:02:04 +02:00
{
2020-06-12 20:02:39 +02:00
public delegate void OkDelegate(int glasses, int price, int signs);
private readonly Slider _glasses;
2020-06-12 20:16:11 +02:00
private readonly Label _glassesLabel;
private readonly Label _infoLabel;
private readonly Label _infoLabelBottom;
2020-06-12 20:02:39 +02:00
private readonly Slider _price;
2020-06-12 20:16:11 +02:00
private readonly Label _priceLabel;
2020-06-12 20:02:39 +02:00
private readonly Slider _signs;
2020-06-12 20:16:11 +02:00
private readonly Label _signsLabel;
private int _lemonadeCost;
2020-06-12 20:02:39 +02:00
private PlayerState _player;
private int _signCost;
2020-06-12 20:16:11 +02:00
2020-06-13 12:58:04 +02:00
//TODO restore last transaction if budget allows
2020-06-12 20:02:39 +02:00
public TransactionScreen(Settings set) : base(200, 20, ConsoleColor.Black, set.Color)
{
ContentPanel.ForeColor = ConsoleColor.DarkGray;
2020-06-12 20:16:11 +02:00
2020-06-12 20:02:39 +02:00
_infoLabel = new Label("[DAY 1]");
_infoLabel.Point = new Point(ContentPanel.Size.Width / 2 - _infoLabel.Content.Length / 2, 0);
ContentPanel.Controls.Add(_infoLabel);
2020-06-12 20:16:11 +02:00
2020-06-12 20:02:39 +02:00
_glassesLabel = new Label("How many glasses of lemonade do you wish to make?");
_glassesLabel.Point = new Point(ContentPanel.Size.Width / 2 - _glassesLabel.Content.Length / 2, 2);
ContentPanel.Controls.Add(_glassesLabel);
2020-06-12 20:16:11 +02:00
2020-06-12 20:02:39 +02:00
_glasses = new Slider {Size = new Size(100, 1)};
_glasses.Point = new Point(ContentPanel.Size.Width / 2 - _glasses.Size.Width / 2, 3);
_glasses.ValueChanged += (screen, args) => CalculateMax();
ContentPanel.Controls.Add(_glasses);
2020-06-12 20:16:11 +02:00
2020-06-12 20:02:39 +02:00
_priceLabel = new Label("What price (in cents) do you wish to charge for lemonade?");
_priceLabel.Point = new Point(ContentPanel.Size.Width / 2 - _priceLabel.Content.Length / 2, 6);
ContentPanel.Controls.Add(_priceLabel);
_price = new Slider {Size = new Size(100, 1)};
_price.Point = new Point(ContentPanel.Size.Width / 2 - _price.Size.Width / 2, 7);
_price.ValueChanged += (screen, args) => CalculateMax();
ContentPanel.Controls.Add(_price);
2020-06-12 20:16:11 +02:00
2020-06-12 20:02:39 +02:00
_signsLabel = new Label("How many advertising signs do you want to make?");
_signsLabel.Point = new Point(ContentPanel.Size.Width / 2 - _signsLabel.Content.Length / 2, 10);
ContentPanel.Controls.Add(_signsLabel);
_signs = new Slider {Size = new Size(100, 1)};
_signs.Point = new Point(ContentPanel.Size.Width / 2 - _signs.Size.Width / 2, 11);
_signs.ValueChanged += (screen, args) => CalculateMax();
ContentPanel.Controls.Add(_signs);
2020-06-12 20:16:11 +02:00
2020-06-12 20:02:39 +02:00
_infoLabelBottom = new Label("Total Expenses: 0/0");
_infoLabelBottom.Point = new Point(ContentPanel.Size.Width / 2 - _infoLabelBottom.Content.Length / 2, 14);
ContentPanel.Controls.Add(_infoLabelBottom);
2020-06-12 20:16:11 +02:00
2020-06-12 20:02:39 +02:00
Button okButton = new Button("OK");
okButton.Point = new Point(ContentPanel.Size.Width / 2 - okButton.Size.Width / 2, 16);
2020-06-13 12:51:14 +02:00
okButton.Click += (sender, e) => Ok?.Invoke(_glasses.Value, _price.Value, _signs.Value);
2020-06-12 20:02:39 +02:00
ContentPanel.Controls.Add(okButton);
2020-06-13 12:51:14 +02:00
Close += (screen, args) => Ok?.Invoke(_glasses.Value, _price.Value, _signs.Value);
2020-06-12 20:02:39 +02:00
}
2020-06-12 20:16:11 +02:00
public event OkDelegate Ok;
public void SetUp(PlayerState player, Settings settings, int playerIndex, int day, Weather weather,
int signCost, int lemonadeCost)
2020-06-12 20:02:39 +02:00
{
TabPoint = 0;
_signCost = signCost;
_lemonadeCost = lemonadeCost;
Title = $"Lemonade - Player {playerIndex + 1}/{settings.PlayerCount}";
_player = player;
_glasses.Value = 0;
_price.Value = 0;
_signs.Value = 0;
CalculateMax();
_infoLabel.Content = $"It is day {day}, the weather is {weather}";
2020-06-13 12:15:11 +02:00
if (weather.W == W.Hot_and_dry)
_infoLabel.Content += ". There is a chance of heatwaves";
else if (weather.W == W.Rainy)
_infoLabel.Content += ". There is a chance of thunderstorms";
2020-06-12 20:02:39 +02:00
_infoLabel.Point = new Point(ContentPanel.Size.Width / 2 - _infoLabel.Content.Length / 2, 0);
_glassesLabel.Content = $"How many glasses of lemonade do you wish to make? {lemonadeCost}ct each";
_glassesLabel.Point = new Point(ContentPanel.Size.Width / 2 - _glassesLabel.Content.Length / 2, 2);
_signsLabel.Content = $"How many advertising signs ({signCost}ct each) do you want to make?";
_signsLabel.Point = new Point(ContentPanel.Size.Width / 2 - _signsLabel.Content.Length / 2, 10);
}
private void CalculateMax()
{
int leftover = _player.Budget - CalculateExpenses();
2020-06-12 20:16:11 +02:00
_glasses.MaxValue = (int) Math.Floor(leftover / (double) _lemonadeCost) + _glasses.Value;
2020-06-12 20:02:39 +02:00
_price.MaxValue = 200;
2020-06-12 20:16:11 +02:00
_signs.MaxValue = (int) Math.Floor(leftover / (double) _signCost) + _signs.Value;
2020-06-13 12:51:14 +02:00
_infoLabelBottom.Content = $"Leftover: {leftover.ToDollar()}/{_player.Budget.ToDollar()}";
2020-06-12 20:02:39 +02:00
_infoLabelBottom.Point = new Point(ContentPanel.Size.Width / 2 - _infoLabelBottom.Content.Length / 2, 14);
}
[Pure]
private int CalculateExpenses() => _lemonadeCost * _glasses.Value + _signCost * _signs.Value;
2020-06-12 20:02:04 +02:00
}
}