Skip to content

Commit

Permalink
keep building id as binary_id, add integer order field, rewrite migra…
Browse files Browse the repository at this point in the history
…tion history
  • Loading branch information
goncalotomas committed Jan 27, 2024
1 parent 8c8c601 commit 14b2f4f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/galaxies/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ defmodule Galaxies.Accounts do
current_level: planet_building.current_level,
upgrade_cost_formula: building.upgrade_cost_formula
},
order_by: [building.id]
order_by: [building.list_order]

Repo.all(query)
end
Expand All @@ -495,7 +495,7 @@ defmodule Galaxies.Accounts do
current_level: planet_building.current_level,
upgrade_cost_formula: building.upgrade_cost_formula
},
order_by: [building.id]
order_by: [building.list_order]

Repo.all(query)
end
Expand Down
9 changes: 4 additions & 5 deletions lib/galaxies/building.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
defmodule Galaxies.Building do
@moduledoc """
Defines the schema for the buildings of the game. We use Ecto.Schema instead
of Galaxies.Schema because we want integer IDs and not UUIDs as the primary
key. The reason for this is that we want the return order for buildings to be
deterministic, and that doesn't happen with standard UUIDv4s.
Defines the schema for the buildings of the game.
"""

use Ecto.Schema
use Galaxies.Schema

schema "buildings" do
field :name, :string
Expand All @@ -21,6 +18,8 @@ defmodule Galaxies.Building do
field :production_formula, :string
field :energy_consumption_formula, :string

field :list_order, :integer

timestamps(type: :utc_datetime_usec)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/galaxies/planet_building.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Galaxies.PlanetBuilding do
field :upgrade_finish_time, :utc_datetime

belongs_to :planet, Galaxies.Planet
belongs_to :building, Galaxies.Building, type: :integer
belongs_to :building, Galaxies.Building

timestamps(type: :utc_datetime_usec)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxies_web/live/resources_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule GalaxiesWeb.ResourcesLive do
<%= if building.current_level == 0 do %>
Build
<% else %>
Upgrade to level <%= building.current_level + 1 %>
Upgrade
<% end %>
</.button>
</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ defmodule Galaxies.Repo.Migrations.AddBuildings do
use Ecto.Migration

def change do
create table("buildings") do
create table("buildings", primary_key: false) do
add :id, :binary_id, primary_key: true
add :name, :string, null: false

add :short_description, :text, null: false
Expand All @@ -16,6 +17,8 @@ defmodule Galaxies.Repo.Migrations.AddBuildings do
add :production_formula, :string
add :energy_consumption_formula, :string

add :list_order, :smallint, null: false

timestamps(type: :utc_datetime_usec)
end

Expand All @@ -28,7 +31,7 @@ defmodule Galaxies.Repo.Migrations.AddBuildings do
null: false,
primary_key: true

add :building_id, references(:buildings, on_delete: :delete_all),
add :building_id, references(:buildings, type: :binary_id, on_delete: :delete_all),
null: false,
primary_key: true

