Exemple #1
0
        private async Task ConsumeAsync()
        {
            BankHeistLevel  heistLevel = _heistSettings.Levels[HeistLevel() - 1];
            BankHeistPayout payout     = _heistSettings.Payouts[HeistLevel() - 1];

            _irc.SendPublicChatMessage(_heistSettings.GameStart
                                       .Replace("@bankname@", heistLevel.LevelBankName));

            Thread.Sleep(5000); // wait in anticipation

            Random rnd    = new Random();
            int    chance = rnd.Next(1, 101); // 1 - 100

            if (chance >= payout.SuccessRate) // failed
            {
                if (_heistSettings.Robbers.Count == 1)
                {
                    _irc.SendPublicChatMessage(_heistSettings.SingleUserFail
                                               .Replace("user@", _heistSettings.Robbers.First().Username)
                                               .Replace("@bankname@", heistLevel.LevelBankName));
                }
                else
                {
                    _irc.SendPublicChatMessage(_heistSettings.Success0);
                }

                return;
            }

            int numWinners = (int)Math.Ceiling(_heistSettings.Robbers.Count * (payout.SuccessRate / 100));
            IEnumerable <BankRobber> winners = _heistSettings.Robbers.OrderBy(x => rnd.Next()).Take(numWinners);

            foreach (BankRobber winner in winners)
            {
                int funds = await _bank.CheckBalanceAsync(winner.Username.ToLower(), _broadcasterId);

                decimal earnings = Math.Ceiling(winner.Gamble * payout.WinMultiplier);

                await _bank.UpdateFundsAsync(winner.Username.ToLower(), _broadcasterId, (int)earnings + funds);

                _resultMessage += $" {winner.Username} ({(int)earnings} {_botConfig.CurrencyType}),";
            }

            // remove extra ","
            _resultMessage = _resultMessage.Remove(_resultMessage.LastIndexOf(','), 1);

            decimal numWinnersPercentage = numWinners / (decimal)_heistSettings.Robbers.Count;

            // display success outcome
            if (winners.Count() == 1 && numWinners == _heistSettings.Robbers.Count)
            {
                BankRobber onlyWinner = winners.First();
                int        earnings   = (int)Math.Ceiling(onlyWinner.Gamble * payout.WinMultiplier);

                _irc.SendPublicChatMessage(_heistSettings.SingleUserSuccess
                                           .Replace("user@", onlyWinner.Username)
                                           .Replace("@bankname@", heistLevel.LevelBankName)
                                           .Replace("@winamount@", earnings.ToString())
                                           .Replace("@pointsname@", _botConfig.CurrencyType));
            }
            else if (numWinners == _heistSettings.Robbers.Count)
            {
                _irc.SendPublicChatMessage(_heistSettings.Success100 + " " + _resultMessage);
            }
            else if (numWinnersPercentage >= 0.34m)
            {
                _irc.SendPublicChatMessage(_heistSettings.Success34 + " " + _resultMessage);
            }
            else if (numWinnersPercentage > 0)
            {
                _irc.SendPublicChatMessage(_heistSettings.Success1 + " " + _resultMessage);
            }

            // show in case Twitch deletes the message because of exceeding character length
            Console.WriteLine("\n" + _resultMessage + "\n");
        }
