Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Getting Started

Albie edited this page Jul 30, 2020 · 11 revisions

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.4-beta and higher

Create a project and add Dragon6

  1. 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.
  2. Once you've created it, right click the project in the Solution explorer and click Manage Nuget Packages
  3. (On Visual Studio, click Browse first) then type into the search box Dragon6.API
  4. Select the one by DragonFruit Network and click the plus button/install button that shows.

Create your StatsClient

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.

  1. In your project, create a new folder called Clients, we'll be using this for storing all the clients we might need.
  2. Create a new file (class) inside the folder called StatsClient.cs
  3. Replace the file contents as shown below (change the namespace to what your's 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.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 (this will lookup ubi_user and ubi_password in the user env vars on windows)
            var username = Environment.GetEnvironmentVariable("ubi_user", EnvironmentVariableTarget.User);
            var password = Environment.GetEnvironmentVariable("ubi_password", EnvironmentVariableTarget.User);
            var newToken = d6Client.GetToken(username, password);

            // write new token to disk async (non-blocking)
            _ = Task.Run(() => FileServices.WriteFile(_tokenFile, newToken));
            
            // return to keep going
            return newToken;
        }
    }
}

Store a single copy of your StatsClient

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>();.

Use your StatsClient instance

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

            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);
        }
    }
}