# Shot logger

Logs every shot the match takes and keeps a running count in the database, so it survives reloads. Uses the `shot_fired`, `ball_pocketed` and `match_end` events.

```lua
-- running totals, restored from last session
local shots = database.read("shots") or 0
local potted = database.read("potted") or 0

client.set_event_callback("shot_fired", function(e)
  shots = shots + 1
  database.write("shots", shots)
  -- angle is radians; show degrees, and power as a percentage
  client.color_log(90, 170, 255,
    string.format("shot %d: %.0f deg @ %d%%%s",
      shots,
      math.deg(e.angle),
      math.floor(e.power * 100 + 0.5),
      e.autoplay and " (auto)" or ""))
end)

client.set_event_callback("ball_pocketed", function(e)
  potted = potted + 1
  database.write("potted", potted)
  client.log(string.format("potted ball %d into pocket %d", e.number, e.pocket))
end)

client.set_event_callback("match_end", function(e)
  client.color_log(
    e.won and 80 or 255, e.won and 255 or 80, 80,
    string.format("match over: %s, %d coins. lifetime %d shots, %d potted",
      e.won and "won" or "lost", e.coins, shots, potted))
end)
```