This commit is contained in:
parent
33281da43f
commit
47b02d37ec
@ -1,6 +1,6 @@
|
|||||||
defmodule Utils do
|
defmodule Utils do
|
||||||
|
|
||||||
@min_print_level 0
|
@min_print_level 1
|
||||||
|
|
||||||
|
|
||||||
def safecast(p, m) when p == nil, do: IO.puts('Trying to safecast #{m} with p as nil')
|
def safecast(p, m) when p == nil, do: IO.puts('Trying to safecast #{m} with p as nil')
|
||||||
@ -43,7 +43,7 @@ defmodule Utils do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def _log(msg, level) do
|
def _log(msg, level) do
|
||||||
if (@min_print_level < level) do
|
if (@min_print_level <= level) do
|
||||||
IO.puts(msg)
|
IO.puts(msg)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
defmodule ServerMacros do
|
defmodule ServerMacros do
|
||||||
|
|
||||||
def create_create_loop(name, do: match_exp, else: process_exp) do
|
def create_create_loop(name, do: match_exp, else: process_exp, after: after_exp) do
|
||||||
function_name = :"#{name}_loop"
|
function_name = :"#{name}_loop"
|
||||||
|
|
||||||
ast1 = quote do
|
ast1 = quote do
|
||||||
@ -14,19 +14,41 @@ defmodule ServerMacros do
|
|||||||
|
|
||||||
ast3 = ast1 ++ match_exp ++ ast2
|
ast3 = ast1 ++ match_exp ++ ast2
|
||||||
|
|
||||||
quote do
|
if after_exp != nil do
|
||||||
def unquote(function_name)(v) do
|
quote do
|
||||||
var!(v) = v
|
def unquote(function_name)(v) do
|
||||||
unquote(process_exp)
|
var!(v) = v
|
||||||
receive do
|
unquote(process_exp)
|
||||||
unquote(ast3)
|
receive do
|
||||||
|
unquote(ast3)
|
||||||
|
after
|
||||||
|
unquote(after_exp)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
quote do
|
||||||
|
def unquote(function_name)(v) do
|
||||||
|
var!(v) = v
|
||||||
|
unquote(process_exp)
|
||||||
|
receive do
|
||||||
|
unquote(ast3)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_create_loop(name, do: exp, else: else_exp) do
|
||||||
|
create_create_loop(name, do: exp, else: else_exp, after: nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_create_loop(name, do: exp, after: after_exp) do
|
||||||
|
create_create_loop(name, do: exp, else: nil, after: after_exp)
|
||||||
|
end
|
||||||
|
|
||||||
def create_create_loop(name, do: exp) do
|
def create_create_loop(name, do: exp) do
|
||||||
create_create_loop(name, do: exp, else: nil)
|
create_create_loop(name, do: exp, else: nil, after: nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
defmacro create_loop(name, clauses) do
|
defmacro create_loop(name, clauses) do
|
||||||
@ -41,6 +63,7 @@ defmodule ServerMacros do
|
|||||||
ast2 = quote do
|
ast2 = quote do
|
||||||
{:decision, v} ->
|
{:decision, v} ->
|
||||||
var!(state) = apply_game(var!(state), v)
|
var!(state) = apply_game(var!(state), v)
|
||||||
|
var!(res) = v
|
||||||
unquote(recal_do)
|
unquote(recal_do)
|
||||||
|
|
||||||
v ->
|
v ->
|
||||||
@ -75,7 +98,7 @@ defmodule Server do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def init(name, participants) do
|
def init(name, participants) do
|
||||||
paxos = Paxos.start(alter_name(name, "_paxos"), Enum.map(participants, fn name -> alter_name(name, "_paxos") end))
|
paxos = Paxos.start(alter_name(name, "_paxos"), Enum.map(participants, fn name -> alter_name(name, "_paxos") end), true)
|
||||||
state = %{
|
state = %{
|
||||||
name: name,
|
name: name,
|
||||||
procs: participants,
|
procs: participants,
|
||||||
@ -96,19 +119,25 @@ defmodule Server do
|
|||||||
|
|
||||||
def try_to_create_game(state, participants) do
|
def try_to_create_game(state, participants) do
|
||||||
game_ids = Map.keys(state.games)
|
game_ids = Map.keys(state.games)
|
||||||
new_game_id = Enum.at(Enum.sort(game_ids), length(game_ids) - 1) + 1
|
latest = Enum.at(Enum.sort(game_ids), length(game_ids) - 1)
|
||||||
|
log("#{inspect(latest)}")
|
||||||
|
new_game_id = if latest do latest else 0 end + 1
|
||||||
|
log("#{inspect(new_game_id)}")
|
||||||
|
|
||||||
# TODO: randomize game state
|
# TODO: randomize game state
|
||||||
new_game_state = [1, 1]
|
new_game_state = [1, 1]
|
||||||
# TODO: randomize Initial hand value
|
# TODO: randomize Initial hand value
|
||||||
hand = Enum.reduce(participants, %{}, fn p, acc -> Map.put(acc, p, 1) end)
|
hand = Enum.reduce(participants, %{}, fn p, acc -> Map.put(acc, p, 1) end)
|
||||||
|
|
||||||
|
res = nil
|
||||||
|
|
||||||
try_propose {:start_game, new_game_id, participants, new_game_state, hand}
|
try_propose {:start_game, new_game_id, participants, new_game_state, hand}
|
||||||
do
|
do
|
||||||
{:decision, {:start_game, ^new_game_id, ^participants, ^new_game_state, ^hand}} ->
|
{:decision, {:start_game, ^new_game_id, ^participants, ^new_game_state, ^hand}} ->
|
||||||
state = apply_game(state, {:start_game, new_game_id, participants, new_game_state, hand})
|
state = apply_game(state, {:start_game, new_game_id, participants, new_game_state, hand})
|
||||||
{state, new_game_id}
|
{state, new_game_id}
|
||||||
else
|
else
|
||||||
|
log("HERE #{inspect(res)}")
|
||||||
try_to_create_game(state, participants)
|
try_to_create_game(state, participants)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -119,13 +148,19 @@ defmodule Server do
|
|||||||
|
|
||||||
def apply_game(state, {:start_game, game_id, participants, new_game_state, hand}) do
|
def apply_game(state, {:start_game, game_id, participants, new_game_state, hand}) do
|
||||||
if state.name in participants do
|
if state.name in participants do
|
||||||
%{state | games: Map.put(state.games, game_id, %{
|
%{state |
|
||||||
game_state: new_game_state,
|
games: Map.put(state.games, game_id, %{
|
||||||
participants: participants,
|
game_state: new_game_state,
|
||||||
hand: hand[state.name]
|
participants: participants,
|
||||||
})}
|
hand: hand[state.name],
|
||||||
|
}),
|
||||||
|
instance: state.instance + 1
|
||||||
|
}
|
||||||
else
|
else
|
||||||
%{state | games: Map.put(state.games, game_id, :not_playing_in_it)}
|
%{state |
|
||||||
|
games: Map.put(state.games, game_id, :not_playing_in_it),
|
||||||
|
instance: state.instance + 1,
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -136,8 +171,24 @@ defmodule Server do
|
|||||||
############
|
############
|
||||||
|
|
||||||
create_loop :start_game do
|
create_loop :start_game do
|
||||||
{:start_game, game_id} ->
|
{:start_game_ans, game_id} ->
|
||||||
log("Started a game #{game_id}")
|
log("Started a game #{game_id}")
|
||||||
|
{:start_game, game_id}
|
||||||
|
after
|
||||||
|
1000 -> :timeout
|
||||||
|
end
|
||||||
|
|
||||||
|
def start_game_loop() do
|
||||||
|
receive do
|
||||||
|
{:start_game_ans, game_id} ->
|
||||||
|
log("Started a game #{game_id}")
|
||||||
|
{:start_game, game_id}
|
||||||
|
v ->
|
||||||
|
Process.send_after(self(), v, 1000)
|
||||||
|
start_game_loop()
|
||||||
|
after
|
||||||
|
1000 -> :timeout
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def start_game(name, participants) do
|
def start_game(name, participants) do
|
||||||
@ -157,5 +208,4 @@ defmodule Server do
|
|||||||
def kill (pids) do
|
def kill (pids) do
|
||||||
pids |> Enum.map(fn m -> Process.exit(m, :kill) end)
|
pids |> Enum.map(fn m -> Process.exit(m, :kill) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user