teamreflex/ChallongePHP

bulkAddParticipant

Closed this issue · 33 comments

bulkAddParticipant

what is the syntax for this ? this is what i have tried:

        $postSquads[] = $comp->addParticipant(['participant[name]' => $team->name, 'participant[seed]' => $key + 1, 'participant[misc]' => $team->id]);


        // 'participant[][name]'=>1, 'participant[][seed]'=>1, 'participant[][misc]'=>'team_id', 'participant[][email]'=>'blah@blah.com',


    // $postSquads = $comp->bulkAddParticipant([
    // 'participant[][name]'=>1, 'participant[][seed]'=>1, 'participant[][misc]'=>'team_id', 'participant[][email]'=>'blah@blah.com',
    // 	// 'participant[][name]'=>2, 'participant[][seed]'=>2, 'participant[][misc]'=>'team_id',
    // 	// 'participant[][name]'=>3, 'participant[][seed]'=>3, 'participant[][misc]'=>'team_id',
    // 	// 'participant[][name]'=>4, 'participant[][seed]'=>4, 'participant[][misc]'=>'team_id',
    // 	// 'participant[][name]'=>5, 'participant[][seed]'=>5, 'participant[][misc]'=>'team_id',
    // 	// 'participant[][name]'=>6, 'participant[][seed]'=>6, 'participant[][misc]'=>'team_id',
    // 	// 'participant[][name]'=>7, 'participant[][seed]'=>7, 'participant[][misc]'=>'team_id'
    // ]);

any examples?

/**
 * Bulk add participants to a tournament (up until it is started).
 * @param array $options
 * @return Collection
 * @throws \JsonException
 * @throws \Reflex\Challonge\Exceptions\InvalidFormatException
 * @throws \Reflex\Challonge\Exceptions\NotFoundException
 * @throws \Reflex\Challonge\Exceptions\ServerException
 * @throws \Reflex\Challonge\Exceptions\UnauthorizedException
 * @throws \Reflex\Challonge\Exceptions\UnexpectedErrorException
 * @throws \Reflex\Challonge\Exceptions\ValidationException
 */
public function bulkAddParticipant(array $options = []): Collection
{
    $response = $this->client->request('POST', "tournaments/{$this->id}/participants/bulk_add", $this->client->mapOptions($options, 'participant'));
    return Collection::make($response)
        ->map(fn (array $participant) => Participant::fromResponse($this->client, $participant['participant']));
}

I tried this but its no longer working?

        $postSquads[] = $comp->addParticipant(['participant[name]' => $team->name, 'participant[seed]' => $key + 1, 'participant[misc]' => $team->id]);

changes to the api?

