Skip to content

Commit 92a72e9

Browse files
committed
refactor unit cost into common component
1 parent 7fec995 commit 92a72e9

File tree

7 files changed

+121
-8
lines changed

7 files changed

+121
-8
lines changed

lib/galaxies/accounts.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,8 @@ defmodule Galaxies.Accounts do
637637
amount: planet_unit.amount,
638638
unit_cost_metal: unit.unit_cost_metal,
639639
unit_cost_crystal: unit.unit_cost_crystal,
640-
unit_cost_deuterium: unit.unit_cost_deuterium
640+
unit_cost_deuterium: unit.unit_cost_deuterium,
641+
unit_cost_energy: unit.unit_cost_energy
641642
},
642643
order_by: [unit.list_order]
643644

lib/galaxies/unit.ex

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ defmodule Galaxies.Unit do
1919
field :unit_cost_metal, :integer
2020
field :unit_cost_crystal, :integer
2121
field :unit_cost_deuterium, :integer
22+
field :unit_cost_energy, :integer, default: 0
2223

2324
field :weapon_points, :integer
2425
field :shield_points, :integer

lib/galaxies_web/components/common_components.ex

+11
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ defmodule GalaxiesWeb.CommonComponents do
9999

100100
assigns = %{metal: metal, crystal: crystal, deuterium: deuterium, energy: energy}
101101

