Advertisement
stackoversnow

Untitled

Oct 27th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. exports.check = functions.https.onRequest(async (req, res) => {
  2.   name = req.form.get('name');
  3.  
  4.   var currentTime = Math.floor(Date.now() / 1000);
  5.   const client = new OneSignal.Client('b76a6035-dc17-4e9b-810f-8b0ef2d50774', 'ZDY1YmQyZGQtMGNhYS00ZTMwLTgxNWItM2M0ZDU4YjYxMjJi');
  6.   const risultati = await firestore.collection("Risultati").doc("risultati").get();
  7.   const ref1 = await firestore.collection("Schedine").get();
  8.  
  9.     const notification1 = {
  10.       headings: {
  11.         'en' : 'Schedina vinta!'
  12.       },
  13.       contents: {
  14.         'en': 'Hai vinto una schedina. Controlla il tuo nuovo saldo.',
  15.       },
  16.       include_external_user_ids: [name],
  17.       android_accent_color:"F44336",
  18.  
  19.     };
  20.  
  21.     const notification2 = {
  22.       headings: {
  23.         'en' : 'Schedina persa!'
  24.       },
  25.       contents: {
  26.         'en': 'Hai perso una schedina.',
  27.       },
  28.       include_external_user_ids: [name],
  29.       android_accent_color:"F44336",
  30.  
  31.     };
  32.  
  33.     const ref2 = await firestore.collection("Schedine").doc(name).collection("in corso").get();
  34.     await ref2.forEach(async doc2 => {
  35.       var i =0;
  36.       var j = 0;
  37.       for(matchId in doc2.data()["Match"]){
  38.         j++;
  39.  
  40.  
  41.         try {
  42.  
  43.           for(id in risultati.data()){
  44.             if(matchId == id && risultati.data()[id]["status"]!=0 && doc2.data()["Match"][matchId]["status"]==0){
  45.               if((doc2.data()["Match"][matchId]["Bet"]==1 || doc2.data()["Match"][matchId]["Bet"]==2 || doc2.data()["Match"][matchId]["Bet"]==3) && doc2.data()["Match"][matchId]["Bet"]==risultati.data()[id].ris){
  46.                 await firestore.collection('Schedine').doc(name).collection("in corso").doc(doc2.id).update({
  47.                     ['Match.'+matchId+'.status']  : 1,
  48.  
  49.               });
  50.  
  51.  
  52.  
  53.  
  54.             } else if(doc2.data()["Match"][matchId]["Bet"]==4 && risultati.data()[id].goal ==1){
  55.               await firestore.collection('Schedine').doc(name).collection("in corso").doc(doc2.id).update({
  56.                   ['Match.'+matchId+'.status']  : 1,
  57.  
  58.             });
  59.  
  60.           } else if(doc2.data()["Match"][matchId]["Bet"]==5 && risultati.data()[id].goal ==0){
  61.               await firestore.collection('Schedine').doc(name).collection("in corso").doc(doc2.id).update({
  62.                   ['Match.'+matchId+'.status']  : 1,
  63.  
  64.             });
  65.  
  66.           } else if(doc2.data()["Match"][matchId]["Bet"]==6 && risultati.data()[id].over ==1){
  67.                 await firestore.collection('Schedine').doc(name).collection("in corso").doc(doc2.id).update({
  68.                     ['Match.'+matchId+'.status']  : 1,
  69.  
  70.               });
  71.  
  72.             } else if(doc2.data()["Match"][matchId]["Bet"]==7 && risultati.data()[id].over ==0){
  73.                   await firestore.collection('Schedine').doc(name).collection("in corso").doc(doc2.id).update({
  74.                       ['Match.'+matchId+'.status']  : 1,
  75.  
  76.                 });
  77.  
  78.                 }  else {
  79.               await firestore.collection('Schedine').doc(name).collection("in corso").doc(doc2.id).update({
  80.                 ['Match.'+matchId+'.status']  : 2,
  81.  
  82.               });
  83.  
  84.               await firestore.collection('Schedine').doc(name).collection("finite").doc(doc2.id).set(doc2.data());
  85.               await firestore.collection('Schedine').doc(name).collection("finite").doc(doc2.id).update({
  86.                 "esito" : 2, "timestamp": currentTime
  87.               });
  88.               await firestore.collection('Schedine').doc(name).collection("in corso").doc(doc2.id).delete();
  89.               try {
  90.               const response = await client.createNotification(notification2);
  91.             } catch (e) {}
  92.  
  93.             }
  94.  
  95.           }
  96.           }
  97.  
  98.       } catch (e) {}
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.         try {
  106.           if(doc2.data()["Match"][matchId]["status"]==1 ){
  107.             i++;
  108.           }
  109.       } catch (e) {}
  110.  
  111.  
  112.  
  113.  
  114.  
  115.       }
  116.  
  117.  
  118.  
  119.       if(i==j && i!=0){
  120.  
  121.           await firestore.collection("Users").doc(name).update({ "vinte" : fieldValue.increment(1)});
  122.         await firestore.collection('Schedine').doc(name).collection("finite").doc(doc2.id).set(doc2.data());
  123.         await firestore.collection('Schedine').doc(name).collection("finite").doc(doc2.id).update({
  124.           "esito" : 1,  "timestamp": currentTime,
  125.         });
  126.         try {
  127.         const response = await client.createNotification(notification1);
  128.       } catch (e) {}
  129.       const refuser = await firestore.collection("Users").doc(name).get();
  130.       var newbalance = refuser.data()["balance"] + doc2.data()["vincita"];
  131.       await firestore.collection("Users").doc(name).update({
  132.         "balance" : newbalance
  133.       });
  134.       await firestore.collection('Schedine').doc(name).collection("in corso").doc(doc2.id).delete();
  135.  
  136.  
  137.  
  138.  
  139.       }
  140.     });
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.   res.end();
  153. });
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement