Files
Deathgarden_Rebirth-Rewrite/dist/app/Models/Game/Matchmaking/Game.php
Vari c8f34d229a Fixed file Put/Get endpoints being in the wrong routes file because of prefix.
Implemented rotue where the host dleetes an user from the match if he didn't join.

Added funktion that tries to fill open games that have missing players.
2024-03-09 12:26:15 +01:00

98 lines
2.6 KiB
PHP

<?php
namespace App\Models\Game\Matchmaking;
use App\Classes\Matchmaking\MatchmakingPlayerCount;
use App\Enums\Game\Matchmaking\MatchmakingSide;
use App\Enums\Game\Matchmaking\MatchStatus;
use App\Models\User\User;
use Cache;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Facades\DB;
/**
* @mixin IdeHelperGame
*/
class Game extends Model
{
use HasFactory, HasUuids;
protected $fillable = [
'status',
'creator_user_id',
];
protected $casts = [
'status' => MatchStatus::class,
];
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'creator_user_id');
}
public function matchConfiguration(): BelongsTo
{
return $this->belongsTo(MatchConfiguration::class);
}
public function players(): BelongsToMany
{
return $this->belongsToMany(User::class)->withPivot('side');
}
public function addQueuedPlayer(QueuedPlayer $player): void
{
DB::transaction(function () use ($player){
$this->players()->attach($player->user, ['side' => $player->side->value]);
foreach ($player->followingUsers as $followingUser) {
$this->players()->attach($followingUser->user, ['side' => $followingUser->side->value]);
$followingUser->delete();
}
$player->delete();
});
}
public function determineHost(): void
{
$hunter = $this->players()->firstWhere('side', '=', MatchmakingSide::Hunter->value);
$this->creator()->associate($hunter);
$this->save();
}
/**
* Get the number of needed players to fill the game.
*
* @return MatchmakingPlayerCount
*/
public function remainingPlayerCount(): MatchmakingPlayerCount
{
$players = $this->players;
$currentHunterCount = 0;
$currentRunnerCount = 0;
foreach ($players as $player) {
if($player->pivot->side === MatchmakingSide::Hunter->value)
++$currentHunterCount;
else
++$currentRunnerCount;
}
$config = $this->matchConfiguration;
if($config === null)
return new MatchmakingPlayerCount();
return new MatchmakingPlayerCount(
$config->hunters - $currentHunterCount,
$config->runners - $currentRunnerCount,
);
}
}