Skip to content

Commit

Permalink
Merge pull request #7 from xoco70/analysis-866w3N
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
Julien committed Sep 10, 2017
2 parents fa35c65 + 713ebb6 commit 027456f
Show file tree
Hide file tree
Showing 18 changed files with 173 additions and 191 deletions.
3 changes: 2 additions & 1 deletion database/factories/ChampionshipSettingsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
$factory->define(ChampionshipSettings::class, function (Faker\Generator $faker) use ($factory) {
$tcs = Championship::all()->pluck('id')->toArray();

$fightingAreas = [1,2,4,8];
$fightingAreas = [1, 2, 4, 8];

return [
'championship_id' => $faker->randomElement($tcs),
'treeType' => $faker->numberBetween(0, 1),
Expand Down
14 changes: 7 additions & 7 deletions src/TreeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ protected function provisionObjects(Request $request, $isTeam, $numFighters)
{
if ($isTeam) {
$championship = Championship::find(2);
factory(Team::class, (int)$numFighters)->create(['championship_id' => $championship->id]);
factory(Team::class, (int) $numFighters)->create(['championship_id' => $championship->id]);
} else {
$championship = Championship::find(1);
$users = factory(User::class, (int)$numFighters)->create();
$users = factory(User::class, (int) $numFighters)->create();
foreach ($users as $user) {
factory(Competitor::class)->create(
['championship_id' => $championship->id,
'user_id' => $user->id,
'confirmed' => 1,
'short_id' => $user->id,
'user_id' => $user->id,
'confirmed' => 1,
'short_id' => $user->id,
]
);
}
Expand Down Expand Up @@ -133,11 +133,11 @@ public function update(Request $request, Championship $championship)
$fight->save();
}
}

return back();
}


function getWinnerId($fighters, $scores, $numFighter)
public function getWinnerId($fighters, $scores, $numFighter)
{
return $scores[$numFighter] != null ? $fighters[$numFighter] : null;
}
Expand Down
26 changes: 14 additions & 12 deletions src/TreeGen/CreateSingleEliminationTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ private function assignPositions()
}

/**
* returns titles depending of number of rounds
* returns titles depending of number of rounds.
*
* @return array
*/
public function getRoundTitles()
Expand All @@ -139,11 +140,13 @@ public function getRoundTitles()
//The minus 3 is to ignore the final, semi final and quarter final rounds

for ($i = 0; $i < $noRounds - 3; $i++) {
$tempRounds[] = 'Last ' . $noTeamsInFirstRound;
$tempRounds[] = 'Last '.$noTeamsInFirstRound;
$noTeamsInFirstRound /= 2;
}

return array_merge($tempRounds, $roundTitles);
}

return $roundTitle[$this->numFighters];
}

Expand All @@ -159,7 +162,7 @@ public function printRoundTitles()
foreach ($roundTitles as $key => $roundTitle) {
$left = $key * ($this->matchWrapperWidth + $this->roundSpacing - 1);

echo '<div class="round-title" style="left: ' . $left . 'px;">' . $roundTitle . '</div>';
echo '<div class="round-title" style="left: '.$left.'px;">'.$roundTitle.'</div>';
}
echo '</div>';
}
Expand All @@ -172,7 +175,7 @@ public function printRoundTitles()
public function getPlayerList($selected)
{
$html = '<select>
<option' . ($selected == '' ? ' selected' : '') . '></option>';
<option'.($selected == '' ? ' selected' : '').'></option>';

foreach ($this->championship->fighters as $fighter) {
$html = $this->addOptionToSelect($selected, $fighter, $html);
Expand Down Expand Up @@ -207,7 +210,7 @@ private function assignFightersToBracket($numRound, $hasPreliminary)
$this->brackets[$roundNumber][$matchNumber] = [$fighter1, $fighter2, $winnerId];
}
}

if ($this->numFighters >= $this->championship->getGroupSize() * 2) {
$lastRound = $this->noRounds;
$lastMatch = $this->numFighters / pow(2, $roundNumber) + 1;
Expand All @@ -218,7 +221,6 @@ private function assignFightersToBracket($numRound, $hasPreliminary)
$fighter2 = $fight->fighter2;
$winnerId = $fight->winner_id;
$this->brackets[$lastRound][$lastMatch + 1] = [$fighter1, $fighter2, $winnerId];

}
}

