Browse Source

push scores to scoreboard in realtime

master
Aaron Parecki 7 years ago
parent
commit
bb8c3fb75e
No known key found for this signature in database GPG Key ID: 276C2817346D6056
4 changed files with 92 additions and 5 deletions
  1. +48
    -0
      app/Events/ScoreUpdatedEvent.php
  2. +16
    -2
      app/Listeners/TweetAcceptedListener.php
  3. +21
    -0
      public/js/scoreboard.js
  4. +7
    -3
      resources/views/scoreboard.blade.php

+ 48
- 0
app/Events/ScoreUpdatedEvent.php View File

@ -0,0 +1,48 @@
<?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\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Team, App\Mission;
class ScoreUpdatedEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $team_slug;
public $mission_slug;
public $score;
public $complete;
public $team_score;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Team $team, Mission $mission, $score, $complete, $team_score)
{
$this->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'];
}
}

+ 16
- 2
app/Listeners/TweetAcceptedListener.php View File

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

+ 21
- 0
public/js/scoreboard.js View File

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

+ 7
- 3
resources/views/scoreboard.blade.php View File

@ -1,5 +1,9 @@
@extends('layouts.app')
@push('scripts')
<script src="{{ asset('js/scoreboard.js') }}"></script>
@endpush
@section('content')
<div class="container">
<div class="row">
@ -20,7 +24,7 @@
</thead>
<tbody>
@foreach($teams as $team)
<tr>
<tr data-team="{{ $team->slug }}">
<td>
<div class="team">
<span class="team-icon" style="background-color: #{{ $team->color }};"></span>
@ -35,14 +39,14 @@
</div>
</td>
@foreach($missions as $mission)
<td class="score">
<td class="score" data-mission="{{ $mission->slug }}">
<div class="team-wrap">
<div class="team">
@if($mission->progress($team)[0] >= 1)
<span class="team-icon outlined {{ $mission->complete($team) ? '' : 'empty' }}"
style="background-color: #{{ $team->color }}; border-color: #{{ $team->color }};">
@if(!Auth::guest())
{{ $mission->score($team) }}
<span>{{ $mission->score($team) }}</span>
@endif
</span>
@endif

Loading…
Cancel
Save