Browse Source

quote tweets when missions are complete

master
Aaron Parecki 6 years ago
parent
commit
433e9e9edc
No known key found for this signature in database GPG Key ID: 276C2817346D6056
6 changed files with 118 additions and 9 deletions
  1. +40
    -0
      app/Events/TweetAcceptedEvent.php
  2. +5
    -1
      app/Http/Controllers/DashboardController.php
  3. +70
    -0
      app/Listeners/TweetAcceptedListener.php
  4. +1
    -2
      app/Mission.php
  5. +2
    -2
      app/Providers/EventServiceProvider.php
  6. +0
    -4
      routes/channels.php

+ 40
- 0
app/Events/TweetAcceptedEvent.php View File

@ -0,0 +1,40 @@
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Tweet;
class TweetAcceptedEvent
{
use Dispatchable, SerializesModels;
public $tweet;
public $previousMissionStatus;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Tweet $tweet, $previousMissionStatus)
{
$this->tweet = $tweet;
$this->previousMissionStatus = $previousMissionStatus;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return [];
}
}

+ 5
- 1
app/Http/Controllers/DashboardController.php View File

@ -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']);
}

+ 70
- 0
app/Listeners/TweetAcceptedListener.php View File

@ -0,0 +1,70 @@
<?php
namespace App\Listeners;
use App\Events\TweetAcceptedEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Log;
use Twitter;
use App\Tweet;
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;
// 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);
}
}
}
}

+ 1
- 2
app/Mission.php View File

@ -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;
}

+ 2
- 2
app/Providers/EventServiceProvider.php View File

@ -13,8 +13,8 @@ class EventServiceProvider extends ServiceProvider
* @var array
*/
protected $listen = [
'App\Events\Event' => [
'App\Listeners\EventListener',
'App\Events\TweetAcceptedEvent' => [
'App\Listeners\TweetAcceptedListener'
],
];

+ 0
- 4
routes/channels.php View File

@ -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;
// })

Loading…
Cancel
Save