-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Arbitrary station positions (drag+drop and refactor) #93
Open
babbaj
wants to merge
23
commits into
otakulan:master
Choose a base branch
from
babbaj:swapping
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
05a2419
Use display order instead of id
babbaj 016eebf
add swapping stations
babbaj 9a5be4a
station layout table
babbaj b89eb41
db refactor/simplification
babbaj 79b3f2d
add association/join
babbaj 28d05a0
render with xy data
babbaj 08fc7c9
drag/drop events
babbaj 131f4e1
drag/drop to swap
babbaj 808f898
core functionality works
babbaj 867a262
ensure columns/rows can always contain the stations
babbaj a80a1cb
save settings
babbaj a04121d
save everything in one transaction
babbaj aac4e0f
save refactor
babbaj 4f2893d
reimpl layout configuration buttons
babbaj a556e39
fix invalid character error
babbaj 5649c31
reorder functions
babbaj 78329f0
buttons for resetting in column major or row major order
babbaj 831aad7
clean up a bit
babbaj c380fd8
fix all pages
babbaj 038d597
simplify stations_status
babbaj 1c6d42c
remove settings.station_count
babbaj 9bee8f8
keep state out of stations table
babbaj 4bb6a54
slight simplification
babbaj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
defmodule Lanpartyseating.StationLogic do | ||
import Ecto.Query | ||
alias Lanpartyseating.StationLayout | ||
use Timex | ||
alias Ecto.Changeset | ||
alias Lanpartyseating.PubSub, as: PubSub | ||
|
@@ -8,19 +9,24 @@ defmodule Lanpartyseating.StationLogic do | |
alias Lanpartyseating.Station, as: Station | ||
alias Lanpartyseating.TournamentReservation, as: TournamentReservation | ||
alias Lanpartyseating.Repo, as: Repo | ||
alias Lanpartyseating.StationLayout, as: Layout | ||
alias Lanpartyseating.StationStatus, as: Status | ||
|
||
def number_stations do | ||
Repo.aggregate(Station, :count) | ||
end | ||
|
||
def get_all_stations(now \\ DateTime.utc_now()) do | ||
def get_station_query(now \\ DateTime.utc_now()) do | ||
tournament_buffer = DateTime.add(DateTime.utc_now(), 45, :minute) | ||
|
||
stations = | ||
from(s in Station, | ||
order_by: [asc: s.id], | ||
from(s in Station, | ||
order_by: [asc: s.station_number], # only needed by autoassign | ||
where: is_nil(s.deleted_at), | ||
preload: [ | ||
stations_status: | ||
^from(Status), # may return nil | ||
station_layout: | ||
^from(Layout), | ||
reservations: | ||
^from( | ||
r in Reservation, | ||
|
@@ -39,7 +45,10 @@ defmodule Lanpartyseating.StationLogic do | |
) | ||
] | ||
) | ||
|> Repo.all() | ||
end | ||
|
||
def get_all_stations(now \\ DateTime.utc_now()) do | ||
stations = get_station_query(now) |> Repo.all() | ||
|
||
case stations do | ||
[] -> | ||
|
@@ -53,18 +62,32 @@ defmodule Lanpartyseating.StationLogic do | |
end | ||
end | ||
|
||
def get_station_layout() do | ||
rows = Repo.all(from(Layout)) | ||
Enum.map(rows, fn r -> {{r.x, r.y}, r.station_number} end) | ||
|> Enum.into(%{}) | ||
end | ||
|
||
def stations_by_xy(stations) do | ||
by_pos = stations | ||
|> Enum.map(fn s -> {{s.station.station_layout.x, s.station.station_layout.y}, s} end) | ||
|> Enum.into(%{}) | ||
|
||
{max_x, max_y} = Map.keys(by_pos) | ||
|> Enum.reduce({0, 0}, fn {acc_x, acc_y}, {x, y} -> {max(x, acc_x), max(y, acc_y)} end) | ||
|
||
# {columns, rows} | ||
{by_pos, {max_x + 1, max_y + 1}} | ||
end | ||
|
||
def set_station_broken(station_number, is_broken) do | ||
station = | ||
from(s in Station, | ||
where: s.station_number == ^station_number | ||
) |> Repo.one() | ||
|
||
station = | ||
Ecto.Changeset.change(station, | ||
is_closed: is_broken | ||
) | ||
result = Repo.insert( | ||
%Lanpartyseating.StationStatus{station_id: station_number, is_broken: is_broken}, | ||
on_conflict: [set: [is_broken: is_broken]], | ||
conflict_target: :station_id | ||
) | ||
|
||
with {:ok, update} <- Repo.update(station), | ||
with {:ok, update} <- result, | ||
{:ok, stations} <- StationLogic.get_all_stations() | ||
do | ||
Phoenix.PubSub.broadcast( | ||
|
@@ -79,98 +102,37 @@ defmodule Lanpartyseating.StationLogic do | |
end | ||
end | ||
|
||
def get_all_stations_sorted_by_number(now \\ DateTime.utc_now()) do | ||
tournament_buffer = DateTime.add(DateTime.utc_now(), 45, :minute) | ||
|
||
stations = | ||
from(s in Station, | ||
order_by: [asc: s.station_number], | ||
where: is_nil(s.deleted_at), | ||
preload: [ | ||
reservations: | ||
^from( | ||
r in Reservation, | ||
where: r.start_date <= ^now, | ||
where: r.end_date > ^now, | ||
where: is_nil(r.deleted_at), | ||
order_by: [desc: r.inserted_at] | ||
), | ||
tournament_reservations: | ||
^from(tr in TournamentReservation, | ||
join: t in assoc(tr, :tournament), | ||
where: t.start_date < ^tournament_buffer, | ||
where: t.end_date > ^now, | ||
where: is_nil(t.deleted_at), | ||
preload: [tournament: t] | ||
) | ||
] | ||
) | ||
|> Repo.all() | ||
|
||
stations_map = | ||
Enum.map(stations, fn station -> | ||
Map.merge(%{station: station}, get_station_status(station)) | ||
end) | ||
|
||
{:ok, stations_map} | ||
end | ||
|
||
def get_station(station_number, now \\ DateTime.utc_now()) do | ||
tournament_buffer = DateTime.add(DateTime.utc_now(), 45, :minute) | ||
|
||
station = from(s in Station, | ||
order_by: [asc: s.id], | ||
where: is_nil(s.deleted_at), | ||
where: s.station_number == ^station_number, | ||
preload: [ | ||
reservations: | ||
^from( | ||
r in Reservation, | ||
where: r.start_date <= ^now, | ||
where: r.end_date > ^now, | ||
where: is_nil(r.deleted_at), | ||
order_by: [desc: r.inserted_at] | ||
), | ||
tournament_reservations: | ||
^from(tr in TournamentReservation, | ||
join: t in assoc(tr, :tournament), | ||
where: t.start_date < ^tournament_buffer, | ||
where: t.end_date > ^now, | ||
where: is_nil(t.deleted_at), | ||
preload: [tournament: t] | ||
) | ||
] | ||
) | ||
|> Repo.one() | ||
station = get_station_query(now) |> Repo.one() | ||
|
||
case station do | ||
nil -> {:error, :station_not_found} | ||
_ -> {:ok, station} | ||
end | ||
end | ||
|
||
def save_station_positions(table) do | ||
Repo.delete_all(Station) | ||
|
||
def save_stations(grid) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename this function to something more clear because it does not actually save the stations as it is written now. |
||
now_naive = | ||
NaiveDateTime.truncate(NaiveDateTime.utc_now(), :second) | ||
|
||
positions = | ||
table | ||
|> Enum.flat_map(fn row -> | ||
row | ||
|> Enum.map(fn station_number -> | ||
%{station_number: station_number, display_order: station_number, inserted_at: now_naive, updated_at: now_naive} | ||
end) | ||
stations = grid | ||
|> Enum.map(fn {_xy, station_number} -> | ||
%{station_number: station_number, inserted_at: now_naive, updated_at: now_naive} | ||
end) | ||
|
||
Repo.insert_all(Station, positions) | ||
:ok | ||
layout = grid | ||
|> Enum.map(fn {{x, y}, num} -> %{station_number: num, x: x, y: y} end) | ||
|
||
Ecto.Multi.new() | ||
# because of the foreign key these need to be deleted and inserted specifically in this order | ||
|> Ecto.Multi.delete_all(:delete_stations, from(Lanpartyseating.Station)) | ||
|> Ecto.Multi.delete_all(:delete_layout, from(Lanpartyseating.StationLayout)) | ||
|> Ecto.Multi.insert_all(:insert_layout, Lanpartyseating.StationLayout, layout) | ||
|> Ecto.Multi.insert_all(:insert_stations, Station, stations) | ||
end | ||
|
||
def get_station_status(station) do | ||
case station do | ||
%Station{is_closed: true} -> | ||
%Station{stations_status: %{is_broken: true}} -> | ||
%{status: :broken, reservation: nil} | ||
|
||
%Station{tournament_reservations: [res | _]} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Lanpartyseating.StationLayout do | ||
alias Lanpartyseating.Station | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
@primary_key {:station_number, :integer, autogenerate: false} | ||
@foreign_key_type :integer | ||
|
||
schema "station_layout" do | ||
has_one :stations, Station, foreign_key: :station_number | ||
field :x, :integer | ||
field :y, :integer | ||
end | ||
|
||
@doc false | ||
def changeset(station, attrs) do | ||
station | ||
|> cast(attrs, [:station_number, :x, :y]) | ||
|> validate_required([:station_number, :x, :y]) | ||
|> validate_number(:x, greater_than: -1) | ||
|> validate_number(:y, greater_than: -1) | ||
|> unique_constraint(:x, :y) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap this in an
@doc """ """
string so it can be rendered out into generated documentation