|
|
- <?php
-
- namespace App\Listeners;
-
- use App\Events\TweetAcceptedEvent;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Log;
- use Twitter;
- use App\Tweet;
- use App\Events\ScoreUpdatedEvent;
-
- class TweetAcceptedListener implements ShouldQueue
- {
- /**
- * Create the event listener.
- *
- * @return void
- */
- public function __construct()
- {
- //
- }
-
- /**
- * Handle the event.
- *
- * @param Event $event
- * @return void
- */
- public function handle(TweetAcceptedEvent $event)
- {
- $tweet = $event->tweet;
-
- if(!$tweet->team || !$tweet->mission)
- return;
-
- // Tweet out when missions are completed
- $newMissionStatus = $tweet->mission->complete($tweet->team);
- if($event->previousMissionStatus == false && $newMissionStatus == true) {
-
- $text = 'Team '.$tweet->team->name.' completed mission '.$tweet->mission_id.'! '.$tweet->mission->hashtag.' #spy30';
- Log::info("Tweeting: $text");
-
- $params = [
- 'attachment_url' => 'https://twitter.com/'.$tweet->player->twitter.'/status/'.$tweet->tweet_id,
- 'status' => $text
- ];
- try {
- Twitter::postTweet($params);
- } catch(\Exception $e) {
- Log::error($e->getMessage());
- }
- }
-
- // Tweet about bonus points when a team visits a TC nobody else has been to
- if($tweet->mission_id == 2 && $tweet->transit_center) {
-
- $this_tc = $tweet->m2_transit_center_id;
-
- $other_teams = Tweet::where('team_id', '!=', $tweet->team_id)
- ->where('m2_complete', 1)
- ->where('m2_transit_center_id', $this_tc)
- ->count();
- if($other_teams == 0) {
- $text = 'Team '.$tweet->team->name.' is currently the only team to have visited '.$tweet->transit_center->name.'! #spy30';
- Log::info("Tweeting: $text");
- $params = [
- 'attachment_url' => 'https://twitter.com/'.$tweet->player->twitter.'/status/'.$tweet->tweet_id,
- 'status' => $text
- ];
- try {
- Twitter::postTweet($params);
- } catch(\Exception $e) {
- Log::error($e->getMessage());
- }
- }
-
- }
-
- // Broadcast this team's current score
- $score = $tweet->mission->score($tweet->team);
- $teamScore = $tweet->team->total_score();
- event(new ScoreUpdatedEvent($tweet->team, $tweet->mission, $score, $newMissionStatus, $teamScore));
-
- }
- }
|