Added BiddingRoundServer "CurrentRoundServer" with a facade to control it. "Listing Biddings" page shows information which round is currently running.
This commit is contained in:
118
lib/beet_round_server/bidding_rounds/bidding_round_facade.ex
Normal file
118
lib/beet_round_server/bidding_rounds/bidding_round_facade.ex
Normal file
@ -0,0 +1,118 @@
|
||||
defmodule BeetRoundServer.BiddingRounds.BiddingRoundFacade do
|
||||
alias BeetRoundServer.BiddingRounds.BiddingRound
|
||||
alias BeetRoundServer.BiddingRounds
|
||||
alias BeetRoundServer.BiddingRounds.BiddingRoundServer
|
||||
|
||||
def restart_if_necessary() do
|
||||
last_round = get_highest_bidding_round()
|
||||
|
||||
if last_round.stopped == false do
|
||||
IO.puts("There is a last round, that wasn't stopped. Should be running...")
|
||||
|
||||
if !isAlive() do
|
||||
IO.puts("...but it isn't. Restarting last round...")
|
||||
restart_hightest_round()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def get_highest_bidding_round() do
|
||||
last_round = BiddingRounds.get_highest_bidding_round!()
|
||||
|
||||
if last_round != nil do
|
||||
last_round
|
||||
else
|
||||
%BiddingRound{round_number: 0, stopped: true, id: "00000000-0000-0000-0000-000000000000"}
|
||||
end
|
||||
end
|
||||
|
||||
def get_current_round do
|
||||
restart_if_necessary()
|
||||
|
||||
if GenServer.whereis(CurrentRoundServer) == nil do
|
||||
IO.puts("CurrentRoundServer isn't alive. Returning 0...")
|
||||
# %BiddingRound{round_number: 0, stopped: true, id: "00000000-0000-0000-0000-000000000000"}
|
||||
0
|
||||
else
|
||||
GenServer.call(CurrentRoundServer, :val)
|
||||
end
|
||||
end
|
||||
|
||||
def start_new_round() do
|
||||
if isAlive() do
|
||||
IO.puts("CurrentRoundServer is alive! Please stop the server before starting a new round")
|
||||
{:error, "A current round is running! Please stop it, before starting a new round."}
|
||||
else
|
||||
IO.puts("CurrentRoundServer isn't alive. Starting instance...")
|
||||
|
||||
last_round = BiddingRounds.get_highest_bidding_round!()
|
||||
|
||||
cond do
|
||||
last_round == nil ->
|
||||
IO.puts("No bidding round found. Starting first round...")
|
||||
|
||||
round_number = 1
|
||||
|
||||
BiddingRoundServer.start(round_number)
|
||||
BiddingRounds.create_bidding_round(%{round_number: round_number})
|
||||
|
||||
last_round.stopped == false ->
|
||||
IO.puts("Last bidding round not stopped. Restarting round...")
|
||||
|
||||
BiddingRoundServer.start(last_round.round_number)
|
||||
|
||||
true ->
|
||||
IO.puts("Last bidding round has stopped. Starting a new round...")
|
||||
|
||||
round_number = last_round.round_number + 1
|
||||
|
||||
BiddingRoundServer.start(round_number)
|
||||
BiddingRounds.create_bidding_round(%{round_number: round_number})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def restart_hightest_round() do
|
||||
IO.puts("Restarting hightest round...")
|
||||
|
||||
if isAlive() do
|
||||
IO.puts("Server is alive. Nothing to do...")
|
||||
IO.puts(["Current round: ", GenServer.call(CurrentRoundServer, :val)])
|
||||
else
|
||||
IO.puts("Server isn't alive. Trying to restart last round.")
|
||||
|
||||
last_round = BiddingRounds.get_highest_bidding_round!()
|
||||
|
||||
cond do
|
||||
last_round == nil ->
|
||||
IO.puts("No bidding round found! Can't restart round...")
|
||||
{:error, "No bidding round found! Nothing to restart."}
|
||||
|
||||
true ->
|
||||
IO.puts("Last bidding round found. Restarting...")
|
||||
|
||||
BiddingRoundServer.start(last_round.round_number)
|
||||
BiddingRounds.update_bidding_round(last_round, %{stopped: false})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def stop_current_round() do
|
||||
IO.puts("Stopping current round...")
|
||||
|
||||
if isAlive() do
|
||||
IO.puts("Server is alive. Shutting down and writing to DB...")
|
||||
current_round_number = GenServer.call(CurrentRoundServer, :val)
|
||||
GenServer.stop(CurrentRoundServer)
|
||||
|
||||
current_round = BiddingRounds.get_bidding_round_by_number!(current_round_number)
|
||||
BiddingRounds.update_bidding_round(current_round, %{stopped: true})
|
||||
else
|
||||
IO.puts("Server isn't alive. Nothing to shut down.")
|
||||
end
|
||||
end
|
||||
|
||||
def isAlive() do
|
||||
GenServer.whereis(CurrentRoundServer) != nil
|
||||
end
|
||||
end
|
||||
39
lib/beet_round_server/bidding_rounds/bidding_round_server.ex
Normal file
39
lib/beet_round_server/bidding_rounds/bidding_round_server.ex
Normal file
@ -0,0 +1,39 @@
|
||||
defmodule BeetRoundServer.BiddingRounds.BiddingRoundServer do
|
||||
use GenServer
|
||||
def inc(pid), do: GenServer.cast(pid, :inc)
|
||||
def dec(pid), do: GenServer.cast(pid, :dec)
|
||||
|
||||
def val(pid) do
|
||||
GenServer.call(pid, :val)
|
||||
end
|
||||
|
||||
def stop(pid) do
|
||||
GenServer.stop(pid)
|
||||
end
|
||||
|
||||
def start(initial_val) do
|
||||
GenServer.start(__MODULE__, initial_val, name: CurrentRoundServer)
|
||||
end
|
||||
|
||||
def init(initial_val) do
|
||||
{:ok, initial_val}
|
||||
end
|
||||
|
||||
def terminate(_reason, val) do
|
||||
IO.puts("Stopping bidding round:")
|
||||
IO.puts(val)
|
||||
:ok
|
||||
end
|
||||
|
||||
def handle_cast(:inc, val) do
|
||||
{:noreply, val + 1}
|
||||
end
|
||||
|
||||
def handle_cast(:dec, val) do
|
||||
{:noreply, val - 1}
|
||||
end
|
||||
|
||||
def handle_call(:val, _from, val) do
|
||||
{:reply, val, val}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user