Ejemplo n.º 1
0
 public ActionResult New(Cup cup, string starttime1, string starttime2, string endtime1, string endtime2, string date)
 {
     if (!HttpContext.User.Identity.IsAuthenticated)
         return Redirect("/account?returnurl=/cup/new");
     cup.User = new User { Id = int.Parse(HttpContext.User.Identity.Name) };
     var starttime = "";
     var endtime = "";
     if (!string.IsNullOrEmpty(starttime1))
         starttime = starttime1 + ":" + (!string.IsNullOrEmpty(starttime2) ? starttime2 : "00");
     if (!string.IsNullOrEmpty(endtime1))
         endtime = endtime1 + ":" + (!string.IsNullOrEmpty(endtime2) ? endtime2 : "00");
     var d = DateTime.MinValue;
     var start = DateTime.TryParse(date + " " + starttime, out d);
     if (d > DateTime.MinValue)
         cup.Start = d;
     var end = DateTime.TryParse(date + " " + endtime, out d);
     if (d > DateTime.MinValue)
         cup.End = d;
     var response = _client.PostAsJsonAsync(_defaulUri, cup).Result;
     if (response.IsSuccessStatusCode)
     {
         return Redirect("/");
     }
     return Redirect("/cups/new");
 }
Ejemplo n.º 2
0
        public void Post(Cup c)
        {
            if (c == null) return;

            using (var ctx = new CupDbContext())
            {
                //DateTime d;
                //if (DateTime.TryParse(c.Date + " " + c.Starttime, out d))
                //    c.Start = d;
                //if (DateTime.TryParse(c.Date + " " + c.Endtime, out d))
                //    c.End = d;
                ctx.Cups.Add(c);
                if (c.Start != null && c.Start.Value.ToShortTimeString() != "00:00")
                    c.Starttime = c.Start.Value.ToShortTimeString();
                if (c.End != null && c.End.Value.ToShortTimeString() != "00:00")
                    c.Endtime = c.End.Value.ToShortTimeString();
                c.User = ctx.Users.FirstOrDefault(x=>x.Id==c.User.Id);
                if (c.AutoGenerateCup)
                {
                    for (var i = 1; i < (c.NumberOfTeams <= 10 ? 2 : c.NumberOfTeams > 20 ? 8 : 4) + 1; i++)
                    {
                        var l = GetLetter(i);
                        ctx.Groups.Add(new Group { Cup = c, Name = "Grupp " + l.Item1, Color = l.Item2 });
                    }

                    var quarterfinal1 = new QuarterFinal { Cup = c };
                    var quarterfinal2 = new QuarterFinal { Cup = c };
                    var quarterfinal3 = new QuarterFinal { Cup = c };
                    var quarterfinal4 = new QuarterFinal { Cup = c };
                    if (c.Groups.Count == 2)
                    {
                        var group1 = c.Groups.OrderBy(x => x.Id).First();
                        var group2 = c.Groups.OrderBy(x => x.Id).Last();
                        quarterfinal1.Description = "2:an i " + group2.Name + " - 3:an i " + group1.Name;
                        quarterfinal1.Name = "Kvartsfinal 1";
                        quarterfinal1.Group1 = group2; //2:an i grupp 2
                        quarterfinal1.Group2 = group1; //3:an i grupp 1

                        quarterfinal2.Description = "2:an i " + group1.Name + " - 3:an i " + group2.Name;
                        quarterfinal2.Name = "Kvartsfinal 2";
                        quarterfinal2.Group1 = group1; //2:an i grupp 1
                        quarterfinal2.Group2 = group2; //3:an i grupp 2

                        quarterfinal3.Description = "1:an i " + group1.Name + " - 4:an i " + group2.Name;
                        quarterfinal3.Name = "Kvartsfinal 3";
                        quarterfinal3.Group1 = group1; //1:an i grupp 1
                        quarterfinal3.Group2 = group2; //4:an i grupp 2

                        quarterfinal4.Description = "1:an i " + group2.Name + " - 4:an i " + group1.Name;
                        quarterfinal4.Name = "Kvartsfinal 4";
                        quarterfinal4.Group1 = group2; //1:an i grupp 2
                        quarterfinal4.Group2 = group1; //4:an i grupp 1
                    }
                    if (c.Groups.Count == 4)
                    {
                        var group1 = c.Groups.OrderBy(x => x.Id).First();
                        var group2 = c.Groups.OrderBy(x => x.Id).Skip(1).First();
                        var group3 = c.Groups.OrderBy(x => x.Id).Skip(2).First();
                        var group4 = c.Groups.OrderBy(x => x.Id).Last();
                        quarterfinal1.Description = "1:an i " + group1.Name + " - 2:an i " + group2.Name;
                        quarterfinal1.Name = "Kvartsfinal 1";
                        quarterfinal1.Group1 = group1; //1:an i grupp 1
                        quarterfinal1.Group2 = group2; //2:an i grupp 2

                        quarterfinal2.Description = "1:an i " + group2.Name + " - 2:an i " + group1.Name;
                        quarterfinal3.Name = "Kvartsfinal 2";
                        quarterfinal2.Group1 = group2; //1:an i grupp 2
                        quarterfinal2.Group2 = group1; //2:an i grupp 1

                        quarterfinal3.Description = "1:an i " + group3.Name + " - 2:an i " + group4.Name;
                        quarterfinal3.Name = "Kvartsfinal 3";
                        quarterfinal3.Group1 = group3; //1:an i grupp 3
                        quarterfinal3.Group2 = group4; //2:an i grupp 4

                        quarterfinal4.Description = "1:an i " + group4.Name + " - 2:an i " + group3.Name;
                        quarterfinal4.Name = "Kvartsfinal 4";
                        quarterfinal4.Group1 = group4; //1:an i grupp 4
                        quarterfinal4.Group2 = group3; //2:an i grupp 3
                    }
                    if (c.Groups.Count == 8)
                    {
                        var group1 = c.Groups.OrderBy(x => x.Id).First();
                        var group2 = c.Groups.OrderBy(x => x.Id).Skip(1).First();
                        var group3 = c.Groups.OrderBy(x => x.Id).Skip(2).First();
                        var group4 = c.Groups.OrderBy(x => x.Id).Skip(3).First();
                        var group5 = c.Groups.OrderBy(x => x.Id).Skip(4).First();
                        var group6 = c.Groups.OrderBy(x => x.Id).Skip(5).First();
                        var group7 = c.Groups.OrderBy(x => x.Id).Skip(6).First();
                        var group8 = c.Groups.OrderBy(x => x.Id).Last();
                        quarterfinal1.Description = "1:an i " + group1.Name + " - 1:an i " + group2.Name;
                        quarterfinal1.Name = "Kvartsfinal 1";
                        quarterfinal1.Group1 = group1; //1:an i grupp 1
                        quarterfinal1.Group2 = group2; //1:an i grupp 2

                        quarterfinal2.Description = "1:an i " + group3.Name + " - 1:an i " + group4.Name;
                        quarterfinal2.Name = "Kvartsfinal 2";
                        quarterfinal2.Group1 = group3; //1:an i grupp 3
                        quarterfinal2.Group2 = group4; //1:an i grupp 4

                        quarterfinal3.Description = "1:an i " + group5.Name + " - 1:an i " + group6.Name;
                        quarterfinal3.Name = "Kvartsfinal 3";
                        quarterfinal3.Group1 = group5; //1:an i grupp 5
                        quarterfinal3.Group2 = group6; //1:an i grupp 6

                        quarterfinal1.Description = "1:an i " + group7.Name + " - 1:an i " + group8.Name;
                        quarterfinal1.Name = "Kvartsfinal 4";
                        quarterfinal4.Group1 = group7; //1:an i grupp 7
                        quarterfinal4.Group2 = group8; //1:an i grupp 8
                    }
                    ctx.QuarterFinals.Add(quarterfinal1);
                    ctx.QuarterFinals.Add(quarterfinal2);
                    ctx.QuarterFinals.Add(quarterfinal3);
                    ctx.QuarterFinals.Add(quarterfinal4);

                    var semifinal1 = new SemiFinal
                    {
                        Cup = c,
                        QuarterFinal1 = quarterfinal1,
                        QuarterFinal2 = quarterfinal2,
                        Description = "Vinnare " + quarterfinal1.Name + " - Vinnare " + quarterfinal2.Name,
                        Name = "Semifinal 1"
                    };

                    var semifinal2 = new SemiFinal
                    {
                        Cup = c,
                        QuarterFinal1 = quarterfinal3,
                        QuarterFinal2 = quarterfinal4,
                        Description = "Vinnare " + quarterfinal3.Name + " - Vinnare " + quarterfinal4.Name,
                        Name = "Semifinal 2"
                    };

                    ctx.SemiFinals.Add(semifinal1);
                    ctx.SemiFinals.Add(semifinal2);

                    var final1 = new Final
                    {
                        Cup = c,
                        SemiFinal1 = semifinal1,
                        SemiFinal2 = semifinal2,
                        Description = "Vinnare " + semifinal1.Name + " - vinnare " + semifinal2.Name,
                        Name = "Final"

                    };


                    var final2 = new Final
                    {
                        Cup = c,
                        SemiFinal1 = semifinal2,
                        SemiFinal2 = semifinal1,
                        Description = "Förloare " + semifinal1.Name + " - förlorare " + semifinal2.Name,
                        Name = "Match" + " om 3:e pris"
                    };


                    ctx.Finals.Add(final1);
                    ctx.Finals.Add(final2);

                }
                ctx.SaveChanges();
            }
        }
