Advertisement
rogerin

Untitled

Nov 16th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module.exports = function(app){
  2.  
  3.     var Betplay = app.models.betplay;
  4.     var Aposta = app.models.aposta;
  5.     var mongoose = require('mongoose');
  6.  
  7.     var respostas = {
  8.         acertos: [
  9.             'Você acertou!! Veja no menu "Minhas Vitórias" e preencha os dados para receber seu premio'
  10.         ],
  11.         erros: [
  12.             'Não foi desta vez.',
  13.             'Continue tentando, sua sorte vai chegar.',
  14.             'UUuuu... Não foi desta vez, mas continue tentando.',
  15.             'Desta vez você errou, mas por sorte, as apostas são ilimitadas, continue tentando.',
  16.             'Quem sabe da proxima vez você acerta, continue tentando.',
  17.             'Putz, não foi dessa vez, quem sabe na próxima.',
  18.             'Você não consegiu desta vez, mas continue apostando!',
  19.             'Errou, mas com apostas ilimitadas, uma hora sua sua sorte chega.',
  20.             'Pissiu, você errou, mas tenho uma dica para você, continue arriscando, as apostas são ilimitadas.'
  21.         ]
  22.     };
  23.  
  24.     var BetplayController = {
  25.         listBetplays: function(req,res) {
  26.             var _query = {
  27.                 status: true
  28.             };
  29.  
  30.             Betplay.find(_query, function(err, betplays){
  31.                 res.json(betplays);
  32.             });
  33.         },
  34.  
  35.         cadastro: function() {
  36.  
  37.         },
  38.  
  39.         loteriaAtiva: function(req,res){
  40.             var notlist = {
  41.                 numero1: 0,
  42.                 numero2: 0,
  43.                 numero3: 0,
  44.                 numero4: 0
  45.             }
  46.             Betplay.findOne({status: true},notlist, function(err, loteria){
  47.                 res.json(loteria);
  48.             });
  49.         },
  50.  
  51.         gerarPrimeira: function(req,res){
  52.             Betplay.findOne({status: true}, function(err, loteria){
  53.                 if(err) {
  54.                     res.json(err);
  55.                 } else {
  56.                     if(loteria) {
  57.                         res.json({teste: "tem cadastro"});
  58.                     } else {
  59.                         var numeros = {
  60.                             numero1: Math.floor(Math.random() * 61),
  61.                             numero2: Math.floor(Math.random() * 61),
  62.                             numero3: Math.floor(Math.random() * 61),
  63.                             numero4: Math.floor(Math.random() * 61),
  64.                             status: true
  65.                         };
  66.  
  67.                         new Betplay(numeros).save(function(err, loteria){
  68.                             if(err){
  69.                                 res.json(err);
  70.                             } else {
  71.                                 res.json(loteria);
  72.                             }
  73.                         });
  74.                     }
  75.                 }
  76.             });
  77.         },
  78.         listar: function(req,res){
  79.             if(req.user.admin){
  80.                 Betplay.find(function(err, loterias){
  81.                     if(err) {
  82.                         res.json(err);
  83.                     } else {
  84.                         res.json(loterias);
  85.                     }
  86.                 });
  87.             } else {
  88.                 res.status(401).end();
  89.             }
  90.         },
  91.         gerarNumero: function(req,res){
  92.             Betplay.findOne({status: true}, function(err, loteria){
  93.                 loteria.status = false;
  94.                 loteria.save(function(err, l){
  95.                     var numeros = {
  96.                         numero1: Math.floor(Math.random() * 61),
  97.                         numero2: Math.floor(Math.random() * 61),
  98.                         numero3: Math.floor(Math.random() * 61),
  99.                         numero4: Math.floor(Math.random() * 61),
  100.                         status: true
  101.                     };
  102.  
  103.                     new Betplay(numeros).save(function(err, loteria){
  104.                         if(err){
  105.                             res.json(err);
  106.                         } else {
  107.                             res.json(loteria);
  108.                         }
  109.                     });
  110.                 });
  111.             });
  112.         },
  113.         apostar: function(req, res){
  114.             var totalApostasUser;
  115.             var totalApostas;
  116.             var numeros = {
  117.                 numero1: req.body.numero1,
  118.                 numero2: req.body.numero2,
  119.                 numero3: req.body.numero3,
  120.                 numero4: req.body.numero4,
  121.                 user:   req.user._id,
  122.                 loteria: req.body.loteria  
  123.             };
  124.            
  125.             var query = {
  126.                 _id: mongoose.Types.ObjectId(req.body.loteria),
  127.                 numero1: parseInt(req.body.numero1),
  128.                 numero2: parseInt(req.body.numero2),
  129.                 numero3: parseInt(req.body.numero3),
  130.                 numero4: parseInt(req.body.numero4),
  131.                 status: true
  132.             };
  133.            
  134.             new Aposta(numeros)
  135.                 .save(function(err, aposta){
  136.                     if(err) {
  137.                         console.log(err);
  138.                         res.json(err);
  139.                     } else {
  140.                         Betplay.findOne({
  141.                             _id: mongoose.Types.ObjectId(req.body.loteria),
  142.                             numero1: parseInt(req.body.numero1),
  143.                             numero2: parseInt(req.body.numero2),
  144.                             numero3: parseInt(req.body.numero3),
  145.                             numero4: parseInt(req.body.numero4),
  146.                             status: true
  147.                         }, function(err, loteria){
  148.                             if(err){
  149.                                 console.log(err);
  150.                                 res.json(err);
  151.                             } else {
  152.                                 Aposta.count({ user: numeros.user }, function(err, result){
  153.                                     if(err){
  154.                                         console.log(err);
  155.                                         res.json(err);
  156.                                     } else {
  157.                                         totalApostasUser = result;
  158.  
  159.                                         Aposta.count({ loteria: req.body.loteria }, function(err, result){
  160.                                             if(err){
  161.                                                 console.log(err);
  162.                                                 res.json(err);
  163.                                             } else {
  164.                                                 totalApostas = result;
  165.  
  166.                                                 if(loteria){
  167.                                                     loteria.status = false;
  168.                                                     loteria.save();
  169.  
  170.                                                     var numero = Math.floor(Math.random() * respostas.acertos.length);
  171.                                                    
  172.                                                     var result = {
  173.                                                         acerto: true,
  174.                                                         totalApostas: totalApostas,
  175.                                                         totalApostasUser: totalApostasUser,
  176.                                                         msg: respostas.acertos[numero]
  177.                                                     }
  178.                                                     res.json(result);
  179.  
  180.                                                 } else {
  181.                                                     var numero = Math.floor(Math.random() * respostas.erros.length);
  182.                        
  183.                                                     var result = {
  184.                                                         acerto: false,
  185.                                                         totalApostas: totalApostas,
  186.                                                         totalApostasUser: totalApostasUser,
  187.                                                         msg: respostas.erros[numero]
  188.                                                     }
  189.                                                     res.json(result);
  190.                                                 }
  191.                                             }
  192.                                         });
  193.                                     }
  194.                                 });
  195.                             }
  196.                         });
  197.                     }
  198.             });
  199.         },
  200.         apostas: function(req,res){
  201.             Aposta
  202.                 .find()
  203.                 .populate({
  204.                     path: 'user',
  205.                     select: 'email admin'
  206.                 })
  207.                 .populate({
  208.                     path: 'loteria',
  209.                     select: 'titulo status validade created_at'
  210.                 })
  211.                 .exec(function (err, apostas) {
  212.                   if (err) {
  213.                     console.log(err);
  214.                     res.json(err);
  215.                   } else {
  216.                     res.json(apostas);
  217.                   }
  218.                 });
  219.         },
  220.         apostasUser: function(req,res){
  221.             Aposta
  222.                 .find({user: req.params.id})
  223.                 .populate({
  224.                     path: 'user',
  225.                     match: { _id: req.params.id},
  226.                      select: 'email'
  227.                 })
  228.                 .populate({
  229.                     path: 'loteria',
  230.                     select: 'titulo status validade created_at'
  231.                 })
  232.                 .exec(function (err, apostas) {
  233.                   if (err) {
  234.                     console.log(err);
  235.                     res.json(err);
  236.                   } else {
  237.                     res.json(apostas);
  238.                   }
  239.                 });
  240.         },
  241.         apostasVitoria: function(req,res){
  242.             Betplay
  243.                 .find({user: req.params.id})
  244.                 .exec(function (err, loteria) {
  245.                   if (err) {
  246.                     console.log(err);
  247.                     res.json(err);
  248.                   } else {
  249.                     res.json(loteria);
  250.                   }
  251.                 });
  252.         }
  253.  
  254.     };
  255.     return BetplayController;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement