Added inern app integration

This commit is contained in:
2026-09-02 09:37:11 +02:00
parent 8aa6428089
commit e58a398c07
30 changed files with 1059 additions and 68 deletions
@@ -101,4 +101,82 @@ class RoomParticipationService
return [$room, $player];
}
public function joinEmbedded(Room $room, User $user, string $playerName): Player
{
[$player, $change, $stateVersion] = DB::transaction(function () use ($room, $user, $playerName): array {
$lockedRoom = Room::query()
->lockForUpdate()
->findOrFail($room->id);
if ($lockedRoom->isClosed()) {
throw ValidationException::withMessages([
'join_code' => 'Dieser Raum ist geschlossen.',
]);
}
$nameIsTaken = $lockedRoom->players()
->where('name', $playerName)
->where(function ($query) use ($user): void {
$query->whereNull('user_id')
->orWhere('user_id', '!=', $user->id);
})
->exists();
if ($nameIsTaken) {
throw ValidationException::withMessages([
'player_name' => 'Dieser Anzeigename ist in dem Raum bereits vorhanden.',
]);
}
$player = $lockedRoom->players()
->where('user_id', $user->id)
->first();
$change = null;
if ($player === null) {
$player = $lockedRoom->players()->create([
'user_id' => $user->id,
'name' => $playerName,
'is_moderator' => false,
'last_seen_at' => now(),
]);
$change = RoomStateChange::PlayerJoined;
} else {
if ($player->inactive_at !== null) {
$change = RoomStateChange::PlayerJoined;
} elseif ($player->name !== $playerName) {
$change = RoomStateChange::PlayerUpdated;
}
$player->update([
'name' => $playerName,
'last_seen_at' => now(),
'inactive_at' => null,
]);
}
if ($change === null) {
return [$player, null, null];
}
$lockedRoom->update([
'last_activity_at' => now(),
'state_version' => $lockedRoom->state_version + 1,
]);
return [$player, $change, $lockedRoom->state_version];
});
if ($change instanceof RoomStateChange && is_int($stateVersion)) {
RoomStateChanged::dispatch(
$room->id,
$stateVersion,
$change,
$this->realtimeState->for($room),
);
}
return $player;
}
}
@@ -15,6 +15,7 @@ class RoomRealtimeStateService
* current_round: int,
* revealed: bool,
* state_version: int,
* allow_participants_to_manage_rounds: bool,
* statistics: array{average: string|null, median: string|null}|null,
* players: array<int, array{
* id: int,
@@ -44,6 +45,7 @@ class RoomRealtimeStateService
'current_round' => $room->current_round,
'revealed' => $room->revealed_at !== null,
'state_version' => $room->state_version,
'allow_participants_to_manage_rounds' => $room->allow_participants_to_manage_rounds,
'statistics' => $this->statistics->currentRound($room),
'players' => $room->players
->map(function ($player) use ($room): array {
@@ -0,0 +1,50 @@
<?php
namespace App\Services\Rooms;
use App\Enums\RoomStateChange;
use App\Events\RoomStateChanged;
use App\Models\Room;
use App\Models\User;
use Illuminate\Support\Facades\DB;
class RoomSettingsService
{
public function __construct(
private readonly RoomRealtimeStateService $realtimeState,
) {}
/** @param array{allow_participants_to_manage_rounds: bool, is_persistent: bool} $settings */
public function update(Room $room, User $user, array $settings): void
{
$stateVersion = DB::transaction(function () use ($room, $user, $settings): ?int {
$lockedRoom = Room::query()
->lockForUpdate()
->findOrFail($room->id);
abort_unless($lockedRoom->owner_id === $user->id, 403);
$lockedRoom->fill($settings);
if (! $lockedRoom->isDirty()) {
return null;
}
$lockedRoom->state_version++;
$lockedRoom->save();
return $lockedRoom->state_version;
});
if ($stateVersion === null) {
return;
}
RoomStateChanged::dispatch(
$room->id,
$stateVersion,
RoomStateChange::RoomSettingsUpdated,
$this->realtimeState->for($room),
);
}
}