# C API: Other players' avatars by id

Friends, lobby members and leaderboard rows by id: a gamertag (`Your Gamertag`, `gt:Your Gamertag`, `Name#1234`), a public slot (`a:<account id>/<slot>`) or a display case (`w:<case key>`), looked up through the accounts server's routes (`/sona/v1/gamertag/<gt>?client=<app>`, `/sona/a/<account>/<slot>`, `/sona/w/<key>` with `/sona/wv/<key>` for its version). Kept in memory and in `<cache_dir>/players.cache`, asked again after `refresh_s`, at most `per_hour` requests in any hour and `in_flight` at once, backed off on 429, 5xx and no network; the last known look, else the placeholder, when they fail. No ctx; owner thread only. The guide: [Other players' avatars by id](../../guides/players.md).

`sona_players_config` (136 bytes, 96 with 4-byte pointers): `{size, max_players, refresh_s, per_hour, in_flight, timeout_ms, host, client, http, cache_dir, placeholder, clock, clock_user, allocator, reserved[8]}`. `sona_player_look` (1200 bytes): `{size, state, error, placeholder, fetched_at, checked_at, next_check, manifest[1000], version[33], name[95], reserved[8]}`. `sona_player_event` (112 bytes): `{size, type, code, reserved, id[96]}`. `sona_players_stats` (96 bytes). States: `SONA_PLAYER_WAITING`, `_FRESH`, `_STALE`, `_PRIVATE`, `_NONE`. Events: `SONA_PLAYER_EVENT_LOOK`, `_GONE`, `_FAILED`. `sona_players` is opaque.

## sona_players_defaults

