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.

70 lines
2.0 KiB

  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. class TweetAcceptedListener implements ShouldQueue
  10. {
  11. /**
  12. * Create the event listener.
  13. *
  14. * @return void
  15. */
  16. public function __construct()
  17. {
  18. //
  19. }
  20. /**
  21. * Handle the event.
  22. *
  23. * @param Event $event
  24. * @return void
  25. */
  26. public function handle(TweetAcceptedEvent $event)
  27. {
  28. $tweet = $event->tweet;
  29. // Tweet out when missions are completed
  30. $newMissionStatus = $tweet->mission->complete($tweet->team);
  31. if($event->previousMissionStatus == false && $newMissionStatus == true) {
  32. $text = 'Team '.$tweet->team->name.' completed mission '.$tweet->mission_id.'! '.$tweet->mission->hashtag;
  33. Log::info("Tweeting: $text");
  34. $params = [
  35. 'attachment_url' => 'https://twitter.com/'.$tweet->player->twitter.'/status/'.$tweet->tweet_id,
  36. 'status' => $text
  37. ];
  38. Twitter::postTweet($params);
  39. }
  40. // Tweet about bonus points when a team visits a TC nobody else has been to
  41. if($tweet->mission_id == 2 && $tweet->transit_center) {
  42. $this_tc = $tweet->m2_transit_center_id;
  43. $other_teams = Tweet::where('team_id', '!=', $tweet->team_id)
  44. ->where('m2_complete', 1)
  45. ->where('m2_transit_center_id', $this_tc)
  46. ->count();
  47. if($other_teams == 0) {
  48. $text = 'Team '.$tweet->team->name.' is currently the only team to have visited '.$tweet->transit_center->name.'!';
  49. Log::info("Tweeting: $text");
  50. $params = [
  51. 'attachment_url' => 'https://twitter.com/'.$tweet->player->twitter.'/status/'.$tweet->tweet_id,
  52. 'status' => $text
  53. ];
  54. Twitter::postTweet($params);
  55. }
  56. }
  57. }
  58. }