jacobhood / Challonge-DotNet

MIT License
2 stars 4 forks source link

# Challonge-DotNet

Challonge-DotNet is a C# implementation of the CHALLONGE! API built on .NET that exposes asynchronous methods for interacting with the API.

Requirements

Installation

Recommended methods:

Usage

Console Application

Add these using directives:

using System.Net.Http;
using System.Collections.Generic;
using Challonge.Api;
using Challonge.Objects;

Create the client and use its methods:

class Program
{
    private static readonly HttpClient _httpClient = new();
    private static readonly ChallongeCredentials _credentials = new("username", "apiKey");
    private static readonly ChallongeClient _client = new(_httpClient, _credentials);

    static void Main()
    {
        IEnumerable<Tournament> tournaments = _client.GetTournamentsAsync().Result;
    }
}

ASP.NET Core Web Application

Challonge-DotNet supports dependency injection using the built-in dependency injection framework.

In Startup.cs:

using Challonge.Extensions.DependencyInjection;
public void ConfigureServices(IServiceCollection services)
{
    services.AddChallonge("username", "apiKey");
}

Add the appropriate using directives to your controller file:

using Challonge.Api;
using Challonge.Objects;

Inject the client:

public class HomeController : Controller
{
    private readonly IChallongeClient _client;

    public HomeController(IChallongeClient client)
    {
        _client = client;
    }
}

You can also inject IChallongeCredentials to access the username and api key used by the client.

Example

Assume we have an already-configured ChallongeClient _client as a field in our class.

public async Task<Tournament> Example()
{
    TournamentInfo info = new()
    {
        AcceptAttachments = true,
        TournamentType = TournamentType.DoubleElimination,
        Name = "SSBM Weekly #46"
    };
    Tournament tournament = await _client.CreateTournamentAsync(info);

    for (int i = 1; i <= 5; i++)
    {
        ParticipantInfo pInfo = new()
        {
            Name = $"player{i}"
        };
        await _client.CreateParticipantAsync(tournament, pInfo);
    }

    IEnumerable<Participant> participants = await _client.RandomizeParticipantsAsync(tournament);

    foreach (Participant p in participants)
    {
        ParticipantInfo pInfo = new()
        {
            Misc = p.Seed % 2 == 0 ? "Even seed" : "Odd seed";
        };
        await _client.UpdateParticipantAsync(p, pInfo);
    }

    tournament = await _client.GetTournamentByIdAsync(tournament.Id);
    return tournament;
}

Tests

The ChallongeTests project contains a minimal set of tests that validate the core functionality of Challonge-DotNet. Visual Studio 2019 is strongly recommended for navigating and running the tests.

First, create CHALLONGE_USERNAME and CHALLONGE_API_KEY environment variables and set them to the appropriate values. Then, run the tests using your preferred method. If you find that the built-in cleanup doesn't execute properly, use this program to remove any residual test tournaments:

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Challonge.Api;
using Challonge.Objects;

namespace DeleteTestTournaments
{
    class Program
    {
        private static readonly ChallongeCredentials _credentials = new(
            Environment.GetEnvironmentVariable("CHALLONGE_USERNAME"), 
            Environment.GetEnvironmentVariable("CHALLONGE_API_KEY"));
        private static readonly ChallongeClient _client = new(new HttpClient(), _credentials);

        static async Task Main()
        {
            foreach(Tournament t in await _client.GetTournamentsAsync())
            {
                if (t.Name.EndsWith("-_-_Challonge-DotNetTest-_-_"))
                {
                    await _client.DeleteTournamentAsync(t);
                }
            }
        }
    }
}

Coverage is not yet complete, and until then, the test project will remain under active development. It will also be updated as needed for testing any changes to the library.

Notes

These observations arose from experimenting and debugging, so they may not be totally accurate.