Skip to content

Commit

Permalink
pave the way for implementing prerequisite checks throughout the game
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalotomas committed Apr 6, 2024
1 parent bcc1413 commit b0b3b2f
Show file tree
Hide file tree
Showing 14 changed files with 870 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/galaxies/planets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule Galaxies.Planets do

@doc """
Enqueues the construction of a building on a planet.
Checks if the user has the prerequisites to enqueue the building.
If the building queue for that planet is empty, resources are subtracted from the planet.
Otherwise just inserts a new planet event
"""
Expand All @@ -40,7 +41,6 @@ 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/player_research.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Galaxies.PlayerResearch do
field :upgrade_finished_at, :utc_datetime_usec

belongs_to :player, Galaxies.Accounts.Player
belongs_to :research, Galaxies.Research
belongs_to :research, Galaxies.Research, type: :integer

timestamps(type: :utc_datetime_usec)
end
Expand Down
16 changes: 16 additions & 0 deletions lib/galaxies/prerequisites/building_prerequisite_building.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Galaxies.Prerequisites.BuildingPrerequisiteBuilding do
@moduledoc """
A Schema to represent the prerequisite buildings required to unlock a specific building.
One example of this is that in order to build a Shipyard, you need a level 2 Robot Factory.
This schema is used to represent building prerequisites on other buildings.
"""
alias Galaxies.Building

use Ecto.Schema

schema "building_prerequisites_buildings" do
belongs_to :building, Building, type: :integer
belongs_to :prerequisite_building, Building, type: :integer
field :prerequisite_building_level, :integer
end
end
17 changes: 17 additions & 0 deletions lib/galaxies/prerequisites/building_prerequisite_research.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Galaxies.Prerequisites.BuildingPrerequisiteResearch do
@moduledoc """
A Schema to represent the prerequisite researches required to unlock a specific building.
One example of this is that in order to build a Nanite Factory, you need a level 10 Computer Tech.
This schema is used to represent research prerequisites for buildings.
"""
alias Galaxies.Building
alias Galaxies.Research

use Ecto.Schema

schema "building_prerequisites_researches" do
belongs_to :building, Building, type: :integer
belongs_to :prerequisite_research, Research, type: :integer
field :prerequisite_research_level, :integer
end
end
17 changes: 17 additions & 0 deletions lib/galaxies/prerequisites/research_prerequisite_building.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Galaxies.Prerequisites.ResearchPrerequisiteBuilding do
@moduledoc """
A Schema to represent the prerequisite buildings required to unlock a specific research.
One example of this is that in order to build Weapons Tech, you need a level 4 Research Lab.
This schema is used to represent building prerequisites for researches.
"""
alias Galaxies.Research
alias Galaxies.Building

use Ecto.Schema

schema "research_prerequisites_buildings" do
belongs_to :research, Research, type: :integer
belongs_to :prerequisite_building, Building, type: :integer
field :prerequisite_building_level, :integer
end
end
17 changes: 17 additions & 0 deletions lib/galaxies/prerequisites/research_prerequisite_research.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Galaxies.Prerequisites.ResearchPrerequisiteResearch do
@moduledoc """
A Schema to represent the prerequisite researches required to unlock a specific research.
One example of this is that in order to research Astrophysics,
you need a) level 4 Espionage Tech and b) level 3 Impulse Drive
This schema is used to represent research prerequisites for other researches.
"""
alias Galaxies.Research

use Ecto.Schema

schema "research_prerequisites_researches" do
belongs_to :research, Research, type: :integer
belongs_to :prerequisite_research, Research, type: :integer
field :prerequisite_research_level, :integer
end
end
17 changes: 17 additions & 0 deletions lib/galaxies/prerequisites/unit_prerequisite_building.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Galaxies.Prerequisites.UnitPrerequisiteBuilding do
@moduledoc """
A Schema to represent the prerequisite buildings required to unlock a specific unit.
One example of this is that in order to build Cruisers, you need a level 5 Shipyard.
This schema is used to represent building prerequisites for units.
"""
alias Galaxies.Unit
alias Galaxies.Building

use Ecto.Schema

schema "unit_prerequisites_buildings" do
belongs_to :unit, Unit, type: :integer
belongs_to :prerequisite_building, Building, type: :integer
field :prerequisite_building_level, :integer
end
end
17 changes: 17 additions & 0 deletions lib/galaxies/prerequisites/unit_prerequisite_research.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Galaxies.Prerequisites.UnitPrerequisiteResearch do
@moduledoc """
A Schema to represent the prerequisite researches required to unlock a specific building.
One example of this is that in order to build a Nanite Factory, you need a level 10 Computer Tech.
This schema is used to represent research prerequisites for buildings.
"""
alias Galaxies.Unit
alias Galaxies.Research

use Ecto.Schema

schema "unit_prerequisites_researches" do
belongs_to :unit, Unit, type: :integer
belongs_to :prerequisite_research, Research, type: :integer
field :prerequisite_research_level, :integer
end
end
3 changes: 2 additions & 1 deletion lib/galaxies/research.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
defmodule Galaxies.Research do
@moduledoc """
Defines the schema for the researches of the game.
Uses integer IDs for simplicity.
"""

use Galaxies.Schema
use Ecto.Schema

