Skip to content

Commit

Permalink
make buildings and unit IDs integers everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalotomas committed Apr 3, 2024
1 parent a866d5f commit bcc1413
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 40 deletions.
2 changes: 1 addition & 1 deletion lib/galaxies/planet_building.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Galaxies.PlanetBuilding do
field :current_level, :integer

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

timestamps(type: :utc_datetime_usec)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxies/planet_unit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Galaxies.PlanetUnit do
field :amount, :integer

belongs_to :planet, Galaxies.Planet, primary_key: true
belongs_to :unit, Galaxies.Unit, primary_key: true
belongs_to :unit, Galaxies.Unit, primary_key: true, type: :integer

timestamps(type: :utc_datetime_usec)
end
Expand Down
1 change: 1 addition & 0 deletions lib/galaxies/planets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ defmodule Galaxies.Planets do
)

planet_building = Enum.find(planet_buildings, fn pb -> pb.building_id == building_id end)
dbg(planet_building)

{cost_metal, cost_crystal, cost_deuterium, _energy} =
Galaxies.calc_upgrade_cost(planet_building.building.upgrade_cost_formula, level)
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxies/planets/enqueued_building.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Galaxies.Planets.EnqueuedBuilding do
field :started_at, :utc_datetime
field :completed_at, :utc_datetime

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

timestamps(type: :utc_datetime_usec)
Expand Down
3 changes: 2 additions & 1 deletion lib/galaxies/unit.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
defmodule Galaxies.Unit do
@moduledoc """
Defines the schema for the unit of the game.
Uses integer IDs for simplicity.
"""

use Galaxies.Schema
use Ecto.Schema

schema "units" do
field :name, :string
Expand Down
6 changes: 5 additions & 1 deletion lib/galaxies_web/live/facilities_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ defmodule GalaxiesWeb.FacilitiesLive do

level = building.current_level + 1

case Accounts.upgrade_planet_building(socket.assigns.current_planet, building_id, level) do
case Accounts.upgrade_planet_building(
socket.assigns.current_planet,
String.to_integer(building_id),
level
) do
:ok ->
# TODO: doing the same as mount seems a bit too much, try to optimize
current_planet = Accounts.get_active_planet(socket.assigns.current_player)
Expand Down
6 changes: 5 additions & 1 deletion lib/galaxies_web/live/resources_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ defmodule GalaxiesWeb.ResourcesLive do

level = building.current_level + 1

case Accounts.upgrade_planet_building(socket.assigns.current_planet, building_id, level) do
case Accounts.upgrade_planet_building(
socket.assigns.current_planet,
String.to_integer(building_id),
level
) do
:ok ->
# TODO: doing the same as mount seems a bit too much, try to optimize
current_planet = Accounts.get_active_planet(socket.assigns.current_player)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule Galaxies.Repo.Migrations.CreateBuildingsTable do

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

add :short_description, :text, null: false
Expand Down
5 changes: 2 additions & 3 deletions priv/repo/migrations/20240129163345_create_units_table.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ defmodule Galaxies.Repo.Migrations.CreateUnitsTable do
use Ecto.Migration

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

add :short_description, :text, null: false
Expand Down Expand Up @@ -37,7 +36,7 @@ defmodule Galaxies.Repo.Migrations.CreateUnitsTable do
null: false,
primary_key: true

add :unit_id, references(:units, type: :binary_id, on_delete: :delete_all),
add :unit_id, references(:units, on_delete: :delete_all),
null: false,
primary_key: true

Expand Down
91 changes: 61 additions & 30 deletions priv/repo/migrations/20240129223345_insert_units.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do

