# Exports and events

Server-side hooks for adding to incidents and writing narratives. The MDT itself listens to c3r's [events](/docs/c3r/api#server-events) and voice dispatch's [`lwk_control:ran`](/docs/lwk_control/api#events).

## Server exports

### AddDialogue

Adds a line of conversation to the officer's open incident. c3r's scripted conversations already go here through `c3r:dialogue`.

```lua
exports.lwk_mdt:AddDialogue(src, who, text)
```

| Parameter | Type | What it is |
|---|---|---|
| `src` | `number` | The officer's server id |
| `who` | `string` | Who said it, up to 20 characters, e.g. `'Officer'` |
| `text` | `string` | The line, up to 300 characters |

Nothing happens if the officer has no open incident. An incident keeps its last 100 lines.

### GetPersonHistory

A person's earlier contacts, newest first, at most 5. Lets an AI subject say "not you again".

```lua
local history = exports.lwk_mdt:GetPersonHistory(src, person)
```

| Parameter | Type | What it is |
|---|---|---|
| `src` | `number` | The officer's server id |
| `person` | `table` | A c3r [person record](/docs/c3r/api#person) |

Returns a list of earlier incidents (the current one isn't counted), each marked with whether it was this officer's report. Empty if they're not on file.

### GetReportDraft

The automatic narrative for a freshly drafted report, so another resource can rewrite it in prose.

```lua
local narrative, hasDialogue, timelineLength = exports.lwk_mdt:GetReportDraft(id)
```

Returns `nil` unless the report is still an untouched draft.

### SetNarrative

Replaces a draft's narrative. It's then frozen, like an officer's own edit.

```lua
local ok = exports.lwk_mdt:SetNarrative(id, text) -- text up to 6000 characters
```

Returns `false` if the report isn't an untouched draft: an officer's edit or signature always wins.

## Server events

### lwk_mdt:reportDrafted

Fires when an incident closes and its draft is ready.

```lua
AddEventHandler('lwk_mdt:reportDrafted', function(id, src)
    local draft = exports.lwk_mdt:GetReportDraft(id)
    if draft then
        exports.lwk_mdt:SetNarrative(id, 'On the above date and time I conducted a traffic stop...')
    end
end)
```

## Events the MDT listens to

| Event | From | Becomes |
|---|---|---|
| `c3r:stopStarted`, `c3r:pursuitStarted`, `c3r:calloutAccepted` | c3r | A new incident |
| `c3r:contact`, `c3r:vehicleContact` | c3r | People and vehicles on the incident |
| `c3r:dialogue` | c3r | The conversation |
| `c3r:note` | c3r | A timeline line (test results, searches) |
| `c3r:stopEnded`, `c3r:pursuitEnded`, `c3r:calloutEnded` | c3r | The incident closes and drafts |
| `lwk_control:ran` | voice dispatch | Radio actions on the timeline |

To put your own line on an officer's timeline, fire `c3r:note` on the server:

```lua
TriggerEvent('c3r:note', src, 'K9 unit alerted on the driver side door')
```
