Пример #1
0
            public async Task ExecuteGroupAsync(CommandContext ctx)
            {
                if (this.Service.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    if (this.Service.GetEventInChannel(ctx.Channel.Id) is AnimalRace)
                    {
                        await this.JoinAsync(ctx);
                    }
                    else
                    {
                        throw new CommandFailedException(ctx, "cmd-err-evt-dup");
                    }
                    return;
                }

                var game = new AnimalRace(ctx.Client.GetInteractivity(), ctx.Channel);

                this.Service.RegisterEventInChannel(game, ctx.Channel.Id);
                try {
                    await ctx.ImpInfoAsync(this.ModuleColor, Emojis.Clock1, "str-game-ar-start", AnimalRace.MaxParticipants);

                    await this.JoinAsync(ctx);

                    await Task.Delay(TimeSpan.FromSeconds(30));

                    if (game.ParticipantCount > 1)
                    {
                        GameStatsService gss = ctx.Services.GetRequiredService <GameStatsService>();
                        await game.RunAsync(this.Localization);

                        if (game.WinnerIds is { })
                        {
                            await Task.WhenAll(game.WinnerIds.Select(w => gss.UpdateStatsAsync(w, s => s.AnimalRacesWon++)));
                        }
                    }
Пример #2
0
 public Animal(string name)
 {
     race       = AnimalRace.Creature;
     this.name  = name;
     home       = null;
     resistance = (int)size;
 }
Пример #3
0
            public Task Race()
            {
                var ar = new AnimalRace(_cs, _bc.BotConfig.RaceAnimals.Shuffle().ToArray());

                if (!_service.AnimalRaces.TryAdd(Context.Guild.Id, ar))
                {
                    return(Context.Channel.SendErrorAsync(GetText("animal_race"), GetText("animal_race_already_started")));
                }

                ar.Initialize();

                var count = 0;

                Task _client_MessageReceived(SocketMessage arg)
                {
                    var _ = Task.Run(() => {
                        try
                        {
                            if (arg.Channel.Id == Context.Channel.Id)
                            {
                                if (ar.CurrentPhase == AnimalRace.Phase.Running && ++count % 9 == 0)
                                {
                                    raceMessage = null;
                                }
                            }
                        }
                        catch { }
                    });

                    return(Task.CompletedTask);
                }

                Task Ar_OnEnded(AnimalRace race)
                {
                    _client.MessageReceived -= _client_MessageReceived;
                    _service.AnimalRaces.TryRemove(Context.Guild.Id, out _);
                    var winner = race.FinishedUsers[0];

                    if (race.FinishedUsers[0].Bet > 0)
                    {
                        return(Context.Channel.SendConfirmAsync(GetText("animal_race"),
                                                                GetText("animal_race_won_money", Format.Bold(winner.Username),
                                                                        winner.Animal.Icon, (race.FinishedUsers[0].Bet * (race.Users.Length - 1)) + _bc.BotConfig.CurrencySign)));
                    }
                    else
                    {
                        return(Context.Channel.SendConfirmAsync(GetText("animal_race"),
                                                                GetText("animal_race_won", Format.Bold(winner.Username), winner.Animal.Icon)));
                    }
                }

                ar.OnStartingFailed     += Ar_OnStartingFailed;
                ar.OnStateUpdate        += Ar_OnStateUpdate;
                ar.OnEnded              += Ar_OnEnded;
                ar.OnStarted            += Ar_OnStarted;
                _client.MessageReceived += _client_MessageReceived;

                return(Context.Channel.SendConfirmAsync(GetText("animal_race"), GetText("animal_race_starting"),
                                                        footer: GetText("animal_race_join_instr", Prefix)));
            }
Пример #4
0
            public async Task Race()
            {
                var ar = new AnimalRace(Context.Guild.Id, (ITextChannel)Context.Channel);

                if (ar.Fail)
                {
                    await Context.Channel.SendErrorAsync("🏁 `Failed starting a race. Another race is probably running.`").ConfigureAwait(false);
                }
            }
Пример #5
0
            public async Task Race(IUserMessage umsg)
            {
                var channel = (ITextChannel)umsg.Channel;

                var ar = new AnimalRace(channel.Guild.Id, channel);

                if (ar.Fail)
                    await channel.SendMessageAsync("🏁 `Failed starting a race. Another race is probably running.`");
            }
Пример #6
0
            public async Task Race()
            {
                var ar = new AnimalRace(Context.Guild.Id, (ITextChannel)Context.Channel, Prefix);

                if (ar.Fail)
                {
                    await ReplyErrorLocalized("race_failed_starting").ConfigureAwait(false);
                }
            }
Пример #7
0
            private async Task Ar_OnStateUpdate(AnimalRace race)
            {
                var text = $@"|🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🏁🔚|
{String.Join("\n", race.Users.Select(p =>
                {
                    var index = race.FinishedUsers.IndexOf(p);
                    var extra = (index == -1 ? "" : $"#{index + 1} {(index == 0 ? "🏆" : "")}");
                    return $"{(int)(p.Progress / 60f * 100),-2}%|{new string('‣', p.Progress) + p.Animal.Icon + extra}";
                }))}
Пример #8
0
            public async Task Race(IUserMessage umsg)
            {
                var channel = (ITextChannel)umsg.Channel;

                var ar = new AnimalRace(channel.Guild.Id, channel);

                if (ar.Fail)
                {
                    await channel.SendMessageAsync("🏁 `Failed starting a race. Another race is probably running.`");
                }
            }
Пример #9
0
 private Task Ar_OnStarted(AnimalRace race)
 {
     if (race.Users.Length == race.MaxUsers)
     {
         return(Context.Channel.SendConfirmAsync(GetText("animal_race"), GetText("animal_race_full")));
     }
     else
     {
         return(Context.Channel.SendConfirmAsync(GetText("animal_race"), GetText("animal_race_starting_with_x", race.Users.Length)));
     }
 }
 private bool checkValues(AnimalRace animalRace)
 {
     if (string.IsNullOrEmpty(animalRace.Name))
     {
         TempData["Message"] = "Por favor insira um nome para a raça!";
         return(false);
     }
     if (animalRace.AnimalRaceID <= -1)
     {
         TempData["Message"] = "Algo de errado aconteceu!";
         return(false);
     }
     return(true);
 }
Пример #11
0
        internal override void Init(CommandGroupBuilder cgb)
        {
            cgb.CreateCommand(Prefix + "race")
            .Description($"Starts a new animal race. | `{Prefix}race`")
            .Do(e =>
            {
                var ar = new AnimalRace(e.Server.Id, e.Channel);
                if (ar.Fail)
                {
                    return;
                }
            });


            cgb.CreateCommand(Prefix + "joinrace")
            .Alias(Prefix + "jr")
            .Description($"Joins a new race. You can specify an amount of flowers for betting (optional). You will get YourBet*(participants-1) back if you win. | `{Prefix}jr` or `{Prefix}jr 5`")
            .Parameter("amount", ParameterType.Optional)
            .Do(async e =>
            {
                int amount;
                if (!int.TryParse(e.GetArg("amount"), out amount) || amount < 0)
                {
                    amount = 0;
                }

                var userFlowers = GamblingModule.GetUserFlowers(e.User.Id);

                if (userFlowers < amount)
                {
                    await e.Channel.SendMessage($"{e.User.Mention} You don't have enough {NadekoBot.Config.CurrencyName}s. You only have {userFlowers}{NadekoBot.Config.CurrencySign}.").ConfigureAwait(false);
                    return;
                }

                if (amount > 0)
                {
                    await FlowersHandler.RemoveFlowers(e.User, "BetRace", (int)amount, true).ConfigureAwait(false);
                }

                AnimalRace ar;
                if (!AnimalRaces.TryGetValue(e.Server.Id, out ar))
                {
                    await e.Channel.SendMessage("No race exists on this server");
                    return;
                }
                await ar.JoinRace(e.User, amount);
            });
        }
Пример #12
0
        internal override void Init(CommandGroupBuilder cgb)
        {
            cgb.CreateCommand(Prefix + "race")
            .Description($"Startet ein neues Tier-Rennen. | `{Prefix}race`")
            .Do(e =>
            {
                var ar = new AnimalRace(e.Server.Id, e.Channel);
                if (ar.Fail)
                {
                    return;
                }
            });

            cgb.CreateCommand(Prefix + "joinrace")
            .Alias(Prefix + "jr")
            .Description($"Tritt einem Rennen bei. Du kannst eine Anzahl an {MidnightBot.Config.CurrencyName} zum Wetten setzen (Optional). Du bekommst deine Wette*(Teilnehmer-1) zurück, wenn du gewinnst. | `{Prefix}jr` oder `{Prefix}jr 5`")
            .Parameter("amount", ParameterType.Optional)
            .Do(async e =>
            {
                int amount;
                if (!int.TryParse(e.GetArg("amount"), out amount) || amount < 0)
                {
                    amount = 0;
                }

                var userFlowers = GamblingModule.GetUserFlowers(e.User.Id);

                if (userFlowers < amount)
                {
                    await e.Channel.SendMessage($"{e.User.Mention} Du hast nicht genug {MidnightBot.Config.CurrencyName}. Du hast nur {userFlowers}{MidnightBot.Config.CurrencySign}.").ConfigureAwait(false);
                    return;
                }

                if (amount > 0)
                {
                    await FlowersHandler.RemoveFlowers(e.User, "BetRace", (int)amount, true).ConfigureAwait(false);
                }

                AnimalRace ar;

                if (!AnimalRaces.TryGetValue(e.Server.Id, out ar))
                {
                    await e.Channel.SendMessage("Es existiert kein Rennen auf diesem Server.");
                    return;
                }
                await ar.JoinRace(e.User, amount);
            });
        }
Пример #13
0
        public void BuyAnimal(AnimalRace desiredRace)
        {
            if (!Prices.animalPrices.ContainsKey(desiredRace))
            {
                System.Console.WriteLine("\n No animals like that are currently being sold\n");
                return;
            }

            int cost = Prices.animalPrices[desiredRace];

            Animal boi;
            bool   success;

            // TODO

            if (SpendMoney(cost))
            {
                switch (desiredRace)
                {
                case AnimalRace.Chicken:
                    boi = new Chicken();
                    break;

                case AnimalRace.Pig:
                    boi = new Pig();
                    break;

                case AnimalRace.Cow:
                    boi = new Cow();
                    break;

                default:
                    boi = new Animal();
                    break;
                }

                animals.Add(boi);
                animalAmount += 1;
                success       = true;
            }
            else
            {
                success = false;
            }

            System.Console.WriteLine(PurchaseResult(("" + desiredRace).ToLower(), success, cost));
        }
Пример #14
0
        internal override void Init(CommandGroupBuilder cgb)
        {
            cgb.CreateCommand(Prefix + "race")
                .Description($"Starts a new animal race. | `{Prefix}race`")
                .Do(e =>
                {
                    var ar = new AnimalRace(e.Server.Id, e.Channel);
                    if (ar.Fail)
                    {
                        return;
                    }
                });


            cgb.CreateCommand(Prefix + "joinrace")
                .Alias(Prefix + "jr")
                .Description($"Joins a new race. You can specify an amount of flowers for betting (optional). You will get YourBet*(participants-1) back if you win. | `{Prefix}jr` or `{Prefix}jr 5`")
                .Parameter("amount", ParameterType.Optional)
                .Do(async e =>
                {

                    int amount;
                    if (!int.TryParse(e.GetArg("amount"), out amount) || amount < 0)
                        amount = 0;

                    var userFlowers = GamblingModule.GetUserFlowers(e.User.Id);

                    if (userFlowers < amount)
                    {
                        await e.Channel.SendMessage($"{e.User.Mention} You don't have enough {NadekoBot.Config.CurrencyName}s. You only have {userFlowers}{NadekoBot.Config.CurrencySign}.").ConfigureAwait(false);
                        return;
                    }

                    if (amount > 0)
                        await FlowersHandler.RemoveFlowers(e.User, "BetRace", (int)amount, true).ConfigureAwait(false);

                    AnimalRace ar;
                    if (!AnimalRaces.TryGetValue(e.Server.Id, out ar))
                    {
                        await e.Channel.SendMessage("No race exists on this server");
                        return;
                    }
                    await ar.JoinRace(e.User, amount);

                });
        }
            private Task Ar_OnEnded(AnimalRace race)
            {
                AnimalRaces.TryRemove(Context.Guild.Id, out _);
                var winner = race.FinishedUsers[0];

                if (race.FinishedUsers[0].Bet > 0)
                {
                    return(Context.Channel.SendConfirmAsync(GetText("animal_race"),
                                                            GetText("animal_race_won_money", Format.Bold(winner.Username),
                                                                    winner.Animal.Icon, (race.FinishedUsers[0].Bet * (race.Users.Length - 1)) + _bc.BotConfig.CurrencySign)));
                }
                else
                {
                    return(Context.Channel.SendConfirmAsync(GetText("animal_race"),
                                                            GetText("animal_race_won", Format.Bold(winner.Username), winner.Animal.Icon)));
                }
            }
            public Task Race()
            {
                var ar = new AnimalRace(_cs, _bc.BotConfig.RaceAnimals.Shuffle().ToArray());

                if (!AnimalRaces.TryAdd(Context.Guild.Id, ar))
                {
                    return(Context.Channel.SendErrorAsync(GetText("animal_race"), GetText("animal_race_already_started")));
                }
                ar.Initialize();

                ar.OnStartingFailed += Ar_OnStartingFailed;
                ar.OnStateUpdate    += Ar_OnStateUpdate;
                ar.OnEnded          += Ar_OnEnded;
                ar.OnStarted        += Ar_OnStarted;

                return(Context.Channel.SendConfirmAsync(GetText("animal_race"), GetText("animal_race_starting"),
                                                        footer: GetText("animal_race_join_instr", Prefix)));
            }
            public async Task ExecuteGroupAsync(CommandContext ctx)
            {
                if (this.Shared.IsEventRunningInChannel(ctx.Channel.Id))
                {
                    if (this.Shared.GetEventInChannel(ctx.Channel.Id) is AnimalRace)
                    {
                        await this.JoinAsync(ctx);
                    }
                    else
                    {
                        throw new CommandFailedException("Another event is already running in the current channel.");
                    }
                    return;
                }

                var game = new AnimalRace(ctx.Client.GetInteractivity(), ctx.Channel);

                this.Shared.RegisterEventInChannel(game, ctx.Channel.Id);
                try {
                    await this.InformAsync(ctx, StaticDiscordEmoji.Clock1, $"The race will start in 30s or when there are 10 participants. Use command {Formatter.InlineCode("game animalrace")} to join the race.");

                    await this.JoinAsync(ctx);

                    await Task.Delay(TimeSpan.FromSeconds(30));

                    if (game.ParticipantCount > 1)
                    {
                        await game.RunAsync();

                        foreach (ulong uid in game.WinnerIds)
                        {
                            await this.Database.UpdateStatsAsync(uid, s => s.AnimalRacesWon++);
                        }
                    }
                    else
                    {
                        await this.InformAsync(ctx, StaticDiscordEmoji.AlarmClock, "Not enough users joined the race.");
                    }
                } finally {
                    this.Shared.UnregisterEventInChannel(ctx.Channel.Id);
                }
            }
        public async Task <IActionResult> Create([Bind("AnimalRaceID,Name")] AnimalRace animalRace)
        {
            if (!GetAuthorization(7, 'c'))
            {
                return(NotFound());
            }
            ViewBag.Permission = getPermissions();
            if (!checkValues(animalRace))
            {
                return(View(animalRace));
            }
            if (ModelState.IsValid)
            {
                _context.Add(animalRace);
                await _context.SaveChangesAsync();

                TempData["Message"] = "Raça de animal criado com sucesso!";
                return(RedirectToAction(nameof(Index)));
            }
            return(View(animalRace));
        }
 public async Task <IActionResult> Edit(int id, [Bind("AnimalRaceID,Name")] AnimalRace animalRace)
 {
     if (!GetAuthorization(7, 'u'))
     {
         return(NotFound());
     }
     ViewBag.Permission = getPermissions();
     if (id != animalRace.AnimalRaceID)
     {
         return(NotFound());
     }
     if (!checkValues(animalRace))
     {
         return(View(animalRace));
     }
     if (ModelState.IsValid)
     {
         try
         {
             _context.Update(animalRace);
             await _context.SaveChangesAsync();
         }
         catch (DbUpdateConcurrencyException)
         {
             if (!AnimalRaceExists(animalRace.AnimalRaceID))
             {
                 return(NotFound());
             }
             else
             {
                 throw;
             }
         }
         TempData["Message"] = "Raça de animal editada com sucesso!";
         return(RedirectToAction(nameof(Index)));
     }
     return(View(animalRace));
 }
Пример #20
0
            public Task Race(params string[] args)
            {
                var(options, success) = OptionsParser.ParseFrom(new RaceOptions(), args);

                var ar = new AnimalRace(options, _cs, _gamesConf.Data.RaceAnimals.Shuffle());

                if (!_service.AnimalRaces.TryAdd(ctx.Guild.Id, ar))
                {
                    return(ctx.Channel.SendErrorAsync(GetText("animal_race"), GetText("animal_race_already_started")));
                }

                ar.Initialize();

                var count = 0;

                Task _client_MessageReceived(SocketMessage arg)
                {
                    var _ = Task.Run(() =>
                    {
                        try
                        {
                            if (arg.Channel.Id == ctx.Channel.Id)
                            {
                                if (ar.CurrentPhase == AnimalRace.Phase.Running && ++count % 9 == 0)
                                {
                                    raceMessage = null;
                                }
                            }
                        }
                        catch { }
                    });

                    return(Task.CompletedTask);
                }

                Task Ar_OnEnded(AnimalRace race)
                {
                    _client.MessageReceived -= _client_MessageReceived;
                    _service.AnimalRaces.TryRemove(ctx.Guild.Id, out _);
                    var winner = race.FinishedUsers[0];

                    if (race.FinishedUsers[0].Bet > 0)
                    {
                        return(ctx.Channel.SendConfirmAsync(GetText("animal_race"),
                                                            GetText("animal_race_won_money", Format.Bold(winner.Username),
                                                                    winner.Animal.Icon, (race.FinishedUsers[0].Bet * (race.Users.Count - 1)) + CurrencySign)));
                    }
                    else
                    {
                        return(ctx.Channel.SendConfirmAsync(GetText("animal_race"),
                                                            GetText("animal_race_won", Format.Bold(winner.Username), winner.Animal.Icon)));
                    }
                }

                ar.OnStartingFailed     += Ar_OnStartingFailed;
                ar.OnStateUpdate        += Ar_OnStateUpdate;
                ar.OnEnded              += Ar_OnEnded;
                ar.OnStarted            += Ar_OnStarted;
                _client.MessageReceived += _client_MessageReceived;

                return(ctx.Channel.SendConfirmAsync(GetText("animal_race"), GetText("animal_race_starting", options.StartTime),
                                                    footer: GetText("animal_race_join_instr", Prefix)));
            }