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.

84 lines
2.6 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. 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. // Tweet out when missions are completed
  31. $newMissionStatus = $tweet->mission->complete($tweet->team);
  32. if($event->previousMissionStatus == false && $newMissionStatus == true) {
  33. $text = 'Team '.$tweet->team->name.' completed mission '.$tweet->mission_id.'! '.$tweet->mission->hashtag.' #spy30';
  34. Log::info("Tweeting: $text");
  35. $params = [
  36. 'attachment_url' => 'https://twitter.com/'.$tweet->player->twitter.'/status/'.$tweet->tweet_id,
  37. 'status' => $text
  38. ];
  39. try {
  40. Twitter::postTweet($params);
  41. } catch(\Exception $e) {
  42. Log::error($e->getMessage());
  43. }
  44. }
  45. // Tweet about bonus points when a team visits a TC nobody else has been to
  46. if($tweet->mission_id == 2 && $tweet->transit_center) {
  47. $this_tc = $tweet->m2_transit_center_id;
  48. $other_teams = Tweet::where('team_id', '!=', $tweet->team_id)
  49. ->where('m2_complete', 1)
  50. ->where('m2_transit_center_id', $this_tc)
  51. ->count();
  52. if($other_teams == 0) {
  53. $text = 'Team '.$tweet->team->name.' is currently the only team to have visited '.$tweet->transit_center->name.'! #spy30';
  54. Log::info("Tweeting: $text");
  55. $params = [
  56. 'attachment_url' => 'https://twitter.com/'.$tweet->player->twitter.'/status/'.$tweet->tweet_id,
  57. 'status' => $text
  58. ];
  59. try {
  60. Twitter::postTweet($params);
  61. } catch(\Exception $e) {
  62. Log::error($e->getMessage());
  63. }
  64. }
  65. }
  66. // Broadcast this team's current score
  67. $score = $tweet->mission->score($tweet->team);
  68. $teamScore = $tweet->team->total_score();
  69. event(new ScoreUpdatedEvent($tweet->team, $tweet->mission, $score, $newMissionStatus, $teamScore));
  70. }
  71. }