Exemplo n.º 1
0
        public ActionResult Add(int idDeputado)
        {
            int idUsuario = SessionManager.GetUsuario().Id;
            Time time = db.Times.FirstOrDefault(_time => _time.UsuarioID == idUsuario);

            if (time == null)
            {
                time = new Time();
                time.Deputados = new List<DeputadoTime>();
                time.UsuarioID = idUsuario;
                db.Times.Add(time);

                //criando a chave
                int timesPorChave = Convert.ToInt32(ConfigurationManager.AppSettings["times_por_chave"]);
                Chave chave = db.Chaves.FirstOrDefault(_chave => _chave.Times.Count() > timesPorChave);
                //se nao há nenhuma chave livre
                if (chave == null)
                {
                    chave = new Chave();
                    db.Chaves.Add(chave);
                    PopularChave(chave, 11);
                }

                chave.Times.Add(time);

                db.SaveChanges();
            }

            Deputado deputado = db.Deputadoes.First(_deputado => _deputado.Id == idDeputado);
            DeputadoTime deputadoTime = new DeputadoTime() { DeputadoID = deputado.Id, TimeID = time.Id };

            time.Deputados.Add(deputadoTime);

            db.SaveChanges();

            return Content("ok");
        }
Exemplo n.º 2
0
        public ActionResult AddBrasao(string img)
        {
            int idUsuario = SessionManager.GetUsuario().Id;
            Time time = db.Times.FirstOrDefault(_time => _time.UsuarioID == idUsuario);

            if (time != null)
            {
                time.pathBrasao = "~/Uploads/" + img;
                db.SaveChanges();
            }
            else
            {
                time = new Time();
                time.Deputados = new List<DeputadoTime>();
                time.UsuarioID = idUsuario;
                time.pathBrasao = "~/Uploads/" + img;
                db.Times.Add(time);

                //criando a chave
                int timesPorChave = Convert.ToInt32(ConfigurationManager.AppSettings["times_por_chave"]);
                Chave chave = db.Chaves.FirstOrDefault(_chave => _chave.Times.Count() > timesPorChave);
                //se nao há nenhuma chave livre
                if (chave == null)
                {
                    chave = new Chave();
                    db.Chaves.Add(chave);
                    PopularChave(chave, 11);
                }

                chave.Times.Add(time);
                db.SaveChanges();
            }

            return Content("ok");
        }
Exemplo n.º 3
0
        public ActionResult FacebookCallback(string code)
        {
            var fb = new FacebookClient();
            dynamic result = fb.Post("oauth/access_token", new
            {
                client_id = ConfigurationManager.AppSettings["facebook_app_id"],
                client_secret = ConfigurationManager.AppSettings["facebook_app_secret"],
                redirect_uri = Util.getFacebookUrl(Url, Request.Url).AbsoluteUri,
                code = code
            });

            var accessToken = result.access_token;

            Session["AccessToken"] = accessToken;

            fb.AccessToken = accessToken;

            dynamic me = fb.Get("me?fields=first_name,last_name,id,email");
            string email = me.email;

            //cria um usuario
            Usuario usuarioFb = new Usuario();
            usuarioFb.Email = me.email;
            usuarioFb.Nome = me.first_name;
            usuarioFb.Sobrenome = me.last_name;
            usuarioFb.IdFacebook = me.id;
            usuarioFb.UltimoLogin = DateTime.Now;

            string foto = GetPictureUrl(me.id);
            usuarioFb.FotoPerfil = foto;

            if (db.Usuarios.Count(_usuario => _usuario.IdFacebook == usuarioFb.IdFacebook) == 0)
            {
                db.Usuarios.Add(usuarioFb);
                db.SaveChanges();

                Time time = db.Times.FirstOrDefault(_time => _time.UsuarioID == usuarioFb.Id);
                Time timeBot = null;

                if (time == null)
                {
                    time = new Time();
                    time.Deputados = new List<DeputadoTime>();
                    time.UsuarioID = usuarioFb.Id;
                    db.Times.Add(time);

                    //criando a chave
                    int timesPorChave = Convert.ToInt32(ConfigurationManager.AppSettings["times_por_chave"]);
                    Chave chave = db.Chaves.FirstOrDefault(_chave => _chave.Times.Count(_time => !_time.bot) <= timesPorChave);
                    //se nao há nenhuma chave livre
                    if (chave == null)
                    {
                        chave = new Chave();
                        db.Chaves.Add(chave);
                        PopularChave(chave, 11);
                    }
                    else
                    {
                        //recupera o primeiro time do computador nesta chave
                        timeBot = chave.Times.First(_time => _time.bot);
                        List<Partida> partidas = db.Partidas.Where(_partida => _partida.TimeDeForaID == timeBot.Id || _partida.TimeDaCasaID == timeBot.Id).ToList();
                        //altera as partidas jah existentes
                        foreach (var partida in partidas)
                        {
                            if (partida.TimeDaCasaID == timeBot.Id)
                            {
                                partida.TimeDaCasaID = time.Id;
                            }
                            else
                            {
                                partida.TimeDeForaID = time.Id;
                            }
                        }

                    }
                    //salva as mudancas
                    chave.Times.Add(time);
                    db.SaveChanges();

                    //se utilizou a substituicao de times apaga o time do computador
                    if (timeBot != null)
                    {
                        //arrumar isso, nao deveria ser necessario recuperar de novo o time para poder excluilo
                        timeBot = db.Times.First(_time => _time.Id == timeBot.Id);
                        db.Times.Remove(timeBot);
                        db.SaveChanges();
                    }
                }
            }
            else
            {
                usuarioFb = db.Usuarios.Where(_usuario => _usuario.IdFacebook == usuarioFb.IdFacebook).First();
                usuarioFb.UltimoLogin = DateTime.Now;
                usuarioFb.FotoPerfil = foto;
                db.SaveChanges();
            }

            SessionManager.SetUsuario(usuarioFb);

            return RedirectToAction("Index", "Home");
        }
