Example #1
0
        public async Task MarketChannelTask(string channel)
        {
            channel = channel.Remove(channel.Length - 1, 1);
            channel = channel.Remove(0, 2);

            StockMarketChannel channelObj  = new StockMarketChannel(channel, Context.Guild.Id.ToString());
            JObject            JSONChannel = db.SerializeObject <StockMarketChannel>(channelObj);
            await db.SetJObjectAsync(JSONChannel, "system");

            await CommandService.PostMessageTask(channel, "This channel has been set as the transaction announcement channel!");

            await ReplyAsync($"Market channel set to <#{channel}>");
        }
        public async Task schedule(DataBaseHandlingService db, CommandHandlingService CommandService, StockMarketService marketService)
        {
            if (plannedEnd.Ticks <= DateTime.Now.Ticks)
            {
                Industry ind = new Industry(await db.getJObjectAsync(this.industryID, "industries"));
                ind.CompanyId = this.currentWinner;
                await db.SetJObjectAsync(ind.SerializeIntoJObject(), "industries");

                await db.RemoveObjectAsync(this.id, "transactions");

                string markchan = (string)await db.GetFieldAsync("MarketChannel", "channel", "system");

                await CommandService.deleteMessageTask(markchan, this.messageID);

                await CommandService.PostMessageTask(markchan, $"Auction with ID {this.id} has been won by <@{(string)await db.GetFieldAsync(this.currentWinner, "name", "companies")}>!");

                return;
            }
            JobDataMap map = new JobDataMap();

            map.Add("market", marketService);
            map.Add("auction", this);
            map.Add("command", CommandService);
            map.Add("db", db);
            IJobDetail job     = JobBuilder.Create <Job>().SetJobData(map).Build();
            ITrigger   trigger = TriggerBuilder.Create().WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromMilliseconds(1)).WithRepeatCount(1)).StartAt(plannedEnd).Build();
            await CommandService.scheduler.ScheduleJob(job, trigger);
        }
            public async Task Execute(IJobExecutionContext context)
            {
                /*Industry ind = new Industry(await db.getJObjectAsync(auction.industryID, "industries"));
                 * ind.CompanyId = auction.currentWinner;
                 * await db.SetJObjectAsync(ind.SerializeIntoJObject(), "industries");
                 * string markchan = (string)await db.GetFieldAsync("MarketChannel", "channel", "system");
                 *
                 * await comm.deleteMessageTask(markchan, auction.messageID);
                 *
                 * await comm.PostMessageTask(markchan, $"Auction with ID {auction.id} has been accepted by <@{(string)await db.GetFieldAsync(auction.currentWinner, "name", "companies")}>!");
                 * await db.RemoveObjectAsync(auction.id, "transactions");*/
                IndustryAuction         auction = (IndustryAuction)context.JobDetail.JobDataMap.Get("auction");
                StockMarketService      market  = (StockMarketService)context.JobDetail.JobDataMap.Get("market");
                CommandHandlingService  comm    = (CommandHandlingService)context.JobDetail.JobDataMap.Get("command");
                DataBaseHandlingService db      = (DataBaseHandlingService)context.JobDetail.JobDataMap.Get("db");

                auction = new IndustryAuction(await db.getJObjectAsync(auction.id, "transactions"));
                Industry ind = new Industry(await db.getJObjectAsync(auction.industryID, "industries"));

                ind.CompanyId = auction.currentWinner;
                await db.SetJObjectAsync(ind.SerializeIntoJObject(), "industries");

                await db.RemoveObjectAsync(auction.id, "transactions");

                string markchan = (string)await db.GetFieldAsync("MarketChannel", "channel", "system");

                await comm.deleteMessageTask(markchan, auction.messageID);

                await comm.PostMessageTask(markchan, $"Auction with ID {auction.id} has been accepted by {(string)await db.GetFieldAsync(auction.currentWinner, "name", "companies")}!");
            }
Example #4
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");
            }
        }
Example #5
0
 public async Task EchoAsync(ITextChannel channel, [Remainder] string contents)
 {
     await comhan.PostMessageTask(channel.Id.ToString(), contents);
 }