# Time played

Each game counts how long the player has spent in it, across every save, and the
Hub shows the total in Game details. It is kept apart from saves: deleting a
save never takes its time away.

## Storage: `localStorage`

Key: `uniplay:playtime:<game id>`. Value: JSON.

```json
{ "v": 1, "seconds": 15120, "lastPlayed": 1790000000000 }
```

- `seconds` is the whole seconds played. `lastPlayed` is the last write, in
  milliseconds since epoch.
- Time counts only while the game's page is visible and the Hub's overlay is
  closed. A single gap between samples counts as at most 10 seconds, so a computer
  asleep with the game open adds nothing.
- Writers add what they counted since their last write: re-read the key right
  before writing and add to it, never overwrite, because the same game may be open
  in two tabs. Write at least every 30 seconds while playing and when the page hides,
  closes or the Hub exits the game.
- Readers must ignore unknown fields and treat a missing, unparsable or malformed
  key as nothing played.
- Storage can be unavailable or full. Games must keep working; the SDK keeps
  counting and writes the time later.

## With the SDK

The SDK does all of this on its own once the game creates its `Uniplay` instance
(`uniplay.playtime.seconds` reads the total). Each game writes only its own key. The
Hub only reads it, and never deletes it with a save.

```ts
const uniplay = new Uniplay({ game: 'heated-heist' }); // counting starts here
console.log(uniplay.playtime.seconds);
```
