# 🗄️ Database Schema

## Database Schema

Phoenix Duels creates its tables automatically on first start, on both SQLite and MySQL. You never need to create them yourself, and you should not edit them by hand while the server is running.

Every table name honours the `database.table-prefix` option. With an empty prefix the tables are named exactly as below; with a prefix of `duels`, `_duels_player_table` becomes `duels_duels_player_table`.

## Core Tables

**Player**

`_duels_player_table` - one row per player.

| Column                | Description                                                            |
| --------------------- | ---------------------------------------------------------------------- |
| id                    | Auto-incrementing primary key.                                         |
| uniqueId              | The player's UUID. Unique.                                             |
| playerName            | The player's last known name.                                          |
| cachedInventory       | The inventory saved before their current match, used to restore it.    |
| cachedPotionEffects   | The potion effects saved alongside that inventory.                     |
| rejectingDuelsRequest | Whether the player has duel requests turned off.                       |
| lastTimePlayed        | JSON map of mode id to timestamp, used for the per-mode cooldown.      |
| otherStats            | JSON map holding the kill, death, win and loss streaks per match type. |
| metadata              | JSON map used by add-ons to store their own values against a player.   |

**Ranking**

`_ranking_score_table` - one row per player per mode.

| Column      | Description                           |
| ----------- | ------------------------------------- |
| id          | Auto-incrementing primary key.        |
| uniqueId    | The player's UUID.                    |
| playerName  | The player's last known name.         |
| arenaModeId | The mode this rating belongs to.      |
| score       | The player's elo rating in that mode. |

**History**

`_history_records_table` - one row per player per finished match.

| Column           | Description                                                          |
| ---------------- | -------------------------------------------------------------------- |
| id               | Auto-incrementing primary key.                                       |
| uniqueId         | The player's UUID.                                                   |
| playerName       | The player's name at the time of the match.                          |
| arenaId          | The map the match was played on.                                     |
| modeId           | The mode that was played.                                            |
| matchType        | `ranked`, `unranked` or `challenge`.                                 |
| kills            | Kills this player got in the match.                                  |
| deaths           | Deaths this player took in the match.                                |
| status           | `1` for a win, `-1` for a loss, `0` for a draw.                      |
| createdAt        | Unix timestamp of when the match ended.                              |
| replayName       | The recorded replay, if one exists. Cleared when the replay expires. |
| arenaInstanceKey | Which arena instance the match ran in, used to play the replay back. |

**Recaps**

`_match_recaps_table` - the snapshot behind the Recap option in match history.

| Column                 | Description                                    |
| ---------------------- | ---------------------------------------------- |
| id                     | Auto-incrementing primary key.                 |
| historyRecordId        | The history row this recap belongs to.         |
| uniqueId               | The player's UUID.                             |
| cachedInventory        | Their inventory at the moment the match ended. |
| cachedArmor            | Their armor at that moment.                    |
| cachedOffhand          | Their off-hand item at that moment.            |
| cachedPotionEffects    | Their active potion effects at that moment.    |
| health / maxHealth     | Their health and maximum health.               |
| foodLevel / saturation | Their hunger and saturation.                   |
| createdAt              | Unix timestamp of when the recap was taken.    |

**Other**

`_kit_layouts_table` - a player's custom layout for a premade kit.

| Column      | Description                             |
| ----------- | --------------------------------------- |
| id          | Auto-incrementing primary key.          |
| uniqueId    | The player's UUID.                      |
| arenaModeId | The mode whose kit this layout is for.  |
| slots       | The arrangement of the kit's items.     |
| trims       | The armor trims applied to that layout. |

`_virtual_storage_table` - the [Lost Items](../features/lost-items.md) storage.

| Column          | Description                      |
| --------------- | -------------------------------- |
| id              | Auto-incrementing primary key.   |
| uniqueId        | The player's UUID. Unique.       |
| serializedItems | The items waiting to be claimed. |

## Add-on Tables

These are only created when the matching add-on is installed.

<table><thead><tr><th width="330">Table</th><th>Created by</th></tr></thead><tbody><tr><td><code>_player_kits_table</code></td><td>Player Kits. Holds <code>uniqueId</code>, <code>code</code> (the kit slot) and <code>items</code>.</td></tr><tr><td><code>_eloranks_players_table</code></td><td>Elo Ranks. Holds <code>uniqueId</code>, <code>currentRank</code> and <code>unlockedRanks</code>.</td></tr><tr><td><code>_rematchrequeue_players_table</code></td><td>Rematch &#x26; Requeue. Holds <code>uniqueId</code> and <code>autoRequeueEnabled</code>.</td></tr></tbody></table>

## Querying stats yourself

> **Warning:**
Kills, deaths, wins, losses and draws are **not** stored as counters anywhere. They are summed live from `_history_records_table` every time they are needed.

So a player's total ranked kills is a query, not a column lookup:

```sql
SELECT SUM(kills) FROM _history_records_table
WHERE uniqueId = ? AND matchType = 'ranked';
```

This is also why /pduels set kills works the way it does. Since there is no counter to overwrite, it inserts a single corrective record with a `modeId` of `manual-adjustment` that makes the sum land on the value you asked for. If you find those rows in the table, that is where they came from.

Source: https://www.phoenixplugins.com/docs/phoenix-duels/plugins/database-schema