|
| 1 | +defmodule Galaxies.Planets.Actions.EnqueueBuilding do |
| 2 | + @behaviour Galaxies.Planets.Action |
| 3 | + |
| 4 | + import Ecto.Query |
| 5 | + |
| 6 | + require Logger |
| 7 | + alias Galaxies.Prerequisites |
| 8 | + alias Galaxies.Planets.PlanetEvent |
| 9 | + alias Galaxies.Planets.PlanetAction |
| 10 | + |
| 11 | + @building_queue_max_size 5 |
| 12 | + |
| 13 | + def perform(player, %PlanetAction{type: :enqueue_building} = action) do |
| 14 | + %{planet_id: planet_id, data: %{building_id: building_id, demolish: demolish}} = action.data |
| 15 | + |
| 16 | + Ecto.Multi.new() |
| 17 | + |> Ecto.Multi.run(:planet, fn repo, _changes -> |
| 18 | + planet = repo.one!(from p in Planet, where: p.id == ^planet_id) |
| 19 | + |
| 20 | + if planet.player_id != player.id do |
| 21 | + Logger.notice( |
| 22 | + "Player #{player.id} tried to build on a planet that does not belong to them (planet_id: #{planet_id})" |
| 23 | + ) |
| 24 | + |
| 25 | + {:error, :invalid_player_action_build_on_other_player_planet} |
| 26 | + else |
| 27 | + {:ok, planet} |
| 28 | + end |
| 29 | + end) |
| 30 | + |> Ecto.Multi.run(:build_queue, fn repo, _changes -> |
| 31 | + build_queue = |
| 32 | + repo.all( |
| 33 | + from pe in PlanetEvent, |
| 34 | + where: |
| 35 | + pe.planet_id == ^planet_id and pe.type == ^:construction_complete and |
| 36 | + not pe.is_processed and not pe.is_cancelled, |
| 37 | + order_by: [asc: pe.inserted_at] |
| 38 | + ) |
| 39 | + |
| 40 | + if length(build_queue) >= @building_queue_max_size do |
| 41 | + {:error, :building_queue_full} |
| 42 | + else |
| 43 | + {:ok, build_queue} |
| 44 | + end |
| 45 | + end) |
| 46 | + |> Ecto.Multi.run(:enqueue_building, fn repo, %{build_queue: queue, planet: planet} -> |
| 47 | + base_event = |
| 48 | + PlanetEvent.changeset(%PlanetEvent{}, %{ |
| 49 | + planet_id: planet_id, |
| 50 | + type: :construction_complete, |
| 51 | + is_processed: false, |
| 52 | + is_cancelled: false, |
| 53 | + building_event: %{ |
| 54 | + building_id: building_id, |
| 55 | + demolish: demolish |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + if Enum.empty?(queue) do |
| 60 | + # no buildings in progress, check for prerequisites |
| 61 | + planet = repo.preload(planet, [:buildings]) |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + if planet.used_fields >= planet.total_fields do |
| 66 | + {:error, :not_enough_planet_fields} |
| 67 | + else |
| 68 | + building = Enum.find(planet.buildings, fn b -> b.id == building_id end) |
| 69 | + |
| 70 | + if building do |
| 71 | + {:error, :building_already_exists} |
| 72 | + else |
| 73 | + {:ok, base_event} |
| 74 | + end |
| 75 | + end |
| 76 | + else |
| 77 | + # adding the building to the end of the queue |
| 78 | + repo.insert(base_event) |
| 79 | + end |
| 80 | + end) |
| 81 | + |> Repo.transaction() |
| 82 | + |
| 83 | + building_queue = get_building_queue(planet_id) |
| 84 | + |
| 85 | + case length(building_queue) do |
| 86 | + length when length >= @building_queue_max_size -> |
| 87 | + {:error, :building_queue_full} |
| 88 | + |
| 89 | + 0 -> |
| 90 | + # no other buildings in progress, subtract building cost |
| 91 | + planet_buildings = |
| 92 | + Repo.all( |
| 93 | + from pb in PlanetBuilding, |
| 94 | + where: pb.planet_id == ^planet_id, |
| 95 | + select: pb, |
| 96 | + preload: [:building] |
| 97 | + ) |
| 98 | + |
| 99 | + planet_building = Enum.find(planet_buildings, fn pb -> pb.building_id == building_id end) |
| 100 | + |
| 101 | + {cost_metal, cost_crystal, cost_deuterium, _energy} = |
| 102 | + Galaxies.calc_upgrade_cost(planet_building.building.upgrade_cost_formula, level) |
| 103 | + |
| 104 | + %{ |
| 105 | + metal_units: planet_metal, |
| 106 | + crystal_units: planet_crystal, |
| 107 | + deuterium_units: planet_deuterium |
| 108 | + } = |
| 109 | + _planet = |
| 110 | + Repo.one!( |
| 111 | + from p in Planet, |
| 112 | + where: p.id == ^planet_id |
| 113 | + ) |
| 114 | + |
| 115 | + if planet_metal >= cost_metal and planet_crystal >= cost_crystal and |
| 116 | + planet_deuterium >= cost_deuterium do |
| 117 | + add_resources(planet_id, -cost_metal, -cost_crystal, -cost_deuterium) |
| 118 | + |
| 119 | + # TODO: replace with a real formula |
| 120 | + construction_duration_seconds = :math.pow(2, level) |> trunc() |
| 121 | + |
| 122 | + event_id = Ecto.UUID.generate() |
| 123 | + now = DateTime.utc_now(:second) |
| 124 | + |
| 125 | + Repo.insert!(%PlanetEvent{ |
| 126 | + planet_id: planet_id, |
| 127 | + completed_at: DateTime.add(now, construction_duration_seconds), |
| 128 | + type: :building_construction, |
| 129 | + building_event: %BuildingEvent{ |
| 130 | + id: event_id, |
| 131 | + list_order: 1, |
| 132 | + planet_id: planet_id, |
| 133 | + building_id: building_id, |
| 134 | + level: level, |
| 135 | + demolish: false, |
| 136 | + started_at: now, |
| 137 | + completed_at: DateTime.add(now, construction_duration_seconds) |
| 138 | + } |
| 139 | + }) |
| 140 | + |
| 141 | + :ok |
| 142 | + else |
| 143 | + {:error, :not_enough_resources} |
| 144 | + end |
| 145 | + |
| 146 | + _ -> |
| 147 | + # adding at the end of the queue which means level could be wrong |
| 148 | + # (e.g. trying to update metal mine twice from level 10 will yield 2 events with level = 11) |
| 149 | + # so we need to set level to a proper value (highest in queue + 1) |
| 150 | + {level, list_order} = |
| 151 | + Enum.reduce(building_queue, {level, 1}, fn %EnqueuedBuilding{ |
| 152 | + list_order: lo, |
| 153 | + building_id: b_id, |
| 154 | + level: lvl |
| 155 | + }, |
| 156 | + {acc_level, acc_order} -> |
| 157 | + level = |
| 158 | + if b_id == building_id and lvl >= acc_level do |
| 159 | + lvl + 1 |
| 160 | + else |
| 161 | + acc_level |
| 162 | + end |
| 163 | + |
| 164 | + order = |
| 165 | + if lo >= acc_order do |
| 166 | + lo + 1 |
| 167 | + else |
| 168 | + acc_order |
| 169 | + end |
| 170 | + |
| 171 | + {level, order} |
| 172 | + end) |
| 173 | + |
| 174 | + # TODO: replace with a real formula |
| 175 | + construction_duration_seconds = :math.pow(2, level) |> trunc() |
| 176 | + |
| 177 | + now = DateTime.utc_now(:second) |
| 178 | + |
| 179 | + # no PlanetEvent is inserted because we are inserting at the end of the queue, |
| 180 | + # so an event already exists to fetch from the head of the queue. |
| 181 | + Repo.insert!(%EnqueuedBuilding{ |
| 182 | + planet_id: planet_id, |
| 183 | + list_order: list_order, |
| 184 | + building_id: building_id, |
| 185 | + level: level, |
| 186 | + demolish: false, |
| 187 | + started_at: now, |
| 188 | + completed_at: DateTime.add(now, construction_duration_seconds, :second) |
| 189 | + }) |
| 190 | + |
| 191 | + :ok |
| 192 | + end |
| 193 | + end |
| 194 | +end |
0 commit comments