102+
~H"""
103+
<.unit_cost metal={@metal} crystal={@crystal} deuterium={@deuterium} energy={@energy} />
104+
"""
105+
end
106+
107+
attr :metal, :integer, required: true
108+
attr :crystal, :integer, required: true
109+
attr :deuterium, :integer, required: true
110+
attr :energy, :integer, required: true
111+
112+
def unit_cost(assigns) do
102113
~H"""
103114
Requirements:
104115
<%= if @metal > 0 do %>

lib/galaxies_web/live/defenses_live.ex

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule GalaxiesWeb.DefensesLive do
22
use GalaxiesWeb, :live_view
3+
import GalaxiesWeb.CommonComponents
34

45
alias Galaxies.Accounts
56

@@ -33,7 +34,15 @@ defmodule GalaxiesWeb.DefensesLive do
3334
style="object-fit: cover;"
3435
src={defense.image_src}
3536
/>
36-
{defense.description_short}
37+
<p>
38+
{defense.description_short}<br />
39+
<.unit_cost
40+
metal={defense.unit_cost_metal}
41+
crystal={defense.unit_cost_crystal}
42+
deuterium={defense.unit_cost_deuterium}
43+
energy={defense.unit_cost_energy}
44+
/>
45+
</p>
3746
</div>
3847
</div>
3948
<div class="ml-2 flex items-center text-sm text-gray-500">

lib/galaxies_web/live/shipyard_live.ex

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule GalaxiesWeb.ShipyardLive do
22
use GalaxiesWeb, :live_view
33

44
alias Galaxies.Accounts
5+
import GalaxiesWeb.CommonComponents
56

67
def mount(_params, _session, socket) do
78
socket = GalaxiesWeb.Common.mount_live_context(socket)
@@ -40,7 +41,15 @@ defmodule GalaxiesWeb.ShipyardLive do
4041
style="object-fit: cover;"
4142
src={ship.image_src}
4243
/>
43-
{ship.description_short}
44+
<p>
45+
{ship.description_short}<br />
46+
<.unit_cost
47+
metal={ship.unit_cost_metal}
48+
crystal={ship.unit_cost_crystal}
49+
deuterium={ship.unit_cost_deuterium}
50+
energy={0}
51+
/>
52+
</p>
4453
</div>
4554
</div>
4655
<div class="ml-2 flex items-center text-sm text-gray-500">

priv/repo/migrations/20240129163345_create_units_table.exs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ defmodule Galaxies.Repo.Migrations.CreateUnitsTable do
1313
add :unit_cost_metal, :integer
1414
add :unit_cost_crystal, :integer
1515
add :unit_cost_deuterium, :integer
16+
add :unit_cost_energy, :integer, default: 0
1617

1718
add :weapon_points, :integer
1819
add :shield_points, :integer

priv/repo/migrations/20240129223345_insert_units.exs

+86-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
2020
weapon_points: 50,
2121
shield_points: 10,
2222
hull_points: 400,
23+
unit_cost_metal: 3000,
24+
unit_cost_crystal: 1000,
25+
unit_cost_deuterium: 0,
2326
inserted_at: now,
2427
updated_at: now
2528
},
@@ -36,6 +39,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
3639
weapon_points: 150,
3740
shield_points: 25,
3841
hull_points: 1000,
42+
unit_cost_metal: 6000,
43+
unit_cost_crystal: 4000,
44+
unit_cost_deuterium: 0,
3945
inserted_at: now,
4046
updated_at: now
4147
},
@@ -52,6 +58,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
5258
weapon_points: 400,
5359
shield_points: 50,
5460
hull_points: 2700,
61+
unit_cost_metal: 20_000,
62+
unit_cost_crystal: 7000,
63+
unit_cost_deuterium: 2000,
5564
inserted_at: now,
5665
updated_at: now
5766
},
@@ -68,6 +77,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
6877
weapon_points: 1000,
6978
shield_points: 200,
7079
hull_points: 6000,
80+
unit_cost_metal: 45_000,
81+
unit_cost_crystal: 15_000,
82+
unit_cost_deuterium: 0,
7183
inserted_at: now,
7284
updated_at: now
7385
},
@@ -84,6 +96,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
8496
weapon_points: 700,
8597
shield_points: 400,
8698
hull_points: 7000,
99+
unit_cost_metal: 30_000,
100+
unit_cost_crystal: 40_000,
101+
unit_cost_deuterium: 15_000,
87102
inserted_at: now,
88103
updated_at: now
89104
},
@@ -100,6 +115,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
100115
weapon_points: 1000,
101116
shield_points: 500,
102117
hull_points: 7500,
118+
unit_cost_metal: 50_000,
119+
unit_cost_crystal: 25_000,
120+
unit_cost_deuterium: 15_000,
103121
inserted_at: now,
104122
updated_at: now
105123
},
@@ -116,6 +134,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
116134
weapon_points: 2000,
117135
shield_points: 500,
118136
hull_points: 11_000,
137+
unit_cost_metal: 60_000,
138+
unit_cost_crystal: 50_000,
139+
unit_cost_deuterium: 15_000,
119140
inserted_at: now,
120141
updated_at: now
121142
},
@@ -132,6 +153,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
132153
weapon_points: 2800,
133154
shield_points: 700,
134155
hull_points: 14_000,
156+
unit_cost_metal: 50_000,
157+
unit_cost_crystal: 50_000,
158+
unit_cost_deuterium: 20_000,
135159
inserted_at: now,
136160
updated_at: now
137161
},
@@ -148,6 +172,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
148172
weapon_points: 12_000,
149173
shield_points: 16_000,
150174
hull_points: 900_000,
175+
unit_cost_metal: 1_000_000,
176+
unit_cost_crystal: 500_000,
177+
unit_cost_deuterium: 100_000,
151178
inserted_at: now,
152179
updated_at: now
153180
},
@@ -164,6 +191,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
164191
weapon_points: 0,
165192
shield_points: 1,
166193
hull_points: 200,
194+
unit_cost_metal: 0,
195+
unit_cost_crystal: 2000,
196+
unit_cost_deuterium: 500,
167197
inserted_at: now,
168198
updated_at: now
169199
},
@@ -180,6 +210,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
180210
weapon_points: 0,
181211
shield_points: 1,
182212
hull_points: 5000,
213+
unit_cost_metal: 2000,
214+
unit_cost_crystal: 2000,
215+
unit_cost_deuterium: 0,
183216
inserted_at: now,
184217
updated_at: now
185218
},
@@ -196,6 +229,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
196229
weapon_points: 0,
197230
shield_points: 1,
198231
hull_points: 100,
232+
unit_cost_metal: 0,
233+
unit_cost_crystal: 1000,
234+
unit_cost_deuterium: 0,
199235
inserted_at: now,
200236
updated_at: now
201237
},
@@ -211,7 +247,10 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
211247
type: :ship,
212248
weapon_points: 5,
213249
shield_points: 5,
214-
hull_points: 400,
250+
hull_points: 4000,
251+
unit_cost_metal: 2000,
252+
unit_cost_crystal: 2000,
253+
unit_cost_deuterium: 0,
215254
inserted_at: now,
216255
updated_at: now
217256
},
@@ -227,7 +266,10 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
227266
type: :ship,
228267
weapon_points: 10,
229268
shield_points: 15,
230-
hull_points: 1200,
269+
hull_points: 12_000,
270+
unit_cost_metal: 6000,
271+
unit_cost_crystal: 6000,
272+
unit_cost_deuterium: 0,
231273
inserted_at: now,
232274
updated_at: now
233275
},
@@ -243,7 +285,10 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
243285
type: :ship,
244286
weapon_points: 5,
245287
shield_points: 10,
246-
hull_points: 1600,
288+
hull_points: 16_000,
289+
unit_cost_metal: 10_000,
290+
unit_cost_crystal: 6000,
291+
unit_cost_deuterium: 2000,
247292
inserted_at: now,
248293
updated_at: now
249294
},
@@ -258,7 +303,10 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
258303
type: :ship,
259304
weapon_points: 10,
260305
shield_points: 25,
261-
hull_points: 3000,
306+
hull_points: 30_000,
307+
unit_cost_metal: 10_000,
308+
unit_cost_crystal: 20_000,
309+
unit_cost_deuterium: 10_000,
262310
inserted_at: now,
263311
updated_at: now
264312
},
@@ -274,7 +322,10 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
274322
type: :ship,
275323
weapon_points: 20,
276324
shield_points: 200,
277-
hull_points: 6000,
325+
hull_points: 70_000,
326+
unit_cost_metal: 50_000,
327+
unit_cost_crystal: 20_000,
328+
unit_cost_deuterium: 15_000,
278329
inserted_at: now,
279330
updated_at: now
280331
},
@@ -291,6 +342,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
291342
weapon_points: 80,
292343
shield_points: 20,
293344
hull_points: 200,
345+
unit_cost_metal: 2000,
346+
unit_cost_crystal: 0,
347+
unit_cost_deuterium: 0,
294348
inserted_at: now,
295349
updated_at: now
296350
},
@@ -307,6 +361,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
307361
weapon_points: 100,
308362
shield_points: 25,
309363
hull_points: 200,
364+
unit_cost_metal: 1500,
365+
unit_cost_crystal: 500,
366+
unit_cost_deuterium: 0,
310367
inserted_at: now,
311368
updated_at: now
312369
},
@@ -323,6 +380,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
323380
weapon_points: 250,
324381
shield_points: 100,
325382
hull_points: 800,
383+
unit_cost_metal: 6000,
384+
unit_cost_crystal: 2000,
385+
unit_cost_deuterium: 0,
326386
inserted_at: now,
327387
updated_at: now
328388
},
@@ -339,6 +399,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
339399
weapon_points: 150,
340400
shield_points: 500,
341401
hull_points: 800,
402+
unit_cost_metal: 2000,
403+
unit_cost_crystal: 6000,
404+
unit_cost_deuterium: 0,
342405
inserted_at: now,
343406
updated_at: now
344407
},
@@ -355,6 +418,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
355418
weapon_points: 1100,
356419
shield_points: 200,
357420
hull_points: 3500,
421+
unit_cost_metal: 20_000,
422+
unit_cost_crystal: 15_000,
423+
unit_cost_deuterium: 2000,
358424
inserted_at: now,
359425
updated_at: now
360426
},
@@ -371,6 +437,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
371437
weapon_points: 3000,
372438
shield_points: 300,
373439
hull_points: 10_000,
440+
unit_cost_metal: 50_000,
441+
unit_cost_crystal: 50_000,
442+
unit_cost_deuterium: 30_000,
374443
inserted_at: now,
375444
updated_at: now
376445
},
@@ -387,6 +456,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
387456
weapon_points: 0,
388457
shield_points: 2000,
389458
hull_points: 4000,
459+
unit_cost_metal: 10_000,
460+
unit_cost_crystal: 10_000,
461+
unit_cost_deuterium: 0,
390462
inserted_at: now,
391463
updated_at: now
392464
},
@@ -403,6 +475,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
403475
weapon_points: 0,
404476
shield_points: 10_000,
405477
hull_points: 20_000,
478+
unit_cost_metal: 50_000,
479+
unit_cost_crystal: 50_000,
480+
unit_cost_deuterium: 0,
406481
inserted_at: now,
407482
updated_at: now
408483
},
@@ -470,6 +545,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
470545
weapon_points: 0,
471546
shield_points: 0,
472547
hull_points: 6500,
548+
unit_cost_metal: 8000,
549+
unit_cost_crystal: 0,
550+
unit_cost_deuterium: 2000,
473551
inserted_at: now,
474552
updated_at: now
475553
},
@@ -486,6 +564,9 @@ defmodule Galaxies.Repo.Migrations.InsertUnits do
486564
weapon_points: 15_000,
487565
shield_points: 0,
488566
hull_points: 12_500,
567+
unit_cost_metal: 125_000,
568+
unit_cost_crystal: 0,
569+
unit_cost_deuterium: 20_000,
489570
inserted_at: now,
490571
updated_at: now
491572
}

0 commit comments

Comments
 (0)