From b0b3b2f177eea4161edf64f7fb916356fd97a907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Tom=C3=A1s?= Date: Sat, 6 Apr 2024 18:25:20 +0100 Subject: [PATCH] pave the way for implementing prerequisite checks throughout the game --- lib/galaxies/planets.ex | 2 +- lib/galaxies/player_research.ex | 2 +- .../building_prerequisite_building.ex | 16 + .../building_prerequisite_research.ex | 17 + .../research_prerequisite_building.ex | 17 + .../research_prerequisite_research.ex | 17 + .../unit_prerequisite_building.ex | 17 + .../unit_prerequisite_research.ex | 17 + lib/galaxies/research.ex | 3 +- ...20240127172200_create_researches_table.exs | 5 +- .../20240127182822_insert_researches.exs | 20 + .../20240129223345_insert_units.exs | 6 +- ...0403092602_create_prerequisites_tables.exs | 70 ++ .../20240406135222_insert_prerequisites.exs | 670 ++++++++++++++++++ 14 files changed, 870 insertions(+), 9 deletions(-) create mode 100644 lib/galaxies/prerequisites/building_prerequisite_building.ex create mode 100644 lib/galaxies/prerequisites/building_prerequisite_research.ex create mode 100644 lib/galaxies/prerequisites/research_prerequisite_building.ex create mode 100644 lib/galaxies/prerequisites/research_prerequisite_research.ex create mode 100644 lib/galaxies/prerequisites/unit_prerequisite_building.ex create mode 100644 lib/galaxies/prerequisites/unit_prerequisite_research.ex create mode 100644 priv/repo/migrations/20240403092602_create_prerequisites_tables.exs create mode 100644 priv/repo/migrations/20240406135222_insert_prerequisites.exs diff --git a/lib/galaxies/planets.ex b/lib/galaxies/planets.ex index 34d5b0c..b614ae7 100644 --- a/lib/galaxies/planets.ex +++ b/lib/galaxies/planets.ex @@ -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 """ @@ -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) diff --git a/lib/galaxies/player_research.ex b/lib/galaxies/player_research.ex index d1b0d40..24fd42d 100644 --- a/lib/galaxies/player_research.ex +++ b/lib/galaxies/player_research.ex @@ -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 diff --git a/lib/galaxies/prerequisites/building_prerequisite_building.ex b/lib/galaxies/prerequisites/building_prerequisite_building.ex new file mode 100644 index 0000000..b8cafe5 --- /dev/null +++ b/lib/galaxies/prerequisites/building_prerequisite_building.ex @@ -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 diff --git a/lib/galaxies/prerequisites/building_prerequisite_research.ex b/lib/galaxies/prerequisites/building_prerequisite_research.ex new file mode 100644 index 0000000..4052332 --- /dev/null +++ b/lib/galaxies/prerequisites/building_prerequisite_research.ex @@ -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 diff --git a/lib/galaxies/prerequisites/research_prerequisite_building.ex b/lib/galaxies/prerequisites/research_prerequisite_building.ex new file mode 100644 index 0000000..8a275a5 --- /dev/null +++ b/lib/galaxies/prerequisites/research_prerequisite_building.ex @@ -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 diff --git a/lib/galaxies/prerequisites/research_prerequisite_research.ex b/lib/galaxies/prerequisites/research_prerequisite_research.ex new file mode 100644 index 0000000..fd14384 --- /dev/null +++ b/lib/galaxies/prerequisites/research_prerequisite_research.ex @@ -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 diff --git a/lib/galaxies/prerequisites/unit_prerequisite_building.ex b/lib/galaxies/prerequisites/unit_prerequisite_building.ex new file mode 100644 index 0000000..db0027c --- /dev/null +++ b/lib/galaxies/prerequisites/unit_prerequisite_building.ex @@ -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 diff --git a/lib/galaxies/prerequisites/unit_prerequisite_research.ex b/lib/galaxies/prerequisites/unit_prerequisite_research.ex new file mode 100644 index 0000000..fbb5378 --- /dev/null +++ b/lib/galaxies/prerequisites/unit_prerequisite_research.ex @@ -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 diff --git a/lib/galaxies/research.ex b/lib/galaxies/research.ex index afe41cc..725b774 100644 --- a/lib/galaxies/research.ex +++ b/lib/galaxies/research.ex @@ -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 diff --git a/priv/repo/migrations/20240127172200_create_researches_table.exs b/priv/repo/migrations/20240127172200_create_researches_table.exs index 2f7a533..5c47fc3 100644 --- a/priv/repo/migrations/20240127172200_create_researches_table.exs +++ b/priv/repo/migrations/20240127172200_create_researches_table.exs @@ -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 @@ -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 diff --git a/priv/repo/migrations/20240127182822_insert_researches.exs b/priv/repo/migrations/20240127182822_insert_researches.exs index 1a9ae45..6ddbb8d 100644 --- a/priv/repo/migrations/20240127182822_insert_researches.exs +++ b/priv/repo/migrations/20240127182822_insert_researches.exs @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -224,6 +241,7 @@ 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.", @@ -231,6 +249,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do # "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.", @@ -238,6 +257,7 @@ defmodule Galaxies.Repo.Migrations.InsertResearches do # "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.", diff --git a/priv/repo/migrations/20240129223345_insert_units.exs b/priv/repo/migrations/20240129223345_insert_units.exs index 9ad25ba..e9d6f11 100644 --- a/priv/repo/migrations/20240129223345_insert_units.exs +++ b/priv/repo/migrations/20240129223345_insert_units.exs @@ -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: @@ -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.", @@ -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: diff --git a/priv/repo/migrations/20240403092602_create_prerequisites_tables.exs b/priv/repo/migrations/20240403092602_create_prerequisites_tables.exs new file mode 100644 index 0000000..8cf8160 --- /dev/null +++ b/priv/repo/migrations/20240403092602_create_prerequisites_tables.exs @@ -0,0 +1,70 @@ +defmodule Galaxies.Repo.Migrations.CreatePrerequisitesTables do + @moduledoc """ + Creates the required tables for supporting prerequisites. + The ability to build instances of the main entities of the game (building, research, unit) + may depend on the level of buildings on the current planet or on the level of player researches. + The tables created in this + """ + use Ecto.Migration + + def change do + create table(:building_prerequisites_buildings) do + add :building_id, references(:buildings), null: false + add :prerequisite_building_id, references(:buildings), null: false + add :prerequisite_building_level, :integer, null: false + end + + create table(:building_prerequisites_researches) do + add :building_id, references(:buildings), null: false + add :prerequisite_research_id, references(:researches), null: false + add :prerequisite_research_level, :integer, null: false + end + + create table(:research_prerequisites_buildings) do + add :research_id, references(:researches), null: false + add :prerequisite_building_id, references(:buildings), null: false + add :prerequisite_building_level, :integer, null: false + end + + create table(:research_prerequisites_researches) do + add :research_id, references(:researches), null: false + add :prerequisite_research_id, references(:researches), null: false + add :prerequisite_research_level, :integer, null: false + end + + create table(:unit_prerequisites_buildings) do + add :unit_id, references(:units), null: false + add :prerequisite_building_id, references(:buildings), null: false + add :prerequisite_building_level, :integer, null: false + end + + create table(:unit_prerequisites_researches) do + add :unit_id, references(:units), null: false + add :prerequisite_research_id, references(:researches), null: false + add :prerequisite_research_level, :integer, null: false + end + + create unique_index(:building_prerequisites_buildings, [ + :building_id, + :prerequisite_building_id + ]) + + create unique_index(:building_prerequisites_researches, [ + :building_id, + :prerequisite_research_id + ]) + + create unique_index(:research_prerequisites_buildings, [ + :research_id, + :prerequisite_building_id + ]) + + create unique_index(:research_prerequisites_researches, [ + :research_id, + :prerequisite_research_id + ]) + + create unique_index(:unit_prerequisites_buildings, [:unit_id, :prerequisite_building_id]) + create unique_index(:unit_prerequisites_researches, [:unit_id, :prerequisite_research_id]) + end +end diff --git a/priv/repo/migrations/20240406135222_insert_prerequisites.exs b/priv/repo/migrations/20240406135222_insert_prerequisites.exs new file mode 100644 index 0000000..bb7c970 --- /dev/null +++ b/priv/repo/migrations/20240406135222_insert_prerequisites.exs @@ -0,0 +1,670 @@ +defmodule Galaxies.Repo.Migrations.InsertPrerequisites do + @moduledoc """ + Sets up the data for building prerequisites for buildings, units and researches. + Uses data from the original game. + For readability and maintainability, buildings are fetched and then used by name. + + """ + + alias Galaxies.Building + alias Galaxies.Repo + alias Galaxies.Research + alias Galaxies.Unit + + alias Galaxies.Prerequisites.BuildingPrerequisiteBuilding + alias Galaxies.Prerequisites.BuildingPrerequisiteResearch + alias Galaxies.Prerequisites.ResearchPrerequisiteBuilding + alias Galaxies.Prerequisites.ResearchPrerequisiteResearch + alias Galaxies.Prerequisites.UnitPrerequisiteBuilding + alias Galaxies.Prerequisites.UnitPrerequisiteResearch + + use Ecto.Migration + + import Ecto.Query + + def change do + # get buildings that have prerequisites or that are used as prerequisites + buildings = Repo.all(Building) + deuterium_synthesizer = Enum.find(buildings, fn b -> b.name == "Deuterium Synthesizer" end) + fusion_reactor = Enum.find(buildings, fn b -> b.name == "Fusion Reactor" end) + robot_factory = Enum.find(buildings, fn b -> b.name == "Robot Factory" end) + nanite_factory = Enum.find(buildings, fn b -> b.name == "Nanite Factory" end) + shipyard = Enum.find(buildings, fn b -> b.name == "Shipyard" end) + research_lab = Enum.find(buildings, fn b -> b.name == "Research Lab" end) + missile_silo = Enum.find(buildings, fn b -> b.name == "Missile Silo" end) + terraformer = Enum.find(buildings, fn b -> b.name == "Terraformer" end) + + # get researches that have prerequisites or that are used as prerequisites (all of them) + researches = Repo.all(Research) + energy_tech = Enum.find(researches, fn r -> r.name == "Energy Technology" end) + computer_tech = Enum.find(researches, fn r -> r.name == "Computer Technology" end) + hyperspace_tech = Enum.find(researches, fn r -> r.name == "Hyperspace Technology" end) + laser_tech = Enum.find(researches, fn r -> r.name == "Laser Technology" end) + ion_tech = Enum.find(researches, fn r -> r.name == "Ion Technology" end) + plasma_tech = Enum.find(researches, fn r -> r.name == "Plasma Technology" end) + armor_tech = Enum.find(researches, fn r -> r.name == "Armor Technology" end) + shield_tech = Enum.find(researches, fn r -> r.name == "Shields Technology" end) + weapons_tech = Enum.find(researches, fn r -> r.name == "Weapons Technology" end) + espionage_tech = Enum.find(researches, fn r -> r.name == "Espionage Technology" end) + astrophysics_tech = Enum.find(researches, fn r -> r.name == "Astrophysics Technology" end) + + intergalactic_research_network_tech = + Enum.find(researches, fn r -> r.name == "Intergalactic Research Network" end) + + graviton_tech = Enum.find(researches, fn r -> r.name == "Graviton Technology" end) + + combustion_drive_tech = + Enum.find(researches, fn r -> r.name == "Combustion Engine Technology" end) + + impulse_drive_tech = Enum.find(researches, fn r -> r.name == "Impulse Engine Technology" end) + + hyperspace_drive_tech = + Enum.find(researches, fn r -> r.name == "Hyperspace Engine Technology" end) + + # get units that have prerequisites (all of them) + units = Repo.all(Unit) + light_fighter = Enum.find(units, fn u -> u.name == "Light Fighter" end) + heavy_fighter = Enum.find(units, fn u -> u.name == "Heavy Fighter" end) + cruiser = Enum.find(units, fn u -> u.name == "Cruiser" end) + battleship = Enum.find(units, fn u -> u.name == "Battleship" end) + interceptor = Enum.find(units, fn u -> u.name == "Interceptor" end) + bomber = Enum.find(units, fn u -> u.name == "Bomber" end) + destroyer = Enum.find(units, fn u -> u.name == "Destroyer" end) + deathstar = Enum.find(units, fn u -> u.name == "Deathstar" end) + reaper = Enum.find(units, fn u -> u.name == "Reaper" end) + small_cargo = Enum.find(units, fn u -> u.name == "Small Cargo Ship" end) + large_cargo = Enum.find(units, fn u -> u.name == "Large Cargo Ship" end) + colony_ship = Enum.find(units, fn u -> u.name == "Colony Ship" end) + recycler = Enum.find(units, fn u -> u.name == "Recycler" end) + espionage_probe = Enum.find(units, fn u -> u.name == "Espionage Probe" end) + solar_satellite = Enum.find(units, fn u -> u.name == "Solar Satellite" end) + crawler = Enum.find(units, fn u -> u.name == "Crawler" end) + rocket_launcher = Enum.find(units, fn u -> u.name == "Rocket Launcher" end) + light_laser = Enum.find(units, fn u -> u.name == "Light Laser Turret" end) + heavy_laser = Enum.find(units, fn u -> u.name == "Heavy Laser Turret" end) + gauss_cannon = Enum.find(units, fn u -> u.name == "Gauss Cannon" end) + ion_cannon = Enum.find(units, fn u -> u.name == "Ion Cannon" end) + plasma_cannon = Enum.find(units, fn u -> u.name == "Plasma Cannon" end) + small_shield = Enum.find(units, fn u -> u.name == "Small Shield Dome" end) + large_shield = Enum.find(units, fn u -> u.name == "Large Shield Dome" end) + anti_ballistic_missile = Enum.find(units, fn u -> u.name == "Interceptor Missile" end) + interplanetary_missile = Enum.find(units, fn u -> u.name == "Interplanetary Missile" end) + + # actually insert prerequisites into appropriate tables + + # Insert buildings required for other buildings + # e.g. Fusion reactor requires a level 5 Deuterium Synthesizer + Repo.insert_all(BuildingPrerequisiteBuilding, [ + %{ + building_id: fusion_reactor.id, + prerequisite_building_id: deuterium_synthesizer.id, + prerequisite_building_level: 5 + }, + %{ + building_id: nanite_factory.id, + prerequisite_building_id: robot_factory.id, + prerequisite_building_level: 10 + }, + %{ + building_id: shipyard.id, + prerequisite_building_id: robot_factory.id, + prerequisite_building_level: 2 + }, + %{ + building_id: missile_silo.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 1 + }, + %{ + building_id: terraformer.id, + prerequisite_building_id: nanite_factory.id, + prerequisite_building_level: 1 + } + ]) + + # Insert researches required for buildings + # e.g. Fusion Reactor requires a level 3 Energy Technology + Repo.insert_all(BuildingPrerequisiteResearch, [ + %{ + building_id: fusion_reactor.id, + prerequisite_research_id: energy_tech.id, + prerequisite_research_level: 3 + }, + %{ + building_id: nanite_factory.id, + prerequisite_research_id: computer_tech.id, + prerequisite_research_level: 10 + }, + %{ + building_id: terraformer.id, + prerequisite_research_id: energy_tech.id, + prerequisite_research_level: 12 + } + ]) + + # Insert buildings required for researches + # e.g. Plasma Tech requires a level 4 Research Lab + Repo.insert_all(ResearchPrerequisiteBuilding, [ + %{ + research_id: energy_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 1 + }, + %{ + research_id: laser_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 1 + }, + %{ + research_id: ion_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 4 + }, + %{ + research_id: hyperspace_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 7 + }, + %{ + research_id: plasma_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 4 + }, + %{ + research_id: espionage_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 3 + }, + %{ + research_id: computer_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 1 + }, + %{ + research_id: astrophysics_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 3 + }, + %{ + research_id: intergalactic_research_network_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 10 + }, + %{ + research_id: graviton_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 12 + }, + %{ + research_id: combustion_drive_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 1 + }, + %{ + research_id: impulse_drive_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 2 + }, + %{ + research_id: hyperspace_drive_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 7 + }, + %{ + research_id: weapons_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 4 + }, + %{ + research_id: shield_tech.id, + prerequisite_building_id: research_lab.id, + prerequisite_building_level: 6 + }, + %{ + research_id: armor_tech.id, + prerequisite_building_id: energy_tech.id, + prerequisite_building_level: 2 + } + ]) + + # Insert research required for other researches + # e.g. Intergalactic Research Network requires a level 8 Hyperspace Tech. + Repo.insert_all(ResearchPrerequisiteResearch, [ + %{ + research_id: laser_tech.id, + prerequisite_research_id: energy_tech.id, + prerequisite_research_level: 2 + }, + %{ + research_id: ion_tech.id, + prerequisite_research_id: energy_tech.id, + prerequisite_research_level: 4 + }, + %{ + research_id: ion_tech.id, + prerequisite_research_id: laser_tech.id, + prerequisite_research_level: 5 + }, + %{ + research_id: hyperspace_tech.id, + prerequisite_research_id: energy_tech.id, + prerequisite_research_level: 5 + }, + %{ + research_id: hyperspace_tech.id, + prerequisite_research_id: shield_tech.id, + prerequisite_research_level: 5 + }, + %{ + research_id: plasma_tech.id, + prerequisite_research_id: energy_tech.id, + prerequisite_research_level: 8 + }, + %{ + research_id: plasma_tech.id, + prerequisite_research_id: laser_tech.id, + prerequisite_research_level: 10 + }, + %{ + research_id: plasma_tech.id, + prerequisite_research_id: ion_tech.id, + prerequisite_research_level: 5 + }, + %{ + research_id: astrophysics_tech.id, + prerequisite_research_id: espionage_tech.id, + prerequisite_research_level: 4 + }, + %{ + research_id: astrophysics_tech.id, + prerequisite_research_id: impulse_drive_tech.id, + prerequisite_research_level: 3 + }, + %{ + research_id: intergalactic_research_network_tech.id, + prerequisite_research_id: computer_tech.id, + prerequisite_research_level: 8 + }, + %{ + research_id: intergalactic_research_network_tech.id, + prerequisite_research_id: hyperspace_tech.id, + prerequisite_research_level: 8 + }, + %{ + research_id: combustion_drive_tech.id, + prerequisite_research_id: energy_tech.id, + prerequisite_research_level: 1 + }, + %{ + research_id: impulse_drive_tech.id, + prerequisite_research_id: energy_tech.id, + prerequisite_research_level: 1 + }, + %{ + research_id: hyperspace_drive_tech.id, + prerequisite_research_id: hyperspace_tech.id, + prerequisite_research_level: 3 + }, + %{ + research_id: shield_tech.id, + prerequisite_research_id: energy_tech.id, + prerequisite_research_level: 3 + } + ]) + + # Insert buildings required to build units. + # e.g. A level 12 Shipyard is required to build Deathstars. + Repo.insert_all(UnitPrerequisiteBuilding, [ + %{ + unit_id: light_fighter.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 1 + }, + %{ + unit_id: heavy_fighter.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 3 + }, + %{ + unit_id: cruiser.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 5 + }, + %{ + unit_id: battleship.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 7 + }, + %{ + unit_id: interceptor.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 8 + }, + %{ + unit_id: bomber.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 8 + }, + %{ + unit_id: destroyer.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 9 + }, + %{ + unit_id: deathstar.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 12 + }, + %{ + unit_id: reaper.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 10 + }, + %{ + unit_id: small_cargo.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 2 + }, + %{ + unit_id: large_cargo.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 4 + }, + %{ + unit_id: colony_ship.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 4 + }, + %{ + unit_id: recycler.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 4 + }, + %{ + unit_id: espionage_probe.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 3 + }, + %{ + unit_id: solar_satellite.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 1 + }, + %{ + unit_id: crawler.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 5 + }, + %{ + unit_id: rocket_launcher.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 1 + }, + %{ + unit_id: light_laser.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 2 + }, + %{ + unit_id: heavy_laser.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 4 + }, + %{ + unit_id: gauss_cannon.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 6 + }, + %{ + unit_id: ion_cannon.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 4 + }, + %{ + unit_id: plasma_cannon.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 8 + }, + %{ + unit_id: small_shield.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 1 + }, + %{ + unit_id: large_shield.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 6 + }, + %{ + unit_id: anti_ballistic_missile.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 1 + }, + %{ + unit_id: anti_ballistic_missile.id, + prerequisite_building_id: missile_silo.id, + prerequisite_building_level: 2 + }, + %{ + unit_id: interplanetary_missile.id, + prerequisite_building_id: shipyard.id, + prerequisite_building_level: 1 + }, + %{ + unit_id: interplanetary_missile.id, + prerequisite_building_id: missile_silo.id, + prerequisite_building_level: 4 + } + ]) + + # Insert researches required to build units. + # e.g. A level 1 Graviton Tech is required to build Deathstars. + Repo.insert_all(UnitPrerequisiteResearch, [ + %{ + unit_id: light_fighter.id, + prerequisite_research_id: combustion_drive_tech.id, + prerequisite_research_level: 1 + }, + %{ + unit_id: heavy_fighter.id, + prerequisite_research_id: armor_tech.id, + prerequisite_research_level: 2 + }, + %{ + unit_id: heavy_fighter.id, + prerequisite_research_id: impulse_drive_tech.id, + prerequisite_research_level: 2 + }, + %{ + unit_id: cruiser.id, + prerequisite_research_id: impulse_drive_tech.id, + prerequisite_research_level: 4 + }, + %{ + unit_id: cruiser.id, + prerequisite_research_id: ion_tech.id, + prerequisite_research_level: 2 + }, + %{ + unit_id: battleship.id, + prerequisite_research_id: hyperspace_drive_tech.id, + prerequisite_research_level: 4 + }, + %{ + unit_id: interceptor.id, + prerequisite_research_id: hyperspace_tech.id, + prerequisite_research_level: 5 + }, + %{ + unit_id: interceptor.id, + prerequisite_research_id: hyperspace_drive_tech.id, + prerequisite_research_level: 5 + }, + %{ + unit_id: interceptor.id, + prerequisite_research_id: laser_tech.id, + prerequisite_research_level: 12 + }, + %{ + unit_id: bomber.id, + prerequisite_research_id: impulse_drive_tech.id, + prerequisite_research_level: 6 + }, + %{ + unit_id: bomber.id, + prerequisite_research_id: plasma_tech.id, + prerequisite_research_level: 5 + }, + %{ + unit_id: destroyer.id, + prerequisite_research_id: hyperspace_tech.id, + prerequisite_research_level: 5 + }, + %{ + unit_id: destroyer.id, + prerequisite_research_id: hyperspace_drive_tech.id, + prerequisite_research_level: 6 + }, + %{ + unit_id: deathstar.id, + prerequisite_research_id: hyperspace_tech.id, + prerequisite_research_level: 6 + }, + %{ + unit_id: deathstar.id, + prerequisite_research_id: hyperspace_drive_tech.id, + prerequisite_research_level: 7 + }, + %{ + unit_id: deathstar.id, + prerequisite_research_id: graviton_tech.id, + prerequisite_research_level: 1 + }, + %{ + unit_id: reaper.id, + prerequisite_research_id: hyperspace_tech.id, + prerequisite_research_level: 6 + }, + %{ + unit_id: reaper.id, + prerequisite_research_id: hyperspace_drive_tech.id, + prerequisite_research_level: 7 + }, + %{ + unit_id: reaper.id, + prerequisite_research_id: shield_tech.id, + prerequisite_research_level: 6 + }, + %{ + unit_id: small_cargo.id, + prerequisite_research_id: combustion_drive_tech.id, + prerequisite_research_level: 2 + }, + %{ + unit_id: large_cargo.id, + prerequisite_research_id: combustion_drive_tech.id, + prerequisite_research_level: 6 + }, + %{ + unit_id: colony_ship.id, + prerequisite_research_id: impulse_drive_tech.id, + prerequisite_research_level: 3 + }, + %{ + unit_id: recycler.id, + prerequisite_research_id: combustion_drive_tech.id, + prerequisite_research_level: 6 + }, + %{ + unit_id: recycler.id, + prerequisite_research_id: shield_tech.id, + prerequisite_research_level: 2 + }, + %{ + unit_id: espionage_probe.id, + prerequisite_research_id: combustion_drive_tech.id, + prerequisite_research_level: 3 + }, + %{ + unit_id: espionage_probe.id, + prerequisite_research_id: espionage_tech.id, + prerequisite_research_level: 2 + }, + %{ + unit_id: crawler.id, + prerequisite_research_id: combustion_drive_tech.id, + prerequisite_research_level: 4 + }, + %{ + unit_id: crawler.id, + prerequisite_research_id: armor_tech.id, + prerequisite_research_level: 4 + }, + %{ + unit_id: crawler.id, + prerequisite_research_id: laser_tech.id, + prerequisite_research_level: 4 + }, + %{ + unit_id: light_laser.id, + prerequisite_research_id: energy_tech.id, + prerequisite_research_level: 1 + }, + %{ + unit_id: light_laser.id, + prerequisite_research_id: laser_tech.id, + prerequisite_research_level: 3 + }, + %{ + unit_id: heavy_laser.id, + prerequisite_research_id: energy_tech.id, + prerequisite_research_level: 3 + }, + %{ + unit_id: heavy_laser.id, + prerequisite_research_id: laser_tech.id, + prerequisite_research_level: 6 + }, + %{ + unit_id: gauss_cannon.id, + prerequisite_research_id: energy_tech.id, + prerequisite_research_level: 6 + }, + %{ + unit_id: gauss_cannon.id, + prerequisite_research_id: weapons_tech.id, + prerequisite_research_level: 3 + }, + %{ + unit_id: gauss_cannon.id, + prerequisite_research_id: shield_tech.id, + prerequisite_research_level: 1 + }, + %{ + unit_id: ion_cannon.id, + prerequisite_research_id: ion_tech.id, + prerequisite_research_level: 4 + }, + %{ + unit_id: plasma_cannon.id, + prerequisite_research_id: plasma_tech.id, + prerequisite_research_level: 7 + }, + %{ + unit_id: small_shield.id, + prerequisite_research_id: shield_tech.id, + prerequisite_research_level: 2 + }, + %{ + unit_id: large_shield.id, + prerequisite_research_id: shield_tech.id, + prerequisite_research_level: 6 + }, + %{ + unit_id: interplanetary_missile.id, + prerequisite_research_id: impulse_drive_tech.id, + prerequisite_research_level: 1 + } + ]) + end +end