public void CompleteBetting(TournamentMatch lastMatch)
        {
            if (activeBets != null)
            {
                double totalBet = activeBets.Values.Sum(v => v.bet);

                var allWonBets = activeBets
                                 .Where(kv => lastMatch.Winners.Contains(lastMatch.Teams.ElementAt(kv.Value.team).Participants.First()))
                                 .Select(kv => (
                                             hero: kv.Key,
                                             bet: kv.Value.bet
                                             ))
                                 .ToList();

                double winningTotalBet = allWonBets.Sum(v => v.bet);

                foreach ((var hero, int bet) in allWonBets.OrderByDescending(b => b.bet))
                {
                    int winnings = (int)(totalBet * bet / winningTotalBet);
                    int newGold  = BLTAdoptAHeroCampaignBehavior.Current.ChangeHeroGold(hero, winnings);
                    Log.LogFeedResponse(hero.FirstName.ToString(),
                                        $"WON BET {Naming.Inc}{winnings}{Naming.Gold}{Naming.To}{newGold}{Naming.Gold}");
                }

                activeBets = null;
            }

            TournamentHub.UpdateBets();
        }
Esempio n. 2
0
 public override void SyncData(IDataStore dataStore)
 {
     using var scopedJsonSync = new ScopedJsonSync(dataStore, nameof(BLTTournamentQueueBehavior));
     if (dataStore.IsSaving)
     {
         var usedHeroList = TournamentQueue.Select(t => t.Hero).ToList();
         dataStore.SyncData("UsedHeroObjectList", ref usedHeroList);
         var queue = TournamentQueue.Select(e => new TournamentQueueEntrySavable
         {
             HeroIndex = usedHeroList.IndexOf(e.Hero),
             IsSub     = e.IsSub,
             EntryFee  = e.EntryFee,
         }).ToList();
         scopedJsonSync.SyncDataAsJson("Queue2", ref queue);
     }
     else
     {
         List <Hero> usedHeroList = null;
         dataStore.SyncData("UsedHeroObjectList", ref usedHeroList);
         List <TournamentQueueEntrySavable> queue = null;
         scopedJsonSync.SyncDataAsJson("Queue2", ref queue);
         if (usedHeroList != null && queue != null)
         {
             TournamentQueue = queue.Select(e => new TournamentQueueEntry
             {
                 Hero     = usedHeroList[e.HeroIndex],
                 IsSub    = e.IsSub,
                 EntryFee = e.EntryFee,
             }).ToList();
         }
     }
     TournamentQueue ??= new();
     TournamentQueue.RemoveAll(e => e.Hero == null || e.Hero.IsDead);
     TournamentHub.UpdateEntrants();
 }
 protected override void OnEndMission()
 {
     // Ensure the betting is closed
     RefundBets();
     CurrentBettingState = BettingState.none;
     activeBets          = null;
     TournamentHub.UpdateBets();
 }
        public BLTAdoptAHeroModule()
        {
            ActionManager.RegisterAll(typeof(BLTAdoptAHeroModule).Assembly);

            GlobalCommonConfig.Register();
            GlobalTournamentConfig.Register();
            GlobalHeroClassConfig.Register();
            GlobalHeroPowerConfig.Register();

            TournamentHub.Register();
            MissionInfoHub.Register();
        }
        private void CloseBetting()
        {
            // We use this being non-null as an indicator that betting was active
            if (activeBets != null)
            {
                var groupedBets = activeBets.Values
                                  .Select(b => (name: TournamentHelpers.TeamNames[b.team], b.bet))
                                  .GroupBy(b => b.name)
                                  .ToList();

                if (groupedBets.Count == 1)
                {
                    // refund bets if only one team was bet on
                    RefundBets();
                    activeBets          = null;
                    CurrentBettingState = BettingState.disabled;
                    Log.LogFeedMessage($"Betting is now CLOSED: only one team bet on, bets refunded");
                    ActionManager.SendChat($"Betting is now CLOSED: only one team bet on, bets refunded");
                }
                else if (!groupedBets.Any())
                {
                    activeBets          = null;
                    CurrentBettingState = BettingState.disabled;
                    Log.LogFeedMessage($"Betting is now CLOSED: no bets placed");
                    ActionManager.SendChat($"Betting is now CLOSED: no bets placed");
                }
                else
                {
                    CurrentBettingState = BettingState.closed;
                    var betTotals = activeBets.Values
                                    .Select(b => (name: TournamentHelpers.TeamNames[b.team], b.bet))
                                    .GroupBy(b => b.name)
                                    .Select(g => $"{g.Key} {g.Select(x => x.bet).Sum()}{Naming.Gold}")
                                    .ToList()
                    ;
                    string msg = $"Betting is now CLOSED: {string.Join(", ", betTotals)}";
                    Log.LogFeedMessage(msg);
                    ActionManager.SendChat(msg);
                }
            }
            else
            {
                CurrentBettingState = BettingState.disabled;
            }

            TournamentHub.UpdateBets();
        }
 public void OpenBetting(TournamentBehavior tournamentBehavior)
 {
     if (BLTAdoptAHeroModule.TournamentConfig.EnableBetting &&
         tournamentBehavior.CurrentMatch != null &&
         (tournamentBehavior.CurrentRoundIndex == 3 || !BLTAdoptAHeroModule.TournamentConfig.BettingOnFinalOnly))
     {
         var    teams = TournamentHelpers.TeamNames.Take(tournamentBehavior.CurrentMatch.Teams.Count());
         string round = tournamentBehavior.CurrentRoundIndex < 3
             ? $"round {tournamentBehavior.CurrentRoundIndex + 1}"
             : "final";
         string msg = $"Betting is now OPEN for {round} match: {string.Join(" vs ", teams)}!";
         Log.LogFeedMessage(msg);
         ActionManager.SendChat(msg);
         activeBets          = new();
         CurrentBettingState = BettingState.open;
     }
     else
     {
         CurrentBettingState = BettingState.disabled;
     }
     TournamentHub.UpdateBets();
 }
        public (bool success, string failReason) PlaceBet(Hero hero, string team, int bet)
        {
            var tournamentBehavior = Mission.Current?.GetMissionBehaviour <TournamentBehavior>();

            if (tournamentBehavior == null)
            {
                return(false, "Tournament is not active");
            }

            if (!BLTAdoptAHeroModule.TournamentConfig.EnableBetting)
            {
                return(false, "Betting is disabled");
            }

            if (CurrentBettingState == BettingState.closed)
            {
                return(false, "Betting is closed");
            }

            if (tournamentBehavior.CurrentRoundIndex != 3 && BLTAdoptAHeroModule.TournamentConfig.BettingOnFinalOnly)
            {
                return(false, "Betting is only allowed on the final");
            }

            if (CurrentBettingState != BettingState.open)
            {
                return(false, "Betting is not open");
            }

            int teamsCount = tournamentBehavior.CurrentMatch.Teams.Count();

            string[] activeTeams = TournamentHelpers.TeamNames.Take(teamsCount).ToArray();
            int      teamIdx     = activeTeams.IndexOf(team.ToLower());

            if (teamIdx == -1)
            {
                return(false, $"Team name must be one of {string.Join(", ", activeTeams)}");
            }

            if (activeBets.TryGetValue(hero, out var existingBet))
            {
                if (existingBet.team != teamIdx)
                {
                    return(false, "You can only bet on one team");
                }
            }

            int heroGold = BLTAdoptAHeroCampaignBehavior.Current.GetHeroGold(hero);

            if (heroGold < bet)
            {
                return(false, Naming.NotEnoughGold(bet, heroGold));
            }

            if (existingBet != null)
            {
                existingBet.bet += bet;
            }
            else
            {
                activeBets.Add(hero, new() { team = teamIdx, bet = bet });
            }

            // Take the actual money
            BLTAdoptAHeroCampaignBehavior.Current.ChangeHeroGold(hero, -bet);

            TournamentHub.UpdateBets();

            return(true, null);
        }