protected string GetType(Tournament tournoi)
        {
            if (tournoi is EliminationTournament)
                return "Elimination";
            if (tournoi is PoolEliminationTournament)
                return "PoolElimination";
            if (tournoi is PoolTournament)
                return "Pool";
            if (tournoi is RoundTournament)
                return "Round";

            throw new ApplicationException("Type de tournoi non reconnu");
        }
Пример #2
0
        public async Task<Tournament> Update(Tournament tournament, string userMatricule)
        {
            //récupération du tournoi existant
            Tournament currentTournament = await Get(tournament.Id);
            if (currentTournament == null)
            {
                throw new NullReferenceException(string.Format("le tournoi avec l'id {0} est introuvable", tournament.Id));
            }

            if (currentTournament.CreatorId != userMatricule)
                throw new InvalidOperationException("Seul le créateur du tournoi peut le modifier");

            //suppression des équipes
            currentTournament.Teams.ToList().ForEach(t => _teamStore.SetState(t, EntityState.Deleted));
            currentTournament.Teams.Clear();


            //recréation des équipes
            List<string> matricules = tournament.Teams.SelectMany(p => p.Players).Where(p => p != null).Select(p => p.Matricule).ToList();
            var players = await _playerStore.GetFromMatricules(matricules);

            foreach (Team team in tournament.Teams.Where(team => team != null && team.Players != null))
            {
                team.Id = 0;
                team.Players = team.Players.Where(p => p != null).Select(m => players.FirstOrDefault(j => m.Matricule == j.Matricule)).Where(p => p != null).ToList();
                currentTournament.Teams.Add(team);
            }
            await _teamStore.Add(tournament.Teams);

            //maj du match
            currentTournament.Date = tournament.Date;
            currentTournament.Name = tournament.Name;
            currentTournament.Private = tournament.Private;
            currentTournament.Ranked = tournament.Ranked;
            currentTournament.SportKey = tournament.SportKey;
            
            var tournamentSystem = _tournamentSystemFactory.Get(currentTournament);
            tournamentSystem.Create();

            await _tournamentStore.SaveChangesAsync();

            return tournament;
        }
        public async Task CreateTournamentFinaleTest()
        {
            var matchBP = new TournamentBP(_playerStoreMock.Object, _teamStoreMock.Object,_sportStoreMock.Object, _tournamentStoreMock.Object, _tournamentSystemFactoryMock.Object);
            var tournament = new Tournament
            {
                CreatorId = "test",
                Id = 1,
                Teams = CreateTeams(4)
            };

            _tournamentStoreMock.Setup(t => t.Get(1)).ReturnsAsync(tournament);
            _tournamentSystemFactoryMock.Setup(m => m.Get(tournament)).Returns(_tournamentSystemMock.Object);
            _tournamentSystemMock.Setup(m => m.Create());
            _tournamentStoreMock.Setup(m => m.SaveChangesAsync()).Returns(() => Task.FromResult(default(Task)));

            await matchBP.CreateMatchs(new Tournament { Id = 1 }, "test");
            _tournamentSystemMock.Verify(m=>m.Create(),Times.Once());

            Assert.IsNotNull(tournament.Qualifications);
        }
Пример #4
0
        public async Task<Tournament> CreateMatchs(Tournament tournament, string userMatricule)
        {
            //récupération du match existant
            Tournament currentTournament = await Get(tournament.Id);
            if (currentTournament == null)
            {
                throw new NullReferenceException(string.Format("le tournoi avec l'id {0} est introuvable", tournament.Id));
            }

            if (currentTournament.State != StateEnum.Opened)
                throw new InvalidOperationException("Le tournoi est déjà en cours ou clos");

            if (currentTournament.CreatorId != userMatricule)
                throw new InvalidOperationException("Seul le créateur du tournoi peut le modifier");

            var tournamentSystem = _tournamentSystemFactory.Get(currentTournament);
            tournamentSystem.Tournament = currentTournament;
            tournamentSystem.Create();

            await _tournamentStore.SaveChangesAsync();

            return currentTournament;
        }