Expand Down
14 changes: 14 additions & 0 deletions priv/repo/migrations/20240126113248_insert_buildings.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Galaxies.Repo.Migrations.InsertBuildings do
Repo.insert_all(Building, [
%{
name: "Metal Mine",
list_order: 10,
image_src: "/images/buildings/metal-mine-v3.webp",
upgrade_cost_formula: "60 * 1.5^(level - 1)$15 * 1.5^(level - 1)$0$0",
short_description:
Expand All @@ -21,6 +22,7 @@ defmodule Galaxies.Repo.Migrations.InsertBuildings do
},
%{
name: "Crystal Mine",
list_order: 20,
image_src: "/images/buildings/crystal-mine.webp",
upgrade_cost_formula: "48 * 1.6^(level - 1)$24 * 1.6^(level - 1)$0$0",
short_description:
Expand All @@ -32,6 +34,7 @@ defmodule Galaxies.Repo.Migrations.InsertBuildings do
},
%{
name: "Deuterium Refinery",
list_order: 30,
image_src: "/images/buildings/deuterium-refinery-v3.webp",
upgrade_cost_formula: "225 * 1.5^(level - 1)$75 * 1.5^(level - 1)$0$0",
short_description:
Expand All @@ -43,6 +46,7 @@ defmodule Galaxies.Repo.Migrations.InsertBuildings do
},
%{
name: "Solar Power Plant",
list_order: 40,
image_src: "/images/buildings/solar-power-plant.webp",
upgrade_cost_formula: "75 * 1.5^(level - 1)$30 * 1.5^(level - 1)$0$0",
short_description:
Expand All @@ -54,6 +58,7 @@ defmodule Galaxies.Repo.Migrations.InsertBuildings do
},
%{
name: "Fusion Reactor",
list_order: 50,
image_src: "/images/buildings/fusion-reactor.webp",
upgrade_cost_formula:
"900 * 1.8^(level - 1)$360 * 1.8^(level - 1)$180 * 1.8^(level - 1)$0",
Expand All @@ -66,6 +71,7 @@ defmodule Galaxies.Repo.Migrations.InsertBuildings do
},
%{
name: "Metal Storage",
list_order: 60,
image_src: "/images/buildings/metal-storage.webp",
upgrade_cost_formula: "500 * 2^level$0$0$0",
short_description:
Expand All @@ -77,6 +83,7 @@ defmodule Galaxies.Repo.Migrations.InsertBuildings do
},
%{
name: "Crystal Storage",
list_order: 70,
upgrade_cost_formula: "500 * 2^level$250 * 2^level$0$0",
image_src: "/images/buildings/crystal-storage.webp",
short_description:
Expand All @@ -88,6 +95,7 @@ defmodule Galaxies.Repo.Migrations.InsertBuildings do
},
%{
name: "Deuterium Tank",
list_order: 80,
image_src: "/images/buildings/deuterium-tank-v4.webp",
upgrade_cost_formula: "500 * 2^level$500 * 2^level$0$0",
short_description:
Expand All @@ -99,6 +107,7 @@ defmodule Galaxies.Repo.Migrations.InsertBuildings do
},
%{
name: "Robot Factory",
list_order: 90,
image_src: "/images/buildings/robot-factory.webp",
upgrade_cost_formula: "400 * 2^(level - 1)$120 * 2^(level - 1)$200 * 2^(level - 1)$0",
short_description:
Expand All @@ -110,6 +119,7 @@ defmodule Galaxies.Repo.Migrations.InsertBuildings do
},
%{
name: "Nanite Factory",
list_order: 100,
image_src: "/images/buildings/nanite-factory.webp",
upgrade_cost_formula:
"1000000 * 2^(level - 1)$500000 * 2^(level - 1)$100000 * 2^(level - 1)$0",
Expand All @@ -122,6 +132,7 @@ defmodule Galaxies.Repo.Migrations.InsertBuildings do
},
%{
name: "Shipyard",
list_order: 110,
image_src: "/images/buildings/hangar.webp",
upgrade_cost_formula: "400 * 2^(level - 1)$200 * 2^(level - 1)$100 * 2^(level - 1)$0",
short_description:
Expand All @@ -133,6 +144,7 @@ defmodule Galaxies.Repo.Migrations.InsertBuildings do
},
%{
name: "Research Lab",
list_order: 120,
image_src: "/images/buildings/research-lab.webp",
upgrade_cost_formula: "200 * 2^(level - 1)$400 * 2^(level - 1)$200 * 2^(level - 1)$0",
short_description:
Expand All @@ -144,6 +156,7 @@ defmodule Galaxies.Repo.Migrations.InsertBuildings do
},
%{
name: "Terraformer",
list_order: 130,
image_src: "/images/buildings/terraformer.webp",
upgrade_cost_formula: "0$50000 * 2^(level - 1)$100000 * 2^(level - 1)$0",
short_description:
Expand All @@ -155,6 +168,7 @@ defmodule Galaxies.Repo.Migrations.InsertBuildings do
},
%{
name: "Missile Silo",
list_order: 140,
image_src: "/images/buildings/missile-silo.webp",
upgrade_cost_formula:
"20000 * 2^(level - 1)$20000 * 2^(level - 1)$1000 * 2^(level - 1)$0",
Expand Down

0 comments on commit 14b2f4f

Please sign in to comment.