mirror of
https://github.com/ericasch6/scrum-poker-internal.git
synced 2026-10-13 19:12:10 +00:00
97 lines
2.1 KiB
PHP
97 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property string $room_id
|
|
* @property int|null $user_id
|
|
* @property string $name
|
|
* @property bool $is_moderator
|
|
* @property Carbon|null $last_seen_at
|
|
* @property Carbon|null $inactive_at
|
|
* @property Carbon|null $created_at
|
|
* @property Carbon|null $updated_at
|
|
* @property-read Room $room
|
|
*/
|
|
class Player extends Model
|
|
{
|
|
protected $fillable = [
|
|
'room_id',
|
|
'user_id',
|
|
'name',
|
|
'is_moderator',
|
|
'last_seen_at',
|
|
'inactive_at',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'is_moderator' => 'boolean',
|
|
'last_seen_at' => 'datetime',
|
|
'inactive_at' => 'datetime',
|
|
];
|
|
}
|
|
|
|
/** @return BelongsTo<Room, $this> */
|
|
public function room(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Room::class);
|
|
}
|
|
|
|
/** @return HasMany<Vote, $this> */
|
|
public function votes(): HasMany
|
|
{
|
|
return $this->hasMany(Vote::class);
|
|
}
|
|
|
|
/** @return BelongsTo<User, $this> */
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function currentVote(): ?Vote
|
|
{
|
|
return $this->votes()
|
|
->where('round', $this->room->current_round)
|
|
->first();
|
|
}
|
|
|
|
public function hasVoted(): bool
|
|
{
|
|
return $this->votes()
|
|
->where('round', $this->room->current_round)
|
|
->exists();
|
|
}
|
|
|
|
public function isModerator(): bool
|
|
{
|
|
return $this->is_moderator;
|
|
}
|
|
|
|
public function markAsSeen(): void
|
|
{
|
|
$this->update([
|
|
'last_seen_at' => now(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param Builder<Player> $query
|
|
* @return Builder<Player>
|
|
*/
|
|
public function scopeActive(Builder $query): Builder
|
|
{
|
|
return $query->whereNull('inactive_at');
|
|
|
|
}
|
|
}
|