Пример #5
0
        public async Task<Tournament> Leave(Tournament tournament, string playerMatricule, string userMatricule)
        {
            var currentMatch = await _tournamentStore.Get(tournament.Id);

            if (currentMatch == null)
                throw new InstanceNotFoundException("Le tournoi est introuvable");

            if (currentMatch.State == StateEnum.Draw || currentMatch.State == StateEnum.Won)
                throw new InvalidOperationException("Le tournoi est déjà clos");

            if (playerMatricule != userMatricule && currentMatch.CreatorId != userMatricule)
                throw new InvalidOperationException("Seul le créateur du tournoi peut modifier sa composition");

            var player = await _playerStore.GetFromMatricule(playerMatricule);
            if (player == null)
                throw new InvalidOperationException("Le joueur est introuvable");

            var team = currentMatch.Teams.SingleOrDefault(t => t.Players.Select(p => p.Matricule.ToLowerInvariant()).Contains(playerMatricule.ToLowerInvariant()));
            if (team == null)
                throw new InvalidOperationException("Le joueur ne fait parti d'aucune équipe");


            team.Players.Remove(player);
            await _playerStore.SaveChangesAsync();


            return await Task.FromResult(currentMatch);
        }
Пример #6
0
        public async Task<Tournament> Join(Tournament tournament, int teamId, string playerMatricule, string userMatricule)
        {
            var currentTournament = await _tournamentStore.Get(tournament.Id);

            if (currentTournament == null)
                throw new InstanceNotFoundException("Le tournoi est introuvable");


            if (currentTournament.State == StateEnum.Draw || currentTournament.State == StateEnum.Won)
                throw new InvalidOperationException("Le tournoi est déjà clos");

            if (playerMatricule != userMatricule && currentTournament.CreatorId != userMatricule)
                throw new InvalidOperationException("Seul le créateur du tournoi peut modifier sa composition");

            var player = await _playerStore.GetFromMatricule(playerMatricule);
            if (player == null)
                throw new InvalidOperationException("Le joueur est introuvable");

            if (currentTournament.Teams.SelectMany(t => t.Players).Select(p => p.Matricule.ToLowerInvariant()).Contains(playerMatricule.ToLowerInvariant()))
                throw new InvalidOperationException("Le joueur fait déjà parti d'une équipe");

            var team = currentTournament.Teams.SingleOrDefault(t => t.Id == teamId);
            if (team == null)
                throw new InvalidOperationException("L'équipe n'est pas liée au match");

            var currentSport = await _sportStore.Get(currentTournament.SportKey);
            if (team.Players.Count >= currentSport.MaxPlayers)
                throw new InvalidOperationException("L'équipe est déjà complète");

            team.Players.Add(player);
            await _playerStore.SaveChangesAsync();

            return await Task.FromResult(currentTournament);
        }
Пример #7
0
        private void RandomizeTeams(Tournament tournament)
        {
            //récupération des joueurs
            var rnd = new Random();
            List<Player> players = tournament.Teams.SelectMany(p => p.Players).Where(p => p != null).OrderBy(item => rnd.Next()).ToList();

            tournament.Teams.ForEach(t => t.Players.Clear());
            while (players.Count > 0)
            {
                foreach (var team in tournament.Teams)
                {
                    if (players.Count <= 0)
                        break;
                    var player = players.First();
                    players.RemoveAt(0);
                    team.Players.Add(player);
                }
            }

            //suppression des équipes vides
            tournament.Teams.Where(t => t.Players.Count == 0).ToList().ForEach(t => _teamStore.SetState(t, EntityState.Deleted));

            tournament.Teams = tournament.Teams.Where(t => t.Players.Count > 0).ToList();
        }
