A few years ago I made a bot to monitor performances of players on an event called World Tournament on a game called Dragon Ball Z Dokkan Battle

The local client used to collect this data is adapted from Tanukijs’s dokkan-bot (my adapted version: dokkan-bot-adapted). The analytics, database, website and Discord bot are my own work.

What is the World Tournament?

Before explaining that we need to understand what Dokkan Battle is, Dokkan Battle (Dokkan for short) is a mobile game in the gacha genre, the main goal of this game is to collect cards and play events The whole game is PVE based and you spend the vast majority of the game playing alone without any form of PVP competition

The World Tournament (which we'll refer to as WT) is an event that takes place a few times a year (3 to 5 times ish) and it is a pretty special event. It takes place from the friday at 8:30am to the monday at 8:00am and what makes this event special is that despite being PVE it has a direct global leaderboard meaning that it makes it the only event in the game with direct player vs player competition Because of that the event is pretty competitive. The goal is to fight teams of bots after which defeating we get points and it is the total amount of points that determines our placement. Because the event lasts for (almost) a full 3 days there are 2 ways to maximize our points and placement

  1. We can grind for as long as possible ideally the full 72 hours (yes this is done at the top level)
  2. Win as fast as possible

Because of the sheer amount of competitiveness at the highest level players at the top love to compare themselves against with a few metrics, like the amount of points per win, the wins per hour, how long they are grinding etc.

Back in june 2024 players started to calculate those directly instead of just looking at the leaderboard every so often

StartingPoint

I myself am very active in this community which made me realize that I could fully automate everything using the internal api of the game and not only for a single player but for any player

This was the starting point of this project

StartingPoint2

How does it work

Getting the data

The way it works is actually pretty straightforward, getting the data is fairly easy and the rest is just a bunch of maths

The way the game displays the internal leaderboard is by calling the api endpoint https://ishin-global.aktsk.com/budokais/{edition}/rankings You have to provide the right headers of course and log in with a valid account but this is the main idea, upon looking at this request from the game we realize that there is a page parameter in the headers and when we manually call pages other than 1 we realize that we can get the data for any player.

GameLeaderBoard

This allows us to get information such as:

  • The player's id
  • Its current rank
  • Current points
  • Current win count
  • Current max wins in a row count
  • Player's name

The game's servers refresh the whole leaderboard every 15 minutes which will be our delta to fetch the data The first iterations of this bot only supported the top 100 but later on the bot could essentially get every players playing the event, however I realized that the only people who could realistically benefit from such a tool are the people grinding the event fairly seriously which is why it was limited to the top 10 000 later on.

Using the data

With all of those informations we can actually calculate a lot of useful metrics like

  • wins
  • points
  • rank

Which I won't go in details about because as you guessed we get this information directly from the raw data so no calculation is required

But we can also get

  • Wins pace (in wins/hour)
  • Points pace (in points/hour)
  • Points/Win ratio
  • Theorical maximum points
  • Theorical maximum wins

I'll describe how they work one by one despite these being really straightforward most of the time

Wins pace

As said before a fetch (meaning the moment we get the data) happens every 15 minutes so if we want to calculate the wins per hour we only have to substract the current wins with the wins from 4 fetches ago and divide it by 4 right ?!??!?!

Yeah but no I wanted to make the bot sort of error proof which is why I decided to also store the hours at which the data had been saved for each player which means that the right formula is actually

wins_pace.append(
    round((wins[-1] - wins[-5]) / (hours[-1] - hours[-5]), 2)
)

You may realize an immediate issue, what if the array of wins has less than 5 elements ? Well in this case we will use a rather simple approach which is simply dividing the total win by the total time elapsed

if len(hours) <= 5:
    points_pace.append(int(player_points / elapsed_hours))
    wins_pace.append(round(player_wins / elapsed_hours, 2))

Points pace

As you may have realized from the code block above the points pace is actually extremely similar to the way we calculate the wins pace one might argue that it's even the same with the only difference being that we make it an integer so to calculate it, basically re-read the section above and replace wins with points

Points/Win ratio

The name of this metric is pretty self explanatory in the way we will calculate it so the formula is really straight forward

player_points_wins_ratio = (
    int(player_points / player_wins) if player_wins > 0 else 0
)

Maximum points

Just like the points pace and wins pace the process here is rather the same so this will be the same for maximum wins The way we calculate it is by taking the maximum pace a player has had, multiply it by the time left until the end and add the current points. This way of calculating it makes this semi-realistic because a player can hardly replicate his maximum pace for a full 72 hours

max_points.append(
    int(
        player_points + max(points_pace) * (left.total_seconds() / 3600)
    )
)
max_wins.append(
    int(player_wins + max(wins_pace) * (left.total_seconds() / 3600))
)

Actually using this data

The first iterations of this were a discord bot because that's where the active WT community was and the first versions were.... in a working state we'll say

FirstVer

At the time the entire data was stored in a single json file

[
    {
        "id": 2287677656,
        "name": "Lotad",
        "wins": [
            812,
            816,
            819,
            823,
            ...
        ],
        ...
    },
    {
        "id": 1887677689,
        "name": "Discord",
        ...
    },
    ...

Meaning that if we wanted to get data for a player we had to load the entire json in memory every single time, we could've cached the json so that we wouldn't have to load it for every user but even then we would have to reload it a few minutes later because of the refresh. As you can guess that was getting pretty bad in the later stages of the event when the data started to get pretty big which means I quickly had to move from this approach to a proper way to store data. At the time I had freshly learned sql in class which made me realize that a proper sql database was the exact thing I needed.

I chose sqlite to use the database and the overall structure of the database is pretty straightforward

CREATE TABLE IF NOT EXISTS players (
    id INTEGER PRIMARY KEY,
    name TEXT,
    wins TEXT,
    points TEXT,
    ranks TEXT,
    hour TEXT,
    points_pace TEXT,
    wins_pace TEXT,
    points_wins TEXT,
    max_points TEXT,
    max_wins TEXT
)

As you can see we store most of the data as TEXT the reason for that being that sqlite does not support storing arrays as columns and this approach itself is pretty anti sql which made me just store the entire array as text (ex "[1, 2, 3]") and I simply had to json.load() it afterwards to properly manipulate it.

Saying this greatly improved the performances would be a gross understatement I went from a few seconds to load a user's data from a few milliseconds

But you may see one of the bottleneck with this approach, if we want to find the user at the rank 5 for example we would have to find the user WHERE the latest hour is the current latest fetch time AND where its ranks[-1] is 5 but how would we access ranks[-1] when it is stored as text???

That's where some funny string manipulation mechanic comes in basically we will reverse the text, go 2 character after the start, separate words by the separator , and look at the text which is the data we want here is the sql query

CAST(SUBSTR({type}, LENGTH({type}) - INSTR(strrev({type}), ',') + 2, LENGTH({type})) AS {datatypes[type]}) AS latest_type_value

Notice the type parameter because this can be applied to not only wins but hours, wins pace etc, basically every data type that we use Here is the way we'd use it (and yes strrev isn't a sqlite builtin function so we have to make it)

datatypes = {
    "wins_pace": "FLOAT",
    "points_pace": "FLOAT",
    "wins": "INTEGER",
    "points": "INTEGER",
    "points_wins": "FLOAT",
    "ranks": "INTEGER",
}
conn.create_function("strrev", 1, lambda s: s[::-1])
query = f"""
    SELECT name,
           CAST(SUBSTR({type}, LENGTH({type}) - INSTR(strrev({type}), ',') + 2, LENGTH({type})) AS {datatypes[type]}) AS latest_type_value
    FROM players
    WHERE CAST(SUBSTR(hour, LENGTH(hour) - INSTR(strrev(hour), ',') + 2, LENGTH(hour)) AS FLOAT) = ?
    ORDER BY latest_type_value DESC
    LIMIT ? OFFSET ?
"""

Front End for the data

As you may have noticed we store everything as a list for everyone meaning that we save every state for each player the reason for that being that with this we can provide convenient graphs to look at player's performances and compare them

Graphs plotting

To provide the graphs I simply used the module matplotlib because I was pretty familiar with it from my physics class I made a render function that takes in argument a list of player's data, what type of data we want to render, the title and some other things

The function is pretty big and fairly complex which is why I won't go in details in how it works but if you want the details you can go to the github repo

The result is pretty nice

WinsPace

You may be wondering why we provide a list of players data in the arguments and the reason for that is that it allows us to plot multiple players on the same graph to have a visual comparison

Compare

Final Result

The final result is pretty nice looking as you can see from the screenshots above and the bot itself has many features thanks to the users who provided ideas ever since the start of this project

The main ones being used being the comparison between players but some neat features include

  • The gap command that allows players to look at how they're doing compared to players right above and under them in the leaderboard

Gap

  • Predict command which looks at the previous WTs and predicts the amount of points a certain rank will end up at based on the difference to the average ( basically useful to know where the top 100 will end up to know if you will get it or any other rank)

Predict

  • Target command which looks at your current points, remaining time and point/win ratio and calculate the pace needed to reach X points at the end

Target

  • Bulk command which allows you to render multiple graphs at once (this is because discord embeds are limited to 1 image) allowing you to have an overview of your performance

Bulk

By the way a website version of this as well as an app made by Darkruss are available

This project started on july 2024 and it has been ongoing ever since, players keep using it and I cannot thank them enough for actually giving feedback and ideas of improvement on it. I learned quite a lot about managing fairly big projects thanks to it and it has been a pleasure to work on it

If you want to support it do not hesitate to leave a star on github and of course feedback and contributions are welcome !