Example #1
0
 public JSONGame(Game model)
 {
     if (model == null) return;
     this.ID = model.ID;
     this.Name = model.Name;
     var t = model.Teams.ToList();
     this.Teams = t.GetJSON();
     //this.Players = model.Players.ToList().GetJSON();
     AssignPlayers();
 }
        public async Task<ActionResult> CreateCSV(CreateFromCSV model)
        {
            Club club = null;
            if (ModelState.IsValid)
            {
                var clubId = db.Users.Find(User.Identity.GetUserId()).Club.ID;
                club = await db.Clubs.FindAsync(clubId);
                StreamReader reader = new StreamReader(model.File.InputStream,System.Text.Encoding.UTF8);
                var csvstring = await reader.ReadToEndAsync();
                var players = Utilities.ImportCSV(csvstring);
                foreach (var item in players)
                {
                    item.Club = club;
                }
                TeamFactory factory = new TeamFactory(players);
                if (model.randomize) factory.ScramblePlayers();
                factory.PlayersPerTeam = model.playersPerTeam;
                Queue<Team> allTeams = new Queue<Team>(factory.CreateTeams());
                int[] teamSize = GetGroupSize(model.groups, allTeams.Count);

                //Beräkna banor
                List<Lane> lanes = await GetLanesAsync(model.SelectedLanes);
                int lanesPerGroup = GetLanesPerGroup(model.groups, lanes.Count);

                for (int i = 0; i < model.groups; i++)
                {
                    Game game = new Game();
                    game.Club = await db.Clubs.FindAsync(clubId);
                    game.Deleted = false;
                    game.Ended = null;
                    game.Name = model.Name == "" ? "Namnlös" : model.Name;
                    game.IsGroup = model.groups > 1;
                    if (model.groups > 1)
                    {
                        game.Group = i + 1;

                    }
                    else
                    {
                        game.Group = 0;
                    }
                    game.IsScramble = false;
                    game.IsTournament = true;
                    game.Started = DateTime.Now;
                    List<Lane> gameLanes = lanes.Skip(i * lanesPerGroup).Take(lanesPerGroup).ToList();
                    game.Lanes = gameLanes;
                    List<Team> gameTeams = allTeams.ToList().Skip(toSkip(i, teamSize)).Take(teamSize[i]).ToList();
                    foreach (var item in gameTeams)
                    {
                        item.SetGame(game);
                    }
                    game.Rounds = Utilities.GetRounds(gameTeams, gameLanes).OrderBy(r=> r.PlayOrder).ToList();
                    game.Teams = gameTeams;
                    game.PlayersPerTeam = 1;
                    db.Games.Add(game);
                    await db.SaveChangesAsync();
                    foreach (var item in game.Rounds)
                    {
                        var _teams = item.Teams.ToArray();
                        item.Team1ID = _teams[0].ID;
                        item.Team2ID = _teams[1].ID;
                    }
                    game.Rounds = Utilities.FixEqualLaneAmount(game.Rounds, game.Lanes);
                }

                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index");
        }
 public async Task<ActionResult> Edit(Game game)
 {
     if (ModelState.IsValid)
     {
         db.Entry(game).State = EntityState.Modified;
         await db.SaveChangesAsync();
         return RedirectToAction("Index");
     }
     return View(game);
 }
        public async Task<ActionResult> CreateFromGames(ICollection<int> games, string Name, int groups, int teamsFromEach, ICollection<int> SelectedLanes)
        {
            Club club = null;
            if (ModelState.IsValid)
            {
                var clubId = db.Users.Find(User.Identity.GetUserId()).Club.ID;
                club = await db.Clubs.FindAsync(clubId);

                List<Game> prevGames = new List<Game>();
                foreach (var item in games)
                {
                    prevGames.Add(await db.Games.FindAsync(item));
                }
                Queue<Team> allTeams = new Queue<Team>();
                foreach (var item in prevGames)
                {
                    foreach (var team in item.Teams.OrderByDescending(t => t.GetTotalResult(item.ID)).ThenByDescending(t => t.GetTotalPoints(item.ID)).Take(teamsFromEach))
                    {
                        allTeams.Enqueue(team);
                    }
                }

                int[] teamSize = GetGroupSize(groups, allTeams.Count);
                int teamsPerGroup;
                //Beräkna banor
                List<Lane> lanes = await GetLanesAsync(SelectedLanes);
                int lanesPerGroup = GetLanesPerGroup(groups, lanes.Count);
                for (int i = 0; i < groups; i++)
                {
                    Game game = new Game();
                    game.Club = await db.Clubs.FindAsync(clubId);
                    game.Deleted = false;
                    game.Ended = null;
                    game.IsScramble = false;
                    game.IsTournament = true;
                    game.Started = DateTime.Now;
                    game.Name = Name == "" ? "Namnlös" : Name;
                    game.IsGroup = groups > 1;
                    if (groups > 1)
                    {
                        game.Group = i + 1;

                    }
                    else
                    {
                        game.Group = 0;
                    }
                    List<Lane> gameLanes = lanes.Skip(i * lanesPerGroup).Take(lanesPerGroup).ToList();
                    game.Lanes = gameLanes;

                    teamsPerGroup = (int)Math.Ceiling((decimal)allTeams.Count / (decimal)groups);

                    List<Team> gameTeams = allTeams.ToList().Skip(toSkip(i, teamSize)).Take(teamSize[i]).ToList();
                    foreach (var item in gameTeams)
                    {
                        item.SetGame(game);

                    }
                    game.Rounds = Utilities.GetRounds(gameTeams, gameLanes).OrderBy(r=> r.PlayOrder).ToList();
                    game.Teams = gameTeams;
                    db.Games.Add(game);
                    await db.SaveChangesAsync();
                    foreach (var item in game.Rounds)
                    {
                        var _teams = item.Teams.ToArray();
                        item.Team1ID = _teams[0].ID;
                        item.Team2ID = _teams[1].ID;
                    }
                    game.Rounds = Utilities.FixEqualLaneAmount(game.Rounds, game.Lanes);
                }

                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index");
        }
        public async Task<ActionResult> CreateQuick(int teams, string Name, int groups, ICollection<int> SelectedLanes)
        {
            Club club = null;
            if (ModelState.IsValid)
            {

                var clubId = db.Users.Find(User.Identity.GetUserId()).Club.ID;
                club = await db.Clubs.FindAsync(clubId);
                TeamFactory factory = new TeamFactory(teams);
                Queue<Team> allTeams = new Queue<Team>(factory.CreateTeams());
                int[] teamSize = GetGroupSize(groups, allTeams.Count);
                List<Team> gameTeams = new List<Team>();
                List<Lane> gameLanes = new List<Lane>();
                List<Round> rounds = new List<Round>();
                //Beräkna banor
                List<Lane> lanes = await GetLanesAsync(SelectedLanes);
                int lanesPerGroup = GetLanesPerGroup(groups, lanes.Count);

                for (int i = 0; i < groups; i++)
                {
                    gameTeams = allTeams.ToList().Skip(toSkip(i, teamSize)).Take(teamSize[i]).ToList();
                    gameLanes = lanes.Skip(i * lanesPerGroup).Take(lanesPerGroup).ToList();
                    rounds = Utilities.GetRounds(gameTeams, gameLanes).OrderBy(r=> r.PlayOrder).ToList();
                    Game game = new Game();
                    foreach (var item in gameTeams)
                    {
                        item.SetGame(game);
                    }
                    foreach (var item in rounds)
                    {
                        item.Game = game;
                    }
                    game.Club = await db.Clubs.FindAsync(clubId);
                    game.Deleted = false;
                    game.Ended = null;
                    game.Name = Name == "" ? "Namnlös" : Name;
                    game.IsGroup = groups > 1;
                    if (groups > 1)
                    {
                        game.Group = i + 1;

                    }
                    else
                    {
                        game.Group = 0;
                    }
                    game.IsScramble = false;
                    game.IsTournament = true;
                    game.Started = DateTime.Now;
                    game.Lanes = gameLanes;
                    game.Rounds = rounds;
                    game.Teams = gameTeams;
                    game.PlayersPerTeam = 1;
                    db.Games.Add(game);
                    await db.SaveChangesAsync();
                    foreach (var item in game.Rounds)
                    {
                        var _teams = item.Teams.ToArray();
                        item.Team1ID = _teams[0].ID;
                        item.Team2ID = _teams[1].ID;
                    }
                    game.Rounds = Utilities.FixEqualLaneAmount(game.Rounds, game.Lanes);
                }

                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index");
        }
        public async Task<ActionResult> Create(Game game)
        {
            if (ModelState.IsValid)
            {
                var club = db.Users.Find(User.Identity.GetUserId()).Club;
                List<Player> players = await GetPlayersAsync(game.SelectedPlayers);
                List<Lane> lanes = (await GetLanesAsync(game.SelectedLanes)).OrderBy(l=> l.Name).ToList();
                TeamFactory teamFactory = new TeamFactory(players);
                teamFactory.PlayersPerTeam = game.PlayersPerTeam;
                game.Lanes = lanes;
                if (game.IsScramble) teamFactory.ScramblePlayers();
                List<Team> teams = teamFactory.CreateTeams();
                if (game.Name == "") game.Name = "Namnlös";
                int[] teamSize = GetGroupSize(game.groups, teams.Count);
                //Beräkna banor
                int lanesPerGroup = GetLanesPerGroup(game.groups, lanes.Count);
                Game g = null;
                for (int i = 0; i < game.groups; i++)
                {
                    g = new Game();
                    g.Club = club;
                    g.Deleted = false;
                    g.Ended = null;
                    g.Name = game.Name;
                    g.IsGroup = game.groups > 0;
                    g.IsScramble = game.IsScramble;
                    g.PlayersPerTeam = game.PlayersPerTeam;
                    g.Started = DateTime.Now;
                    if (game.groups > 1)
                    {
                        g.Group = i + 1;

                    }
                    else
                    {
                        g.Group = 0;
                    }
                    List<Lane> gameLanes = lanes.Skip(i * lanesPerGroup).Take(lanesPerGroup).ToList();
                    g.Lanes = gameLanes;

                    List<Team> gameTeams = teams.Skip(toSkip(i, teamSize)).Take(teamSize[i]).ToList();
                    foreach (var item in gameTeams)
                    {
                        item.SetGame(g);

                    }
                    g.Rounds = Utilities.GetRounds(gameTeams, gameLanes).OrderBy(r=> r.PlayOrder).ToList();
                    g.Teams = gameTeams;
                    db.Games.Add(g);
                    await db.SaveChangesAsync();
                    foreach (var item in g.Rounds)
                    {
                        var _teams = item.Teams.ToArray();
                        item.Team1ID = _teams[0].ID;
                        item.Team2ID = _teams[1].ID;
                    }
                    g.Rounds = Utilities.FixEqualLaneAmount(g.Rounds, g.Lanes);
                }
                await db.SaveChangesAsync();
                if (g.IsGroup || g.IsTournament) return RedirectToAction("Index");
                return RedirectToAction("Details", new { @id = g.ID });
            }

            return View(game);
        }
        public async Task<ActionResult> RestartGame(Game game)
        {

            var club = db.Users.Find(User.Identity.GetUserId()).Club;
            var g = await db.Games.FindAsync(game.ID);

            var oldTeams = g.Teams;
            var teamSize = g.Teams.ToArray()[0].Players.Count;
            g.Throws.Clear();
            g.Rounds.Clear();
            foreach (var item in g.Teams)
            {
                item.Rounds.Clear();
            }
            g.Started = DateTime.Now;
            g.Deleted = false;


            var newPlayers = await GetPlayersAsync(game.SelectedPlayers);
            TeamFactory factory = new TeamFactory(newPlayers);
            factory.PlayersPerTeam = teamSize;

            var countAltName = oldTeams.Count + 1;
            var newTeams = factory.CreateTeams();
            //foreach (var item in g.Teams)
            //{
            //    item.Rounds.Clear();
            //}
            foreach (var item in newTeams)
            {
                item.AltName = countAltName.ToString();
                countAltName++;
                g.Teams.Add(item);
            }


            foreach (var item in g.Teams)
            {
                if (item.Rounds != null)
                {
                    item.Rounds.Clear();
                }
                else
                {
                    item.Rounds = new List<Round>();
                }

                item.SetGame(g);
            }

            var newRounds = Utilities.GetRounds(g.Teams, g.Lanes.ToList());
            g.Rounds = newRounds;
            //db.Entry(g).State = EntityState.Modified;
            await db.SaveChangesAsync();
            foreach (var item in g.Rounds)
            {
                if (item.Game == null)
                {
                    item.Game = game;
                }
                var _teams = item.Teams.ToArray();
                item.Team1ID = _teams[0].ID;
                item.Team2ID = _teams[1].ID;
            }
            g.Rounds = Utilities.FixEqualLaneAmount(g.Rounds, g.Lanes);
            await db.SaveChangesAsync();
            return RedirectToAction("Details", "Games", new { id = g.ID });
        }
        // GET: Games/Create
        public ActionResult Create()
        {

            var clubID = db.Users.Find(User.Identity.GetUserId()).Club.ID;
            ViewBag.ClubId = clubID;
            Game game = new Game()
            {
                Club = db.Clubs.Find(clubID)
            };

            return View(game);
        }
        // GET: Games/Create
        public async Task<ActionResult> CreateNG(CreateGameModel model)
        {
            if (model.SelectedPlayers == null) return null;
            //CreateGameModel model = JsonConvert.DeserializeObject<CreateGameModel>(res);
            var user = GetUser();
            List<Player> players = new List<Player>();
            List<Team> teams = new List<Team>();
            if (model.SelectedPlayers != null)
            {
                players = await GetPlayersAsync(model.GetSelectedPlayersIDs());
                TeamFactory teamFactory = new TeamFactory(players);
                teamFactory.PlayersPerTeam = model.TeamSize;
                if (model.ScrambledTeams) teamFactory.ScramblePlayers();
                teams = teamFactory.CreateTeams();
            }
            var club = user.Club;
            List<Lane> lanes = await GetLanesAsync(model.GetSelectedLanesIDs());
            if (model.SelectedTeams != null)
            {
                teams.AddRange(await CopyTeams(model.SelectedTeams));
            }

            var counter = 1;
            if (teams.Count < 3) return RedirectToAction("Create");
            foreach (var item in teams)
            {
                item.AltName = counter.ToString();
                counter++;
            }
            if (model.SaveToMemory)
            {
                var setting = new TeamSetting(teams);
                setting.SetCreated(user);
                setting.ClubID = club.ID;
                setting.SetName(model.MemoryName);
                db.SavedTeams.Add(setting);
            }

            if (model.GameName == "") model.GameName = "Namnlös";
            int[] teamSize = GetGroupSize(model.Groups, teams.Count);
            //Beräkna banor
            int lanesPerGroup = GetLanesPerGroup(model.Groups, lanes.Count);
            Game g = null;
            for (int i = 0; i < model.Groups; i++)
            {
                g = new Game();
                g.Club = club;
                g.Deleted = false;
                g.Ended = null;
                g.Name = model.GameName;
                g.IsGroup = model.Groups > 0;
                g.IsScramble = model.ScrambledTeams;
                g.PlayersPerTeam = model.TeamSize;
                g.Started = DateTime.Now;
                if (model.Groups > 1)
                {
                    g.Group = i + 1;

                }
                else
                {
                    g.Group = 0;
                }
                List<Lane> gameLanes = lanes.Skip(i * lanesPerGroup).Take(lanesPerGroup).ToList();
                g.Lanes = gameLanes;

                List<Team> gameTeams = teams.Skip(toSkip(i, teamSize)).Take(teamSize[i]).ToList();
                foreach (var item in gameTeams)
                {
                    item.SetGame(g);

                }
                g.Rounds = Utilities.GetRounds(gameTeams, gameLanes).OrderBy(r=> r.PlayOrder).ToList();
                g.Teams = gameTeams;
                db.Games.Add(g);
                await db.SaveChangesAsync();
                foreach (var item in g.Rounds)
                {
                    var _teams = item.Teams.ToArray();
                    item.Team1ID = _teams[0].ID;
                    item.Team2ID = _teams[1].ID;
                }
                g.Rounds = Utilities.FixEqualLaneAmount(g.Rounds, g.Lanes);
                await db.SaveChangesAsync();
            }

            return RedirectToAction("Index");
        }
 private bool CheckIsDemoPartial(Game game)
 {
     if (!game.IsDemo)
     {
         if (User.Identity.IsAuthenticated)
         {
             if (db.Users.Find(User.Identity.GetUserId()).Club.ID != game.Club.ID)
             {
                 Response.Redirect("~/Account/Login?returnUrl=" + Request.Url.LocalPath);
                 return true;
             }
         }
         else
         {
             Response.Redirect("~/Account/Login?returnUrl=" + Request.Url.LocalPath);
             return true;
         }
     }
     return false;
 }
        public PartialViewResult RenderScoreboard(Game game)
        {
            CheckIsDemoPartial(game);

            return PartialView("_Scoreboard", game);
        }
        private async Task<Game> GenerateDemo()
        {
            var players = new List<Player>();
            players.Add(new Player() { Club = null, CreatedOn = DateTime.Now, Deleted = false, FirstName = "Bosse & ", LastName = "Kristina", Gender = Gender.Male, IsTemporary = true });
            players.Add(new Player() { Club = null, CreatedOn = DateTime.Now, Deleted = false, FirstName = "Tore & ", LastName = "Fredrika", Gender = Gender.Male, IsTemporary = true });
            players.Add(new Player() { Club = null, CreatedOn = DateTime.Now, Deleted = false, FirstName = "Klas & ", LastName = "Kerstin", Gender = Gender.Male, IsTemporary = true });
            players.Add(new Player() { Club = null, CreatedOn = DateTime.Now, Deleted = false, FirstName = "Staffan & ", LastName = "Lisa", Gender = Gender.Male, IsTemporary = true });
            players.Add(new Player() { Club = null, CreatedOn = DateTime.Now, Deleted = false, FirstName = "Mats & ", LastName = "Olivia", Gender = Gender.Male, IsTemporary = true });
            players.Add(new Player() { Club = null, CreatedOn = DateTime.Now, Deleted = false, FirstName = "Stellan & ", LastName = "Margret", Gender = Gender.Male, IsTemporary = true });
            players.Add(new Player() { Club = null, CreatedOn = DateTime.Now, Deleted = false, FirstName = "Fia & ", LastName = "Lars", Gender = Gender.Male, IsTemporary = true });
            players.Add(new Player() { Club = null, CreatedOn = DateTime.Now, Deleted = false, FirstName = "Daniel & ", LastName = "Sofia", Gender = Gender.Male, IsTemporary = true });
            players.Add(new Player() { Club = null, CreatedOn = DateTime.Now, Deleted = false, FirstName = "Tilda & ", LastName = "Mikael", Gender = Gender.Male, IsTemporary = true });
            players.Add(new Player() { Club = null, CreatedOn = DateTime.Now, Deleted = false, FirstName = "Inga & ", LastName = "Håkan", Gender = Gender.Male, IsTemporary = true });
            players.Add(new Player() { Club = null, CreatedOn = DateTime.Now, Deleted = false, FirstName = "Joakim & ", LastName = "Louise", Gender = Gender.Male, IsTemporary = true });
            TeamFactory factory = new TeamFactory(players) { PlayersPerTeam = 1 };
            List<Team> allTeams = new List<Team>(factory.CreateTeams());

            var g = new Game();
            var lanes = new List<Lane>();
            lanes.Add(new Lane("A") { Club = null, Deleted = false });
            lanes.Add(new Lane("B") { Club = null, Deleted = false });
            g.Club = null;
            g.Deleted = false;
            g.Ended = null;
            g.IsDemo = true;
            g.IsGroup = false;
            g.IsScramble = false;
            g.IsTournament = false;
            g.Lanes = lanes;
            g.LastActive = DateTime.Now;
            g.Name = "Demo";
            g.Teams = allTeams;
            g.Players = players;
            foreach (var item in g.Teams)
            {
                item.SetGame(g);
            }
            g.Rounds = Utilities.GetRounds(g.Teams, lanes);
            db.Games.Add(g);
            await db.SaveChangesAsync();

            var counter = 1;
            foreach (var item in g.Rounds)
            {
                var _teams = item.Teams.ToArray();
                item.Team1ID = _teams[0].ID;
                item.Team2ID = _teams[1].ID;
                item.PlayOrder = counter;
                counter++;
            }
            await db.SaveChangesAsync();
            return g;
        }
Example #13
0
 public PlayerList(Game game)
 {
     this.Players = game.Players;
 }
Example #14
0
 public TeamSetting(Game game)
 {
     this.Teams = game.Teams;
 }