예제 #1
0
        public ActionResult Result(FormCollection formCollection)
        {
            var MatchResultRepository = GetRepository<MatchResult>();
            var MatchRepository = GetRepository<Match>();
            var repositoryBoxer = GetRepository<Boxer>();
            var repositoryType = GetRepository<ResultType>();

            foreach (var key in formCollection.AllKeys)
            {
                    MatchResult MResult = new MatchResult();
                    string[] Values = formCollection[key].Split(',');
                    if (Values[0].ToLower() != "submit" && !string.IsNullOrEmpty(Values[1]))
                    {
                        bool ResultEntered = false;
                        // if Winner is not null or draw
                        if (Values[1] != "0" && !string.IsNullOrEmpty(Values[1]))
                        {
                            MResult.Winner = repositoryBoxer.Get(int.Parse(Values[1]));
                            if (!string.IsNullOrEmpty(Values[2])) MResult.ResultType = repositoryType.Get(Values[2]); //set method
                            if (!string.IsNullOrEmpty(Values[3])) MResult.Round = int.Parse(Values[3]); //set round

                            ResultEntered = true;
                        }
                        else if(Values[1] == "0")
                        {
                            MResult.ResultType = repositoryType.Get("Draw");
                            ResultEntered = true;
                        }

                        //If the result has been entered then update records and save!
                        if (ResultEntered)
                        {
                            Match Match = MatchRepository.Get(int.Parse(Values[0]));
                            //update records for prospects only!

                            //If winner is first boxers set values correctly
                            if (int.Parse(Values[1]) == Match.Boxers.First().Id)
                            {
                                if (Match.Boxers.First().IsProspect)
                                {
                                    Match.Boxers.First().Record.Won += 1;
                                    if (!string.IsNullOrEmpty(Values[3]) && int.Parse(Values[3]) > 0) Match.Boxers.First().Record.KO += 1;
                                }
                                if (Match.Boxers.Last().IsProspect) Match.Boxers.Last().Record.Lost += 1;

                            }
                            else if (int.Parse(Values[1]) == Match.Boxers.Last().Id)
                            {// if winner is Last (2nd) boxer
                                //if first boxer is a prospect then update record
                                if (Match.Boxers.Last().IsProspect)
                                {
                                    Match.Boxers.Last().Record.Won += 1;
                                    if (!string.IsNullOrEmpty(Values[3]) && int.Parse(Values[3]) > 0) Match.Boxers.Last().Record.KO += 1;
                                }

                                if (Match.Boxers.First().IsProspect) Match.Boxers.First().Record.Lost += 1;
                            }
                            else
                            {// if drawn
                                if (Match.Boxers.First().IsProspect) Match.Boxers.First().Record.Drawn += 1;
                                if (Match.Boxers.Last().IsProspect) Match.Boxers.Last().Record.Drawn += 1;
                            }

                            MResult.Match = Match;
                            DataSession.Save(MResult);
                        }

                    }
            }

            //update the season table to make sure we are in all good
            new GetFromCache().UpdateSeasonTableModelCaching();

            return Redirect("/ben");
        }
예제 #2
0
        public ActionResult EditMatch(int id, int? GetBoxer_Dialog_Prospect_Id, int? GetBoxer_Dialog_Opponent_Id, int matchlength, DateTime matchdate, string Location, string Details, string Winner, string Type, string Round)
        {
            var Matchreppository = GetRepository<Match>();
            var Boxerrepository = GetRepository<Boxer>();

            var Opponent = new Boxer();
            var Prospect = new Boxer();

            //set prospect if have
            if (GetBoxer_Dialog_Prospect_Id != null)
                Prospect = Boxerrepository.Get((int)GetBoxer_Dialog_Prospect_Id);

            //set oppoent if have
            if (GetBoxer_Dialog_Opponent_Id != null)
                Opponent = Boxerrepository.Get((int)GetBoxer_Dialog_Opponent_Id);

            //get match
            Match dbmatch = Matchreppository.Get(id);

            //remove boxers
            dbmatch.Boxers.Clear();

            //add boxers to match if we have them
            if (Prospect.Id > 0)
                dbmatch.Boxers.Add(Prospect);

            if (Opponent.Id > 0)
                dbmatch.Boxers.Add(Opponent);

            //Update db Match
            dbmatch.MatchDate = matchdate;
            dbmatch.Location = Location;
            dbmatch.Details = Details;
            dbmatch.Rounds = matchlength;

            //Now Detail with the result section of the Match
            var ResultTypeRep = GetRepository<ResultType>();
            var MatchResultRep = GetRepository<MatchResult>();
            bool IsValid = true;

            var matchResult = new MatchResult();
            if (dbmatch.Boxers.Count() == 2)
            {
                if (Winner == "0")
                {
                    //if draw set Result Type ONLY!!!
                    matchResult.ResultType = ResultTypeRep.All().Where(x => x.Type == "Draw").Single();
                }
                else if (Winner != string.Empty)
                {
                    //if there is a result (not a draw) set the winner!
                    if (Winner == "prospect" & Type != "")
                    {
                        matchResult.Winner = Prospect;
                    }
                    else if (Winner == "opponent" & Type != "")
                    {
                        matchResult.Winner = Opponent;
                    }
                    else
                    {
                        IsValid = false;
                    }

                    //Set the Type
                    if (Type != "") matchResult.ResultType = ResultTypeRep.All().Where(x => x.Type == Type).Single();
                    //set the round
                    if (Round != "") matchResult.Round = int.Parse(Round);
                }
                else
                {
                    var MatchResult = MatchResultRep.Get(id);
                    if (MatchResult != null)
                    {
                        MatchResultRep.Delete(MatchResult);
                    }
                }
            }

            if(IsValid)
            {
                //If it has a result then remove it
                if (dbmatch.HasResult())
                    MatchResultRep.Delete(dbmatch.Result);

                DataSession.Save(dbmatch);
                //only add match if there is one!
                if (matchResult.ResultType != null)
                {
                    matchResult.Match = dbmatch;
                    DataSession.Save(matchResult);
                }
            }

            return Redirect("/ben/matches");
        }