schema "researches" do
field :name, :string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ defmodule Galaxies.Repo.Migrations.CreateResearchesTable do
use Ecto.Migration

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

add :short_description, :text, null: false
Expand All @@ -30,7 +29,7 @@ defmodule Galaxies.Repo.Migrations.CreateResearchesTable do
null: false,
primary_key: true

add :research_id, references(:researches, type: :binary_id, on_delete: :delete_all),
add :research_id, references(:researches, on_delete: :delete_all),
null: false,
primary_key: true

Expand Down
20 changes: 20 additions & 0 deletions priv/repo/migrations/20240127182822_insert_researches.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
Research,
[
%{
id: 1,
name: "Espionage Technology",
list_order: 10,
image_src: "/images/researches/espionage.webp",
Expand All @@ -24,6 +25,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
%{
id: 2,
name: "Computer Technology",
list_order: 20,
image_src: "/images/researches/computer.webp",
Expand All @@ -36,6 +38,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
%{
id: 3,
name: "Energy Technology",
list_order: 30,
image_src: "/images/researches/energy.webp",
Expand All @@ -48,6 +51,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
%{
id: 4,
name: "Laser Technology",
list_order: 40,
image_src: "/images/researches/laser.webp",
Expand All @@ -60,6 +64,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
%{
id: 5,
name: "Ion Technology",
list_order: 50,
image_src: "/images/researches/ion.webp",
Expand All @@ -72,6 +77,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
%{
id: 6,
name: "Plasma Technology",
list_order: 60,
image_src: "/images/researches/plasma.webp",
Expand All @@ -85,6 +91,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
%{
id: 7,
name: "Graviton Technology",
list_order: 70,
image_src: "/images/researches/graviton.webp",
Expand All @@ -97,6 +104,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
%{
id: 8,
name: "Weapons Technology",
list_order: 80,
image_src: "/images/researches/weapons.webp",
Expand All @@ -109,6 +117,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
%{
id: 9,
name: "Shields Technology",
list_order: 90,
image_src: "/images/researches/shield.webp",
Expand All @@ -121,6 +130,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
%{
id: 10,
name: "Armor Technology",
list_order: 100,
image_src: "/images/researches/armor.webp",
Expand All @@ -133,6 +143,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
%{
id: 11,
name: "Hyperspace Technology",
list_order: 110,
image_src: "/images/researches/hyperspace.webp",
Expand All @@ -146,6 +157,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
%{
id: 12,
name: "Combustion Engine Technology",
list_order: 120,
image_src: "/images/researches/combustion.webp",
Expand All @@ -158,6 +170,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
%{
id: 13,
name: "Impulse Engine Technology",
list_order: 130,
image_src: "/images/researches/impulse.webp",
Expand All @@ -170,6 +183,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
%{
id: 14,
name: "Hyperspace Engine Technology",
list_order: 140,
image_src: "/images/researches/warp.webp",
Expand All @@ -183,6 +197,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
# %{
# id: 15,
# name: "Cargo Technology",
# list_order: 150,
# image_src: "/images/researches/cargo.webp",
Expand All @@ -196,6 +211,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
# updated_at: now
# },
%{
id: 15,
name: "Astrophysics Technology",
list_order: 160,
image_src: "/images/researches/astrophysics.webp",
Expand All @@ -210,6 +226,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
},
%{
id: 16,
name: "Intergalactic Research Network",
list_order: 170,
image_src: "/images/researches/intergalactic-research-network.webp",
Expand All @@ -224,20 +241,23 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do
updated_at: now
# },
# %{
# id: 17,
# name: "Mineral Extration Technology",
# image_src: "/images/researches/.webp",
# short_description: "Increases metal mine production on all planets.",
# long_description:
# "Since metal is the most widely used resource in the industry, efforts have been made to increase production power. As a result of using the raw resources processed in metal mines more effectively, metal production power also increases considerably."
# },
# %{
# id: 17,
# name: "Crystallization Technology",
# image_src: "/images/researches/.webp",
# short_description: "Increases crystal mine production on all planets.",
# long_description:
# "Since crystals are frangible, processing and using them require great skill. As the industry's need for crystals increases, efforts are being made to avoid wasting crystals."
# },
# %{
# id: 18,
# name: "Fuel Cell Technology",
# image_src: "/images/researches/.webp",
# short_description: "Increases deuterium production on all planets.",
Expand Down
6 changes: 3 additions & 3 deletions priv/repo/migrations/20240129223345_insert_units.exs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
},
%{
id: 301,
name: "Spy Probe",
name: "Espionage Probe",
list_order: 10,
image_src: "/images/units/espionage-probe.webp",
short_description:
Expand Down Expand Up @@ -249,7 +249,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
},
%{
id: 404,
name: "Colonizer",
name: "Colony Ship",
list_order: 10,
image_src: "/images/units/colony.webp",
short_description: "These ships are specially designed to colonize new planets.",
Expand Down Expand Up @@ -280,7 +280,7 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
},
%{
id: 501,
name: "Missile Launcher",
name: "Rocket Launcher",
list_order: 10,
image_src: "/images/units/missile-launcher.webp",
short_description:
Expand Down
Loading

0 comments on commit b0b3b2f

Please sign in to comment.