private Tuple <RoundModel, PlayerModel> ValidateIds(Guid roundId, Guid playerId) { var player = _playerService.GetPlayer(playerId); if (player == null) { throw new ArgumentException("No player of that ID found", nameof(playerId)); } var balance = _ledgerService.GetBalance(playerId); if (balance <= 0) { throw new InvalidOperationException("Player doesn't have any elections available"); } var round = GetRound(roundId); if (!round.IsOpen) { throw new ArgumentException("That Round ID is not open", nameof(roundId)); } return(Tuple.Create(round, player)); }
public override void Execute() { var players = _playerService.GetAllPlayers(); if (!players.Any()) { return; } var maxNameLength = players.Max(a => a.Name.Length); var builder = new StringBuilder(); builder.Append("Name").Append(' ', maxNameLength - 4); builder.Append(' ', ColumnPaddingSpaces); builder.Append("Balance"); builder.AppendLine(); builder.Append('-', builder.Length - 1); builder.AppendLine(); foreach (var p in players.OrderBy(a => a.Name)) { var balance = _ledgerService.GetBalance(p.Id); builder.Append(p.Name).Append(' ', maxNameLength - p.Name.Length); builder.Append(' ', ColumnPaddingSpaces); builder.Append(balance); builder.AppendLine(); } Console.WriteLine(builder.ToString()); }
protected override void ExecuteCore(Guid playerId) { var player = PlayerService.GetPlayer(playerId); var balance = _ledgerService.GetBalance(playerId); if (balance <= 0) { ConsoleHelpers.WriteRedLine($"This player has {balance} credits and is ineligible to cash out."); return; } Console.WriteLine($"{player.Name} has {balance} credits. How many are you cashing out?"); var creditsString = Console.ReadLine(); int credits; if (string.IsNullOrEmpty(creditsString) || !int.TryParse(creditsString, out credits)) { ConsoleHelpers.WriteRedLine("Invalid input, try again"); return; } if (credits <= 0) { return; } if (credits > balance) { ConsoleHelpers.WriteRedLine($"{player.Name} only has {balance} credits. You can't cash out {credits}."); return; } _ledgerService.PurchaseCredits(playerId, -credits); ConsoleHelpers.WriteGreenLine($"Successfully cashed out {player.Name} with {credits} credits"); }
protected override void ExecuteCore(Guid playerId) { var player = PlayerService.GetPlayer(playerId); var balance = _ledgerService.GetBalance(playerId); var creditPlural = balance != 1 ? "s" : string.Empty; Console.WriteLine($"{player.Name} currently has {balance} credit{creditPlural}."); Console.WriteLine("Detailed credit listing:"); var textyTable = new Dictionary <string, List <string> >(); textyTable["Type"] = new List <string>(); textyTable["Amount"] = new List <string>(); textyTable["Time"] = new List <string>(); textyTable["Round"] = new List <string>(); var history = _ledgerService.GetPlayerHistory(playerId); foreach (var item in history.OrderBy(a => a.TransactionTime)) { textyTable["Type"].Add(item.Type.ToString()); textyTable["Amount"].Add(item.Amount.ToString()); textyTable["Time"].Add(item.TransactionTime.ToString()); int?roundNumber = null; if (item.RoundId.HasValue) { roundNumber = _roundService.GetRound(item.RoundId.Value).RoundNumber; } textyTable["Round"].Add(roundNumber?.ToString()); } var output = new StringBuilder(); AppendHeader(textyTable, "Type", output); AppendHeader(textyTable, "Amount", output); AppendHeader(textyTable, "Time", output); AppendHeader(textyTable, "Round", output); output.AppendLine(); output.Append('-', output.Length - 1).AppendLine(); for (var i = 0; i < history.Count; ++i) { AppendRow(textyTable, new [] { "Type", "Amount", "Time", "Round" }, output, i); } Console.WriteLine(output.ToString()); }
protected override void ExecuteCore(Guid playerId) { var player = PlayerService.GetPlayer(playerId); var balance = _ledgerService.GetBalance(playerId); if (balance <= 0) { ConsoleHelpers.WriteRedLine($"{player.Name} does not have enough credits to elect."); return; } var round = _roundService.GetOpenRoundId(); if (round == null) { ConsoleHelpers.WriteRedLine("No Round is currently open - open a new Round first."); return; } _roundService.AddElectionToRound(round.Value, playerId); ConsoleHelpers.WriteGreenLine($"Added {player.Name} to the Round"); }