diff --git a/app/Events/TweetAcceptedEvent.php b/app/Events/TweetAcceptedEvent.php new file mode 100644 index 0000000..6a11ba7 --- /dev/null +++ b/app/Events/TweetAcceptedEvent.php @@ -0,0 +1,40 @@ +tweet = $tweet; + $this->previousMissionStatus = $previousMissionStatus; + } + + /** + * Get the channels the event should broadcast on. + * + * @return Channel|array + */ + public function broadcastOn() + { + return []; + } +} diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 1a9d859..4624323 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -5,7 +5,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use Twitter; use App\Tweet; -use App\Events\NewTweetEvent, App\Events\TweetClaimedEvent; +use App\Events\NewTweetEvent, App\Events\TweetClaimedEvent, App\Events\TweetAcceptedEvent; use DB; class DashboardController extends Controller @@ -87,6 +87,9 @@ class DashboardController extends Controller public function score_tweet(Request $request) { $tweet = Tweet::where('id', $request->input('tweet_id'))->first(); if($tweet) { + // Calculate the previous mission status before saving this tweet + $previousMissionStatus = $tweet->mission->complete($tweet->team); + $tweet->m1_transit_line_id = null; $tweet->m1_non_trimet = null; $tweet->m2_transit_center_id = null; @@ -102,6 +105,7 @@ class DashboardController extends Controller } $tweet->processed = 1; $tweet->save(); + event(new TweetAcceptedEvent($tweet, $previousMissionStatus)); } return response()->json(['result'=>'ok']); } diff --git a/app/Listeners/TweetAcceptedListener.php b/app/Listeners/TweetAcceptedListener.php new file mode 100644 index 0000000..a582718 --- /dev/null +++ b/app/Listeners/TweetAcceptedListener.php @@ -0,0 +1,70 @@ +tweet; + + // 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; + Log::info("Tweeting: $text"); + + $params = [ + 'attachment_url' => 'https://twitter.com/'.$tweet->player->twitter.'/status/'.$tweet->tweet_id, + 'status' => $text + ]; + Twitter::postTweet($params); + } + + // 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.'!'; + Log::info("Tweeting: $text"); + $params = [ + 'attachment_url' => 'https://twitter.com/'.$tweet->player->twitter.'/status/'.$tweet->tweet_id, + 'status' => $text + ]; + Twitter::postTweet($params); + } + + } + + } +} diff --git a/app/Mission.php b/app/Mission.php index 750a1ea..a1601cd 100644 --- a/app/Mission.php +++ b/app/Mission.php @@ -166,14 +166,13 @@ class Mission extends Model $score += (50 * ($unique_transit_centers - 6)); } - // Triple points for each transit center that no other team goes to + // Double points for each transit center that no other team goes to $this_team_tcs = Tweet::where('team_id', $team->id)->where('m2_complete', 1) ->distinct()->pluck('m2_transit_center_id')->toArray(); $other_visited_tcs = Tweet::where('team_id', '!=', $team->id)->where('m2_complete', 1) ->distinct()->pluck('m2_transit_center_id')->toArray(); $distinct_tcs = array_diff($this_team_tcs, $other_visited_tcs); - Log::info('Team '.$team->name.' visited '.count($distinct_tcs).' TCs that no other team visited, doubling their score'); if(count($distinct_tcs) > 0) { $score *= 2; } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index fca6152..01a8856 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -13,8 +13,8 @@ class EventServiceProvider extends ServiceProvider * @var array */ protected $listen = [ - 'App\Events\Event' => [ - 'App\Listeners\EventListener', + 'App\Events\TweetAcceptedEvent' => [ + 'App\Listeners\TweetAcceptedListener' ], ]; diff --git a/routes/channels.php b/routes/channels.php index a569806..f16a20b 100644 --- a/routes/channels.php +++ b/routes/channels.php @@ -14,7 +14,3 @@ Broadcast::channel('App.User.{id}', function ($user, $id) { return (int) $user->id === (int) $id; }); - -// Broadcast::channel('tweet-queue', function($user) { -// return true; -// })