> For the complete documentation index, see [llms.txt](https://organized-chaos.gitbook.io/organized-chaos-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://organized-chaos.gitbook.io/organized-chaos-docs/the-lost-treasure/database.md).

# Database

What is stored in MySQL, and what happens without a database.

With `oxmysql` running, the credits sitting on a machine and what every player has put through it are kept in MySQL. Without it, credits are kept in the resource's own storage and no figures are recorded.

```lua
Config.Database = {
    Enabled   = 'auto',            -- 'auto' | true | false
    Resource  = 'oxmysql',
    Prefix    = 'lost_treasure',   -- the start of every table name

    LogRounds       = false,       -- one row per round
    LogRoundsMin    = 0,           -- ...only when stake or win reaches this
    LogTransactions = true,        -- one row per insert and cash-out

    FlushSeconds = 30,             -- how often totals are written back
}
```

These settings are in `config/server/server.lua`.

## Tables

The tables are created on the first start. To create them yourself, import `sql/lost_treasure.sql`, which is the same schema.

| Table                        | One row per             | Holds                                                                                              |
| ---------------------------- | ----------------------- | -------------------------------------------------------------------------------------------------- |
| `lost_treasure_players`      | player                  | Credits on the machine, rounds, wagered, won, net, deposited, cashed out, biggest win, last played |
| `lost_treasure_rounds`       | round, with `LogRounds` | Bet, stake, win, net, mode, tier, buy, capped, seed, credits after                                 |
| `lost_treasure_transactions` | insert or cash-out      | Kind, amount, credits after                                                                        |
| `lost_treasure_bank`         | ledger (one row)        | The house ledger: wagered, paid, net, retained, pool                                               |
| `lost_treasure_bank_daily`   | day                     | The same ledger per day, for the staff dashboard                                                   |

`net` is `won - wagered`. A negative figure is what the player is down, which is the house's take from them.

{% hint style="info" %}
`LogRounds` is off by default, because the per-player row already answers how much a player has wagered, won and lost. Turn it on when you want to look at single rounds, and be aware that a busy floor writes a lot of rows.
{% endhint %}

## When things are written

Balances and totals are collected in memory and written back every `FlushSeconds`, when a player disconnects, and when the resource stops. A spin never waits on a query.

Two things are handled at once instead:

* **Opening a machine.** The player's stored balance is read then. If that read fails, the machine reports itself unavailable instead of letting them play against a balance nobody could confirm.
* **Inserts and cash-outs.** Money crosses between the framework and the machine there, so both sides are written immediately. A crash cannot keep the money on both sides.

## Looking at the figures

From the console, or as staff in game:

```
lost_treasure_stats                    # your own, in game
lost_treasure_stats 12                 # a server id
lost_treasure_stats char1:abcd1234...  # a stored identifier, online or not
```

From another resource:

```lua
local stats = exports.lost_treasure:getPlayerStats(identifier)
-- credits, rounds, wagered, won, net, lost, deposited, cashedOut, biggestWin
```

The export returns `nil` without a database.
