Exemplo n.º 1
0
        private void UpdateOrderPrice(BuyOrderType orderType, string marketNameOrId, string itemName, decimal newPrice)
        {
            var character = Context.Player.Character;

            if (character == null)
            {
                Context.Respond("You cannot do this while dead.");
                return;
            }

            if (!DefinitionResolver.TryGetDefinitionByName(itemName, out var itemDefinition))
            {
                Context.Respond($"Unable to find an item by the name of {itemName}");
                return;
            }

            if (newPrice < new decimal(0.01))
            {
                Context.Respond($"{newPrice} is too small. Please use a higher value.");
                return;
            }

            var manager      = EconomyPlugin.GetManager <MarketManager>();
            var orderManager = EconomyPlugin.GetManager <MarketOrderManager>();

            manager.GetMarkets()
            .Then(markets =>
            {
                MarketDataObject market = null;
                if (long.TryParse(marketNameOrId, out var marketId))
                {
                    market = markets.FirstOrDefault(m => m.Id == marketId);
                }
                else
                {
                    market = markets.FirstOrDefault(m =>
                                                    m.Name.Equals(marketNameOrId, StringComparison.InvariantCultureIgnoreCase));
                }

                if (market == null)
                {
                    Context.Respond($"Unable to find market by name or id of '{marketNameOrId}'.");
                    return;
                }

                orderManager.GetMarketOrder(orderType, market.Id, itemDefinition.Id)
                .Then(order =>
                {
                    orderManager.UpdateOrderPrice(order.Id, newPrice)
                    .Then(() => Context.Respond(
                              $"Successfully updated Order#{order.Id}'s price to {Utilities.FriendlyFormatCurrency(newPrice)}."))
                    .Catch(HandleError);
                })
                .Catch(HandleError);
            })
            .Catch(HandleError);
        }
Exemplo n.º 2
0
        private void CreateOrder(BuyOrderType orderType, string marketNameOrId, string itemName, decimal pricePerOne,
                                 decimal quantity)
        {
            var character = Context.Player.Character;

            if (character == null)
            {
                Context.Respond("You cannot do this while dead.");
                return;
            }

            if (quantity < new decimal(0.01))
            {
                Context.Respond($"{quantity} is too low. Please use a higher value.");
                return;
            }

            if (pricePerOne < new decimal(0.01))
            {
                Context.Respond($"{pricePerOne} is too low. Please use a higher value.");
                return;
            }

            if (!DefinitionResolver.TryGetDefinitionByName(itemName, out var itemDefinition))
            {
                Context.Respond($"Unable to find an item by the name of {itemName}");
                return;
            }

            var manager      = EconomyPlugin.GetManager <MarketManager>();
            var orderManager = EconomyPlugin.GetManager <MarketOrderManager>();

            manager.GetMarketByNameOrId(marketNameOrId, Context.Player.SteamUserId)
            .Then(market =>
            {
                if (market == null)
                {
                    Context.Respond($"Unable to find a market by the name or id of {marketNameOrId}.");
                    return;
                }

                orderManager.UpdateOrAddMarketOrder(orderType, market.Id,
                                                    itemDefinition.Id, pricePerOne, quantity)
                .Then(order =>
                {
                    Context.Respond(
                        $"Successfully created Order#{order.Id} for {quantity}x {itemDefinition.DisplayNameText} @ {Utilities.FriendlyFormatCurrency(pricePerOne)} per 1.");
                })
                .Catch(HandleError);
            })
            .Catch(HandleError);
        }
Exemplo n.º 3
0
        private void UpdateOrderQuantity(BuyOrderType orderType, string marketNameOrId, string itemName, decimal newQuantity)
        {
            var character = Context.Player.Character;

            if (character == null)
            {
                Context.Respond("You cannot do this while dead.");
                return;
            }

            if (!DefinitionResolver.TryGetDefinitionByName(itemName, out var itemDefinition))
            {
                Context.Respond($"Unable to find an item by the name of {itemName}");
                return;
            }

            if (newQuantity < new decimal(0.01))
            {
                Context.Respond($"{newQuantity} is too small. Please use a higher value.");
                return;
            }

            var manager      = EconomyPlugin.GetManager <MarketManager>();
            var orderManager = EconomyPlugin.GetManager <MarketOrderManager>();

            manager.GetMarketByNameOrId(marketNameOrId, Context.Player.SteamUserId)
            .Then(market =>
            {
                if (market == null)
                {
                    Context.Respond($"Unable to find market by name or id of '{marketNameOrId}'.");
                    return;
                }

                orderManager.GetMarketOrder(orderType, market.Id, itemDefinition.Id)
                .Then(order =>
                {
                    orderManager.UpdateOrderQuantity(order.Id, newQuantity)
                    .Then(() => Context.Respond(
                              $"Successfully updated Order#{order.Id}'s quantity to {newQuantity}."))
                    .Catch(HandleError);
                })
                .Catch(HandleError);
            })
            .Catch(HandleError);
        }