Ejemplo n.º 3
0
        static void CreateMatches(CupDbContext ctx, Group @group, Cup cup)
        {
            int r;
            var n = group.Teams.Count;
            var nn = n + (n & 1);
            var m = nn >> 1;
            for (r = 0; r < nn - 1; r++)
            {



                for (var s = 0; s < m - 1; s++)
                    ctx.GroupGames.Add(new GroupGame
                    {
                        Start = DateTime.Now,
                        End = DateTime.Now,
                        Group = group,
                        Cup = cup,
                        TeamHome = group.Teams.ElementAt(Mod(s + r, nn - 1)),
                        TeamVisitor = group.Teams.ElementAt(Mod(-1 - s + r, nn - 1))
                    });
                //Console.Write((s > 0 ? " ," : "") + Mod(s + r, nn - 1) + " - " + Mod(-1 - s + r, nn - 1));
                if ((n & 1) == 1) { }
                //Console.Write(" || står över " + Mod(m - 1 + r, nn - 1));
                else
                    ctx.GroupGames.Add(new GroupGame
                    {
                        Start = DateTime.Now,
                        End = DateTime.Now,
                        Group = group,
                        Cup = cup,
                        TeamHome = group.Teams.ElementAt(Mod(m - 1 + r, nn - 1)),
                        TeamVisitor = group.Teams.ElementAt(nn - 1)
                    });
                //Console.Write(" , " + Mod(m - 1 + r, nn - 1) + " - " + (nn - 1));
            }
        }
Ejemplo n.º 4
0
        public void Put(int id, Cup value, bool generateteamstogroups = false)
        {
            using (var ctx = new CupDbContext())
            {
                var c = ctx.Cups.Find(id);
                if (c == null) return;
                if (generateteamstogroups)
                {
                    var numberofTeamsPerGroup = 0;
                    for (int i = 3; i < 24; i++)
                    {
                        if (c.NumberOfTeams % i != 0) continue;
                        numberofTeamsPerGroup = i;
                        break;
                    }

                    foreach (var group in c.Groups)
                    {
                        int i = 0;
                        foreach (var team in c.Teams.Where(x => x.Group == null))
                        {
                            if (i == numberofTeamsPerGroup)
                                break;
                            if (team.Group != null)
                                continue;
                            team.Group = group;
                            group.Teams.Add(team);
                            i++;

                        }
                    }
                    foreach (var group in c.Groups.ToList())
                    {
                        CreateMatches(ctx, group, c);
                    }
                    c.Generated = true;

                }
                else
                {
                    c.Name = value.Name;
                    c.BaseColor = value.BaseColor;
                    c.NumberOfTeams = value.NumberOfTeams;
                }

                ctx.SaveChanges();
            }
        }