# Arena Session

Unity Core can request a short-lived Arena Gateway credential through either an
optional in-memory Direct login or the currently active Host Session. The
public result and method are identical for both sources.

## Prerequisites

1. Set `IGPConfig.appId` to the platform-assigned game ID.
2. Choose either source:
   - explicitly call the phone SMS login APIs; or
   - initialize Core and let the active Desktop or Mobile Host attach with a
     signed-in user and Arena capability 11.
3. Request the session without passing an app ID, user identity, or account token.

Direct login is optional and does not require `InitializeAsync()`:

```csharp
await runtime.SendPhoneSignInCodeAsync(phone, captchaVerifyParam);
await runtime.SignInWithPhoneCodeAsync(phone, verificationCode, inviteCode);

IGPArenaSession session =
    await runtime.RequestArenaSessionAsync(cancellationToken);
```

If the game does not perform Direct login, prepare the active Host first:

```csharp
if (!runtime.IsInitialized && !await runtime.InitializeAsync())
{
    throw new InvalidOperationException("IGP Core initialization failed.");
}

IGPArenaSession session = await runtime.RequestArenaSessionAsync(cancellationToken);
```

The result contains the attached `AppId`, Gateway `ServerUrl`, short-lived
`Token`, UTC expiry, and the attached player's ID, name, and optional avatar.
The Direct path derives `gameId` from the configured app ID and binds the player
to the Direct account. The Host path binds both identities to its validated
attached session.

At call start, Core checks `HasUserSession`. When true, it selects Direct and
calls Curio `POST /arena/session`, using the internal account access token and a
numeric `gameId`. When false, it checks the active Host gateway. The source is
not reconsidered after the request starts: a Direct `401` refresh failure,
permission failure, timeout, network failure, cancellation, or invalid response
does not fall back to Host. `SignOut()` clears the Direct session, so a later
call may use Host. `HasUserSession` reports only Direct state; Host login never
sets it.

## Public Data Structures

Both result types are immutable and live in `IGP.UnitySDK.Models`.

| Type/field | Type | Required/default | Meaning and lifecycle |
| --- | --- | --- | --- |
| `IGPArenaSession.AppId` | `int` | Required; positive | Configured Direct game ID or attached Host app ID, validated against the selected source. |
| `IGPArenaSession.ServerUrl` | `string` | Required; absolute HTTP(S) URL | Normalized Arena Gateway base URL. Query and fragment components are removed. |
| `IGPArenaSession.Token` | `string` | Required; nonblank | Short-lived signed credential. Memory-only and excluded from Newtonsoft serialization and `ToString()`. |
| `IGPArenaSession.ExpiresAtUtc` | `DateTimeOffset` | Required; future UTC timestamp | Credential expiry. Request another Arena Session before constructing a new client after expiry. |
| `IGPArenaSession.Player` | `IGPArenaSessionPlayer` | Required | Player identity signed into the selected Direct or Host source. |
| `IGPArenaSessionPlayer.Id` | `string` | Required; nonblank | Stable user ID bound into the signed credential. |
| `IGPArenaSessionPlayer.Name` | `string` | Required; nonblank | Display name supplied for room presentation. |
| `IGPArenaSessionPlayer.Avatar` | `string?` | Optional; defaults to `null` | Avatar URL or identifier when the selected source provides one. |

The object is a point-in-time handoff. Core does not retain it, mutate it, or
replace its token. Every explicit call requests a short-lived credential from
the source selected for that call. Desktop and Mobile use the same Host command;
a Host that has not implemented it advertises capability 11 as unavailable.

Core does not cache, persist, refresh, log, or automatically consume the Arena
credential. Request a new value when needed and pass the fields explicitly to
the chosen Gateway control-plane client. The client remains responsible for its
own construction and lifecycle.

`IGPArenaSession.ToString()` redacts the credential, and the Core model excludes
the token from Newtonsoft JSON serialization. Game code must also keep the
token out of logs, diagnostics, scenes, assets, PlayerPrefs, and other storage.

Expected failures:

| Code | Meaning |
| --- | --- |
| `USER_SESSION_REQUIRED` | No Direct session exists and the active Host is unattached, signed out, or lacks Arena capability. |
| `APP_ID_REQUIRED` | The Direct path cannot resolve a positive app ID. |
| Direct/server error code | The selected Direct request failed; the same request does not use Host. |
| Host command error code | The active Host command failed; the same request does not use another source. |
| `ARENA_SESSION_INVALID_RESPONSE` | The selected source returned an invalid, expired, or identity-mismatched session. |
