public async Task <CommandResult> HandleCommandAsync(string[] args, bool background, bool loggedIn) { if (!background && args.Length == 1 && (args[0] == "?" || args[0].ToLower() == "help")) { _console.WriteLine("MARKET: Provides functions for interacting with the marketplace."); _console.WriteLine("Subcommands"); _console.WriteLine("list: Displays the market data for a given location - MARKET list <Location Symbol/Ship Id>"); _console.WriteLine("buy: Purchase cargo from a market for the given ship - MARKET <Ship Id> buy <Good> <Quantity>"); _console.WriteLine("sell: Sell cargo to a market from the given ship - MARKET <Ship Id> sell <Good> <Quantity>"); _console.WriteLine(" Buy and sell quantities support the following formats:"); _console.WriteLine(" [number]: A flat number amount to buy or sell."); _console.WriteLine(" max: Purchase or sell the maximum quantity possible."); _console.WriteLine(" m[number]: Buy up to an amount or sell down to an amount."); _console.WriteLine(" [number]%: Buy a percent of the available ship space or sell a percent of the amount in cargo."); return(CommandResult.SUCCESS); } else if (!background && args.Length == 2 && args[0].ToLower() == "list") { var symbol = args[1].ToUpper(); string shipId = null; if (_shipProvider.TryGetShipDataByLocalId(args[1], out var shipData)) { if (string.IsNullOrWhiteSpace(shipData.Ship.Location)) { _console.WriteLine("Ship is not currently docked."); return(CommandResult.FAILURE); } symbol = shipData.Ship.Location; shipId = shipData.ServerId; } var canGetLive = _shipProvider.GetShipData().Any(t => t.Ship.Location == symbol); if (canGetLive) { await _marketInfo.RefreshMarketData(symbol, false); _console.WriteLine("Retrieved updated market data for " + symbol + "."); } if (_marketInfo.HasMarket(symbol)) { _console.WriteLine("Displaying market data for " + symbol + "."); if (shipId != null) { _navManager.NavigateTo(_navManager.BaseUri + "ships/" + shipId + "/market"); } else { _navManager.NavigateTo(_navManager.BaseUri + "markets/" + symbol); } return(CommandResult.SUCCESS); } else { _console.WriteLine("Market data unavailable for " + symbol + "."); } return(CommandResult.FAILURE); } else if (args.Length == 4 && args[1].ToLower() == "buy") { if (_shipProvider.TryGetShipDataByLocalId(args[0], out var shipData)) { if (!string.IsNullOrWhiteSpace(shipData.Ship.Location)) { var quantityType = GetQuantityType(args[3], out int quantity); if (quantityType != QuantityType.INVALID) { return(await ProcessPurchase(shipData, quantityType, quantity, args[2], background, false)); } else { _console.WriteLine("Invalid quantity provided. Must be at least 1."); } } else { _console.WriteLine("Ship is not currently docked."); } } else { _console.WriteLine("Invalid ship id. Please use number ids and not the full string id."); } return(CommandResult.FAILURE); } else if (args.Length == 4 && args[1].ToLower() == "sell") { if (_shipProvider.TryGetShipDataByLocalId(args[0], out var shipData)) { if (!string.IsNullOrWhiteSpace(shipData.Ship.Location)) { var quantityType = GetQuantityType(args[3], out int quantity); if (quantityType != QuantityType.INVALID) { var good = shipData.Ship.Cargo.FirstOrDefault(t => t.Good == args[2].ToUpper()); if (good != null) { if (quantityType == QuantityType.TO_AMOUNT) { quantity = good.Quantity - quantity; if (quantity < 1) { if (!background) { _console.WriteLine("Cargo already at or over given sell limit."); } return(CommandResult.SUCCESS); } } else if (quantityType == QuantityType.PERCENT) { quantity = (int)(good.Quantity * (quantity / 100d)); } var quantityExceeded = quantityType == QuantityType.MAX || quantity > good.Quantity; if (quantityExceeded) { quantity = good.Quantity; } if (!background) { if (quantityType != QuantityType.MAX && quantityExceeded) { _console.WriteLine("Insufficient quantity available for sale. Selling maximum."); } else if (quantityType == QuantityType.MAX) { _console.WriteLine("Selling maximum. Sell quantity: " + quantity + "."); } } var quantityRemaining = quantity; var anySold = false; while (quantityRemaining > 0) { var quantityProcessing = Math.Min(TRADE_CAP, quantityRemaining); quantityRemaining -= quantityProcessing; var httpResult = await _http.PostAsJsonAsync("/my/sell-orders", new TransactionRequest { ShipId = shipData.ServerId, Good = good.Good, Quantity = quantityProcessing }); if (httpResult.IsSuccessStatusCode) { var saleResult = await httpResult.Content.ReadFromJsonAsync <TransactionResult>(_serializerOptions); _userInfo.SetCredits(saleResult.Credits); _shipProvider.UpdateShipCargo(saleResult.Ship.Id, saleResult.Ship.Cargo); if (!background) { _console.WriteLine(quantityProcessing + " units of cargo sold successfully. Total made: " + saleResult.Order.Total + " credits."); } anySold = true; if (quantityRemaining <= 0) { return(CommandResult.SUCCESS); } } else { var errorResponseString = await httpResult.Content.ReadAsStringAsync(); var errorResponse = JsonSerializer.Deserialize <ErrorResponse>(errorResponseString, _serializerOptions); if (errorResponse.Error == null) { errorResponse.Error = JsonSerializer.Deserialize <Error>(errorResponseString, _serializerOptions); } if (errorResponse.Error.Message.ToLower().Contains("good is not listed in planet marketplace")) { _console.WriteLine("The good specified cannot be sold at this ships market."); } else { _console.WriteLine(errorResponse.Error.Message); if (background) { _console.WriteLine("Unhandled error occurred during route purchase. Attempting to continue."); } } return(anySold ? CommandResult.SUCCESS : CommandResult.FAILURE); } } } else if (!background) { _console.WriteLine("The good specified could not be found in the ships cargo."); } else { _console.WriteLine("Route ship did not have good to sell. Attempting to continue."); } if (background) { return(CommandResult.SUCCESS); } } else { _console.WriteLine("Invalid quantity provided. Must be at least 1."); } } else { _console.WriteLine("Ship is not currently docked."); } } else { _console.WriteLine("Invalid ship id. Please use number ids and not the full string id."); } return(CommandResult.FAILURE); } return(CommandResult.INVALID); }
public async Task <CommandResult> HandleCommandAsync(string[] args, bool background, bool loggedIn) { if (!background && args.Length == 1 && (args[0] == "?" || args[0].ToLower() == "help")) { _console.WriteLine("SHIP: Provides functions for managing ships."); _console.WriteLine("Subcommands"); _console.WriteLine("map: Displays the local map for the ship - SHIP <Ship Id> map"); _console.WriteLine("cargo: Displays the cargo of ship - SHIP <Ship Id> cargo"); _console.WriteLine("transfer: Transfers cargo between your ships - SHIP <Ship Id> transfer <Good> <Quantity> <Transfer to Ship Id>"); _console.WriteLine("jettison: Jettisons cargo from the ship - SHIP <Ship Id> jettison <Good> <Quantity>"); _console.WriteLine("fly: Enacts a flightplan for a ship - SHIP <Ship Id> fly <Location Symbol>"); _console.WriteLine("warp: Warps a ship through the docked wormhole - SHIP <Ship Id> warp"); _console.WriteLine("rename: Renames a ship - SHIP <Ship Id> rename <New Name>"); _console.WriteLine("info: Prints the specifications of ship - SHIP <Ship Id> info"); _console.WriteLine("scrap: Scraps the ship for credits - SHIP <Ship Id> scrap"); return(CommandResult.SUCCESS); } else if (!background && args.Length == 2 && args[1].ToLower() == "map") { if (_shipProvider.TryGetShipDataByLocalId(args[0], out var shipData)) { _console.WriteLine("Displaying local map for " + shipData.DisplayName + "."); _navManager.NavigateTo(_navManager.BaseUri + "ships/" + shipData.ServerId + "/map"); return(CommandResult.SUCCESS); } else { _console.WriteLine("Invalid ship id. Please use number ids and not the full string id."); } return(CommandResult.FAILURE); } else if (!background && args.Length == 2 && args[1].ToLower() == "cargo") { if (_shipProvider.TryGetShipDataByLocalId(args[0], out var shipData)) { _console.WriteLine("Displaying cargo for " + shipData.DisplayName + "."); _navManager.NavigateTo(_navManager.BaseUri + "ships/" + shipData.ServerId + "/cargo"); return(CommandResult.SUCCESS); } else { _console.WriteLine("Invalid ship id. Please use number ids and not the full string id."); } return(CommandResult.FAILURE); } else if (args.Length == 3 && args[1].ToLower() == "fly") { if (_shipProvider.TryGetShipDataByLocalId(args[0], out var shipData)) { if (!string.IsNullOrWhiteSpace(shipData.Ship.Location)) { using var httpResult = await _http.PostAsJsonAsync("/my/flight-plans", new FlightRequest { ShipId = shipData.ServerId, Destination = args[2].ToUpper() }); if (httpResult.IsSuccessStatusCode) { var flightResult = await httpResult.Content.ReadFromJsonAsync <FlightResponse>(_serializerOptions); _shipProvider.AddFlightPlan(shipData.ServerId, flightResult.FlightPlan); if (!background) { _console.WriteLine("Flight started successfully. Destination: " + args[2].ToUpper() + "."); } return(CommandResult.SUCCESS); } else { var error = await httpResult.Content.ReadFromJsonAsync <ErrorResponse>(_serializerOptions); if (error.Error.Message.ToLower().Contains("ship destination is same as departure")) { if (!background) { _console.WriteLine("Ship is already docked in specified location."); } return(CommandResult.SUCCESS); } else if (error.Error.Message.StartsWith("Destination does not exist.")) { _console.WriteLine("Destination does not exist. Please check destination and try again."); } else { await _shipProvider.RefreshShipData(); _console.WriteLine(error.Error.Message); } } } else { _console.WriteLine("Ship is already in transit on an existing flight plan."); } } else { _console.WriteLine("Invalid ship id. Please use number ids and not the full string id."); } return(CommandResult.FAILURE); } else if (args.Length == 2 && args[1].ToLower() == "warp") { if (_shipProvider.TryGetShipDataByLocalId(args[0], out var shipData)) { if (!string.IsNullOrWhiteSpace(shipData.Ship.Location)) { using var httpResult = await _http.PostAsJsonAsync("/my/warp-jumps", new WarpRequest { ShipId = shipData.ServerId }); if (httpResult.IsSuccessStatusCode) { var flightResult = await httpResult.Content.ReadFromJsonAsync <FlightResponse>(_serializerOptions); _shipProvider.AddFlightPlan(shipData.ServerId, flightResult.FlightPlan); if (!background) { _console.WriteLine("Warp started successfully. Destination: " + flightResult.FlightPlan.Destination + "."); } return(CommandResult.SUCCESS); } else { var error = await httpResult.Content.ReadFromJsonAsync <ErrorResponse>(_serializerOptions); if (error.Error.Message.ToLower().Contains("ship was lost or destroyed upon entering the wormhole")) { _console.WriteLine("Ship was lost while attempting to traverse the wormhole."); _console.WriteLine("In an op shop somewhere, another teapot is sold."); await _shipProvider.RefreshShipData(); } else if (error.Error.Message.StartsWith("Destination does not exist.")) { _console.WriteLine("Destination does not exist. Please check destination and try again."); } else { await _shipProvider.RefreshShipData(); _console.WriteLine(error.Error.Message); } } } else { _console.WriteLine("Ship is already in transit on an existing flight plan."); } } else { _console.WriteLine("Invalid ship id. Please use number ids and not the full string id."); } return(CommandResult.FAILURE); } else if (!background && args.Length == 3 && args[1].ToLower() == "rename") { if (_shipProvider.TryGetShipDataByLocalId(args[0], out var shipData)) { var lastName = shipData.DisplayName; _shipProvider.UpdateShipName(shipData.ServerId, args[2]); _console.WriteLine("Ship " + lastName + " renamed to " + shipData.DisplayName + "."); return(CommandResult.SUCCESS); } else { _console.WriteLine("Invalid ship id. Please use number ids and not the full string id."); } return(CommandResult.FAILURE); } else if (!background && args.Length == 5 && args[1].ToLower() == "transfer") { if (_shipProvider.TryGetShipDataByLocalId(args[0], out var shipData)) { if (_shipProvider.TryGetShipDataByLocalId(args[4], out var shipToData)) { if (shipData.ServerId != shipToData.ServerId) { if (shipData.Ship.Location != null && shipData.Ship.Location == shipToData.Ship.Location) { var cargo = shipData.Ship.Cargo.FirstOrDefault(t => t.Good.ToUpper() == args[2].ToUpper()); if (cargo != null) { if (int.TryParse(args[3], out int quantity) && quantity > 0) { if (quantity > cargo.Quantity) { quantity = cargo.Quantity; } using var httpResult = await _http.PostAsJsonAsync("/my/ships/" + shipData.ServerId + "/transfer", new TransferRequest { Good = cargo.Good, Quantity = quantity, ToShipId = shipToData.ServerId }); if (httpResult.IsSuccessStatusCode) { var transferResult = await httpResult.Content.ReadFromJsonAsync <TransferResponse>(_serializerOptions); _shipProvider.UpdateShipCargo(transferResult.FromShip.Id, transferResult.FromShip.Cargo); _shipProvider.UpdateShipCargo(transferResult.ToShip.Id, transferResult.ToShip.Cargo); _console.WriteLine(quantity + " units of " + cargo.Good + " transferred from " + shipData.DisplayName + " to " + shipToData.DisplayName + "."); return(CommandResult.SUCCESS); } else { var error = await httpResult.Content.ReadFromJsonAsync <ErrorResponse>(_serializerOptions); await _shipProvider.RefreshShipData(); _console.WriteLine(error.Error.Message); } } else { _console.WriteLine("Invalid quantity provided. Please provide a valid number greater than 0."); } } else { _console.WriteLine("Invalid cargo type. Please specify a cargo the ship contains."); } } else { _console.WriteLine("Ships must be docked in the same location to transfer."); } } else { _console.WriteLine("You cannot transfer cargo to the same ship."); } } else { _console.WriteLine("Invalid ship id for ship to transfer to. Please use number ids and not the full string id."); } } else { _console.WriteLine("Invalid ship id for ship to transfer from. Please use number ids and not the full string id."); } return(CommandResult.FAILURE); } else if (!background && args.Length == 4 && args[1].ToLower() == "jettison") { if (_shipProvider.TryGetShipDataByLocalId(args[0], out var shipData)) { var cargo = shipData.Ship.Cargo.FirstOrDefault(t => t.Good.ToUpper() == args[2].ToUpper()); if (cargo != null) { if (int.TryParse(args[3], out int quantity) && quantity > 0) { if (quantity > cargo.Quantity) { quantity = cargo.Quantity; } using var httpResult = await _http.PostAsJsonAsync("/my/ships/" + shipData.ServerId + "/jettison", new JettisonRequest { Good = cargo.Good, Quantity = quantity }); if (httpResult.IsSuccessStatusCode) { var jettisonResult = await httpResult.Content.ReadFromJsonAsync <JettisonResponse>(_serializerOptions); var cargoToAdd = shipData.Ship.Cargo.ToList(); cargo.Quantity -= quantity; if (cargo.Quantity <= 0) { cargoToAdd.Remove(cargo); } _shipProvider.UpdateShipCargo(shipData.ServerId, cargoToAdd.ToArray()); _console.WriteLine(quantity + " units of " + cargo.Good + " jettisoned. " + jettisonResult.QuantityRemaining + " units remaining."); return(CommandResult.SUCCESS); } else { var error = await httpResult.Content.ReadFromJsonAsync <ErrorResponse>(_serializerOptions); await _shipProvider.RefreshShipData(); _console.WriteLine(error.Error.Message); } } else { _console.WriteLine("Invalid quantity provided. Please provide a valid number greater than 0."); } } else { _console.WriteLine("Invalid cargo type. Please specify a cargo the ship contains."); } } else { _console.WriteLine("Invalid ship id. Please use number ids and not the full string id."); } return(CommandResult.FAILURE); } else if (!background && args.Length == 2 && args[1].ToLower() == "scrap") { if (_shipProvider.TryGetShipDataByLocalId(args[0], out var shipData)) { using var httpResult = await _http.DeleteAsync("/my/ships/" + shipData.ServerId); if (httpResult.IsSuccessStatusCode) { var name = shipData.DisplayName; var message = await httpResult.Content.ReadFromJsonAsync <ScrapResponse>(_serializerOptions); if (_navManager.Uri.EndsWith("/map/" + shipData.ServerId)) { _navManager.NavigateTo(_navManager.BaseUri + "/map/"); } _ = _shipProvider.RefreshShipData(); _ = _userInfo.RefreshData(); _console.WriteLine("Ship " + name + " has been scrapped."); _console.WriteLine(message.Success); return(CommandResult.SUCCESS); } else { var error = await httpResult.Content.ReadFromJsonAsync <ErrorResponse>(_serializerOptions); await _shipProvider.RefreshShipData(); _console.WriteLine(error.Error.Message); } } else { _console.WriteLine("Invalid ship id. Please use number ids and not the full string id."); } return(CommandResult.FAILURE); } else if (args.Length == 2 && args[1].ToLower() == "info") { if (_shipProvider.TryGetShipDataByLocalId(args[0], out var shipData)) { _console.WriteLine("Displaying info for " + shipData.DisplayName + "."); await _console.WriteLine("Server Id: " + shipData.ServerId, 500); await _console.WriteLine("Type: " + shipData.Ship.Type, 500); await _console.WriteLine("Class: " + shipData.Ship.Class, 100); await _console.WriteLine("Manufacturer: " + shipData.Ship.Manufacturer, 100); await _console.WriteLine("Cargo Capacity: " + shipData.Ship.MaxCargo, 100); await _console.WriteLine("Speed: " + shipData.Ship.Speed, 100); await _console.WriteLine("Plating: " + shipData.Ship.Plating, 100); await _console.WriteLine("Weapons: " + shipData.Ship.Weapons, 100); return(CommandResult.SUCCESS); } else { _console.WriteLine("Invalid ship id. Please use number ids and not the full string id."); } return(CommandResult.FAILURE); } return(CommandResult.INVALID); }