From bb8c3fb75ecdff537141cc572b1591d2b74cedac Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Fri, 7 Jul 2017 16:25:41 -0700 Subject: [PATCH] push scores to scoreboard in realtime --- app/Events/ScoreUpdatedEvent.php | 48 +++++++++++++++++++++++++ app/Listeners/TweetAcceptedListener.php | 18 ++++++++-- public/js/scoreboard.js | 21 +++++++++++ resources/views/scoreboard.blade.php | 10 ++++-- 4 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 app/Events/ScoreUpdatedEvent.php create mode 100644 public/js/scoreboard.js diff --git a/app/Events/ScoreUpdatedEvent.php b/app/Events/ScoreUpdatedEvent.php new file mode 100644 index 0000000..8c60e2c --- /dev/null +++ b/app/Events/ScoreUpdatedEvent.php @@ -0,0 +1,48 @@ +team_slug = $team->slug; + $this->mission_slug = $mission->slug; + $this->score = $score; + $this->complete = $complete; + $this->team_score = $team_score; + } + + /** + * Get the channels the event should broadcast on. + * + * @return Channel|array + */ + public function broadcastOn() + { + return ['score']; + } +} diff --git a/app/Listeners/TweetAcceptedListener.php b/app/Listeners/TweetAcceptedListener.php index a582718..08a1d90 100644 --- a/app/Listeners/TweetAcceptedListener.php +++ b/app/Listeners/TweetAcceptedListener.php @@ -8,6 +8,7 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Log; use Twitter; use App\Tweet; +use App\Events\ScoreUpdatedEvent; class TweetAcceptedListener implements ShouldQueue { @@ -42,7 +43,11 @@ class TweetAcceptedListener implements ShouldQueue 'attachment_url' => 'https://twitter.com/'.$tweet->player->twitter.'/status/'.$tweet->tweet_id, 'status' => $text ]; - Twitter::postTweet($params); + 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 @@ -61,10 +66,19 @@ class TweetAcceptedListener implements ShouldQueue 'attachment_url' => 'https://twitter.com/'.$tweet->player->twitter.'/status/'.$tweet->tweet_id, 'status' => $text ]; - Twitter::postTweet($params); + 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)); + } } diff --git a/public/js/scoreboard.js b/public/js/scoreboard.js new file mode 100644 index 0000000..bc212ab --- /dev/null +++ b/public/js/scoreboard.js @@ -0,0 +1,21 @@ +Echo.channel('score') + .listen('ScoreUpdatedEvent', (e) => { + + var sel = "[data-team="+e.team_slug+"] [data-mission="+e.mission_slug+"]"; + + $(sel+" .team-icon span").text(e.score); + + if(e.complete) { + $(sel+".team-icon").removeClass("empty"); + } else { + $(sel+".team-icon").addClass("empty"); + } + + $("[data-team="+e.team_slug+"] .total.score b").text(e.team_score); + console.log(e); + + }); + +setTimeout(function(){ + window.location.reload(); +}, 300*1000); diff --git a/resources/views/scoreboard.blade.php b/resources/views/scoreboard.blade.php index ebc1c3e..3fbc691 100644 --- a/resources/views/scoreboard.blade.php +++ b/resources/views/scoreboard.blade.php @@ -1,5 +1,9 @@ @extends('layouts.app') +@push('scripts') + +@endpush + @section('content')
@@ -20,7 +24,7 @@ @foreach($teams as $team) - +
@@ -35,14 +39,14 @@
@foreach($missions as $mission) - +
@if($mission->progress($team)[0] >= 1) @if(!Auth::guest()) - {{ $mission->score($team) }} + {{ $mission->score($team) }} @endif @endif