Guest User

Untitled

a guest
Jun 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. exports.calculateRanking = functions.firestore.document('players/{playerId}').onUpdate((change, context) => {
  2.  
  3. if (typeof change.before.data().score === 'undefined' && change.after.data().score > 0) {
  4.  
  5. var db = admin.firestore();
  6. var batch = db.batch();
  7. return db.collection("players").orderBy("score", "desc").get().then(snapshot => {
  8. var docs = snapshot.docs;
  9. var map = {};
  10. for (var i = 0; i < docs.length; i++){
  11. var player = docs[i];
  12.  
  13. var position = i + 1;
  14. var industry = player.industry;
  15. var age = player.age;
  16.  
  17. if (map[industry] !== null){
  18. map[industry] += 1;
  19. }
  20. else{
  21. map[industry] = 1;
  22. }
  23.  
  24. if (map[age] !== null){
  25. map[age] += 1;
  26. }
  27. else{
  28. map[age] = 1;
  29. }
  30.  
  31. if (map[industry+age] !== null){
  32. map[industry+age] += 1;
  33. }
  34. else{
  35. map[industry+age] = 1;
  36. }
  37.  
  38. console.log(player.name);
  39.  
  40. return player.ref.collection("rankings").get().then(rankings => {
  41. return rankings.docs.forEach(ranking => {
  42.  
  43. console.log("Inside ranking loop");
  44.  
  45. var currGeneralRanking = ranking.data().generalRanking.curr;
  46. var currIndustryRanking = ranking.data().industryRanking.curr;
  47. var currAgeRanking = ranking.data().ageRanking.curr;
  48. var currIndustryAgeRanking = ranking.data().industryAgeRanking.curr;
  49.  
  50. var newGeneralRanking = position;
  51. var newIndustryRanking = map[industry];
  52. var newAgeRanking = map[age];
  53. var newIndustryAgeRanking = map[industry+age];
  54.  
  55. var newCurrGeneral;
  56. var newCurrIndustry;
  57. var newCurrAge;
  58. var newCurrIndustryAge;
  59.  
  60. var newPrevGeneral;
  61. var newPrevIndustry;
  62. var newPrevAge;
  63. var newPrevIndustryAge;
  64.  
  65. if (currGeneralRanking !== 0){
  66. if (newGeneralRanking !== currGeneralRanking){
  67. newPrevGeneral = currGeneralRanking;
  68. }
  69. else{
  70. newPrevGeneral = ranking.data().ranking.prev;
  71. }
  72. }
  73. else {
  74. newPrevGeneral = newGeneralRanking;
  75. }
  76.  
  77. newCurrGeneral = newGeneralRanking;
  78.  
  79. if (currIndustryRanking !== 0){
  80. if (newIndustryRanking !== currIndustryRanking){
  81. newPrevIndustry = currIndustryRanking;
  82. }
  83. else{
  84. newPrevIndustry = ranking.data().industryRanking.prev;
  85. }
  86. }
  87. else{
  88. newPrevIndustry = newIndustryRanking;
  89. }
  90.  
  91. newCurrIndustry = newIndustryRanking;
  92.  
  93. if (currAgeRanking !== 0) {
  94. if (newAgeRanking !== currAgeRanking) {
  95. newPrevAge = currAgeRanking;
  96. }
  97. else{
  98. newPrevAge = ranking.data().ageRanking.prev;
  99. }
  100. }
  101. else{
  102. newPrevAge = newAgeRanking;
  103. }
  104.  
  105. newCurrAge = newAgeRanking;
  106.  
  107. if (currIndustryAgeRanking !== 0) {
  108. if (newIndustryAgeRanking === currAgeIndustryRanking){
  109. newPrevIndustryAge = currAgeIndustryRanking;
  110. }
  111. else{
  112. newPrevIndustryAge = ranking.data().industryAgeRanking.prev;
  113. }
  114. }
  115. else{
  116. newPrevIndustryAge = newIndustryAgeRanking;
  117. }
  118.  
  119. newCurrIndustryAge = newIndustryAgeRanking;
  120.  
  121. return batch.update(ranking.ref, {
  122. "generalRanking": {
  123. "curr": newCurrGeneral,
  124. "prev": newPrevGeneral
  125. },
  126. "industryRanking": {
  127. "curr": newCurrIndustry,
  128. "prev": newPrevIndustry
  129. },
  130. "ageRanking": {
  131. "curr": newCurrAge,
  132. "prev": newPrevAge
  133. },
  134. "industryAgeRanking": {
  135. "curr": newCurrIndustryAge,
  136. "prev": newPrevIndustryAge
  137. }
  138. });
  139. });
  140. }).catch(error => {
  141. return console.log(error);
  142. });
  143. }
  144. return batch.commit().then(result => {
  145. return console.log(result);
  146. }).catch(error => {
  147. return console.log(error);
  148. });
  149. }).then(result => {
  150. return console.log('Transaction success', result);
  151. }).catch(err => {
  152. return console.log('Transaction failure:', err);
  153. });
  154. }
  155.  
  156. return;
  157. });
Add Comment
Please, Sign In to add comment