示例#1
0
        public async Task BuyOfferAsync([Summary("Company ticker")] string ticker, [Summary("Industry ID")] string industryID, [Summary("Price")] double price)
        {
            string AuthorMoneyt = (string)await _dataBaseService.GetFieldAsync(Context.User.Id.ToString(), "money", "users");

            double AuthorMoney;

            if (price < 0)
            {
                await ReplyAsync("You cant sell for a negative amount of money!");

                return;
            }

            if (AuthorMoneyt == null)
            {
                AuthorMoney = 50000;
                await _dataBaseService.SetFieldAsync(Context.User.Id.ToString(), "money", AuthorMoney, "users");
            }
            else
            {
                AuthorMoney = double.Parse(AuthorMoneyt);
            }

            Industry industry = new Industry(await _dataBaseService.getJObjectAsync(industryID, "industries"));
            Company  company  = await _companyService.getCompany(ticker);

            if (!company.employee.Keys.Contains(Context.User.Id.ToString()))
            {
                await ReplyAsync("You cannot buy industries for corps you are not an employee of.");

                return;
            }
            if (!new List <int> {
                1, 3, 5, 7
            }.Contains(company.employee[Context.User.Id.ToString()].position.manages))
            {
                await ReplyAsync($"You do not have the permission to buy industry in {company.name}");
            }
            IndustryTransaction transaction = new IndustryTransaction(price, "buy", Context.User.Id.ToString(), industryID, _dataBaseService, _commandService);

            try
            {
                JObject tmp = _dataBaseService.SerializeObject(transaction);
                await _dataBaseService.SetJObjectAsync(tmp, "transactions");
                await ReplyAsync($"Sell offer lodged in <#{await _dataBaseService.GetFieldAsync("MarketChannel", "channel", "system")}>");
            }
            catch (Exception e)
            {
                await Log.Logger(Log.Logs.ERROR, e.Message);
                await ReplyAsync("Something went wrong: " + e.Message);
            }
        }
示例#2
0
        public async Task AcceptOfferAsync([Summary("Company Ticker")] string ticker, [Summary("The ID of the offer.")] string offerID)
        {
            Collection <string> IDs = await _dataBaseService.getIDs("transactions");

            string userMoneyt;
            string authorMoneyt;
            double userMoney;
            double authorMoney;

            if (IDs.Contains(offerID) == true)
            {
                IndustryTransaction transaction = new IndustryTransaction(await _dataBaseService.getJObjectAsync(offerID, "transactions"));
                JObject             obj         = await _dataBaseService.getJObjectAsync(transaction.industryID, "industries");

                Industry ind    = new Industry(obj);
                Company  exeCom = await _companyService.getCompany(ticker);

                Company transCom = null;
                if (transaction.type == "auction")
                {
                    await ReplyAsync("You must bid for auctions.");
                }
                if (transaction.type == "buy")
                {
                    transCom = await _companyService.getCompany(ind.CompanyId);

                    if (!exeCom.employee.Keys.Contains(Context.User.Id.ToString()))
                    {
                        await ReplyAsync("You cannot sell industries for corps you are not an employee of.");

                        return;
                    }
                    if (!new List <int> {
                        1, 3, 5, 7
                    }.Contains(exeCom.employee[Context.User.Id.ToString()].position.manages))
                    {
                        await ReplyAsync($"You do not have the permission to sell industry in {exeCom.name}");
                    }
                }
                if (!exeCom.employee.Keys.Contains(Context.User.Id.ToString()))
                {
                    await ReplyAsync("You cannot buy industries for corps you are not an employee of.");

                    return;
                }
                if (!new List <int> {
                    1, 3, 5, 7
                }.Contains(exeCom.employee[Context.User.Id.ToString()].position.manages))
                {
                    await ReplyAsync($"You do not have the permission to buy industry in {exeCom.name}");
                }
                if (transaction.author != Context.User.Id.ToString())
                {
                    // Gets money values of the command user and the transaction author
                    userMoneyt = (string)await _dataBaseService.GetFieldAsync(Context.User.Id.ToString(), "money", "users");

                    if (userMoneyt == null)
                    {
                        userMoney = 50000;
                    }
                    else
                    {
                        userMoney = double.Parse(userMoneyt);
                    }

                    authorMoneyt = (string)await _dataBaseService.GetFieldAsync(transaction.author, "money", "users");

                    if (authorMoneyt == null)
                    {
                        authorMoney = 50000;
                    }
                    else
                    {
                        authorMoney = double.Parse(authorMoneyt);
                    }

                    // Transfers the money
                    if (transaction.type == "buy")
                    {
                        userMoney   += transaction.price;
                        authorMoney -= transaction.price;
                    }
                    else
                    {
                        userMoney   -= transaction.price;
                        authorMoney += transaction.price;
                    }

                    if (transaction.type == "sell")
                    {
                        ind.CompanyId = ticker;
                        await _dataBaseService.SetJObjectAsync(ind.SerializeIntoJObject(), "industries");
                    }
                    else
                    {
                        ind.CompanyId = transCom.id;
                        await _dataBaseService.SetJObjectAsync(ind.SerializeIntoJObject(), "industries");
                    }

                    if (userMoney < 0)
                    {
                        await ReplyAsync("You cannot complete this transaction as it would leave you with a negative amount on money.");
                    }
                    else
                    {
                        await _dataBaseService.SetFieldAsync(Context.User.Id.ToString(), "money", userMoney, "users");

                        await _dataBaseService.SetFieldAsync(transaction.author, "money", authorMoney, "users");

                        await _dataBaseService.RemoveObjectAsync(offerID, "transactions");

                        ITextChannel chnl = (ITextChannel)await Context.Client.GetChannelAsync((UInt64)await _dataBaseService.GetFieldAsync("MarketChannel", "channel", "system"));

                        await chnl.DeleteMessageAsync(Convert.ToUInt64(transaction.messageID));

                        await ReplyAsync("Transaction complete!");

                        await _commandService.PostMessageTask((string)await _dataBaseService.GetFieldAsync("MarketChannel", "channel", "system"), $"<@{transaction.author}>'s Transaction with ID {transaction.id} has been accepted by <@{Context.User.Id}>!");

                        //await transaction.authorObj.SendMessageAsync($"Your transaction with the id {transaction.id} has been completed by {Context.User.Username.ToString()}");
                    }
                }
                else
                {
                    await ReplyAsync("You cannot accept your own transaction!");
                }
            }
            else
            {
                await ReplyAsync("That is not a valid transaction ID");
            }
        }