```c
void sona_players_defaults(sona_players_config *c);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `c` | `sona_players_config *` | The [`sona_players_config`](../fields.md#sona_players_config) to fill with the defaults | required |

**Returns** Nothing.

**Example**:

```c
sona_players_config x_players_config;
memset(&x_players_config, 0, sizeof x_players_config);
x_players_config.size = sizeof x_players_config;
sona_players_defaults(&x_players_config);
```

Zeroes the config and sets `size`; zero fields take the defaults (2048 looks, an interval of 600 s, 60 an hour, 4 at once, a timeout of 15 s). NULL does nothing.

## sona_players_open

```c
sona_result sona_players_open(const sona_players_config *c, sona_players **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `c` | `const sona_players_config *` | The options, a [`sona_players_config`](../fields.md#sona_players_config) | required |
| `out` | `sona_players **` | Receives the new players lookup | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_UNSUPPORTED`, `SONA_E_MEMORY`, `SONA_E_IO`, `SONA_E_CORRUPT` ([what each means](../../troubleshooting.md#error-codes)).

Reads the cache file when `cache_dir` is set (a file that does not decode is ignored). `SONA_E_ARGUMENT` for a config with a small `size`, a host that is not `http(s)://` (release builds: `https://` or loopback `http://`), `in_flight` over 16, or a placeholder that is not a manifest; `SONA_E_IO` when `cache_dir` cannot be made; `SONA_E_UNSUPPORTED` with no HTTP layer.

## sona_players_close

```c
void sona_players_close(sona_players *p);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `p` | `sona_players *` | The players lookup | required |

**Returns** Nothing.

Writes the cache file when it changed; answers still in flight are dropped. NULL does nothing.

## sona_players_watch

```c
sona_result sona_players_watch(sona_players *p, const char *const *ids, uint32_t count, int32_t priority);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `p` | `sona_players *` | The players lookup | required |
| `ids` | `const char *const *` | Player ids, `count` of them | required |
| `count` | `uint32_t` | How many entries the array holds | - |
| `priority` | `int32_t` | Higher is looked up sooner | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_LIMIT`, `SONA_E_MEMORY`, `SONA_E_APP` ([what each means](../../troubleshooting.md#error-codes)).

Watches ids and returns at once; known looks are there now, the rest are asked by priority (higher first), then in the order first watched, one request per distinct id. `SONA_E_ARGUMENT` when an id is not an id (it becomes `SONA_PLAYER_NONE`; the others are watched), `SONA_E_LIMIT` past `max_players`.

## sona_players_unwatch

```c
void sona_players_unwatch(sona_players *p, const char *const *ids, uint32_t count);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `p` | `sona_players *` | The players lookup | required |
| `ids` | `const char *const *` | Player ids, `count` of them | required |
| `count` | `uint32_t` | How many entries the array holds | - |

**Returns** Nothing.

Stops asking; looks stay cached. `ids` NULL: every id.

## sona_players_refresh

```c
sona_result sona_players_refresh(sona_players *p, const char *const *ids, uint32_t count);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `p` | `sona_players *` | The players lookup | required |
| `ids` | `const char *const *` | Player ids, `count` of them | required |
| `count` | `uint32_t` | How many entries the array holds | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

Asks again now, inside the limits. `ids` NULL: every watched id. `SONA_E_NOT_FOUND` for an id not watched.

## sona_players_update

```c
uint32_t sona_players_update(sona_players *p);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `p` | `sona_players *` | The players lookup | required |

**Returns** `uint32_t`.

Each frame: takes answers, sends due requests, writes the cache file (at most every 2 s). Returns the events queued.

## sona_players_poll

```c
int sona_players_poll(sona_players *p, sona_player_event *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `p` | `sona_players *` | The players lookup | required |
| `out` | `sona_player_event *` | Receives the [`sona_player_event`](../fields.md#sona_player_event) | required |

**Returns** `int`.

1 when an event was written, 0 when there are none. `LOOK` when a look arrives or changes, `GONE` when it turns private or goes, `FAILED` once when checks start failing (`code`: `SONA_E_IO`, `SONA_E_LIMIT`, `SONA_E_GATE`, `SONA_E_CORRUPT`, `SONA_E_APP`).

## sona_players_look

```c
sona_result sona_players_look(sona_players *p, const char *id, sona_player_look *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `p` | `sona_players *` | The players lookup | required |
| `id` | `const char *` | An id as text | required |
| `out` | `sona_player_look *` | Receives the [`sona_player_look`](../fields.md#sona_player_look) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

Always fills `out` with a look to draw (the placeholder when nothing is known). `SONA_E_NOT_FOUND` for an id never watched nor cached, `SONA_E_ARGUMENT` for one that is not an id.

## sona_players_forget

```c
sona_result sona_players_forget(sona_players *p, const char *id);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `p` | `sona_players *` | The players lookup | required |
| `id` | `const char *` | An id as text | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_NOT_FOUND` ([what each means](../../troubleshooting.md#error-codes)).

Drops the id's look from memory and, at the next write, from the cache file, and stops watching it. `SONA_E_NOT_FOUND` when there was none.

## sona_players_stats_get

```c
sona_result sona_players_stats_get(const sona_players *p, sona_players_stats *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `p` | `const sona_players *` | The players lookup | required |
| `out` | `sona_players_stats *` | Receives the [`sona_players_stats`](../fields.md#sona_players_stats) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

Looks kept, ids watched, requests in flight and due, and counts since open: requests, changed, the same, 304s, gone, failed, read from disk, disk writes, waits for `per_hour`; `paused_until`.

## sona_players_placeholder

```c
void sona_players_placeholder(uint8_t out[SONA_MANIFEST_BYTES]);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `out` | `uint8_t [SONA_MANIFEST_BYTES]` | Receives the manifest's 1,000 bytes | required |

**Returns** Nothing.

**Example**:

```c
uint8_t x_out[SONA_MANIFEST_BYTES];
memset(x_out, 0, sizeof x_out);
sona_players_placeholder(x_out);
```

The built-in placeholder: the site's faceless grey mannequin, nothing worn.
