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.

124 lines
3.3 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_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)->where('m1_complete', 1)->get();
  17. $lines = [];
  18. foreach($tweets as $tweet) {
  19. if($tweet->m1_transit_line_id && !in_array($tweet->m1_transit_line_id, $lines)) {
  20. $lines[] = $tweet->m1_transit_line_id;
  21. }
  22. }
  23. $cache[$team->id] = count($lines);
  24. return count($lines);
  25. }
  26. private function unique_transit_centers(Team $team) {
  27. static $cache = [];
  28. if(array_key_exists($team->id, $cache))
  29. return $cache[$team->id];
  30. $tweets = Tweet::where('team_id', $team->id)->where('m2_complete', 1)->get();
  31. $centers = [];
  32. foreach($tweets as $tweet) {
  33. if($tweet->m2_transit_center_id && !in_array($tweet->m2_transit_center_id, $centers)) {
  34. $centers[] = $tweet->m2_transit_center_id;
  35. }
  36. }
  37. $cache[$team->id] = count($centers);
  38. return count($centers);
  39. }
  40. private function unique_documents(Team $team) {
  41. static $cache = [];
  42. if(array_key_exists($team->id, $cache))
  43. return $cache[$team->id];
  44. $tweets = Tweet::where('team_id', $team->id)->whereNotNull('m7_document_id')->get();
  45. $documents = [];
  46. foreach($tweets as $tweet) {
  47. if($tweet->m7_document_id && !in_array($tweet->m7_document_id, $documents)) {
  48. $documents[] = $tweet->m7_document_id;
  49. }
  50. }
  51. $cache[$team->id] = count($documents);
  52. return count($documents);
  53. }
  54. private function num_tweets_for_mission(Team $team, $m) {
  55. static $cache = [3=>[], 4=>[], 5=>[], 6=>[]];
  56. if(array_key_exists($team->id, $cache[$m]))
  57. return $cache[$m][$team->id];
  58. $num = Tweet::where('team_id', $team->id)->where('m'.$m.'_complete', 1)->count();
  59. $cache[$m][$team->id] = $num;
  60. return $num;
  61. }
  62. public function complete(Team $team) {
  63. switch($this->id) {
  64. case 1:
  65. // 5 unique transit lines
  66. return $this->unique_transit_lines($team) >= 5;
  67. case 2:
  68. // 3 unique transit centers
  69. return $this->unique_transit_centers($team) >= 3;
  70. case 3:
  71. case 4:
  72. case 5:
  73. case 6:
  74. return $this->num_tweets_for_mission($team, $this->id) >= 1;
  75. case 7:
  76. return $this->unique_documents($team) >= 3;
  77. }
  78. }
  79. public function progress(Team $team) {
  80. switch($this->id) {
  81. case 1:
  82. return [$this->unique_transit_lines($team), 5];
  83. case 2:
  84. return [$this->unique_transit_lines($team), 3];
  85. case 3:
  86. case 4:
  87. case 5:
  88. case 6:
  89. return [$this->num_tweets_for_mission($team, $this->id), 1];
  90. case 7:
  91. return [$this->unique_documents($team), 3];
  92. }
  93. return [false,false];
  94. }
  95. public function score(Team $team) {
  96. switch($this->id) {
  97. case 1:
  98. break;
  99. case 2:
  100. break;
  101. }
  102. }
  103. }