Expand All @@ -233,12 +235,12 @@ private function addOptionToSelect($selected, $fighter, $html): string
{
if ($fighter != null) {
$select = $selected != null && $selected->id == $fighter->id ? ' selected' : '';
$html .= '<option' . $select
. ' value='
. ($fighter->id ?? '')
. '>'
. $fighter->name
. '</option>';
$html .= '<option'.$select
.' value='
.($fighter->id ?? '')
.'>'
.$fighter->name
.'</option>';
}

return $html;
Expand Down
2 changes: 0 additions & 2 deletions src/TreeGen/PlayOffCompetitorTreeGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ protected function addFighterToGroup(FightersGroup $group, $competitor, $fighter
->get()
->get($numFighterToUpdate - 1);


// we must update the right result
$competitorGroup->competitor_id = $competitor->id;
$competitorGroup->save();
}

}
2 changes: 0 additions & 2 deletions src/TreeGen/PlayOffTeamTreeGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ protected function addFighterToGroup(FightersGroup $group, $team, $fighterToUpda
->get()
->get($numFighterToUpdate - 1);


// we must update the right result
$competitorGroup->team_id = $team->id;
$competitorGroup->save();
}

}
4 changes: 1 addition & 3 deletions src/TreeGen/PlayOffTreeGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ protected function getByeGroup($fighters)
return $this->createByeGroup($byeCount);
}



/**
* Chunk Fighters into groups for fighting, and optionnaly shuffle.
*
Expand Down Expand Up @@ -92,7 +90,7 @@ protected function generateAllTrees()
$fighters = $this->championship->fighters;
// This means that when playoff, we only generate 1 group
// Could be better, for now it is ok
if (sizeof($fighters) > 1) {
if (count($fighters) > 1) {
$this->generateGroupsForRound($fighters, 1);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/TreeGen/SingleEliminationCompetitorTreeGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ protected function addFighterToGroup(FightersGroup $group, $competitor, $fighter
->get()
->get($numFighterToUpdate - 1);


// we must update the right result
$competitorGroup->competitor_id = $competitor->id;
$competitorGroup->save();
Expand Down
1 change: 0 additions & 1 deletion src/TreeGen/SingleEliminationTeamTreeGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ protected function addFighterToGroup(FightersGroup $group, $team, $fighterToUpda
->get()
->get($numFighterToUpdate - 1);


// we must update the right result
$competitorGroup->team_id = $team->id;
$competitorGroup->save();
Expand Down
18 changes: 9 additions & 9 deletions src/TreeGen/SingleEliminationTreeGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Illuminate\Support\Collection;
use Xoco70\LaravelTournaments\Exceptions\TreeGenerationException;
use Xoco70\LaravelTournaments\Models\ChampionshipSettings;
use Xoco70\LaravelTournaments\Models\SingleEliminationFight;
use Xoco70\LaravelTournaments\Models\PreliminaryFight;
use Xoco70\LaravelTournaments\Models\SingleEliminationFight;

abstract class SingleEliminationTreeGen extends TreeGen
{
Expand All @@ -23,6 +23,7 @@ protected function getByeGroup($fighters)
$firstRoundGroupSize = $this->firstRoundGroupSize();
$treeSize = $this->getTreeSize($fighterCount, $firstRoundGroupSize);
$byeCount = $treeSize - $fighterCount;

return $this->createByeGroup($byeCount);
}

Expand Down Expand Up @@ -54,7 +55,7 @@ protected function pushGroups($numRounds, $numFighters)
}

/**
* Create empty groups after round 1
* Create empty groups after round 1.
*
* @param $numFighters
*/
Expand Down Expand Up @@ -150,7 +151,6 @@ protected function generateAllTrees()
$this->generateGroupsForRound($usersByArea, 1);
$this->pushEmptyGroupsToTree($numFighters); // Abstract
$this->addParentToChildren($numFighters);

}

/**
Expand All @@ -176,7 +176,8 @@ public function generateGroupsForRound(Collection $usersByArea, $round)
}

/**
* Check if there is enough fighters, throw exception otherwise
* Check if there is enough fighters, throw exception otherwise.
*
* @throws TreeGenerationException
*/
private function minFightersCheck()
Expand All @@ -189,18 +190,17 @@ private function minFightersCheck()

$minFighterCount = $fighters->count() / $areas;


if ($this->settings->hasPreliminary && $fighters->count() / ($this->settings->preliminaryGroupSize * $areas) < 1) {
throw new TreeGenerationException(trans('laravel-tournaments::core.min_competitor_required', [
'number' => $this->settings->preliminaryGroupSize * $areas,
'fighter_type' => $fighterType
'number' => $this->settings->preliminaryGroupSize * $areas,
'fighter_type' => $fighterType,
]));
}