Reflex\Challonge\Exceptions\ValidationException^ {#3012
#message: "Validation error(s) for create or update method"
#code: 0
#file: "C:\laragon\www\613\vendor\team-reflex\challonge-php\src\Challonge\ClientWrapper.php"
#line: 111
trace: {
C:\laragon\www\613\vendor\team-reflex\challonge-php\src\Challonge\ClientWrapper.php:111 { …}
C:\laragon\www\613\vendor\team-reflex\challonge-php\src\Challonge\ClientWrapper.php:66 { …}
C:\laragon\www\613\vendor\team-reflex\challonge-php\src\Challonge\DTO\Tournament.php:282 { …}

$comp->bulkAddParticipant([
  "participants" => [
    ["name" => "test1"],
    ["name" => "test2"],
  ]
]);

Does something like that work? Unfortunately I've completely switched industries over the last 18 months so I'm no longer working with esports or even PHP and not keeping up with Challonge changes. I can take a closer look over the weekend if the issue persists

Thanks for the quick reply,
does that mean this repo will no longer be maintained?

--
No that didn't work, no error response, just empty array.

I'm not actively keeping up to date with Challonge for changes but I'll look into issues that get raised whenever I get some time

Had a quick tinker and it looks like they've enabled JSON body submissions? I don't remember it working in the past.. Right now it's submitting everything via query params, however submitting this via Postman raw JSON does work:

{
    "participants": [
        { "name": "Test 1" },
        { "name": "Test 2" }
    ]
}

If you wanted a quick workaround, you could either:

  1. I'm assuming you're using Laravel, so you could just quickly use the built-in HTTPClient to submit this request manually, then pass the response into the Participant::fromResponse helper.
  2. Just loop over your participant list and do a request for each one should work, albeit much slower and maybe hitting rate limits.

I'm gonna have to refresh myself on PSR-7, PHP array/object JSON stuff etc since I've become a TS drone, but at least the issue is clear now.

So i'm not really sure how to do the steps you mentioned.
should i be looking at working with the api directly somehow?

Yeah something along the lines of this, assuming your Laravel installation is one with the built-in HTTP client.

Basically just manually working with the API for this one method (bulk add participant), and pass the response into this package's named constructors so you can continue to use the DTO objects like normal.

$uri = "https://api.challonge.com/v1/tournaments/$tournament->id/participants/bulk_add.json?api_key=$apiKey";
$response = Http::acceptJson()->post($uri, [
    ['name' => 'Team 1'],
    ['name' => 'Team 2'],
]);
return $response->collect('participants')
    ->map(fn (array $participant) => Participant::fromResponse($this->client, $participant['participant']));

Thanks checking now.
it would be good to use version control on the api?

dump($response->collect('participants')); is empty and no participants are being added to the tournament

Ah I forgot the key:

$response = Http::acceptJson()->post($uri, [
    'participants' => [
        ['name' => 'Team 1'],
        ['name' => 'Team 2'],
    ]
]);

Essentially you need to recreate this in the HTTP client of your choice, whether that's Laravel's built-in, Guzzle or whatever PSR-18 one you're passing into the new Challonge() constructor.

Yes! that is working. legend thanks.

So long term what's the plan young man?

I`m going to take a look at challongephp again in the course of the next days, since we need it soon, than i also can try to fix this one, so you dont need to invest youre time @kylewardnz :)

Sorry I got sidetracked in the weekend. I've got a fix, just making the key mapping a default option and manually checking everything.

Alright I just pushed to fix/json-encode. If you want to give it a test using your codebase then it should be okay. I'll test it out a bit now manually, and once it's confirmed good I'll make a proper 3.2 release. May need to replace the / in the branch name with -, I don't remember the exact convention

{
    "repositories": [
        {
            "type": "git",
            "url": "https://github.com/teamreflex/ChallongePHP"
        }
    ],
    "require": {
        "team-reflex/challonge-php": "dev-fix/json-encode"
    }
}

Bulk add participant works like so, if your mapOptions is true:

$tournament->bulkAddParticipant([
    ['name' => 'Team 1'],
    ['name' => 'Team 2'],
]);

If your mapOptions is false, you need to nest it all under the participants key:

$tournament->bulkAddParticipant([
    'participants' => [
        ['name' => 'Team 1'],
        ['name' => 'Team 2'],
    ],
]);

Everything else should work as normal, just added a little edge case check to bulkAddParticipant rather than removing the mapping thing altogether.

@interludic most likeley you should be good to go if you now update to ~4 (ofc if you're app is already PHP 8.x compatible). Therfore this issue should be done. Can you confirm that?

@kylewardnz seems like your change broke at least the creation of tournaments and single players in our usecase, i have to test a bit more but it worked with just my changes. I think challonge only changed the expected data structure on a few endpoints. I will do a full test later.

okay, i could verify that the tournament creation and participant adding (have only testet those) just work with the old x-www-form-urlencoded data. I'm going to implement a new parameter to switch between the two and make a new pr.

so #24 implements the fix to make the other requests work again. I also could test the bulk adding now and it seems to work.

@kylewardnz the wiki should be also updated with the new data structure you mentioned in #20 (comment) :)

zzz I've never seen an API have this many quirks and inconsistencies. Thanks, merged and tagged 4.1

@kylewardnz thanks for the fast merge :) i think you updated the wiki for every function, which i think is not right. You maybe have to change everything but the bulkadd documentation back to the old state. Sorry for that :D

@Apfelwurm I think it's all correct as it assumes $mapOptions is true, as that then changes ["name" => "Tournament Name"] to ["tournament[name]" => "Tournament Name"] internally rather than forcing the user to.

@kylewardnz Ohh okay, you are right, my bad :) @interludic can you confirm that the fix is also working for you so that the issue can be closed? :)

Does this require laravel 9?

Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires team-reflex/challonge-php 4.1 -> satisfiable by team-reflex/challonge-php[4.1].
    - team-reflex/challonge-php 4.1 requires illuminate/collections ~9 -> found illuminate/collections[v9.0.0-beta.1, ..., 9.x-dev] but these were not loaded, likely because it conflicts with another require.

maybe it's time i rolled my own...

Oh, jep, it does now.
@kylewardnz would it be possible to release a 3.2 after the 4.1 release based on the changes we implemented but without the bumped php & laravel dependency? Then we could readd php7 and laravel 8 support on this including the fix for bulkAddParticipant. I'm not to deep into composer, so no Idea if you can release an older Version after a newer one.

@interludic @kylewardnz any news here? :)

Just pushed to 3.x, adding 7.4 to the PHP versions and downgrading laravel/collections to ~8, let me know if that's what you needed and I'll publish it to 3.2

jesus christ what is with github notifications not working lately.. Tagged at 3.2. Feel free to reopen or make a new issue if it's still a problem