コード例 #1
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");
        }
コード例 #2
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");
        }
コード例 #3
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++;
            }
        }
コード例 #4
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");
        }