Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- exports.calculateRanking = functions.firestore.document('players/{playerId}').onUpdate((change, context) => {
- if (typeof change.before.data().score === 'undefined' && change.after.data().score > 0) {
- var db = admin.firestore();
- var batch = db.batch();
- return db.collection("players").orderBy("score", "desc").get().then(snapshot => {
- var docs = snapshot.docs;
- var map = {};
- for (var i = 0; i < docs.length; i++){
- var player = docs[i];
- var position = i + 1;
- var industry = player.industry;
- var age = player.age;
- if (map[industry] !== null){
- map[industry] += 1;
- }
- else{
- map[industry] = 1;
- }
- if (map[age] !== null){
- map[age] += 1;
- }
- else{
- map[age] = 1;
- }
- if (map[industry+age] !== null){
- map[industry+age] += 1;
- }
- else{
- map[industry+age] = 1;
- }
- console.log(player.name);
- return player.ref.collection("rankings").get().then(rankings => {
- return rankings.docs.forEach(ranking => {
- console.log("Inside ranking loop");
- var currGeneralRanking = ranking.data().generalRanking.curr;
- var currIndustryRanking = ranking.data().industryRanking.curr;
- var currAgeRanking = ranking.data().ageRanking.curr;
- var currIndustryAgeRanking = ranking.data().industryAgeRanking.curr;
- var newGeneralRanking = position;
- var newIndustryRanking = map[industry];
- var newAgeRanking = map[age];
- var newIndustryAgeRanking = map[industry+age];
- var newCurrGeneral;
- var newCurrIndustry;
- var newCurrAge;
- var newCurrIndustryAge;
- var newPrevGeneral;
- var newPrevIndustry;
- var newPrevAge;
- var newPrevIndustryAge;
- if (currGeneralRanking !== 0){
- if (newGeneralRanking !== currGeneralRanking){
- newPrevGeneral = currGeneralRanking;
- }
- else{
- newPrevGeneral = ranking.data().ranking.prev;
- }
- }
- else {
- newPrevGeneral = newGeneralRanking;
- }
- newCurrGeneral = newGeneralRanking;
- if (currIndustryRanking !== 0){
- if (newIndustryRanking !== currIndustryRanking){
- newPrevIndustry = currIndustryRanking;
- }
- else{
- newPrevIndustry = ranking.data().industryRanking.prev;
- }
- }
- else{
- newPrevIndustry = newIndustryRanking;
- }
- newCurrIndustry = newIndustryRanking;
- if (currAgeRanking !== 0) {
- if (newAgeRanking !== currAgeRanking) {
- newPrevAge = currAgeRanking;
- }
- else{
- newPrevAge = ranking.data().ageRanking.prev;
- }
- }
- else{
- newPrevAge = newAgeRanking;
- }
- newCurrAge = newAgeRanking;
- if (currIndustryAgeRanking !== 0) {
- if (newIndustryAgeRanking === currAgeIndustryRanking){
- newPrevIndustryAge = currAgeIndustryRanking;
- }
- else{
- newPrevIndustryAge = ranking.data().industryAgeRanking.prev;
- }
- }
- else{
- newPrevIndustryAge = newIndustryAgeRanking;
- }
- newCurrIndustryAge = newIndustryAgeRanking;
- return batch.update(ranking.ref, {
- "generalRanking": {
- "curr": newCurrGeneral,
- "prev": newPrevGeneral
- },
- "industryRanking": {
- "curr": newCurrIndustry,
- "prev": newPrevIndustry
- },
- "ageRanking": {
- "curr": newCurrAge,
- "prev": newPrevAge
- },
- "industryAgeRanking": {
- "curr": newCurrIndustryAge,
- "prev": newPrevIndustryAge
- }
- });
- });
- }).catch(error => {
- return console.log(error);
- });
- }
- return batch.commit().then(result => {
- return console.log(result);
- }).catch(error => {
- return console.log(error);
- });
- }).then(result => {
- return console.log('Transaction success', result);
- }).catch(err => {
- return console.log('Transaction failure:', err);
- });
- }
- return;
- });
Add Comment
Please, Sign In to add comment