private async Task <long> Paint(Dictionary <Point, TileState> screen, ChannelReader <long> output, ChannelWriter <long> input) { bool enableOutput = true; try { AnsiConsole.Clear(); } catch (Exception) { enableOutput = false; AnsiConsole.WriteLine("Console manipulation has been disabled due to redirection"); } long score = 0, paddleX = 0, paddleY = 0; await input.WriteAsync(0); while (await output.WaitToReadAsync()) { var x = (int)await output.ReadAsync(); var y = (int)await output.ReadAsync(); var state = await output.ReadAsync(); if (x == -1 && y == 0) { score = state; if (enableOutput) { AnsiConsole.Cursor.SetPosition(0, 0); AnsiConsole.Write("Score: " + score); } } else { var tile = (TileState)state; if (tile == TileState.HorizontalPaddle) { paddleX = x; paddleY = y; } else if (tile == TileState.Ball) { var num = 0; if (x > paddleX) { num = 1; } else if (x < paddleX) { num = -1; } await input.WriteAsync(num); } screen[new(x, y)] = tile;
public static void WriteHeader() { AnsiConsole.Clear(); var rule = new Rule("Express.NET") { Alignment = Justify.Center, Border = BoxBorder.Double, Style = Style.Parse("red"), }; AnsiConsole.Render(rule); }
private static async Task Main(string[] args) { using var http = new HttpClient(); var gw2 = new Gw2Client(http); var(ingredients, recipes) = await Progress() .StartAsync( async ctx => { var recipesProgress = ctx.AddTask( "Fetching recipes", new ProgressTaskSettings { AutoStart = false } ); var ingredientsProgress = ctx.AddTask( "Fetching ingredients", new ProgressTaskSettings { AutoStart = false } ); var craftingRecipes = await GetRecipes(gw2.Crafting, recipesProgress); var groupedByIngredient = craftingRecipes .SelectMany( recipe => recipe.Ingredients .Where(ingredient => ingredient.Kind == IngredientKind.Item) .Select(ingredient => (Ingredient: ingredient.Id, Recipe: recipe)) ) .ToLookup(grouping => grouping.Ingredient, grouping => grouping.Recipe); var ingredientIndex = groupedByIngredient.Select(grouping => grouping.Key) .ToHashSet(); var craftingIngredients = await GetItems( ingredientIndex, gw2.Items, ingredientsProgress ); var ingredientsById = craftingIngredients.ToDictionary(item => item.Id); var mostCommon = groupedByIngredient .OrderByDescending(grouping => grouping.Count()) .Select(grouping => ingredientsById[grouping.Key]) .ToList(); return(Ingredients: mostCommon, Craftable: groupedByIngredient); } ); do { AnsiConsole.Clear(); var choice = AnsiConsole.Prompt( new SelectionPrompt <Item>().Title("Pick an ingredient to see the available recipes") .MoreChoicesText("Scroll down for less commonly used ingredients") .AddChoices(ingredients) .UseConverter(item => item.Name) .PageSize(20) ); await using var ingredientIcon = await http.GetStreamAsync(choice.Icon !); var choiceTable = new Table().AddColumn("Icon") .AddColumn("Ingredient") .AddColumn("Description"); choiceTable.AddRow( new CanvasImage(ingredientIcon).MaxWidth(32), new Markup(choice.Name.EscapeMarkup()), new Markup(choice.Description.EscapeMarkup()) ); AnsiConsole.Write(choiceTable); var outputs = await Progress() .StartAsync( async ctx => { var itemIds = recipes[choice.Id] .Select(recipe => recipe.OutputItemId) .ToHashSet(); return(await GetItems( itemIds, gw2.Items, ctx.AddTask("Fetching output items") )); } ); var recipesTable = new Table().AddColumn("Recipe").AddColumn("Description"); foreach (var recipe in outputs) { recipesTable.AddRow(recipe.Name.EscapeMarkup(), recipe.Description.EscapeMarkup()); } AnsiConsole.Write(recipesTable); } while (AnsiConsole.Confirm("Do you want to choose again?")); }
private static async Task MainAsync() { var portfolioRepository = await PortfolioEventStoreStream.Factory(); var key = string.Empty; while (key != "X") { AnsiConsole.Clear(); PrintMenuItem("Deposit", ConsoleColor.DarkRed); PrintMenuItem("Withdraw", ConsoleColor.DarkCyan); PrintMenuItem("Buy Stock", ConsoleColor.DarkGray); PrintMenuItem("Sell Stock", ConsoleColor.DarkGreen); PrintMenuItem("Portfolio", ConsoleColor.DarkYellow); PrintMenuItem("Events", ConsoleColor.Yellow); AnsiConsole.Write("> "); key = Console.ReadLine()?.ToUpperInvariant(); AnsiConsole.WriteLine(); var username = GetUsername(); var portfolio = await portfolioRepository.Get(username); switch (key) { case "D": var moneyToDeposit = GetAmount(); portfolio.Deposit(moneyToDeposit); AnsiConsole.WriteLine($"{username} Received: {moneyToDeposit}"); break; case "W": var moneyToWithdraw = GetAmount(); portfolio.Withdrawal(moneyToWithdraw); AnsiConsole.WriteLine($"{username} Withdrew: {moneyToWithdraw}"); break; case "P": AnsiConsole.WriteLine($"Money: {portfolio.GetState().Money:0.##}"); if (portfolio.GetState().Shares != null) { foreach (var stockOwned in portfolio.GetState().Shares) { AnsiConsole.WriteLine($"Stock Ticker: {stockOwned.Key} Quantity: {stockOwned.Value.NumberOfShares} Average Price: {stockOwned.Value.Price:0.##} Profit: {portfolio.GetState().Profit:0.##}"); } } break; case "B": var amountOfSharesToBuy = GetAmount(); var stock = GetStock(); var price = GetPrice(); portfolio.BuyShares(stock, amountOfSharesToBuy, price); AnsiConsole.WriteLine($"{username} Bought: {stock} Amount: {amountOfSharesToBuy} Price: {price:0.##}"); break; case "S": var amountOfSharesToSell = GetAmount(); stock = GetStock(); price = GetPrice(); portfolio.SellShares(stock, amountOfSharesToSell, price); AnsiConsole.WriteLine($"{username} Sold: {stock} Amount: {amountOfSharesToSell} Price: {price:0.##}"); break; case "E": var events = await portfolioRepository.GetAllEvents(username); Console.WriteLine($"Events: {username}"); foreach (var evnt in events) { switch (evnt) { case SharesSold sharesSold: AnsiConsole.WriteLine($"[{sharesSold.DateTime}] {sharesSold.Amount} shares SOLD from {sharesSold.Stock} at Price {sharesSold.Price:0.##}"); break; case SharesBought sharesBought: AnsiConsole.WriteLine($"[{sharesBought.DateTime}] {sharesBought.Amount} shares BOUGHT from {sharesBought.Stock} at Price {sharesBought.Price:0.##}"); break; case DepositMade depositMade: AnsiConsole.WriteLine($"[{depositMade.DateTime}] {depositMade.Amount} EUR DEPOSIT"); break; case WithdrawalMade withdrawalMade: AnsiConsole.WriteLine($"[{withdrawalMade.DateTime}] {withdrawalMade.Amount} EUR WITHDRAWAL"); break; } } break; } await portfolioRepository.Save(portfolio); AnsiConsole.WriteLine(); Console.ReadLine(); } }