Пример #8
0
        public async Task<Tournament> Add(Tournament tournament, string userMatricule)
        {
            tournament.CreatorId = userMatricule;
            foreach (Team team in tournament.Teams.Where(team => team != null && team.Players != null))
            {
                List<string> players = team.Players.Where(p => p != null).Select(p => p.Matricule).ToList();
                team.Players = await _playerStore.GetFromMatricules(players);
            }

            tournament.Sport = await _sportStore.Get(tournament.SportKey);
             

            var tournamentSystem = _tournamentSystemFactory.Get(tournament);
            tournamentSystem.Tournament = tournament;
            tournamentSystem.Create();

            await _teamStore.Add(tournament.Teams);
            await _tournamentStore.Add(tournament);
            await _tournamentStore.SaveChangesAsync();

            return tournament;
        }
 public async Task<IHttpActionResult> Join(Tournament tournament, int teamId )
 {
     return Ok(TournamentModel.From(await _tournamentBP.Join(tournament, teamId, _userMatricule, _userMatricule)));
 }
 public async Task<IHttpActionResult> Post(Tournament tournament)
 {
     var newMatch = await _tournamentBP.Add(tournament, _userMatricule);
     return Ok(TournamentModel.From(newMatch));
 }
 public async Task<IHttpActionResult> Put(int id, Tournament tournament)
 {
     var newMatch = await _tournamentBP.Update(tournament, _userMatricule);
     return Ok(TournamentModel.From(newMatch));
 }
Пример #12
0
 public async Task<Tournament> SetState(Tournament tournament, EntityState entityState)
 {
     _matchContext.Entry(tournament).State = entityState;
     return await Task.FromResult(tournament);
 }
Пример #13
0
 public async Task<Tournament> Add(Tournament tournament)
 {
     _matchContext.Tournaments.Add(tournament);
     return await Task.FromResult(tournament);
 }
Пример #14
0
        private static void From(TournamentModel tournamentModel, Tournament tournament)
        {
            if (tournament == null)
                return;

                tournamentModel.Type = tournament.Type;
                tournamentModel.CreatorId = tournament.CreatorId;
                tournamentModel.Creator = PlayerModel.From(tournament.Creator);
                tournamentModel.Date = tournament.Date;
                tournamentModel.Id = tournament.Id;
                tournamentModel.Name = tournament.Name;
                tournamentModel.Private = tournament.Private;
                tournamentModel.Ranked = tournament.Ranked;
                tournamentModel.Randomize = tournament.Randomize;
                tournamentModel.SportKey = tournament.SportKey;
                tournamentModel.WinnerId = tournament.WinnerId;
                tournamentModel.Teams = tournament.Teams.Select(TeamModel.From).ToList();
                tournamentModel.State = tournament.State;
            tournamentModel.Qualifications = tournament.Qualifications.Select(QualificationModel.From).ToList();
        }
Пример #15
0
        public static TournamentModel From(Tournament tournament)
        {
            if (tournament == null)
                return null;

            if (tournament is EliminationTournament)
                return From((EliminationTournament)tournament);
            if (tournament is PoolTournament)
                return From((PoolTournament)tournament);
            if (tournament is PoolEliminationTournament)
                return From((PoolEliminationTournament)tournament);
            if (tournament is RoundTournament)
                return From((RoundTournament)tournament);

            return null;
        }
 public async Task<IHttpActionResult> Leave(Tournament tournament)
 {
     return Ok(TournamentModel.From(await _tournamentBP.Leave(tournament, _userMatricule, _userMatricule)));
 }
Пример #17
0
        public async Task<Tournament> Start(Tournament tournament, string userMatricule)
        {
            //récupération du tournoi existant
            Tournament currentTournament = await Get(tournament.Id);
            if (currentTournament == null)
            {
                throw new NullReferenceException(string.Format("le tournoi avec l'id {0} est introuvable", tournament.Id));
            }


            if (currentTournament.State != StateEnum.Opened)
                throw new InvalidOperationException("Le tournoi est déjà en cours ou clos");

            if (currentTournament.CreatorId != userMatricule)
                throw new InvalidOperationException("Seul le créateur du tournoi peut le démarrer");

            if (currentTournament.Randomize)
                RandomizeTeams(tournament);

            currentTournament.State = StateEnum.InProgress;

            ///TODO: création des pools et des qualifs
            var tournamentSystem = _tournamentSystemFactory.Get(currentTournament);
            tournamentSystem.Create();

            await _tournamentStore.SaveChangesAsync();

            return currentTournament;
        }
 public ITournamentSystem Get(Tournament tournament)
 {
     var type = tournament.GetType();
     return (ITournamentSystem)_container.Resolve(typeof(ITournamentSystem<>).MakeGenericType(type));
 }