Made game_state work
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Andre Henriques 2024-01-16 20:42:57 +00:00
parent 33281da43f
commit 47b02d37ec
2 changed files with 71 additions and 21 deletions

View File

@ -1,6 +1,6 @@
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')
@ -43,7 +43,7 @@ defmodule Utils do
end
def _log(msg, level) do
if (@min_print_level < level) do
if (@min_print_level <= level) do
IO.puts(msg)
end
end

View File

@ -1,6 +1,6 @@
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"
ast1 = quote do
@ -13,20 +13,42 @@ defmodule ServerMacros do
end
ast3 = ast1 ++ match_exp ++ ast2
quote do
def unquote(function_name)(v) do
var!(v) = v
unquote(process_exp)
receive do
unquote(ast3)
if after_exp != nil do
quote do
def unquote(function_name)(v) do
var!(v) = v
unquote(process_exp)
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
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
create_create_loop(name, do: exp, else: nil)
create_create_loop(name, do: exp, else: nil, after: nil)
end
defmacro create_loop(name, clauses) do
@ -41,6 +63,7 @@ defmodule ServerMacros do
ast2 = quote do
{:decision, v} ->
var!(state) = apply_game(var!(state), v)
var!(res) = v
unquote(recal_do)
v ->
@ -75,7 +98,7 @@ defmodule Server do
end
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 = %{
name: name,
procs: participants,
@ -96,19 +119,25 @@ defmodule Server do
def try_to_create_game(state, participants) do
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
new_game_state = [1, 1]
# TODO: randomize Initial hand value
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}
do
{: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, new_game_id}
else
log("HERE #{inspect(res)}")
try_to_create_game(state, participants)
end
end
@ -119,13 +148,19 @@ defmodule Server do
def apply_game(state, {:start_game, game_id, participants, new_game_state, hand}) do
if state.name in participants do
%{state | games: Map.put(state.games, game_id, %{
game_state: new_game_state,
participants: participants,
hand: hand[state.name]
})}
%{state |
games: Map.put(state.games, game_id, %{
game_state: new_game_state,
participants: participants,
hand: hand[state.name],
}),
instance: state.instance + 1
}
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
@ -136,8 +171,24 @@ defmodule Server do
############
create_loop :start_game do
{:start_game, game_id} ->
{:start_game_ans, 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
def start_game(name, participants) do
@ -157,5 +208,4 @@ defmodule Server do
def kill (pids) do
pids |> Enum.map(fn m -> Process.exit(m, :kill) end)
end
end