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.7 KiB

<?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.' '.env('GAME_HASHTAG');
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.'! '.env('GAME_HASHTAG');
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));
}
}