Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- exports.checkSchedine = functions.pubsub
- .schedule('45 * * * *')
- .onRun(async (context) => {
- var currentTime = Math.floor(Date.now() / 1000);
- const risultati = await firestore.collection("Risultati").doc("risultati").get();
- const ref1 = await firestore.collection("Schedine").get();
- await ref1.forEach(async doc => {
- const notification1 = {
- headings: {
- 'en' : 'Schedina vinta!'
- },
- contents: {
- 'en': 'Hai vinto una schedina. Controlla il tuo nuovo saldo.',
- },
- include_external_user_ids: [doc.id],
- android_accent_color:"F44336",
- };
- const notification2 = {
- headings: {
- 'en' : 'Schedina persa!'
- },
- contents: {
- 'en': 'Hai perso una schedina.',
- },
- include_external_user_ids: [doc.id],
- android_accent_color:"F44336",
- };
- const ref2 = await firestore.collection("Schedine").doc(doc.id).collection("in corso").get();
- await ref2.forEach(async doc2 => {
- var i =0;
- var j = 0;
- for(matchId in doc2.data()["Match"]){
- j++;
- for(id in risultati.data()){
- if(matchId == id && risultati.data()[id]["status"]!=0 && doc2.data()["Match"][matchId]["status"]==0){
- 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){
- await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).update({
- ['Match.'+matchId+'.status'] : 1,
- });
- } else if(doc2.data()["Match"][matchId]["Bet"]==4 && risultati.data()[id].goal ==1){
- await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).update({
- ['Match.'+matchId+'.status'] : 1,
- });
- } else if(doc2.data()["Match"][matchId]["Bet"]==5 && risultati.data()[id].goal ==0){
- await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).update({
- ['Match.'+matchId+'.status'] : 1,
- });
- } else if(doc2.data()["Match"][matchId]["Bet"]==6 && risultati.data()[id].over ==1){
- await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).update({
- ['Match.'+matchId+'.status'] : 1,
- });
- } else if(doc2.data()["Match"][matchId]["Bet"]==7 && risultati.data()[id].over ==0){
- await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).update({
- ['Match.'+matchId+'.status'] : 1,
- });
- } else {
- await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).update({
- ['Match.'+matchId+'.status'] : 2,
- });
- await firestore.collection('Schedine').doc(doc.id).collection("finite").doc(doc2.id).set(doc2.data());
- await firestore.collection('Schedine').doc(doc.id).collection("finite").doc(doc2.id).update({
- "esito" : 2, "timestamp": currentTime
- });
- await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).delete();
- try {
- const response = await client.createNotification(notification2);
- } catch (e) {}
- }
- }
- }
- if(doc2.data()["Match"][matchId]["status"]==1 ){
- i++;
- }
- }
- if(i==j && i!=0){
- await firestore.collection("Users").doc(doc.id).update({ "vinte" : fieldValue.increment(1)});
- await firestore.collection('Schedine').doc(doc.id).collection("finite").doc(doc2.id).set(doc2.data());
- await firestore.collection('Schedine').doc(doc.id).collection("finite").doc(doc2.id).update({
- "esito" : 1, "timestamp": currentTime,
- });
- try {
- const response = await client.createNotification(notification1);
- } catch (e) {}
- const refuser = await firestore.collection("Users").doc(doc.id).get();
- var newbalance = refuser.data()["balance"] + doc2.data()["vincita"];
- await firestore.collection("Users").doc(doc.id).update({
- "balance" : newbalance
- });
- await firestore.collection('Schedine').doc(doc.id).collection("in corso").doc(doc2.id).delete();
- }
- });
- });
- return null;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement