more work on server
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Andre Henriques 2024-01-18 00:29:02 +00:00
parent 2a8dfb787e
commit 03af54b32f

View File

@ -17,7 +17,7 @@ defmodule ServerMacros do
ast3 = ast1 ++ match_exp ++ ast2 ast3 = ast1 ++ match_exp ++ ast2
quote do quote do
def unquote(function_name)(v, t) do defp unquote(function_name)(v, t) do
var!(v) = v var!(v) = v
unquote(process_exp) unquote(process_exp)
receive do receive do
@ -73,7 +73,7 @@ defmodule Server do
require Utils require Utils
import Utils import Utils
create_log 3 create_log 2
def start(name, participants) do def start(name, participants) do
log("starting server") log("starting server")
@ -110,7 +110,7 @@ defmodule Server do
end end
def try_to_play_checks(state, game_id, move, pid_to_inform, repeat \\ false) do defp try_to_play_checks(state, game_id, move, pid_to_inform, repeat \\ false) do
cond do cond do
state.games[game_id] == :not_playing_in_game -> state.games[game_id] == :not_playing_in_game ->
safecast(pid_to_inform, {:make_move, game_id, :not_playing}) safecast(pid_to_inform, {:make_move, game_id, :not_playing})
@ -142,7 +142,7 @@ defmodule Server do
end end
end end
def try_to_play(state, game_id, move, pid_to_inform) do defp try_to_play(state, game_id, move, pid_to_inform) do
name = state.name name = state.name
new_hand = get_hand_for_game_state(state.games[game_id].game_state) new_hand = get_hand_for_game_state(state.games[game_id].game_state)
try_propose {:make_move, game_id, name, move, new_hand} try_propose {:make_move, game_id, name, move, new_hand}
@ -177,7 +177,7 @@ defmodule Server do
end end
end end
def get_game_state(state, game_id, pid_to_inform, repeat \\ false) do defp get_game_state(state, game_id, pid_to_inform, repeat \\ false) do
cond do cond do
state.games[game_id] == :not_playing_in_game -> state.games[game_id] == :not_playing_in_game ->
safecast(pid_to_inform, {:game_state, game_id, :not_playing}) safecast(pid_to_inform, {:game_state, game_id, :not_playing})
@ -205,7 +205,7 @@ defmodule Server do
end end
end end
def get_hand_for_game_state(game_state) do defp get_hand_for_game_state(game_state) do
r1 = Enum.random(0..100) r1 = Enum.random(0..100)
if r1 <= 10 do if r1 <= 10 do
:+ :+
@ -217,7 +217,7 @@ defmodule Server do
end end
end end
def try_to_create_game(state, participants) do defp try_to_create_game(state, participants) do
game_ids = Map.keys(state.games) game_ids = Map.keys(state.games)
latest = Enum.at(Enum.sort(game_ids), length(game_ids) - 1) latest = Enum.at(Enum.sort(game_ids), length(game_ids) - 1)
new_game_id = if latest do latest else 0 end + 1 new_game_id = if latest do latest else 0 end + 1
@ -239,14 +239,14 @@ defmodule Server do
# Utils # Utils
# #
def is_finished(state, game) do defp is_finished(state, game) do
case state.games[game] do case state.games[game] do
{:finished, _} -> true {:finished, _} -> true
_ -> false _ -> false
end end
end end
def qurey_status(state) do defp qurey_status(state) do
v = Paxos.get_decision(state.paxos, state.instance, 100) v = Paxos.get_decision(state.paxos, state.instance, 100)
or_state v != nil do or_state v != nil do
state = apply_game(state, v) state = apply_game(state, v)
@ -254,7 +254,7 @@ defmodule Server do
end end
end end
def set_modifed(state, game, val \\ false) do defp set_modifed(state, game, val \\ false) do
or_state not is_finished(state, game) do or_state not is_finished(state, game) do
%{state | games: Map.put(state.games, game, %{state.games[game] | modified: val})} %{state | games: Map.put(state.games, game, %{state.games[game] | modified: val})}
end end
@ -264,7 +264,7 @@ defmodule Server do
# Apply Game States # Apply Game States
# #
def get_index(indexed_game_state, spos, index) do defp get_index(indexed_game_state, spos, index) do
index = spos + index index = spos + index
len = length(indexed_game_state) len = length(indexed_game_state)
cond do cond do
@ -277,8 +277,8 @@ defmodule Server do
end end
end end
def simplify_game_state_pluses([], indexed_game_state), do: {false, indexed_game_state} defp simplify_game_state_pluses([], indexed_game_state), do: {false, indexed_game_state}
def simplify_game_state_pluses([{:+, i} | tl], indexed_game_state) do defp simplify_game_state_pluses([{:+, i} | tl], indexed_game_state) do
before_i = get_index(indexed_game_state, i, -1) before_i = get_index(indexed_game_state, i, -1)
after_i = get_index(indexed_game_state, i, 1) after_i = get_index(indexed_game_state, i, 1)
@ -310,14 +310,14 @@ defmodule Server do
end end
end end
def is_merged(item) do defp is_merged(item) do
case item do case item do
{:merged, _} -> true {:merged, _} -> true
_ -> false _ -> false
end end
end end
def expand_merge(indexed_game_state) do defp expand_merge(indexed_game_state) do
{{:merged, n}, i} = indexed_game_state |> Enum.find(fn {x, _} -> is_merged(x) end) {{:merged, n}, i} = indexed_game_state |> Enum.find(fn {x, _} -> is_merged(x) end)
b_i = get_index(indexed_game_state, i, - 1) b_i = get_index(indexed_game_state, i, - 1)
@ -350,7 +350,7 @@ defmodule Server do
end end
end end
def reindex(list, flat \\ true) do defp reindex(list, flat \\ true) do
list = if flat do list = if flat do
list |> Enum.map(fn {n, _} -> n end) list |> Enum.map(fn {n, _} -> n end)
else else
@ -360,9 +360,9 @@ defmodule Server do
[list, 0..(length(list) - 1)] |> Enum.zip() [list, 0..(length(list) - 1)] |> Enum.zip()
end end
def remove_merged([], rec, _), do: rec defp remove_merged([], rec, _), do: rec
def remove_merged([{:merged, n} | tl], rec, add) do defp remove_merged([{:merged, n} | tl], rec, add) do
if add do if add do
remove_merged(tl, rec ++ [n], false) remove_merged(tl, rec ++ [n], false)
else else
@ -370,15 +370,15 @@ defmodule Server do
end end
end end
def remove_merged([n | tl], rec, add), do: defp remove_merged([n | tl], rec, add), do:
remove_merged(tl, rec ++ [n], add) remove_merged(tl, rec ++ [n], add)
def remove_merged(list) do defp remove_merged(list) do
log("#{inspect(list)}") log("#{inspect(list)}")
remove_merged(list, [], true) remove_merged(list, [], true)
end end
def simplify_game_state(game_state) do defp simplify_game_state(game_state) do
log("game_state: #{inspect(game_state)}") log("game_state: #{inspect(game_state)}")
indexed_game_state = indexed_game_state =
game_state |> game_state |>
@ -405,7 +405,7 @@ defmodule Server do
end end
end end
def apply_game(state, {:make_move, game_id, player_name, pos_move, new_hand}) do defp apply_game(state, {:make_move, game_id, player_name, pos_move, new_hand}) do
game = state.games[game_id] game = state.games[game_id]
case game do case game do
{:finished, _} -> {:finished, _} ->
@ -428,7 +428,7 @@ defmodule Server do
end end
end end
def apply_game(state, {:start_game, game_id, participants, new_game_state, hand}) do defp apply_game(state, {:start_game, game_id, participants, new_game_state, hand}) do
cond do cond do
state.games[game_id] -> state.games[game_id] ->
raise "Game Already Exists" raise "Game Already Exists"
@ -450,7 +450,7 @@ defmodule Server do
end end
end end
def apply_game(_, _), do: raise "Do not know how to apply game state" defp apply_game(_, _), do: raise "Do not know how to apply game state"
############ ############
# Interface # Interface
@ -513,7 +513,6 @@ defmodule Server do
make_move_loop(game_id, 10000) make_move_loop(game_id, 10000)
end end
############ ############
# Debug # Debug
############ ############
@ -527,3 +526,95 @@ defmodule Server 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
defmodule Client do
import Utils
require Utils
create_log 3
def play_game(process, game_id) do
end
def to_name(list) when is_list(list), do: list |> Enum.map(fn x -> to_name(x) end)
def to_name(atom) when is_atom(atom), do: atom
def to_name(i) do
[ "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm", "Md", "No", "Lr", "Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds", "Rg", "Cn", "Nh", "Fl", "Mc", "Lv", "Ts", "Og" ] |>
Enum.at(i - 1)
end
def interpolate(list) do
[list, 0..length(list) - 1] |>
Enum.zip() |>
Enum.reduce([], fn {v, i}, acc -> acc ++ [i, v] end) |>
Enum.map(fn x -> String.pad_leading("#{x}", 2, " ") end)
end
def printpt(game_state) do
rad = 8;
printpt1(game_state, 0, rad)
end
defp count_char(str, char) do
String.split(str, "") |> Enum.reduce(0, fn x, acc -> if x == char do acc + 1 else acc end end)
end
def printpt1(_, i, rad) when i > rad * 2, do: nil
def printpt1(game_state, i, rad) do
res = printpt2(game_state, i, -5, rad, "")
" xxx \n"
" xxx yyy \n"
" xxx yyy \n"
" xxx yyy \n"
" xxx yyy \n"
" xxx yyy \n"
" xxx yyy \n"
" xxx yyy \n"
" xxx yyy \n"
" xxx yyy \n"
" xxx yyy \n"
" xxx yyy \n"
" xxx yyy \n"
" xxx yyy \n"
" xxx yyy \n"
" xxx \n"
if i != 15 do
res = case i do
0 ->
x = count_char(res, "x")
log("#{x}")
#TODO
res
16 ->
#TODO
res
v ->
#TODO
res
end
IO.write(" #{res} \n")
end
printpt1(game_state, i + 1, rad)
end
def printpt2(_, _, j, rad, res) when j > rad * 4 + 10, do: res
def printpt2(game_state, i, j, rad, res) do
dist = :math.sqrt((i - rad)*(i - rad) + (j / 2 - rad)*(j / 2 - rad));
v = if (dist > rad - 1 and dist < rad + 1) do
case i do
0 -> "x"
16 -> "x"
_ -> if j < rad * 2 do "x" else "y" end
end
else
" "
end
printpt2(game_state, i, j + 1, rad, res <> v)
end
end