Пример #1
0
        public ActionResult SubmitMatch(Match model)
        {
            if (ModelState.IsValid)
            {
                var match = new Match()
                {
                    Team1 = model.Team1, Team2 = model.Team2, Team1Score = model.Team1Score, Team2Score = model.Team2Score, MembersGoalsSplitTeam1 = model.MembersGoalsSplitTeam1, Colour = model.Colour
                };

                string[] names    = { model.Team1, model.Team2 };
                bool     notValid = false;

                if (!matchManager.PlayerValid(names))
                {
                    notValid = true;
                    ModelState.AddModelError("playererror", "Wybrałeś dwie te same drużyny");
                }
                if (!matchManager.ScoreValid(match.Team1Score, match.Team2Score))
                {
                    notValid = true;
                    ModelState.AddModelError("playererror", "Nieprawidłowy wynik meczu");
                }
                if (!matchManager.YourTeamValid(match.Team1, Helpers.UserName()))
                {
                    notValid = true;
                    ModelState.AddModelError("playererror", "Twoja drużyna powinna być po lewej stronie");
                }
                if (!matchManager.MatchValidate(names))
                {
                    notValid = true;
                    ModelState.AddModelError("playererror", "W lidze można zagrać tylko raz z tą samą drużyną");
                }
                if (Helpers.UserName() == " ")
                {
                    return(RedirectToAction("Index", "Home"));
                }
                if (!matchManager.IsPlayedMatch(names, Helpers.UserName()))
                {
                    notValid = true;
                    ModelState.AddModelError("playererror", "Twoja drużyna nie gra w tym meczu");
                }
                if (!matchManager.MembersGoalsValid(model.MemberGoals, model.Team1Score))
                {
                    notValid = true;
                    ModelState.AddModelError("playererror", "Suma strzelonych bramek przez graczy musi być równa liczbie strzelonych bramek przez drużynę");
                }

                if (!notValid)
                {
                    TempData["Valid"] = true;
                    TempData["Score"] = model.Team1 + " " + model.Team1Score + " : " + model.Team2Score + " " + model.Team2;

                    string Tname = Helpers.UserName() == model.Team1 ? model.Team2 : model.Team1;

                    TempData["Email"] = (from n in db.Users
                                         where n.Name == Tname
                                         select n.Email).SingleOrDefault();
                    matchManager.AddMatch(match);

                    return(RedirectToAction("Index", "Match", new { suc = true }));
                }
            }
            model.MemberGoals  = 0;
            model.Team1Score   = 0;
            ViewBag.UserList   = GetUserList();
            ViewBag.ColourList = GetColourList();

            return(View("Index", model));
        }