Exemplo n.º 4
0
        private void RemoveOrder(BuyOrderType orderType, string marketNameOrId, string itemName)
        {
            var character = Context.Player.Character;

            if (character == null)
            {
                Context.Respond("You cannot do this while dead.");
                return;
            }

            if (!DefinitionResolver.TryGetDefinitionByName(itemName, out var itemDefinition))
            {
                Context.Respond($"Unable to find an item by the name of {itemName}");
                return;
            }

            var manager      = EconomyPlugin.GetManager <MarketManager>();
            var orderManager = EconomyPlugin.GetManager <MarketOrderManager>();

            manager.GetMarketByNameOrId(marketNameOrId, Context.Player.SteamUserId)
            .Then(market =>
            {
                if (market == null)
                {
                    Context.Respond($"Unable to find market by name or id of '{marketNameOrId}'.");
                    return;
                }

                orderManager.GetMarketOrder(orderType, market.Id, itemDefinition.Id)
                .Then(order =>
                {
                    orderManager.DeleteOrder(order.Id)
                    .Then(() => Context.Respond(
                              $"Successfully removed Order#{order.Id}."))
                    .Catch(HandleError);
                })
                .Catch(HandleError);
            })
            .Catch(HandleError);
        }
Exemplo n.º 5
0
        public void Sell(string itemName, decimal quantity)
        {
            var character = Context.Player.Character;

            if (character == null)
            {
                Context.Respond("You cannot do this while dead.");
                return;
            }

            if (quantity < new decimal(0.01))
            {
                Context.Respond($"{quantity} is too low. Please use a higher value.");
                return;
            }

            if (!DefinitionResolver.TryGetDefinitionByName(itemName, out var itemDefinition))
            {
                Context.Respond($"Unable to find an item by the name of {itemName}");
                return;
            }

            var controllingCube = Context.Player.Controller.ControlledEntity as IMyCubeBlock;

            if (controllingCube == null)
            {
                Context.Respond("Trading by hand is not supported.");
                return;
            }


            var marketManager  = GetManager <MarketManager>();
            var orderManager   = GetManager <MarketOrderManager>();
            var accountManager = GetManager <AccountsManager>();

            marketManager.GetConnectedMarket(controllingCube.CubeGrid)
            .Then(market =>
            {
                if (market == null)
                {
                    Context.Respond("Unable to find any connected markets. Have you docked to a market?");
                    return;
                }

                orderManager.GetMarketOrder(BuyOrderType.Buy, market.Id, itemDefinition.Id)
                .Then(order =>
                {
                    if (order == null)
                    {
                        Context.Respond($"Unable to find any buy orders for {itemDefinition.DisplayNameText}.");
                        return;
                    }

                    if (order.Quantity != -1 && order.Quantity < quantity)
                    {
                        Context.Respond($"Trying to sell more than the order desires by {quantity - order.Quantity}. Buy order only wants {order.Quantity}.");
                        return;
                    }

                    if (order.Quantity != -1)
                    {
                        // If quantity isn't infinite. Update order quantity.
                        orderManager.UpdateOrderQuantity(order.Id, order.Quantity - quantity)
                        .Catch(error => Log.Error(error));
                    }

                    // Actually do the transfer... f**k.
                    var stationGrid = MyAPIGateway.Entities.GetEntityById(market.ParentGridId);

                    accountManager.GetPrimaryAccount(Context.Player.SteamUserId)
                    .Then(account =>
                    {
                        var sellAmount = quantity * order.Price;
                        TransferInventory(controllingCube.CubeGrid, stationGrid as IMyCubeGrid,
                                          itemDefinition as MyPhysicalItemDefinition, quantity, false, market.IsNPC)
                        .Then(() =>
                        {
                            accountManager.AdjustAccountBalance(account.Id, sellAmount,
                                                                market.AccountId,
                                                                $"Exchanged {quantity}x {itemDefinition.DisplayNameText} for {Utilities.FriendlyFormatCurrency(sellAmount)}.");
                            Context.Respond($"Exchanged {quantity}x {itemDefinition.DisplayNameText} for {Utilities.FriendlyFormatCurrency(sellAmount)}.");
                        })
                        .Catch(HandleError);
                    })
                    .Catch(HandleError);
                })
                .Catch(HandleError);
            })
            .Catch(HandleError);
        }