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.

87 lines
2.6 KiB

7 years ago
  1. <?php
  2. namespace App\Listeners;
  3. use App\Events\TweetAcceptedEvent;
  4. use Illuminate\Queue\InteractsWithQueue;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Log;
  7. use Twitter;
  8. use App\Tweet;
  9. use App\Events\ScoreUpdatedEvent;
  10. class TweetAcceptedListener implements ShouldQueue
  11. {
  12. /**
  13. * Create the event listener.
  14. *
  15. * @return void
  16. */
  17. public function __construct()
  18. {
  19. //
  20. }
  21. /**
  22. * Handle the event.
  23. *
  24. * @param Event $event
  25. * @return void
  26. */
  27. public function handle(TweetAcceptedEvent $event)
  28. {
  29. $tweet = $event->tweet;
  30. if(!$tweet->team || !$tweet->mission)
  31. return;
  32. // Tweet out when missions are completed
  33. $newMissionStatus = $tweet->mission->complete($tweet->team);
  34. if($event->previousMissionStatus == false && $newMissionStatus == true) {
  35. $text = 'Team '.$tweet->team->name.' completed mission '.$tweet->mission_id.'! '.$tweet->mission->hashtag.' #spy30';
  36. Log::info("Tweeting: $text");
  37. $params = [
  38. 'attachment_url' => 'https://twitter.com/'.$tweet->player->twitter.'/status/'.$tweet->tweet_id,
  39. 'status' => $text
  40. ];
  41. try {
  42. Twitter::postTweet($params);
  43. } catch(\Exception $e) {
  44. Log::error($e->getMessage());
  45. }
  46. }
  47. // Tweet about bonus points when a team visits a TC nobody else has been to
  48. if($tweet->mission_id == 2 && $tweet->transit_center) {
  49. $this_tc = $tweet->m2_transit_center_id;
  50. $other_teams = Tweet::where('team_id', '!=', $tweet->team_id)
  51. ->where('m2_complete', 1)
  52. ->where('m2_transit_center_id', $this_tc)
  53. ->count();
  54. if($other_teams == 0) {
  55. $text = 'Team '.$tweet->team->name.' is currently the only team to have visited '.$tweet->transit_center->name.'! #spy30';
  56. Log::info("Tweeting: $text");
  57. $params = [
  58. 'attachment_url' => 'https://twitter.com/'.$tweet->player->twitter.'/status/'.$tweet->tweet_id,
  59. 'status' => $text
  60. ];
  61. try {
  62. Twitter::postTweet($params);
  63. } catch(\Exception $e) {
  64. Log::error($e->getMessage());
  65. }
  66. }
  67. }
  68. // Broadcast this team's current score
  69. $score = $tweet->mission->score($tweet->team);
  70. $teamScore = $tweet->team->total_score();
  71. event(new ScoreUpdatedEvent($tweet->team, $tweet->mission, $score, $newMissionStatus, $teamScore));
  72. }
  73. }