Demo setup

This commit is contained in:
Mikko Ahlroth 2017-11-05 13:59:39 +02:00
parent e200a0611f
commit 788c9db781
5 changed files with 65 additions and 2 deletions

View file

@ -25,6 +25,29 @@ defmodule DuckTag.DB.Manager do
status: DuckTag.DB.Manager.player_status(),
score: integer
}
def add(name) do
Player.write(%Player{name: name, status: :wait_start, score: 0})
end
def add_score(%Player{} = player, score) do
Player.write(%{player | score: player.score + score})
end
end
deftable Device, [:rfid, :player_id], type: :set do
@type t :: %Device{
rfid: String.t(),
player_id: integer
}
def add(tag_id, player_id) do
if not Player.member?(player_id) do
Logger.error("Unknown player #{player_id}!")
else
Device.write(%Device{rfid: tag_id, player_id: player_id})
end
end
end
end
@ -53,6 +76,7 @@ defmodule DuckTag.DB.Manager do
Logger.debug("Stopping mnesia")
stop_db()
Process.sleep(1_000)
Logger.debug("Starting mnesia again, because mnesia likes to be used this way")
start_db()

View file

@ -1,6 +1,7 @@
defmodule DuckTag.Application do
use Application
import Supervisor.Spec, warn: false
require Logger
@doc """
Get the workers that should be started for the given role.
@ -21,6 +22,7 @@ defmodule DuckTag.Application do
def workers(:respawn) do
[
DuckTag.Player.JudgeAPI.child_spec(name: DuckTag.Player.JudgeAPI),
rfid_worker()
]
end
@ -42,6 +44,8 @@ defmodule DuckTag.Application do
# Define workers and child supervisors to be supervised
children = Enum.concat(workers(:common), workers(device_type))
Logger.debug("DuckTag.Supervisor starting up children:\n#{inspect(children)}")
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: DuckTag.Supervisor]

View file

@ -6,5 +6,11 @@ defmodule DuckTag.RFIDHandler do
def tag_scanned(uid) do
IO.puts("TAG SCANNED")
IO.inspect(uid)
tagged(DuckTag.role(), uid)
end
def tagged(:player, tag_id) do
DuckTag.Player.TagHandler.tagged(tag_id)
end
end

View file

@ -18,8 +18,8 @@ defmodule DuckTag.Player.JudgeAPI do
{:ok, try_connect()}
end
def handle_info(:retry_connect, {:connected, node}), do: {:ok, {:connected, node}}
def handle_info(:retry_connect, _state), do: {:ok, try_connect()}
def handle_info(:retry_connect, {:connected, node}), do: {:noreply, {:connected, node}}
def handle_info(:retry_connect, _state), do: {:noreply, try_connect()}
defp try_connect() do
remote_node = DuckTag.judge_node()

29
lib/player/tag_handler.ex Normal file
View file

@ -0,0 +1,29 @@
defmodule DuckTag.Player.TagHandler do
@moduledoc """
Tagging handler for player.
"""
alias DuckTag.DB.Manager.GameDB
use Amnesia
require Logger
@points_per_tag 100
def tagged(tag_id) do
Amnesia.transaction do
case GameDB.Device.read(tag_id) do
nil ->
Logger.debug("I was tagged by unknown tag #{tag_id}")
%GameDB.Device{} = device ->
case GameDB.Player.read(device.player_id) do
nil ->
Logger.debug("Tag #{tag_id} associated with nonexistent player #{device.player_id}")
%GameDB.Player{} = player ->
Logger.debug("I was tagged by #{player.name}. #{@points_per_tag} points!")
GameDB.Player.add_score(player, @points_per_tag)
end
end
end
end
end