Exemplo n.º 1
0
        private RankingView buscarOponentesNaoRepetidos(List <RankingView> listaJogadores, List <Jogo> ultimosJogos, int reduzVerificacao = 0)
        {
            bool   isDesafiante;
            int    qtddTentativas = 0;
            Random r = new Random();

            while (qtddTentativas < 30)
            {
                int randomIndex = r.Next(1, listaJogadores.Count); //Choose a random object in the list
                isDesafiante = true;
                for (int j = 0; j < ultimosJogos.Count() - reduzVerificacao; j++)
                {
                    if ((ultimosJogos[j].desafiado_id == listaJogadores[randomIndex].userProfile_id) || (ultimosJogos[j].desafiante_id == listaJogadores[randomIndex].userProfile_id))
                    {
                        isDesafiante = false;
                        break;
                    }
                }
                if (isDesafiante)
                {
                    RankingView desafiante = listaJogadores[randomIndex];
                    listaJogadores.RemoveAt(randomIndex);
                    listaJogadores.RemoveAt(0);
                    return(desafiante);
                }
                qtddTentativas++;
            }
            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts an enumerable set of Ranking models into a Ranking View Models.
        /// </summary>
        /// <param name="rankings">Ranking models you are converting.</param>
        /// <param name="begin">Beginning index of the rankings.</param>
        /// <param name="asccending">Sort direction of the rankings.</param>
        private IEnumerable <RankingView> ConvertRankingsToRankingViews(IEnumerable <Ranking> rankings, int?begin = 0, bool asccending = true)
        {
            IList <RankingView> rankingViews = new List <RankingView>();
            IList <Ranking>     RankingList  = rankings.ToList();

            for (int i = 0; i < RankingList.Count; i++)
            {
                RankingView rankingView = ConvertRankingToRankingView(RankingList[i], i, begin, asccending, RankingList.Count);
                rankingViews.Add(rankingView);
            }

            return(rankingViews);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts a Ranking model into a Ranking View Model.
        /// </summary>
        /// <param name="ranking">Ranking model you are converting.</param>
        /// <param name="index">Index of ranking.</param>
        /// <param name="begin">Beginning index of the rankings.</param>
        /// <param name="ascending">Sort direction of the rankings.</param>
        /// <param name="count">Size of the queried rankings.</param>
        private RankingView ConvertRankingToRankingView(Ranking ranking, int index, int?begin = 0, bool ascending = true, int?count = 0)
        {
            if (null == begin)
            {
                begin = 0;
            }

            RankingView rankingView = new RankingView();

            if (ascending)
            {
                rankingView.Rank = index + 1 + (int)begin;
            }
            else
            {
                rankingView.Rank = (int)count + (int)begin - index;
            }
            rankingView.Username        = ranking.Username;
            rankingView.Rating          = ranking.Rating;
            rankingView.LeaderboardName = ranking.Leaderboard.LeaderboardName;

            return(rankingView);
        }
Exemplo n.º 4
0
        private List <RankingView> selecionarJogadorParaFicarFora(List <RankingView> jogadores, int rodadaAnterior, int rodadaAtual, int classeId)
        {
            List <Jogo> jogo    = db.Jogo.Where(j => j.rodada_id == rodadaAnterior && j.situacao_Id != 4 && j.situacao_Id != 5 && j.desafiado.classeId == classeId).ToList();
            UserProfile jogador = null;
            RankingView rv      = null;

            for (int i = 0; i < jogo.Count(); i++)
            {
                if ((rodadaAnterior % 2 == 0) && (jogo[i].desafiante.situacao.Equals("ativo")))
                {
                    jogador = jogo[i].desafiante;
                }
                else if ((jogo[i].desafiado.situacao.Equals("ativo")))
                {
                    jogador = jogo[i].desafiado;
                }
            }
            UserProfile curinga = db.UserProfiles.Where(u => u.situacao.Equals("curinga")).Single();

            if (jogador != null)
            {
                criarJogo(jogador.UserId, curinga.UserId, rodadaAtual, true);
                var itemToRemove = jogadores.SingleOrDefault(r => r.userProfile_id == jogador.UserId);
                if (itemToRemove != null)
                {
                    jogadores.Remove(itemToRemove);
                }
            }
            else
            {
                criarJogo(jogadores[0].userProfile_id, curinga.UserId, rodadaAtual, true);
                rv = jogadores[0];
                jogadores.Remove(rv);
            }

            return(jogadores);
        }
Exemplo n.º 5
0
        private RankingView selecionarAdversario(List <RankingView> listaJogadores, RankingView desafiado, int rodadaAnteriorId)
        {
            RankingView desafiante = null;

            if (listaJogadores.Count() == 2)
            {
                desafiante = listaJogadores[1];
                listaJogadores.RemoveAt(1);
                listaJogadores.RemoveAt(0);
                return(desafiante);
            }
            // O jogador não deve repetir o mesmo jogo das 3 últimas rodadas

            // busca os 3 últimos jogos do desafiado
            List <Jogo> jogosAnteriores = db.Jogo.Where(j => (j.rodada_id <= rodadaAnteriorId &&
                                                              (j.desafiado_id == desafiado.userProfile_id || j.desafiante_id == desafiado.userProfile_id))).
                                          Take(3).OrderByDescending(j => j.Id).ToList();

            for (int i = 0; i <= 2; i++)
            {
                // busca um oponente mais próximo que não tenha jogado nos últimos 3 jogos ou nos últimos 2 jogos ou no último jogo
                desafiante = buscarOponentesNaoRepetidos(listaJogadores, jogosAnteriores, i);
                if (desafiante != null)
                {
                    return(desafiante);
                }
            }
            // caso não encontre em nenhuma situação acima, realiza um sorteio
            Random r           = new Random();
            int    randomIndex = r.Next(1, listaJogadores.Count); //Choose a random object in the list

            desafiante = listaJogadores[randomIndex];             //add it
            listaJogadores.RemoveAt(randomIndex);
            listaJogadores.RemoveAt(0);
            return(desafiante);
        }
Exemplo n.º 6
0
        private void EfetuarSorteio(int idRodada, int barragemId, int classeId)
        {
            // excluir os jogos já sorteados para o caso de estar sorteando novamente
            db.Database.ExecuteSqlCommand("DELETE j fROM jogo j INNER JOIN UserProfile u ON j.desafiado_id=u.UserId WHERE u.classeId = " + classeId + " AND j.rodada_id =" + idRodada);
            // monta a lista ordenada pelo último rancking consolidado
            int Id_rodadaAnterior        = db.Rancking.Where(r => r.rodada.isAberta == false && r.rodada_id < idRodada && r.rodada.barragemId == barragemId).Max(r => r.rodada_id);
            List <RankingView> jogadores = db.RankingView.Where(r => r.barragemId == barragemId && r.classeId == classeId && r.situacao.Equals("ativo")).OrderByDescending(r => r.totalAcumulado).ToList();

            // se a quantidade de participantes ativos for impar o sistema escolherá,
            // de acordo com a regra estabelecida, um jogador para ficar de fora
            if (jogadores.Count % 2 != 0)
            {
                jogadores = selecionarJogadorParaFicarFora(jogadores, Id_rodadaAnterior, idRodada, classeId);
            }
            // o mais bem posicionado no ranking será desafiado por alguém pior posicionado
            RankingView desafiante = null;

            while (jogadores.Count > 0)
            {
                RankingView desafiado = jogadores[0];
                desafiante = selecionarAdversario(jogadores, desafiado, Id_rodadaAnterior);
                criarJogo(desafiado.userProfile_id, desafiante.userProfile_id, idRodada);
            }
        }
Exemplo n.º 7
0
        public void InitCommand()
        {
            ChooseImageCommand = new RelayCommand(() =>
            {
                var dialog = new OpenFileDialog()
                {
                    //TODO: filters
                };
                dialog.DefaultExt = ".jpg";
                dialog.Filter     = "Image (*.jpg)|*.jpg|Image (*.png)|*.png";

                if (dialog.ShowDialog() == true)
                {
                    Image = dialog.FileName;
                    ViewModelLocator.DisplayImage = Image;
                    ChoosenSplit = null;
                    navigationService.NavigateTo(ViewModelLocator.DisplayImageKey, Image, true);
                }
            });

            SoundCommand = new RelayCommand(() =>
            {
                if (play)
                {
                    ImageSound = "/Images/mute.png";
                }
                else
                {
                    ImageSound = "/Images/speaker.png";
                }
                play = !play;
                PlayOrStop();
            });

            MixImageCommand = new RelayCommand(() =>
            {
                if (Image == null)
                {
                    MessageBox.Show("Wybierz obraz", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else if (ChoosenSplit == null)
                {
                    MessageBox.Show("Wybierz poziom", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    if (ChoosenSplit == "3x3")
                    {
                        TrzyNaTrzyViewModel.StartTime = DateTime.Now;
                        TrzyNaTrzyViewModel.GameList.Clear();
                        TrzyNaTrzyViewModel.IsMixed  = true;
                        TrzyNaTrzyViewModel.GameList = GameHelper.SplitImage(Image, 3);
                    }
                    else if (ChoosenSplit == "4x4")
                    {
                        CzteryNaCzteryViewModel.StartTime = DateTime.Now;
                        CzteryNaCzteryViewModel.GameList.Clear();
                        CzteryNaCzteryViewModel.IsMixed  = true;
                        CzteryNaCzteryViewModel.GameList = GameHelper.SplitImage(Image, 4);
                    }
                    else if (ChoosenSplit == "5x5")
                    {
                        PiecNaPiecViewModel.StartTime = DateTime.Now;
                        PiecNaPiecViewModel.GameList.Clear();
                        PiecNaPiecViewModel.IsMixed  = true;
                        PiecNaPiecViewModel.GameList = GameHelper.SplitImage(Image, 5);
                    }
                }
            });

            RankingCommand = new RelayCommand(() =>
            {
                App.Current.MainWindow.Hide();
                RankingView rankWindow = new RankingView();
                rankWindow.ShowDialog();

                App.Current.MainWindow.Show();
            });
        }