-
Notifications
You must be signed in to change notification settings - Fork 6
Getting Started
You want to make an app that uses Ubisoft's API? Great! We'll guide you through the process below.
Applies to Dragon6.API version 2020.0730.9-beta and higher
- If you haven't already, create a c# project in Visual Studio, Rider or whatever IDE you're using that targets .NET Core 3.1 or higher.
- Once you've created it, right click the project in the Solution explorer and click
Manage Nuget Packages
- (On Visual Studio, click Browse first) then type into the search box
Dragon6.API
- Select the one by DragonFruit Network and click the plus button/install button that shows. Make sure the version you're installing is equal to or higher than the version stated above - if it isn't check the enable prerelease versions to get the rest
In order to access the stats, we need to create a HttpClient
that can inject the correct headers to get the stats. Fortunately we've done most of the heavy lifting - all you need to do is write a single function to store a key generated from your account info.
- In your project, create a new folder called
Clients
, we'll be using this for storing all the clients we might need. - Create a new file (class) inside the folder called
StatsClient.cs
- Replace the file contents as shown below (change the namespace to what yours was originally):
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using DragonFruit.Common.Data.Services;
using DragonFruit.Six.API;
using DragonFruit.Six.API.Data;
using DragonFruit.Six.API.Data.Tokens;
using DragonFruit.Six.API.Data.Extensions;
namespace DragonFruit.Six.Web.Services
{
public class StatsClient : Dragon6Client
{
// change this to whatever you want
private readonly string _tokenFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DragonFruit Network", "ubi.token");
public StatsClient()
{
Directory.CreateDirectory(Path.GetDirectoryName(_tokenFile));
}
/// <summary>
/// Tells the Dragon6 Client how to get a token in the case it's restarted or expired
/// </summary>
protected override TokenBase GetToken()
{
if (File.Exists(_tokenFile))
{
// if we have a file with some potentially valid keys, try that first
var token = FileServices.ReadFile<UbisoftToken>(_tokenFile);
if (!token.Expired)
return token;
}
// store logins somewhere that is NOT in the code
var username = "username";
var password = "password";
var newToken = this.GetUbiToken(username, password);
// write new token to disk async (non-blocking)
_ = Task.Run(() => FileServices.WriteFile(_tokenFile, newToken));
// return to keep going
return newToken;
}
}
}
Create a new StatsClient
and store it somewhere accessible by all parts of your code. You should only create one of these for the lifetime of your app, so it should be static
or dependency-injected by services.AddSingleton<StatsClient>();
.
In the code you want to access your app, access the StatsClient
instance and invoke one of the many extensions listed on the readme:
using System;
using System.Threading.Tasks;
using DragonFruit.Six.API.Data.Extensions;
using DragonFruit.Six.API.Enums;
using DragonFruit.Six.API.Helpers;
using DragonFruit.Six.API.Tests.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace DragonFruit.Six.API.Demo
{
internal static class Program
{
internal static StatsClient Client = new StatsClient(); //create your client
private static async Task Main(string[] args)
{
using var operatorInformationTask = Task.Run(() => Client.GetOperatorInfo()); //store this in memory to get operator stats
var playerInfo = Client.GetUser(Platform.PC, LookupMethod.UserId, "14c01250-ef26-4a32-92ba-e04aa557d619"); //lookup a user (by ubisoft id)
// var playerInfo = Client.GetUser(Platforms.PC, LookupMethod.Name, "PaPa.Curry"); //or by name
var level = Client.GetLevel(playerInfo); //lookup a player's level info
var loginInfo = Client.GetLoginInfo(playerInfo); //lookup the last time a user logged into the game
var seasonStats = Client.GetSeasonStats(playerInfo, "EMEA"); //get a user's ranked stats for the region (and optionally the season id)
var generalStats = Client.GetStats(playerInfo); //get a user's general stats (overall kd, wl, etc.)
var opStats = Client.GetOperatorStats(playerInfo, await operatorInformationTask); //get a user's operator stats (using that operator info file we got on the second line)
var weapons = Client.GetWeaponStats(playerInfo); //get the player's weapon-specific stats
var stats = new JObject
{
{ "general", JToken.FromObject(generalStats) },
{ "ranked", JToken.FromObject(seasonStats) },
{ "operator", JToken.FromObject(opStats) },
{ "weapon", JToken.FromObject(weapons) }
};
var account = new JObject
{
{ "account", JToken.FromObject(playerInfo) },
{ "login", JToken.FromObject(loginInfo) },
{ "level", JToken.FromObject(level) },
{ "stats", JToken.FromObject(stats) },
};
Console.Write(JsonConvert.SerializeObject(account, Formatting.Indented));
Environment.Exit(0);
}
}
}