1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Generating season matches with Laravel?

Discussão em 'Outras Linguagens' iniciado por ash, Setembro 27, 2024 às 21:53.

  1. ash

    ash Guest

    I've wrote a command to generate a season record along with matches between teams.

    It works great, but it doesn't seem to take into account if a team already has a match.

    This means, team 1 could play team 3, but team 4 could also play team 1, meaning team 1 has two matches on day one?

    How can I fix this and avoid matches overlapping?

    class GenerateSeasonCommand extends Command
    {
    protected $signature = 'generate:season';
    protected $description = 'Command description';

    public function handle(): void
    {
    $season = Season::query()->create([
    'name' => $this->ask('What is the name of the season?'),
    ]);

    $seasonGameData = [];
    $teams = Team::all();

    foreach ($teams as $team) {
    $matchDay = 0;
    foreach (Team::query()->whereNot('id', $team->id)->get() as $otherTeam) {
    $matchDay++;
    $seasonGameData[] = [
    'season_id' => $season->id,
    'home_team_id' => $team->id,
    'away_team_id' => $otherTeam->id,
    'game_mode_id' => $this->getRandomGameModeId(),
    'map_id' => Map::query()->orderByRaw('RAND()')->first()->id,
    'match_day' => $matchDay,
    'created_at' => now()
    ];
    }
    }

    SeasonGame::query()->insert($seasonGameData);
    }

    private function getRandomGameModeId() : int
    {
    return mt_rand(1, 4) >= 4 ?
    mt_rand(1, 4) :
    mt_rand(1, 3);
    }
    }

    Continue reading...

Compartilhe esta Página