Skip to content

Commit

Permalink
Rename Recorder.Row table to Replay
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Kendal committed Mar 3, 2017
1 parent b6b58d1 commit 4ec2d65
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 24 deletions.
Binary file modified databases/dev/schema.DAT
Binary file not shown.
6 changes: 6 additions & 0 deletions lib/battle_snake/replay.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ defmodule BattleSnake.Replay do
alias BattleSnake.Replay.Recorder
alias BattleSnake.Replay.PlayBack

require Mnesia.Row

Mnesia.Row.defrow(
[:id, :attributes],
[:id, :attributes])

@moduledoc """
Top level API for Replays.
"""
Expand Down
14 changes: 5 additions & 9 deletions lib/battle_snake/replay/play_back.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule BattleSnake.Replay.PlayBack do
use GenServer
alias BattleSnake.GameServer.PubSub
alias BattleSnake.Replay.Recorder.Row
alias BattleSnake.Replay
require BattleSnake.Replay
require Record
require Row

@attributes [:receiver, :replay_speed, :frames, :topic]
defstruct(@attributes)
Expand All @@ -22,11 +22,10 @@ defmodule BattleSnake.Replay.PlayBack do
########

def init(game_id) do
alias BattleSnake.Replay.Recorder.Row
alias BattleSnake.Replay

tab = Row
[row] = Mnesia.dirty_read(tab, game_id)
attributes = Row.row(row, :attributes)
[row] = Mnesia.dirty_read(Replay, game_id)
attributes = Replay.row(row, :attributes)
bin = Keyword.fetch!(attributes, :bin)
recorder = :erlang.binary_to_term(bin)
frames = recorder.frames
Expand Down Expand Up @@ -72,9 +71,6 @@ defmodule BattleSnake.Replay.PlayBack do
frame = %Frame{data: frame}
topic = topic

require Logger
Logger.debug "broadcasting to #{topic}"

PubSub.broadcast(topic, frame)

new_state = put_in state.frames, rest
Expand Down
18 changes: 5 additions & 13 deletions lib/battle_snake/replay/recorder.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
defmodule BattleSnake.Replay.Recorder.Row do
require Mnesia.Row

Mnesia.Row.defrow(
[:id, :attributes],
[:id, :attributes])
end

defmodule BattleSnake.Replay.Recorder do
alias BattleSnake.Replay.Recorder
alias BattleSnake.GameStateEvent
alias BattleSnake.Replay.Recorder.Row
alias BattleSnake.GameServer.PubSub
alias BattleSnake.GameStateEvent
alias BattleSnake.Replay
alias BattleSnake.Replay.Recorder
use GenServer

#############
Expand Down Expand Up @@ -96,8 +88,8 @@ defmodule BattleSnake.Replay.Recorder do
bin = :erlang.term_to_binary(rewound_state, [:compressed])
attributes = [bin: bin, created_at: created_at]

:ok = %Row{id: state.topic, attributes: attributes}
|> Row.struct2record
:ok = %Replay{id: state.topic, attributes: attributes}
|> Replay.struct2record
|> Mnesia.dirty_write

{:noreply, state, :hibernate}
Expand Down
4 changes: 2 additions & 2 deletions lib/mnesia/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ defmodule Mnesia do

BattleSnake.World.create_table(disc_copies: nodes)

attributes = BattleSnake.Replay.Recorder.Row.attributes()
attributes = BattleSnake.Replay.attributes()
:mnesia.create_table(
BattleSnake.Replay.Recorder.Row, [
BattleSnake.Replay, [
attributes: attributes,
disc_copies: nodes])

Expand Down

0 comments on commit 4ec2d65

Please sign in to comment.