You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.0 KiB

  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Mission extends Model
  5. {
  6. protected $fillable = [
  7. 'hashtag',
  8. ];
  9. public function tweets() {
  10. return $this->hasMany('App\Tweet');
  11. }
  12. private function unique_trimet_transit_lines(Team $team) {
  13. static $cache = [];
  14. if(array_key_exists($team->id, $cache))
  15. return $cache[$team->id];
  16. $tweets = Tweet::where('team_id', $team->id)
  17. ->where('m1_complete', 1)->whereNotNull('m1_transit_line_id')->get();
  18. $lines = [];
  19. foreach($tweets as $tweet) {
  20. if($tweet->m1_transit_line_id && !in_array($tweet->m1_transit_line_id, $lines)) {
  21. $lines[] = $tweet->m1_transit_line_id;
  22. }
  23. }
  24. $cache[$team->id] = count($lines);
  25. return count($lines);
  26. }
  27. private function unique_transit_centers(Team $team) {
  28. static $cache = [];
  29. if(array_key_exists($team->id, $cache))
  30. return $cache[$team->id];
  31. $tweets = Tweet::where('team_id', $team->id)->where('m2_complete', 1)->get();
  32. $centers = [];
  33. foreach($tweets as $tweet) {
  34. if($tweet->m2_transit_center_id && !in_array($tweet->m2_transit_center_id, $centers)) {
  35. $centers[] = $tweet->m2_transit_center_id;
  36. }
  37. }
  38. $cache[$team->id] = count($centers);
  39. return count($centers);
  40. }
  41. private function unique_documents(Team $team) {
  42. static $cache = [];
  43. if(array_key_exists($team->id, $cache))
  44. return $cache[$team->id];
  45. $tweets = Tweet::where('team_id', $team->id)->whereNotNull('m7_document_id')->get();
  46. $documents = [];
  47. foreach($tweets as $tweet) {
  48. if($tweet->m7_document_id && !in_array($tweet->m7_document_id, $documents)) {
  49. $documents[] = $tweet->m7_document_id;
  50. }
  51. }
  52. $cache[$team->id] = count($documents);
  53. return count($documents);
  54. }
  55. private function num_tweets_for_mission(Team $team, $m) {
  56. static $cache = [3=>[], 4=>[], 5=>[], 6=>[]];
  57. if(array_key_exists($team->id, $cache[$m]))
  58. return $cache[$m][$team->id];
  59. $num = Tweet::where('team_id', $team->id)->where('m'.$m.'_complete', 1)->count();
  60. $cache[$m][$team->id] = $num;
  61. return $num;
  62. }
  63. public function complete(Team $team) {
  64. switch($this->id) {
  65. case 1:
  66. // 5 unique trimet transit lines
  67. return $this->unique_trimet_transit_lines($team) >= 5;
  68. case 2:
  69. // 3 unique transit centers
  70. return $this->unique_transit_centers($team) >= 3;
  71. case 3:
  72. case 4:
  73. case 5:
  74. case 6:
  75. return $this->num_tweets_for_mission($team, $this->id) >= 1;
  76. case 7:
  77. return $this->unique_documents($team) >= 3;
  78. }
  79. }
  80. public function progress(Team $team) {
  81. switch($this->id) {
  82. case 1:
  83. return [$this->unique_trimet_transit_lines($team), 5];
  84. case 2:
  85. return [$this->unique_transit_centers($team), 3];
  86. case 3:
  87. case 4:
  88. case 5:
  89. case 6:
  90. return [$this->num_tweets_for_mission($team, $this->id), 1];
  91. case 7:
  92. return [$this->unique_documents($team), 3];
  93. }
  94. return [false,false];
  95. }
  96. public function score(Team $team) {
  97. switch($this->id) {
  98. case 1:
  99. // No points unless they hit 5 trimet transit lines
  100. if($this->complete($team)) {
  101. $score = 50;
  102. // 20 bonus for each line that is not trimet
  103. $tweet = Tweet::where('team_id', $team->id)->where('m1_complete', 1)->whereNotNull('m1_non_trimet')->get();
  104. $lines = [];
  105. foreach($tweet as $tweet) {
  106. if(!in_array($tweet->m1_non_trimet, $lines)) {
  107. $lines[] = $tweet->m1_non_trimet;
  108. }
  109. }
  110. $score += count($lines) * 20;
  111. return $score;
  112. } else {
  113. return 0;
  114. }
  115. break;
  116. case 2:
  117. // No points unless they hit 3 transit centers
  118. if($this->complete($team)) {
  119. $score = 50;
  120. // 20 bonus points for any TC with another team
  121. // ?? Increasing bonus points for additional transit centers
  122. // Triple points for each? transit center that no other team goes to
  123. } else {
  124. return 0;
  125. }
  126. break;
  127. case 3:
  128. case 4:
  129. case 6:
  130. if($this->complete($team)) {
  131. return 50;
  132. } else {
  133. return 0;
  134. }
  135. case 5:
  136. if($this->complete($team)) {
  137. $score = 50;
  138. return $score;
  139. } else {
  140. return 0;
  141. }
  142. case 7:
  143. if($this->complete($team)) {
  144. $score = 50;
  145. // +5 for each additional photo
  146. $extra_docs = $this->unique_documents($team) - 3;
  147. if($extra_docs > 0) {
  148. $score += $extra_docs * 5;
  149. }
  150. return $score;
  151. } else {
  152. return 0;
  153. }
  154. }
  155. }
  156. }