--- /dev/null
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\Episode;
+use App\Models\EpisodePlayer;
+use App\Models\Event;
+use App\Models\User;
+use Carbon\Carbon;
+use Illuminate\Console\Command;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Support\Facades\Http;
+
+class SyncBeerLeague extends Command {
+
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'sync:beer';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Synchronize Beer League schedule';
+
+ /**
+ * Execute the console command.
+ *
+ * @return int
+ */
+ public function handle(): int {
+ $events = Event::query()->where('external_schedule', '=', 'beer')
+ ->where(function (Builder $query) {
+ $query->whereNull('end');
+ $query->orWhere('end', '>', now());
+ })
+ ->get();
+
+ if (empty($events)) {
+ return Command::SUCCESS;
+ }
+
+ $schedule = Http::get('https://sheets.googleapis.com/v4/spreadsheets/1LMW5cq35rlPTXIfKQ7bkpMm0vTwG2sj7SqEKrzMy-cw/values/MATCH%20SUBMISSION', [
+ 'alt' => 'json',
+ 'key' => config('google.api_key'),
+ ])->json();
+
+ foreach ($events as $event) {
+ try {
+ $this->line('syncing '.$event->name);
+ $this->syncEvent($event, $schedule['values']);
+ } catch (\Exception $e) {
+ $this->error('error syncing event '.$event->name.': '.$e->getMessage());
+ }
+ }
+
+ return Command::SUCCESS;
+ }
+
+ private function syncEvent(Event $event, $schedule): void {
+ for ($i = 5; $i < count($schedule); ++$i) {
+ $this->syncSchedule($event, $schedule[$i]);
+ }
+ }
+
+ private function syncSchedule(Event $event, $row): void {
+ $ext_id = 'beer:'.$event->id.':'.$row[0];
+ $episode = Episode::query()->firstWhere('ext_id', '=', $ext_id);
+ if (!$episode) {
+ $episode = new Episode();
+ $episode->ext_id = $ext_id;
+ $episode->confirmed = true;
+ }
+ $episode->event()->associate($event);
+ $episode->title = $row[2].' vs '.$row[3];
+ $episode->comment = $row[1];
+ $start = Carbon::createFromFormat('m/d/Y h:i:s A', $row[4].' '.$row[5], 'America/Detroit')->setTimezone('UTC');
+ if (!$episode->start || $start->ne($episode->start)) {
+ $episode->start = $start;
+ }
+ $episode->estimate = 90 * 60;
+ $episode->save();
+ }
+
+}