Exemplo n.º 4
0
        private void PopularChave(Chave chave,int totalTimes)
        {
            Usuario bot = db.Usuarios.FirstOrDefault(_usuario => _usuario.IdFacebook == "bot");

            if (bot == null)
            {
                bot = new Usuario {
                    Nome = "jogador ",
                    Sobrenome = "do sistema",
                    IdFacebook = "bot",
                    UltimoLogin = DateTime.Now,
                    FotoPerfil = "~/Images/usuario.png"
                };
                db.Usuarios.Add(bot);
                db.SaveChanges();
            }

            int j = 1;
            for (int i = chave.Times.Count(); i <= totalTimes; i++)
            {
                Time time = new Time();
                time.Deputados = new List<DeputadoTime>();
                time.UsuarioID = bot.Id;
                time.Chave = chave;
                time.pathBrasao = "~/Content/Images/brasao" + (int)(j) + ".jpg";

                time.bot = true;

                time.Nome = "Computador " + i;

                db.Times.Add(time);
                db.SaveChanges();
                var deputados = db.Deputadoes.OrderBy(r => Guid.NewGuid()).Take(14).
                    ToList().ConvertAll(_deputado => new DeputadoTime { DeputadoID = _deputado.Id, TimeID = time.Id });
                time.Deputados.AddRange(deputados);
                db.SaveChanges();
                j++;
            }
        }
Exemplo n.º 5
0
        private bool GerarTabelaJogos(Chave chave)
        {
            int conta = 1;
            //0:Domingo
            //1:Quarta
            int nextGame = 0;
            DateTime AtualGame = DateTime.Now;

            List<DateTime> datasJogos = new List<DateTime>();
            if (DateTime.Now.DayOfWeek == DayOfWeek.Sunday ||
                DateTime.Now.DayOfWeek == DayOfWeek.Monday ||
                DateTime.Now.DayOfWeek == DayOfWeek.Tuesday)
            {
                nextGame = 1;
            }

            //Numero de jogos por time
            while (conta <= 20)
            {
                AtualGame = GetNextGame(AtualGame, nextGame);
                datasJogos.Add(AtualGame);
                if (nextGame == 1)
                {
                    nextGame = 0;
                }
                else
                {
                    nextGame = 1;
                }
                conta++;
            }

            int j = 0;
            int casa = 0;
            int fora = 0;
            Random random = new Random();
            for (int i = 0; i < (chave.Times.Count - 1); i++)
            {
                j = i + 1;
                for (; j < chave.Times.Count; j++)
                {

                    if (random.NextDouble() < .5)
                    {
                        casa = i;
                        fora = j;
                    }
                    else
                    {
                        casa = j;
                        fora = i;
                    }
                    DateTime dataTemp = DateTime.Now;
                    foreach (DateTime date in datasJogos)
                    {
                        int tempCasaID = chave.Times[casa].Id;
                        int tempForaID = chave.Times[fora].Id;
                        var temp = db.Partidas.FirstOrDefault(_partida => (
                            _partida.TimeDaCasaID == tempCasaID ||
                            _partida.TimeDaCasaID == tempForaID ||
                            _partida.TimeDeForaID == tempCasaID ||
                            _partida.TimeDeForaID == tempForaID) &&
                            (EntityFunctions.TruncateTime(_partida.DataDoJogo) == EntityFunctions.TruncateTime(date)));

                        if (temp == null)
                        {
                            dataTemp = date;
                            break;
                        }

                    }
                    Partida partida = new Partida
                    {
                        TimeDaCasa = chave.Times[casa],
                        TimeDeFora = chave.Times[fora],
                        DataDoJogo = dataTemp,
                        PlacarTimeDaCasa = 0,
                        PlacarTimeDeFora = 0
                    };
                    db.Partidas.Add(partida);
                    db.SaveChanges();
                }
            }
            db.SaveChanges();

            return true;
        }