Skip to content

Commit 57d2519

Browse files
committed
Add Building cache, new testable events
1 parent acabaa8 commit 57d2519

36 files changed

+1392
-614
lines changed

config/dev.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ config :galaxies, dev_routes: true
7373

7474
# Do not include metadata nor timestamps in development logs
7575
config :logger, :console, format: "[$level] $message\n"
76-
config :logger, level: :info
76+
config :logger, level: :debug
7777

7878
# Set a higher stacktrace during development. Avoid configuring such
7979
# in production as building large stacktraces may be expensive.

lib/galaxies/accounts.ex

+13-9
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ defmodule Galaxies.Accounts do
4949
preload: :research
5050
)
5151

52-
if player_research.current_level == level - 1 do
52+
if player_research.level == level - 1 do
5353
{:ok, _} =
5454
Repo.update(
5555
PlayerResearch.upgrade_changeset(player_research, %{
56-
current_level: level
56+
level: level
5757
})
5858
)
5959

6060
{:ok, player_research}
6161
else
62-
{:error, "Cannot upgrade from level #{player_research.current_level} to #{level}"}
62+
{:error, "Cannot upgrade from level #{player_research.level} to #{level}"}
6363
end
6464
end)
6565
|> Ecto.Multi.run(:update_planet, fn repo, %{player_research: player_research} ->
@@ -205,7 +205,7 @@ defmodule Galaxies.Accounts do
205205
%{
206206
player_id: player.id,
207207
research_id: research.id,
208-
current_level: 0,
208+
level: 0,
209209
inserted_at: now,
210210
updated_at: now
211211
}
@@ -234,7 +234,11 @@ defmodule Galaxies.Accounts do
234234
image_id: 1,
235235
metal_units: 5_000.0,
236236
crystal_units: 5_000.0,
237-
deuterium_units: 500.0
237+
deuterium_units: 500.0,
238+
total_energy: 0,
239+
available_energy: 0,
240+
total_fields: 250,
241+
used_fields: 0
238242
})
239243
end)
240244
|> Ecto.Multi.run(:planet_buildings, fn repo, %{player: player, planet: planet} ->
@@ -244,7 +248,7 @@ defmodule Galaxies.Accounts do
244248
%{
245249
planet_id: planet.id,
246250
building_id: building.id,
247-
current_level: 0,
251+
level: 0,
248252
inserted_at: now,
249253
updated_at: now
250254
}
@@ -532,7 +536,7 @@ defmodule Galaxies.Accounts do
532536
description_short: building.short_description,
533537
description_long: building.long_description,
534538
image_src: building.image_src,
535-
current_level: planet_building.current_level,
539+
level: planet_building.level,
536540
upgrade_cost_formula: building.upgrade_cost_formula
537541
},
538542
order_by: [building.list_order]
@@ -556,7 +560,7 @@ defmodule Galaxies.Accounts do
556560
description_short: building.short_description,
557561
description_long: building.long_description,
558562
image_src: building.image_src,
559-
current_level: planet_building.current_level,
563+
level: planet_building.level,
560564
upgrade_cost_formula: building.upgrade_cost_formula
561565
},
562566
order_by: [building.list_order]
@@ -579,7 +583,7 @@ defmodule Galaxies.Accounts do
579583
description_short: research.short_description,
580584
description_long: research.long_description,
581585
image_src: research.image_src,
582-
current_level: player_research.current_level,
586+
level: player_research.level,
583587
upgrade_cost_formula: research.upgrade_cost_formula,
584588
is_upgrading: player_research.is_upgrading,
585589
upgrade_finished_at: player_research.upgrade_finished_at

lib/galaxies/application.ex

+12-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ defmodule Galaxies.Application do
1414
{Phoenix.PubSub, name: Galaxies.PubSub},
1515
# Start the Finch HTTP client for sending emails
1616
{Finch, name: Galaxies.Finch},
17-
# Start a one-off task for loading game entity prerequisites
18-
{Task, &Galaxies.Prerequisites.load_static_prerequisites/0},
17+
# Start a one-off task for loading static game entities
18+
{Task, &load_static_entities/0},
1919
# Start a worker by calling: Galaxies.Worker.start_link(arg)
2020
# {Galaxies.Worker, arg},
2121
# Start to serve requests, typically the last entry
@@ -35,4 +35,14 @@ defmodule Galaxies.Application do
3535
GalaxiesWeb.Endpoint.config_change(changed, removed)
3636
:ok
3737
end
38+
39+
defp load_static_entities() do
40+
tasks = [
41+
Task.async(fn -> Galaxies.Prerequisites.load_static_prerequisites() end),
42+
Task.async(fn -> Galaxies.Cached.Buildings.load_static_buildings() end)
43+
]
44+
45+
Task.await_many(tasks, :infinity)
46+
:ok
47+
end
3848
end

