   require BattleSnake.Replay
   require Record
 
+  @enforce_keys [:size, :buffer]
+
   defstruct([
-    :frame_buffer,
-    :watched_frame_buffer,
-    :receiver,
+    :buffer,
+    :size,
     :replay_speed,
     :topic,
+    pos: 0,
     state: :suspend
   ])
 

    attributes = Replay.row(row, :attributes)
    bin = Keyword.fetch!(attributes, :bin)
    recorder = :erlang.binary_to_term(bin)
    frame_buffer = recorder.frames
    topic = "replay:#{game_id}"
    replay_speed = 50
    init(topic, frame_buffer, self(), replay_speed)
  end

  def init(topic, frame_buffer, receiver, replay_speed) do
    new_state = struct(__MODULE__,
      topic: topic,
      replay_speed: replay_speed,
      receiver: receiver,
      watched_frame_buffer: [],
      frame_buffer: frame_buffer)

    Dbg.trace self(), [:r]
    {:ok, new_state}
  end


    time = state.replay_speed
    topic = state.topic

    {pos, new_state} = get_and_update_in(state.pos, &{&1, &1 - 1})
    frame = :array.get(pos, state.buffer)

    Process.send_after(self(), {:broadcast_prev_frame, :auto}, time)
    PubSub.broadcast(topic, %Frame{data: frame})
