teamreflex / ChallongePHP

Package for interfacing with the Challonge API.
MIT License
23 stars 9 forks source link

bulkAddParticipant #20

Closed interludic closed 1 year ago

interludic commented 2 years ago

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']));
}
interludic commented 2 years ago

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 { …}

interludic commented 2 years ago

Here is the documentation: https://api.challonge.com/v1/documents/participants/bulk_add https://api.challonge.com/v1/documents/participants/create https://stackoverflow.com/questions/55320917/how-to-pass-bulk-participants-to-challonge-rest-api

kylekz commented 2 years ago
$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

interludic commented 2 years ago

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.

kylekz commented 2 years ago

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

interludic commented 2 years ago

Thanks

It was working previously.

Was hoping to get it back up next week?

On Thu, 28 Jul 2022, 16:22 Kyle, @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/teamreflex/ChallongePHP/issues/20#issuecomment-1197715062, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQIXXK3LCJFQBPARXIYFWLVWIRKRANCNFSM543RP2SA . You are receiving this because you authored the thread.Message ID: @.***>

kylekz commented 2 years ago

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.

interludic commented 2 years ago

Awesome glad you know what's going on. Are they looking after you? It's not a love job is it?

On Thu, 28 Jul 2022, 19:09 Kyle, @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/teamreflex/ChallongePHP/issues/20#issuecomment-1197875379, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQIXXOHTOYVSFYTT4PCYSDVWJE43ANCNFSM543RP2SA . You are receiving this because you authored the thread.Message ID: @.***>

interludic commented 2 years ago

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

kylekz commented 2 years ago

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']));
interludic commented 2 years ago

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

interludic commented 2 years ago

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

kylekz commented 2 years ago

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.

interludic commented 2 years ago

Yes! that is working. legend thanks.

So long term what's the plan young man?

Apfelwurm commented 2 years ago

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 :)

kylekz commented 2 years ago

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

kylekz commented 2 years ago

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.

Apfelwurm commented 2 years ago

@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?

Apfelwurm commented 2 years ago

@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.

Apfelwurm commented 2 years ago

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.

Apfelwurm commented 2 years ago

so https://github.com/teamreflex/ChallongePHP/pull/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 https://github.com/teamreflex/ChallongePHP/issues/20#issuecomment-1200969460 :)

kylekz commented 2 years ago

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

Apfelwurm commented 2 years ago

@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

kylekz commented 2 years ago

@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.

Apfelwurm commented 2 years ago

@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? :)

interludic commented 2 years ago

I will need a few more days.

On Fri, 26 Aug 2022, 05:00 Alex, @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/teamreflex/ChallongePHP/issues/20#issuecomment-1227648387, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQIXXKQBSIRE2XF4SX322LV267GFANCNFSM543RP2SA . You are receiving this because you were mentioned.Message ID: @.***>

interludic commented 2 years ago

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...

Apfelwurm commented 2 years ago

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 commented 2 years ago

Awesome thanks!

On Mon, 1 Aug 2022 at 19:45, Kyle @.***> wrote:

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.

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

— Reply to this email directly, view it on GitHub https://github.com/teamreflex/ChallongePHP/issues/20#issuecomment-1200969460, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQIXXOZGI7GS7FY7LLBK73VW6MCXANCNFSM543RP2SA . You are receiving this because you authored the thread.Message ID: @.***>

Apfelwurm commented 2 years ago

@interludic @kylewardnz any news here? :)

kylekz commented 2 years ago

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

interludic commented 2 years ago

Ok thanks!!

I'm sure it's good to go

On Sat, 22 Oct 2022, 01:08 Kyle, @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/teamreflex/ChallongePHP/issues/20#issuecomment-1287018355, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQIXXO3ZW4BORXNZ5RAA6DWEKPUBANCNFSM543RP2SA . You are receiving this because you were mentioned.Message ID: @.***>

kylekz commented 1 year ago

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