--- /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 SyncSpeedGaming extends Command {
+
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'sync:speedgaming';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Synchronize SpeedGaming schedule';
+
+ /**
+ * Execute the console command.
+ *
+ * @return int
+ */
+ public function handle() {
+ $events = Event::where('external_schedule', 'LIKE', 'sg:%')
+ ->where(function (Builder $query) {
+ $query->whereNull('end');
+ $query->orWhere('end', '<', now());
+ })
+ ->get();
+ foreach ($events as $event) {
+ try {
+ $this->line('syncing '.$event->name);
+ $this->syncEvent($event);
+ } catch (\Exception $e) {
+ $this->error('error syncing event '.$event->name.': '.$e->getMessage());
+ }
+ }
+ return 0;
+ }
+
+ private function syncEvent(Event $event) {
+ $sgHandle = substr($event->external_schedule, 3);
+ $from = now()->sub(1, 'day');
+ $to = now()->add(6, 'day');
+ $sgSchedule = HTTP::get('https://speedgaming.org/api/schedule/', [
+ 'event' => $sgHandle,
+ 'from' => $from->toIso8601String(),
+ 'to' => $to->toIso8601String(),
+ ])->json();
+ $this->purgeSchedule($event, $sgSchedule);
+ foreach ($sgSchedule as $sgEntry) {
+ try {
+ $this->syncSchedule($event, $sgEntry);
+ } catch (Exception $e) {
+ $this->error('error syncing episode '.$sgEntry['id'].': '.$e->getMessage());
+ }
+ }
+ }
+
+ private function purgeSchedule(Event $event, $sgSchedule) {
+ $ext_ids = [];
+ foreach ($sgSchedule as $sgEntry) {
+ $ext_ids[] = 'sg:'.$sgEntry['id'];
+ }
+ $event->episodes()->whereNotIn('ext_id', $ext_ids)->delete();
+ }
+
+ private function syncSchedule(Event $event, $sgEntry) {
+ $ext_id = 'sg:'.$sgEntry['id'];
+ $episode = Episode::firstWhere('ext_id', '=', $ext_id);
+ if (!$episode) {
+ $episode = new Episode();
+ $episode->ext_id = $ext_id;
+ }
+ $episode->event()->associate($event);
+ $episode->title = $sgEntry['match1']['title'];
+ $start = Carbon::parse($sgEntry['when']);
+ if ($start->ne($episode->start)) {
+ $episode->start = $start;
+ }
+ $episode->estimate = $sgEntry['length'] * 60;
+ $episode->confirmed = $sgEntry['approved'];
+ $episode->comment = $sgEntry['match1']['note'];
+ $episode->save();
+ $this->purgePlayers($episode, $sgEntry);
+ foreach ($sgEntry['match1']['players'] as $sgPlayer) {
+ try {
+ $this->syncPlayer($episode, $sgPlayer);
+ } catch (Exception $e) {
+ $this->error('error syncing player '.$sgPlayer['id'].': '.$e->getMessage());
+ }
+ }
+ }
+
+ private function purgePlayers(Episode $episode, $sgEntry) {
+ $ext_ids = [];
+ foreach ($sgEntry['match1']['players'] as $sgPlayer) {
+ $ext_ids[] = 'sg:'.$sgPlayer['id'];
+ }
+ $episode->players()->whereNotIn('ext_id', $ext_ids)->delete();
+ }
+
+ private function syncPlayer(Episode $episode, $sgPlayer) {
+ $ext_id = 'sg:'.$sgPlayer['id'];
+ $player = $episode->players()->firstWhere('ext_id', '=', $ext_id);
+ if (!$player) {
+ $player = new EpisodePlayer();
+ $player->ext_id = $ext_id;
+ $player->episode()->associate($episode);
+ }
+ $user = $this->getUserBySGPlayer($sgPlayer);
+ if ($user) {
+ $player->user()->associate($user);
+ } else {
+ $player->user()->disassociate();
+ }
+ if (!empty($sgPlayer['displayName'])) {
+ $player->name_override = $sgPlayer['displayName'];
+ }
+ if (!empty($sgPlayer['streamingFrom'])) {
+ $player->stream_override = $sgPlayer['streamingFrom'];
+ }
+ $player->save();
+ }
+
+ private function getUserBySGPlayer($player) {
+ if (!empty($player['discordId'])) {
+ $user = User::find($player['discordId']);
+ if ($user) return $user;
+ }
+ if (!empty($player['discordTag'])) {
+ $tag = explode('#', $player['discordTag']);
+ $user = User::firstWhere([
+ ['username', 'LIKE', $tag[0]],
+ ['username', 'LIKE', $tag[1]],
+ ]);
+ if ($user) return $user;
+ }
+ return null;
+ }
+
+}
*/
protected function schedule(Schedule $schedule)
{
- // $schedule->command('inspire')->hourly();
+ $schedule->command('sync:speedgaming')->everyFifteenMinutes();
}
/**
--- /dev/null
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\Episode;
+use Illuminate\Http\Request;
+
+class EpisodeController extends Controller
+{
+
+ public function search(Request $request) {
+ $episodes = Episode::with('event')
+ ->where('confirmed', '=', true)
+ ->where('event.visible', '=', true);
+ return $episodes->get()->toJson();
+ }
+
+ public function single(Request $request, Episode $episode) {
+ $this->authorize('view', $episode);
+ return $episode->toJson();
+ }
+
+}
--- /dev/null
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\Event;
+use Illuminate\Http\Request;
+
+class EventController extends Controller
+{
+
+ public function search(Request $request) {
+ $events = Event::where('visible', '=', true);
+ return $events->get()->toJson();
+ }
+
+ public function single(Request $request, Event $event) {
+ $this->authorize('view', $event);
+ return $event->toJson();
+ }
+
+}
--- /dev/null
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class Episode extends Model
+{
+
+ use HasFactory;
+
+ public function event() {
+ return $this->belongsTo(Event::class);
+ }
+
+ public function players() {
+ return $this->hasMany(EpisodePlayer::class);
+ }
+
+ protected $casts = [
+ 'confirmed' => 'boolean',
+ ];
+
+}
--- /dev/null
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class EpisodePlayer extends Model
+{
+ use HasFactory;
+
+ public function episode() {
+ return $this->belongsTo(Episode::class);
+ }
+
+ public function user() {
+ return $this->belongsTo(User::class);
+ }
+
+ protected $hidden = [
+ 'created_at',
+ 'updated_at',
+ ];
+
+}
--- /dev/null
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class Event extends Model
+{
+
+ use HasFactory;
+
+ public function episodes() {
+ return $this->hasMany(Episode::class);
+ }
+
+ protected $casts = [
+ 'visible' => 'boolean',
+ ];
+
+}
--- /dev/null
+<?php
+
+namespace App\Policies;
+
+use App\Models\Episode;
+use App\Models\User;
+use Illuminate\Auth\Access\HandlesAuthorization;
+
+class EpisodePolicy
+{
+ use HandlesAuthorization;
+
+ /**
+ * Determine whether the user can view any models.
+ *
+ * @param \App\Models\User $user
+ * @return \Illuminate\Auth\Access\Response|bool
+ */
+ public function viewAny(?User $user)
+ {
+ return true;
+ }
+
+ /**
+ * Determine whether the user can view the model.
+ *
+ * @param \App\Models\User $user
+ * @param \App\Models\Episode $episode
+ * @return \Illuminate\Auth\Access\Response|bool
+ */
+ public function view(User $user, Episode $episode)
+ {
+ return $episode->event->visible;
+ }
+
+ /**
+ * Determine whether the user can create models.
+ *
+ * @param \App\Models\User $user
+ * @return \Illuminate\Auth\Access\Response|bool
+ */
+ public function create(User $user)
+ {
+ return $user->isAdmin();
+ }
+
+ /**
+ * Determine whether the user can update the model.
+ *
+ * @param \App\Models\User $user
+ * @param \App\Models\Episode $episode
+ * @return \Illuminate\Auth\Access\Response|bool
+ */
+ public function update(User $user, Episode $episode)
+ {
+ return $user->isAdmin();
+ }
+
+ /**
+ * Determine whether the user can delete the model.
+ *
+ * @param \App\Models\User $user
+ * @param \App\Models\Episode $episode
+ * @return \Illuminate\Auth\Access\Response|bool
+ */
+ public function delete(User $user, Episode $episode)
+ {
+ return false;
+ }
+
+ /**
+ * Determine whether the user can restore the model.
+ *
+ * @param \App\Models\User $user
+ * @param \App\Models\Episode $episode
+ * @return \Illuminate\Auth\Access\Response|bool
+ */
+ public function restore(User $user, Episode $episode)
+ {
+ return false;
+ }
+
+ /**
+ * Determine whether the user can permanently delete the model.
+ *
+ * @param \App\Models\User $user
+ * @param \App\Models\Episode $episode
+ * @return \Illuminate\Auth\Access\Response|bool
+ */
+ public function forceDelete(User $user, Episode $episode)
+ {
+ return false;
+ }
+}
--- /dev/null
+<?php
+
+namespace App\Policies;
+
+use App\Models\Event;
+use App\Models\User;
+use Illuminate\Auth\Access\HandlesAuthorization;
+
+class EventPolicy
+{
+ use HandlesAuthorization;
+
+ /**
+ * Determine whether the user can view any models.
+ *
+ * @param \App\Models\User $user
+ * @return \Illuminate\Auth\Access\Response|bool
+ */
+ public function viewAny(?User $user)
+ {
+ return true;
+ }
+
+ /**
+ * Determine whether the user can view the model.
+ *
+ * @param \App\Models\User $user
+ * @param \App\Models\Event $event
+ * @return \Illuminate\Auth\Access\Response|bool
+ */
+ public function view(?User $user, Event $event)
+ {
+ return $event->visible;
+ }
+
+ /**
+ * Determine whether the user can create models.
+ *
+ * @param \App\Models\User $user
+ * @return \Illuminate\Auth\Access\Response|bool
+ */
+ public function create(User $user)
+ {
+ return $user->isAdmin();
+ }
+
+ /**
+ * Determine whether the user can update the model.
+ *
+ * @param \App\Models\User $user
+ * @param \App\Models\Event $event
+ * @return \Illuminate\Auth\Access\Response|bool
+ */
+ public function update(User $user, Event $event)
+ {
+ return $user->isAdmin();
+ }
+
+ /**
+ * Determine whether the user can delete the model.
+ *
+ * @param \App\Models\User $user
+ * @param \App\Models\Event $event
+ * @return \Illuminate\Auth\Access\Response|bool
+ */
+ public function delete(User $user, Event $event)
+ {
+ return false;
+ }
+
+ /**
+ * Determine whether the user can restore the model.
+ *
+ * @param \App\Models\User $user
+ * @param \App\Models\Event $event
+ * @return \Illuminate\Auth\Access\Response|bool
+ */
+ public function restore(User $user, Event $event)
+ {
+ return false;
+ }
+
+ /**
+ * Determine whether the user can permanently delete the model.
+ *
+ * @param \App\Models\User $user
+ * @param \App\Models\Event $event
+ * @return \Illuminate\Auth\Access\Response|bool
+ */
+ public function forceDelete(User $user, Event $event)
+ {
+ return false;
+ }
+}
--- /dev/null
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ Schema::create('events', function (Blueprint $table) {
+ $table->id();
+ $table->string('name')->unique();
+ $table->text('title');
+ $table->timestamp('start')->nullable()->default(null);
+ $table->timestamp('end')->nullable()->default(null);
+ $table->boolean('visible')->default(false);
+ $table->string('external_schedule')->nullable()->default(null);
+ $table->timestamps();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::dropIfExists('events');
+ }
+};
--- /dev/null
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ Schema::create('episodes', function (Blueprint $table) {
+ $table->id();
+ $table->foreignId('event_id')->constrained();
+ $table->text('title');
+ $table->timestamp('start');
+ $table->integer('estimate')->nullable()->default(null);
+ $table->boolean('confirmed')->default(false);
+ $table->text('comment')->default('');
+ $table->string('ext_id')->nullable()->default(null);
+ $table->timestamps();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::dropIfExists('episodes');
+ }
+};
--- /dev/null
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ Schema::create('episode_players', function (Blueprint $table) {
+ $table->id();
+ $table->foreignId('episode_id')->constrained();
+ $table->foreignId('user_id')->nullable()->default(null)->constrained();
+ $table->string('name_override')->default('');
+ $table->string('stream_override')->default('');
+ $table->string('ext_id')->nullable()->default(null);
+ $table->timestamps();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::dropIfExists('episode_players');
+ }
+};
Route::get('discord-guilds/{guild_id}', 'App\Http\Controllers\DiscordGuildController@single');
Route::get('discord-guilds/{guild_id}/channels', 'App\Http\Controllers\DiscordChannelController@search');
+Route::get('event', 'App\Http\Controllers\EventController@search');
+Route::get('event/{event:name}', 'App\Http\Controllers\EventController@single');
+
Route::get('markers/{map}', 'App\Http\Controllers\TechniqueController@forMap');
Route::get('protocol/{tournament}', 'App\Http\Controllers\ProtocolController@forTournament');