if ($minFighterCount < ChampionshipSettings::MIN_COMPETITORS_BY_AREA) {
throw new TreeGenerationException(trans('laravel-tournaments::core.min_competitor_required', [
'number' => ChampionshipSettings::MIN_COMPETITORS_BY_AREA,
'fighter_type' => $fighterType
'number' => ChampionshipSettings::MIN_COMPETITORS_BY_AREA,
'fighter_type' => $fighterType,
]));
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/TreeGen/TreeGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ abstract protected function generateFights();

abstract protected function createByeFighter();

abstract protected function addFighterToGroup(FightersGroup $group, $fighter,$fighterToUpdate);
abstract protected function addFighterToGroup(FightersGroup $group, $fighter, $fighterToUpdate);

abstract protected function getByeGroup($fighters);

abstract protected function getFighters();

abstract protected function getNumRounds($fightersCount);



abstract protected function chunkAndShuffle(Collection $fightersByEntity);

abstract protected function syncGroup(FightersGroup $group, $fighters);
Expand Down Expand Up @@ -99,6 +97,7 @@ private function getFightersByEntity($fighters): Collection
if (($this->groupBy) != null) {
return $fighters->groupBy($this->groupBy); // Collection of Collection
}

return $fighters->chunk(1); // Collection of Collection
}

Expand Down Expand Up @@ -171,14 +170,13 @@ private function insertByes(Collection $fighters, Collection $byeGroup)
$sizeGroupBy = count($byeGroup);

$frequency = $sizeGroupBy != 0
? (int)floor($sizeFighters / $sizeGroupBy)
? (int) floor($sizeFighters / $sizeGroupBy)
: -1;

// Create Copy of $competitors
return $this->getFullFighterList($fighters, $frequency, $sizeGroupBy, $bye);
}


/**
* @param $order
* @param $round
Expand All @@ -193,7 +191,6 @@ protected function saveGroup($order, $round, $parent): FightersGroup
? $group->area = $this->getNumArea($round, $order)
: $group->area = 1; // Area limited to 1 in playoff


$group->order = $order;
$group->round = $round;
$group->championship_id = $this->championship->id;
Expand Down Expand Up @@ -438,7 +435,6 @@ protected function getNumArea($round, $order)
return $numArea;
}


protected function generateAllFights()
{
$this->generateFights(); // Abstract
Expand Down
6 changes: 3 additions & 3 deletions src/models/Championship.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User;
use Xoco70\LaravelTournaments\Contracts\TreeGenerable;
use Xoco70\LaravelTournaments\TreeGen\SingleEliminationCompetitorTreeGen;
use Xoco70\LaravelTournaments\TreeGen\SingleEliminationTeamTreeGen;
use Xoco70\LaravelTournaments\TreeGen\PlayOffCompetitorTreeGen;
use Xoco70\LaravelTournaments\TreeGen\PlayOffTeamTreeGen;
use Xoco70\LaravelTournaments\TreeGen\SingleEliminationCompetitorTreeGen;
use Xoco70\LaravelTournaments\TreeGen\SingleEliminationTeamTreeGen;

class Championship extends Model
{
Expand Down Expand Up @@ -156,7 +156,7 @@ public function fightersGroups()
*/
public function fights()
{
return $this->hasManyThrough(Fight::class, FightersGroup::class)->orderBy('id','asc');
return $this->hasManyThrough(Fight::class, FightersGroup::class)->orderBy('id', 'asc');
}

/**
Expand Down
10 changes: 4 additions & 6 deletions src/models/Fight.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function group()
protected static function getFightersWithByes(FightersGroup $group)
{
if ($group == null) {
return null;
return;
}
$fighters = $group->getFightersWithBye();
$fighterType = $group->getFighterType();
Expand Down Expand Up @@ -92,7 +92,6 @@ public function team1()
return $this->belongsTo(Team::class, 'c1', 'id');
}


/**
* Get Second Fighter.
*
Expand Down Expand Up @@ -137,19 +136,18 @@ public function getFighterAttr($numFighter, $attr)
{
$isTeam = $this->group->championship->category->isTeam;
if ($isTeam) {
$teamToUpdate = 'team' . $numFighter;
$teamToUpdate = 'team'.$numFighter;

return optional($this->$teamToUpdate)->$attr;
}
$competitorToUpdate = 'competitor' . $numFighter;
$competitorToUpdate = 'competitor'.$numFighter;
if ($attr == 'name') {
return $this->$competitorToUpdate == null
? ''
: $this->$competitorToUpdate->user->firstname . ' ' . $this->$competitorToUpdate->user->lastname;
: $this->$competitorToUpdate->user->firstname.' '.$this->$competitorToUpdate->user->lastname;
} elseif ($attr == 'short_id') {
return optional($this->$competitorToUpdate)->short_id;
}
return null;
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/models/FightersGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Support\Facades\DB;
use Kalnoy\Nestedset\NodeTrait;


class FightersGroup extends Model
{
protected $table = 'fighters_groups';
Expand Down Expand Up @@ -195,17 +194,15 @@ public function hasDeterminedParent()
*/
public function getValueToUpdate()
{
try{
try {
if ($this->championship->category->isTeam()) {
return $this->teams->map->id[0];
}

return $this->competitors->map->id[0];

}catch (ErrorException $e){
return null;
} catch (ErrorException $e) {
return;
}

}

/**
Expand Down

0 comments on commit 027456f

Please sign in to comment.