public TournamentViewModel(Tournament tournament)
 {
     _tournament = tournament;
 }
        public List<Match> RegenerateMatches(Tournament tournament)
        {
            //TODO: Regenerate match when round completed
            List<Match> result = new List<Match>();
            List<string> winnerList = GetWinnersList(tournament);

            int regeneratedMatchCount = 0;
            foreach (Match m in tournament.Matches)
            {
                if (m.RoundStep == tournament.CurrentRoundStep)
                {
                    regeneratedMatchCount++;
                }
            }
            regeneratedMatchCount = regeneratedMatchCount / 2;
            if (regeneratedMatchCount == 0)
            {
                //TODO: We have a winner code
                tournament.Winner = "A winner!"; //TODO: Add the good winner
                tournament.Status = false;
            }
            else
            {
                for (int i = 0; i < regeneratedMatchCount; i++)
                {
                    Match match = new Match();
                    match.Player1 = "Not assigned";
                    match.Player2 = "Not assigned";
                    //match.Status = "Awaiting";
                    match.RoundStep = tournament.CurrentRoundStep + 1;
                    result.Add(match);
                }
            }
            //Assign winners to next round
            int counter = 0;
            foreach (Match m in result)
            {
                m.Player1 = winnerList[counter];
                m.Player2 = winnerList[counter + 1];
                counter = counter + 2;
            }
            return result;
        }
 public List<string> GetWinnersList(Tournament tournament)
 {
     List<string> winnersList = new List<string>();
     foreach (Match m in tournament.Matches)
     {
         if(m.RoundStep == tournament.CurrentRoundStep)
             winnersList.Add(m.Winner);
     }
     return winnersList;
 }
 public bool MustBeRegenerated(Tournament tournament)
 {
     foreach (Match m in tournament.Matches)
     {
         if (m.RoundStep == tournament.CurrentRoundStep)
         {
             if (m.Winner == null)
             {
                 return false;
             }
         }
     }
     return true;
 }
        public ActionResult CreateTournament([Bind(Include = "Id,Title,Room,Description,Type,Date,Start,End")] Activity activity, FormCollection form)
        {
            //Time manager
            string hourStart = form[6].ToString();
            string minuteStart = form[7].ToString();
            string hourEnd = form[8].ToString();
            string minuteEnd = form[9].ToString();
            activity.Start = hourStart + ":" + minuteStart;
            activity.End = hourEnd + ":" + minuteEnd;
            Congress c = (Congress)Session["Congress"];
            activity.Congres = db.Congresses.Find(c.Id);

            //Manage current user
            string userIdentity = "";
            UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
            var user = User.Identity;
            ApplicationUser utilisateur = UserManager.FindById(User.Identity.GetUserId());
            if (utilisateur != null)
            {
                activity.UserIdentity = utilisateur.UserName;
            }

            //Create tournament
            Tournament tournament = new Tournament();
            tournament.Activity = activity;
            tournament.RoundCount = form[4];
            tournament.CurrentRoundStep = 1;
            tournament.Status = true;

            //Generate matches
            tournament.Matches = new List<Match>();
            switch (tournament.RoundCount)
            {
                case "2x2":
                    for (int i = 0; i < 4; i++)
                    {
                        Match match = new Match();
                        match.Player1 = "Not assigned";
                        match.Player2 = "Not assigned";
                        //match.Status = "Awaiting";
                        match.Round = "1st";
                        match.RoundStep = 1;
                        tournament.Matches.Add(match);
                    }
                    break;
                case "4x4":
                    for (int i = 0; i < 8; i++)
                    {
                        Match match = new Match();
                        match.Player1 = "Not assigned";
                        match.Player2 = "Not assigned";
                        //match.Status = "Awaiting";
                        match.Round = "1st";
                        match.RoundStep = 1;
                        tournament.Matches.Add(match);
                    }
                    break;
                case "8x8":
                    for (int i = 0; i < 16; i++)
                    {
                        Match match = new Match();
                        match.Player1 = "Not assigned";
                        match.Player2 = "Not assigned";
                        //match.Status = "Awaiting";
                        match.Round = "1st";
                        match.RoundStep = 1;
                        tournament.Matches.Add(match);
                    }
                    break;
                case "16x16":
                    for (int i = 0; i < 32; i++)
                    {
                        Match match = new Match();
                        match.Player1 = "Not assigned";
                        match.Player2 = "Not assigned";
                        //match.Status = "Awaiting";
                        match.Round = "1st";
                        match.RoundStep = 1;
                        tournament.Matches.Add(match);
                    }
                    break;
                case "32x32":
                    for (int i = 0; i < 64; i++)
                    {
                        Match match = new Match();
                        match.Player1 = "Not assigned";
                        match.Player2 = "Not assigned";
                        //match.Status = "Awaiting";
                        match.Round = "1st";
                        match.RoundStep = 1;
                        tournament.Matches.Add(match);
                    }
                    break;
                default:
                    break;
            }

            if (ModelState.IsValid)
            {
                db.Tournaments.Add(tournament);
                db.SaveChanges();
                return RedirectToAction("IndexTournament");
            }
            ViewBag.Id = new SelectList(db.Tournaments, "Id", "RoundCount", activity.Id);
            return View(activity);
        }