lib/galaxies/building.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ defmodule Galaxies.Building do
2323

2424
field :list_order, :integer
2525

26-
timestamps(type: :utc_datetime_usec)
26+
timestamps(type: :utc_datetime)
2727
end
2828

2929
def terraformer_extra_fields(level) when rem(level, 2) == 0, do: 6

lib/galaxies/buildings.ex

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ defmodule Galaxies.Buildings do
44
"""
55

66
# TODO: Maybe read hardcoded IDs from file or Application.compile_env?
7+
@production_building_ids [1, 2, 3, 4, 5]
78
@terraformer_building_id 13
89

10+
def production_building?(building_id) do
11+
building_id in @production_building_ids
12+
end
13+
914
@doc """
1015
Determines the increase in used fields when upgrading a building.
1116
An upgrade will increase the number of used fields by 1 while a downgrade
@@ -21,7 +26,7 @@ defmodule Galaxies.Buildings do
2126
"""
2227
def total_fields_increase(_building_id, _level, true), do: 0
2328

24-
def total_fields_increase(@terraformer_building_id, level, _demolish) do
29+
def total_fields_increase(@terraformer_building_id, level, false) do
2530
if rem(level, 2) == 0 do
2631
6
2732
else

lib/galaxies/cached/buildings.ex

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule Galaxies.Cached.Buildings do
2+
@moduledoc """
3+
A local copy of all the game's buildings, which are considered immutable.
4+
"""
5+
alias Galaxies.Repo
6+
require Logger
7+
8+
@cache_name :galaxies_buildings
9+
10+
def load_static_buildings do
11+
started_at = DateTime.utc_now(:millisecond)
12+
13+
for building <- Repo.all(Galaxies.Building) do
14+
:persistent_term.put({@cache_name, building.id}, building)
15+
end
16+
17+
Logger.info(
18+
"Loaded static buildings in #{DateTime.diff(DateTime.utc_now(:millisecond), started_at, :millisecond)}ms"
19+
)
20+
end
21+
22+
def get_building_by_id(building_id) do
23+
:persistent_term.get({@cache_name, building_id})
24+
end
25+
end

lib/galaxies/formulas/buildings.ex

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
defmodule Galaxies.Formulas.Buildings do
2+
@moduledoc """
3+
Formulas for buildings.
4+
"""
5+
6+
@universe_speed 100
7+
8+
@default_attrs %{
9+
universe_speed: @universe_speed
10+
}
11+
12+
def construction_time_seconds(building_id, level, robotics_level, nanites_level) do
13+
{metal, crystal, _deuterium, _energy} = upgrade_cost(building_id, level)
14+
15+
num = metal + crystal
16+
denomenator = 2500 * (1 + robotics_level) * :math.pow(2, nanites_level) * @universe_speed
17+
18+
ceil(num / denomenator)
19+
end
20+
21+
def upgrade_cost(building_id, level) do
22+
building = Galaxies.Cached.Buildings.get_building_by_id(building_id)
23+
24+
[metal_cost, crystal_cost, deuterium_cost, energy_cost] =
25+
building.upgrade_cost_formula
26+
|> String.split("$")
27+
|> Enum.map(fn expression ->
28+
{:ok, result} =
29+
Abacus.eval(expression, Map.merge(@default_attrs, %{"level" => level}))
30+
31+
ceil(result)
32+
end)
33+
34+
{metal_cost, crystal_cost, deuterium_cost, energy_cost}
35+
end
36+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule Galaxies.Formulas.Buildings.Terraformer do
2+
@moduledoc """
3+
Formulas for the Terraformer building.
4+
"""
5+
6+
@doc """
7+
Returns the increase in fields when upgrading the Terraformer to a given level.
8+
The Terraformer cannot be
9+
"""
10+
def field_increase(level) when level > 0 do
11+
trunc(Float.floor(5.5 * level) - Float.floor(5.5 * (level - 1)))
12+
end
13+
14+
def total_fields(planet) do
15+
if planet.total_fields < 100 do
16+
1
17+
else
18+
0
19+
end
20+
end
21+
end

lib/galaxies/planet_building.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule Galaxies.PlanetBuilding do
55

66
@primary_key false
77
schema "planet_buildings" do
8-
field :current_level, :integer
8+
field :level, :integer
99

1010
belongs_to :planet, Galaxies.Planet, primary_key: true, type: :integer
1111
belongs_to :building, Galaxies.Building, primary_key: true, type: :integer
@@ -20,6 +20,6 @@ defmodule Galaxies.PlanetBuilding do
2020

2121
def complete_upgrade_changeset(planet_building, attrs) do
2222
planet_building
23-
|> cast(attrs, [:current_level, :is_upgrading, :upgrade_finished_at])
23+
|> cast(attrs, [:level, :is_upgrading, :upgrade_finished_at])
2424
end
2525
end

0 commit comments

Comments
 (0)