119 lines
3.6 KiB
Elixir
119 lines
3.6 KiB
Elixir
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
|