xoco70 / laravel-tournaments

Laravel Package that allows you to generate customizable tournaments trees. - This project repo is no longer being maintained
GNU General Public License v3.0
228 stars 51 forks source link

404 after install #39

Closed duckietm closed 4 years ago

duckietm commented 4 years ago

Hello,

First of all thank you for all your effort :)

I followed the following instructions :

composer create-project laravel/laravel "My Web Dir" 5.8 --prefer-dist ++ Set the .env file DB and URL php artisan key:generate I have set IIS to have the home dir : MyWebDir/Public and if I request the page I get the Welcome screen

composer require "xoco70/laravel-tournaments" php artisan vendor:publish --tag=laravel-tournaments --force php artisan migrate composer dump-autoload

Demo data : php artisan db:seed --class=LaravelTournamentSeeder (to get going)

Then I got to : http://url/laravel-tournaments and then I receive an 404 page not found.....

duckietm commented 4 years ago

if I look in the /routes/web.php I only see : Route::get('/', function () {return view('welcome'); });

Is there not something missing :)

Julian367 commented 4 years ago

The same here

xoco70 commented 4 years ago

let me check it

xoco70 commented 4 years ago

yep, it seem that I remove the controller in commit: d5134117202048a7fc75a401f622f5d17af7afdd

What you should do meanwhile is:

Add TreeController.php:

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Xoco70\LaravelTournaments\Exceptions\TreeGenerationException;
use Xoco70\LaravelTournaments\Models\Championship;
use Xoco70\LaravelTournaments\Models\ChampionshipSettings;
use Xoco70\LaravelTournaments\Models\Competitor;
use Xoco70\LaravelTournaments\Models\FightersGroup;
use Xoco70\LaravelTournaments\Models\Team;
use Xoco70\LaravelTournaments\Models\Tournament;

class TreeController extends Controller
{
    /**
     * Display a listing of trees.
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $tournament = Tournament::with(
            'competitors',
            'championships.settings',
            'championships.category')->first();

        return view('laravel-tournaments::tree.index')
            ->with('tournament', $tournament);
    }

    /**
     * Build Tree.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\Response|string
     */
    public function store(Request $request, $championshipId)
    {
        $this->deleteEverything();
        $numFighters = $request->numFighters;
        $isTeam = $request->isTeam ?? 0;
        $championship = $this->provisionObjects($request, $isTeam, $numFighters);
        $generation = $championship->chooseGenerationStrategy();

        try {
            $generation->run();
        } catch (TreeGenerationException $e) {
            redirect()->back()
                ->withErrors($e->getMessage());
        }

        return back()
            ->with('numFighters', $numFighters)
            ->with('isTeam', $isTeam);
    }

    private function deleteEverything()
    {
        DB::table('fight')->delete();
        DB::table('fighters_groups')->delete();
        DB::table('fighters_group_competitor')->delete();
        DB::table('fighters_group_team')->delete();
        DB::table('competitor')->delete();
        DB::table('team')->delete();
        DB::table('users')->where('id', '<>', 1)->delete();
    }

    /**
     * @param Request $request
     * @param $isTeam
     * @param $numFighters
     *
     * @return Championship
     */
    protected function provisionObjects(Request $request, $isTeam, $numFighters)
    {
        if ($isTeam) {
            $championship = Championship::find(2);
            factory(Team::class, (int) $numFighters)->create(['championship_id' => $championship->id]);
        } else {
            $championship = Championship::find(1);
            $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,
                    ]
                );
            }
        }
        $championship->settings = ChampionshipSettings::createOrUpdate($request, $championship);

        return $championship;
    }

    /**
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request, Championship $championship)
    {
        $numFighter = 0;
        $query = FightersGroup::with('fights')
            ->where('championship_id', $championship->id);

        $fighters = $request->singleElimination_fighters;
        $scores = $request->score;
        if ($championship->hasPreliminary()) {
            $query = $query->where('round', '>', 1);
            $fighters = $request->preliminary_fighters;
        }
        $groups = $query->get();

        foreach ($groups as $group) {
            foreach ($group->fights as $fight) {
                $fight->c1 = $fighters[$numFighter];
                $fight->winner_id = $this->getWinnerId($fighters, $scores, $numFighter);
                $numFighter++;

                $fight->c2 = $fighters[$numFighter];
                if ($fight->winner_id == null) {
                    $fight->winner_id = $this->getWinnerId($fighters, $scores, $numFighter);
                }
                $numFighter++;
                $fight->save();
            }
        }

        return back();
    }

    public function getWinnerId($fighters, $scores, $numFighter)
    {
        return $scores[$numFighter] != null ? $fighters[$numFighter] : null;
    }
}

add the associated routes in routes/web.php

Route::get('/', 'TreeController@index')->name('tree.index');
Route::post('/championships/{championship}/trees', 'TreeController@store')->name('tree.store');
Route::put('/championships/{championship}/trees', 'TreeController@update')->name('tree.update');

Should work then.