public ActionResult AddBattle(TourneyModel model)
        {
            var db = new ZkDataContext();

            {
                var tb = new TourneyBattle(Global.Server, new TourneyBattle.TourneyPrototype()
                {
                    Title       = model.Title,
                    TeamPlayers = new List <List <string> >()
                    {
                        model.Team1Ids.Select(x => db.Accounts.Find(x)?.Name).Where(x => x != null).ToList(),
                        model.Team2Ids.Select(x => db.Accounts.Find(x)?.Name).Where(x => x != null).ToList()
                    }
                });
                Global.Server.AddBattle(tb);
            }
            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        public ActionResult AddMultipleBattles(string battleList)
        {
            if (!Global.IsTourneyController)
            {
                return(DenyAccess());
            }
            var db = new ZkDataContext();

            string[] splitters   = { "//" };
            string[] battleSpecs = battleList.Split(splitters, System.StringSplitOptions.RemoveEmptyEntries);

            foreach (var bSpec in battleSpecs)
            {
                bool     validBattle = true;
                string[] bItems      = bSpec.Split(',');
                string   bName       = bItems[0];
                // must have an even number of players; the first N/2 are assigned to the first team
                if (bItems.Length % 2 == 0)
                {
                    validBattle = false;
                }
                ;
                int        playersPerTeam = (bItems.Length - 1) / 2;
                List <int> team1Ids       = new List <int>();
                List <int> team2Ids       = new List <int>();
                // if any of the remaining entries are not ints or account names the battle is invalid
                for (int i = 1; i <= playersPerTeam; i++)
                {
                    Account tryAcc = Account.AccountByName(db, bItems[i]);
                    if (tryAcc != null)
                    {
                        team1Ids.Add(tryAcc.AccountID);
                    }
                    else
                    {
                        try { team1Ids.Add(Int32.Parse(bItems[i])); }
                        catch (FormatException) { validBattle = false; }
                    }
                }
                for (int i = playersPerTeam + 1; i < bItems.Length; i++)
                {
                    Account tryAcc = Account.AccountByName(db, bItems[i]);
                    if (tryAcc != null)
                    {
                        team2Ids.Add(tryAcc.AccountID);
                    }
                    else
                    {
                        try { team2Ids.Add(Int32.Parse(bItems[i])); }
                        catch (FormatException) { validBattle = false; }
                    }
                }

                if (validBattle)
                {
                    var tb = new TourneyBattle(Global.Server, new TourneyBattle.TourneyPrototype()
                    {
                        Title       = bName,
                        FounderName = Global.Account.Name,
                        TeamPlayers = new List <List <string> >()
                        {
                            team1Ids.Select(x => db.Accounts.Find(x)?.Name).Where(x => x != null).ToList(),
                            team2Ids.Select(x => db.Accounts.Find(x)?.Name).Where(x => x != null).ToList()
                        }
                    });
                    Global.Server.AddBattle(tb);
                }
            }

            return(RedirectToAction("Index"));
        }