Exemple #2
0
 public void Produce(BankRobber robber)
 {
     _heistSettings.Robbers.Add(robber);
 }
        /// <summary>
        /// Engage in the bank heist minigame
        /// </summary>
        /// <param name="chatter">User that sent the message</param>
        public async Task <DateTime> BankHeist(TwitchChatter chatter)
        {
            try
            {
                BankHeist bankHeist = new BankHeist();
                int       funds     = await _bank.CheckBalanceAsync(chatter.Username, _broadcasterInstance.DatabaseId);

                int  gambleIndex = chatter.Message.IndexOf(" ");
                bool isValid     = true;
                int  gamble      = 0;

                if (gambleIndex != -1)
                {
                    string parseGamble = chatter.Message.Substring(gambleIndex);
                    isValid = int.TryParse(parseGamble, out gamble);
                }

                if (_heistSettingsInstance.IsHeistOnCooldown())
                {
                    TimeSpan cooldown = _heistSettingsInstance.CooldownTimePeriod.Subtract(DateTime.Now);

                    if (cooldown.Minutes >= 1)
                    {
                        _irc.SendPublicChatMessage(_heistSettingsInstance.CooldownEntry
                                                   .Replace("@timeleft@", cooldown.Minutes.ToString()));
                    }
                    else
                    {
                        _irc.SendPublicChatMessage(_heistSettingsInstance.CooldownEntry
                                                   .Replace("@timeleft@", cooldown.Seconds.ToString())
                                                   .Replace("minutes", "seconds"));
                    }

                    return(DateTime.Now);
                }

                if (bankHeist.HasRobberAlreadyEntered(chatter.Username))
                {
                    _irc.SendPublicChatMessage($"You are already in this heist @{chatter.DisplayName}");
                    return(DateTime.Now);
                }

                // check if funds and gambling amount are valid
                if (!isValid)
                {
                    _irc.SendPublicChatMessage($"Please gamble with a positive amount of {_botConfig.CurrencyType} @{chatter.DisplayName}");
                    return(DateTime.Now);
                }
                else if (funds > 0 && gamble == 0 && (chatter.Message.ToLower() == "!bankheist" || chatter.Message.ToLower() == "!heist"))
                {
                    // make sure the user can gamble something if an amount wasn't specified
                    if (funds > _heistSettingsInstance.MaxGamble)
                    {
                        gamble = _heistSettingsInstance.MaxGamble;
                    }
                    else
                    {
                        gamble = funds;
                    }
                }
                else if (gamble < 1)
                {
                    _irc.SendPublicChatMessage($"You cannot gamble less than one {_botConfig.CurrencyType} @{chatter.DisplayName}");
                    return(DateTime.Now);
                }
                else if (funds < 1)
                {
                    _irc.SendPublicChatMessage($"You need at least one {_botConfig.CurrencyType} to join the heist @{chatter.DisplayName}");
                    return(DateTime.Now);
                }
                else if (funds < gamble)
                {
                    _irc.SendPublicChatMessage($"You do not have enough to gamble {gamble} {_botConfig.CurrencyType} @{chatter.DisplayName}");
                    return(DateTime.Now);
                }
                else if (gamble > _heistSettingsInstance.MaxGamble)
                {
                    _irc.SendPublicChatMessage($"{_heistSettingsInstance.MaxGamble} {_botConfig.CurrencyType} is the most you can put in. " +
                                               $"Please try again with less {_botConfig.CurrencyType} @{chatter.DisplayName}");
                    return(DateTime.Now);
                }

                if (!bankHeist.IsEntryPeriodOver())
                {
                    // make heist announcement if first robber and start recruiting members
                    if (_heistSettingsInstance.Robbers.Count == 0)
                    {
                        _heistSettingsInstance.EntryPeriod = DateTime.Now.AddSeconds(_heistSettingsInstance.EntryPeriodSeconds);
                        _irc.SendPublicChatMessage(_heistSettingsInstance.EntryMessage.Replace("user@", chatter.Username));
                    }

                    // join bank heist
                    BankRobber robber = new BankRobber {
                        Username = chatter.Username, Gamble = gamble
                    };
                    bankHeist.Produce(robber);
                    await _bank.UpdateFundsAsync(chatter.Username, _broadcasterInstance.DatabaseId, funds - gamble);

                    // display new heist level
                    if (!string.IsNullOrEmpty(bankHeist.NextLevelMessage()))
                    {
                        _irc.SendPublicChatMessage(bankHeist.NextLevelMessage());
                    }

                    // display if more than one robber joins
                    if (_heistSettingsInstance.Robbers.Count > 1)
                    {
                        _irc.SendPublicChatMessage($"@{chatter.DisplayName} has joined the heist");
                    }
                }
            }
            catch (Exception ex)
            {
                await _errHndlrInstance.LogError(ex, "MinigameFeature", "BankHeist(TwitchChatter)", false, "!bankheist", chatter.Message);
            }

            return(DateTime.Now);
        }