Repo.insert_all(Galaxies.Unit, [
%{
id: 101,
name: "Light Fighter",
list_order: 10,
image_src: "/images/units/light-fighter.webp",
Expand All @@ -23,6 +24,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 102,
name: "Heavy Fighter",
list_order: 20,
image_src: "/images/units/heavy-fighter.webp",
Expand All @@ -38,6 +40,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 103,
name: "Cruiser",
list_order: 10,
image_src: "/images/units/cruiser.webp",
Expand All @@ -53,6 +56,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 104,
name: "Battleship",
list_order: 10,
image_src: "/images/units/battleship.webp",
Expand All @@ -68,6 +72,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 105,
name: "Interceptor",
list_order: 10,
image_src: "/images/units/interceptor.webp",
Expand All @@ -83,6 +88,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 106,
name: "Bomber",
list_order: 10,
image_src: "/images/units/bombardier.webp",
Expand All @@ -98,6 +104,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 107,
name: "Destroyer",
list_order: 10,
image_src: "/images/units/dreadnaught.webp",
Expand All @@ -113,6 +120,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 108,
name: "Reaper",
list_order: 10,
image_src: "/images/units/battleship-v3.webp",
Expand All @@ -128,6 +136,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 109,
name: "Deathstar",
list_order: 10,
image_src: "/images/units/juggernaut.webp",
Expand All @@ -143,6 +152,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 201,
name: "Solar Satellite",
list_order: 10,
image_src: "/images/units/solar-satellite.webp",
Expand All @@ -158,6 +168,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 202,
name: "Crawler",
list_order: 10,
image_src: "/images/units/crawler.webp",
Expand All @@ -173,6 +184,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 301,
name: "Spy Probe",
list_order: 10,
image_src: "/images/units/espionage-probe.webp",
Expand All @@ -188,6 +200,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 401,
name: "Small Cargo Ship",
list_order: 10,
image_src: "/images/units/small-cargo.webp",
Expand All @@ -203,6 +216,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 402,
name: "Large Cargo Ship",
list_order: 10,
image_src: "/images/units/large-cargo.webp",
Expand All @@ -218,6 +232,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 403,
name: "Recycler",
list_order: 10,
image_src: "/images/units/large-cargo-v2.webp",
Expand All @@ -233,6 +248,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 404,
name: "Colonizer",
list_order: 10,
image_src: "/images/units/colony.webp",
Expand All @@ -247,6 +263,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 405,
name: "Asteroid Miner",
list_order: 10,
image_src: "/images/units/asteroid-miner.webp",
Expand All @@ -262,6 +279,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 501,
name: "Missile Launcher",
list_order: 10,
image_src: "/images/units/missile-launcher.webp",
Expand All @@ -277,6 +295,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 502,
name: "Light Laser Turret",
list_order: 10,
image_src: "/images/units/light-laser.webp",
Expand All @@ -292,6 +311,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 503,
name: "Heavy Laser Turret",
list_order: 10,
image_src: "/images/units/heavy-laser.webp",
Expand All @@ -307,6 +327,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 504,
name: "Ion Cannon",
list_order: 10,
image_src: "/images/units/ion-cannon.webp",
Expand All @@ -322,6 +343,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 505,
name: "Gauss Cannon",
list_order: 10,
image_src: "/images/units/gauss-cannon.webp",
Expand All @@ -337,6 +359,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 506,
name: "Plasma Cannon",
list_order: 10,
image_src: "/images/units/plasma-cannon.webp",
Expand All @@ -351,7 +374,40 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
inserted_at: now,
updated_at: now
},
%{
id: 507,
name: "Small Shield Dome",
list_order: 10,
image_src: "/images/units/small-shield.webp",
short_description:
"The small shield dome covers the defense units and ships with a protective energy shield using a generator. This can absorb additional energy from the outside and is still permeable enough to let your own defenses fire.",
long_description:
"The small shield dome covers facilities and units with a protective energy shield using a generator. This can absorb additional energy from the outside and is still permeable enough to let own defense systems fire. Due to the high energy voltage required, only a limited number of shield domes can be built per planet. This amount can be increased by the ongoing progress of the empire.",
type: :defense,
weapon_points: 0,
shield_points: 2000,
hull_points: 4000,
inserted_at: now,
updated_at: now
},
%{
id: 508,
name: "Large Shield Dome",
list_order: 10,
image_src: "/images/units/large-shield.webp",
short_description:
"Further research into shield technologies has significantly improved the resilience of small shield domes. Large shield domes thus cover a much larger area of the planet, which means that its facilities and units can be protected much more efficiently.",
long_description:
"Further research into shield technologies has significantly improved the resilience of small shield domes. Large shield domes thus cover a much larger area of the planet, which means that more facilities and units can be protected more efficiently. Due to the high energy voltage required, only a limited number of shield domes can be built per planet. That number can be increased by ongoing progress of the empire.",
type: :defense,
weapon_points: 0,
shield_points: 10_000,
hull_points: 20_000,
inserted_at: now,
updated_at: now
},
# %{
# id: 509,
# name: "Fortress",
# image_src: "/images/units/.webp",
# short_description:
Expand All @@ -364,6 +420,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
# hull_points: 960_000
# },
# %{
# id: 510,
# name: "Doom Cannon",
# image_src: "/images/units/.webp",
# short_description:
Expand All @@ -376,6 +433,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
# hull_points: 3_600_000
# },
# %{
# id: 511,
# name: "Orbital Defense Platform",
# image_src: "/images/units/.webp",
# short_description:
Expand All @@ -387,37 +445,8 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
# shield_points: 1_000_000,
# hull_points: 22_400_000
# },
%{
name: "Small Shield Dome",
list_order: 10,
image_src: "/images/units/small-shield.webp",
short_description:
"The small shield dome covers the defense units and ships with a protective energy shield using a generator. This can absorb additional energy from the outside and is still permeable enough to let your own defenses fire.",
long_description:
"The small shield dome covers facilities and units with a protective energy shield using a generator. This can absorb additional energy from the outside and is still permeable enough to let own defense systems fire. Due to the high energy voltage required, only a limited number of shield domes can be built per planet. This amount can be increased by the ongoing progress of the empire.",
type: :defense,
weapon_points: 0,
shield_points: 2000,
hull_points: 4000,
inserted_at: now,
updated_at: now
},
%{
name: "Large Shield Dome",
list_order: 10,
image_src: "/images/units/large-shield.webp",
short_description:
"Further research into shield technologies has significantly improved the resilience of small shield domes. Large shield domes thus cover a much larger area of the planet, which means that its facilities and units can be protected much more efficiently.",
long_description:
"Further research into shield technologies has significantly improved the resilience of small shield domes. Large shield domes thus cover a much larger area of the planet, which means that more facilities and units can be protected more efficiently. Due to the high energy voltage required, only a limited number of shield domes can be built per planet. That number can be increased by ongoing progress of the empire.",
type: :defense,
weapon_points: 0,
shield_points: 10_000,
hull_points: 20_000,
inserted_at: now,
updated_at: now
},
# %{
# id: 512,
# name: "Atmospheric Shield",
# image_src: "/images/units/.webp",
# short_description:
Expand All @@ -430,6 +459,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
# hull_points: 4_000_000
# },
%{
id: 601,
name: "Interceptor Missile",
list_order: 10,
image_src: "/images/units/interceptor-missile.webp",
Expand All @@ -444,6 +474,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
updated_at: now
},
%{
id: 602,
name: "Interplanetary Missile",
list_order: 10,
image_src: "/images/units/interplanetary-missile.webp",
Expand Down

0 comments on commit bcc1413

Please sign in to comment.