Skip to content

Commit

Permalink
add planets
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalotomas committed Jan 25, 2024
1 parent 5ec8c56 commit 51a24a1
Show file tree
Hide file tree
Showing 24 changed files with 1,306 additions and 143 deletions.
631 changes: 628 additions & 3 deletions lib/galaxies/accounts.ex

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion lib/galaxies/accounts/player.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
defmodule Galaxies.Accounts.Player do
alias Galaxies.Planet
use Galaxies.Schema
import Ecto.Changeset
import Ecto.Query

schema "players" do
field :email, :string
field :username, :string
field :password, :string, virtual: true, redact: true
field :hashed_password, :string, redact: true
field :confirmed_at, :naive_datetime
field :current_planet_id, :binary_id

has_many :planets, Galaxies.Planet

timestamps(type: :utc_datetime)
end

@doc """
Builds a query for fetching the active planet
"""
def get_active_planet_query(player) do
query =
from player in __MODULE__,
join: planet in Planet,
on: planet.player_id == ^player.id,
where: player.id == ^player.id and planet.id == ^player.current_planet_id,
select: planet

{:ok, query}
end

@doc """
A player changeset for registration.
Expand All @@ -39,7 +56,7 @@ defmodule Galaxies.Accounts.Player do
"""
def registration_changeset(player, attrs, opts \\ []) do
player
|> cast(attrs, [:email, :password, :username])
|> cast(attrs, [:email, :password, :username, :current_planet_id])
|> validate_email(opts)
|> validate_password(opts)
|> validate_username(opts)
Expand Down
30 changes: 21 additions & 9 deletions lib/galaxies/planet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,33 @@ defmodule Galaxies.Planet do

schema "planets" do
field :name, :string
# planets are identified by a set of coordinates
# {galaxy, system, slot}

# planets are identified by a set of coordinates {galaxy, system, slot}
field :galaxy, :integer
field :system, :integer
field :slot, :integer
# planet resources

field :metal_units, :integer
field :crystal_units, :integer
field :deuterium_units, :integer
# resource growth rate is a column of planets as an optimization.
# this avoids requiring to fetch the planet buildings in order to
# increment resources from the frontend.
field :metal_growth_rate, :float
field :crystal_growth_rate, :float
field :deuterium_growth_rate, :float

field :available_energy, :integer
field :total_energy, :integer

field :used_fields, :integer
field :total_fields, :integer

field :min_temperature, :integer
field :max_temperature, :integer

# resource growth rate is an optimization that avoids calculating the growth rate
# from building levels just to update the resource count periodically.
field :metal_growth_rate, :decimal
field :crystal_growth_rate, :decimal
field :deuterium_growth_rate, :decimal

# image_id is used to refer to multiple planet images for the same planet type.
field :image_id, :integer

timestamps(type: :utc_datetime)

Expand Down
16 changes: 9 additions & 7 deletions lib/galaxies_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,8 @@ defmodule GalaxiesWeb.CoreComponents do

def nav_menu_player_card(assigns) do
~H"""
<div class="flex flex-shrink-0 bg-gray-700 p-4">
<.link navigate={~p"/players/settings"} class="group block w-full flex-shrink-0">
<%!-- <div class="flex flex-shrink-0 bg-gray-700 p-4">
<div class="group block w-full flex-shrink-0">
<div class="flex items-center">
<div>
<img
Expand All @@ -670,13 +670,15 @@ defmodule GalaxiesWeb.CoreComponents do
</div>
<div class="ml-3">
<p class="text-sm font-medium text-white"><%= @username %></p>
<p class="text-xs font-medium text-gray-300 group-hover:text-gray-200">
Account Settings
</p>
<.link navigate={~p"/players/settings"}>
<p class="text-xs font-medium text-gray-300 group-hover:text-gray-200">
Settings
</p>
</.link>
</div>
</div>
</.link>
</div>
</div>
</div> --%>
"""
end

Expand Down
70 changes: 12 additions & 58 deletions lib/galaxies_web/components/layouts/app.html.heex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<ul class="relative z-10 flex items-center gap-4 px-4 sm:px-6 lg:px-8 justify-end">
<%= if @current_player do %>
<li class="text-[0.8125rem] leading-6 text-white">
<%= @current_player.email %>
<%= @current_player.username %>
</li>
<li>
<.link
Expand Down Expand Up @@ -111,11 +111,7 @@

<div class="h-0 flex-1 overflow-y-auto pt-5 pb-4">
<div class="flex flex-shrink-0 items-center px-4">
<img
class="h-8 w-auto"
src="https://tailwindui.com/img/logos/mark.svg?color=indigo&shade=500"
alt="Your Company"
/>
<img class="h-12 w-auto" src="/images/logo.webp" alt="17 Galaxies" />
</div>
<.navigation_menu class="mt-5 space-y-1 px-2" />
</div>
Expand All @@ -130,14 +126,10 @@
<!-- Static sidebar for desktop -->
<div class="hidden md:fixed md:inset-y-0 md:flex md:w-64 md:flex-col">
<!-- Sidebar component, swap this element with another sidebar if you like -->
<div class="flex min-h-0 flex-1 flex-col bg-gray-900/80">
<div class="flex min-h-0 flex-1 flex-col bg-slate-950/80">
<div class="flex flex-1 flex-col overflow-y-auto pt-5 pb-4">
<div class="flex flex-shrink-0 items-center px-4">
<img
class="h-8 w-auto"
src="https://tailwindui.com/img/logos/mark.svg?color=indigo&shade=500"
alt="Your Company"
/>
<img class="h-24 w-auto" src="/images/logo.webp" alt="17 Galaxies" />
</div>
<.navigation_menu class="mt-5 flex-1 space-y-1 px-2" />
</div>
Expand Down Expand Up @@ -176,51 +168,13 @@
<h1 class="text-2xl font-semibold text-gray-900">@page_title</h1>
</div> --%>
<div class="mx-auto max-w-7xl px-4 sm:px-6 md:px-8">
<div>
<dl class="mt-5 grid grid-cols-1 divide-y divide-gray-200 overflow-hidden rounded-lg bg-white shadow md:grid-cols-5 md:divide-y-0 md:divide-x">
<div class="px-4 py-5 sm:p-6">
<dt class="text-base font-normal text-gray-900">Metal</dt>
<dd class="mt-1">
<div class="text-2xl font-semibold text-indigo-600">
71,897
</div>
</dd>
</div>

<div class="px-4 py-5 sm:p-6">
<dt class="text-base font-normal text-gray-900">Crystal</dt>
<dd class="mt-1">
<div class="text-2xl font-semibold text-indigo-600">
71,897
</div>
</dd>
</div>
<div class="px-4 py-5 sm:p-6">
<dt class="text-base font-normal text-gray-900">Deuterium</dt>
<dd class="mt-1">
<div class="text-2xl font-semibold text-indigo-600">
71,897
</div>
</dd>
</div>
<div class="px-4 py-5 sm:p-6">
<dt class="text-base font-normal text-gray-900">Energy</dt>
<dd class="mt-1">
<div class="text-2xl font-semibold text-indigo-600">
71,897
</div>
</dd>
</div>

<div class="px-4 py-5 sm:p-6">
<dt class="text-base font-normal text-gray-900">Dark Matter</dt>
<dd class="mt-1">
<div class="text-2xl font-semibold text-indigo-600">
71,897
</div>
</dd>
</div>
</dl>
<div class="mx-auto max-w-xl">
<.live_component
class="max-w-2xl"
module={GalaxiesWeb.Components.PlanetResources}
id="hero"
planet={@current_planet}
/>
</div>

<%!-- <ul>
Expand All @@ -245,7 +199,7 @@
</ul> --%>
<%!-- <%= @inner_content %> --%>
<main class="px-4 py-20 sm:px-6 lg:px-8 text-white">
<div class="mx-auto max-w-2xl">
<div class="mx-auto max-w-4xl">
<.flash_group flash={@flash} />
<%= @inner_content %>
</div>
Expand Down
55 changes: 55 additions & 0 deletions lib/galaxies_web/components/planet_resources.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
defmodule GalaxiesWeb.Components.PlanetResources do
use GalaxiesWeb, :live_component

def render(assigns) do
~H"""
<div>
<%!-- swap md:grid-cols-4 for md:grid-cols-5 when adding dark matter --%>
<dl class="mt-5 grid grid-cols-1 divide-y divide-gray-200 overflow-hidden rounded-lg bg-white shadow md:grid-cols-4 md:divide-y-0 md:divide-x">
<div class="px-2 py-2 sm:p-6">
<dt class="text-base font-normal text-gray-900">Metal</dt>
<dd class="mt-1">
<div class="text-2xl font-semibold text-indigo-600">
<%= @planet.metal_units %>
</div>
</dd>
</div>
<div class="px-4 py-5 sm:p-6">
<dt class="text-base font-normal text-gray-900">Crystal</dt>
<dd class="mt-1">
<div class="text-2xl font-semibold text-indigo-600">
<%= @planet.crystal_units %>
</div>
</dd>
</div>
<div class="px-4 py-5 sm:p-6">
<dt class="text-base font-normal text-gray-900">Deuterium</dt>
<dd class="mt-1">
<div class="text-2xl font-semibold text-indigo-600">
<%= @planet.deuterium_units %>
</div>
</dd>
</div>
<div class="px-4 py-5 sm:p-6">
<dt class="text-base font-normal text-gray-900">Energy</dt>
<dd class="mt-1">
<div class="text-2xl font-semibold text-indigo-600">
<%= @planet.available_energy %>
</div>
</dd>
</div>
<%!-- <div class="px-4 py-5 sm:p-6">
<dt class="text-base font-normal text-gray-900">Dark Matter</dt>
<dd class="mt-1">
<div class="text-2xl font-semibold text-indigo-600">
71,897
</div>
</dd>
</div> --%>
</dl>
</div>
"""
end
end
8 changes: 8 additions & 0 deletions lib/galaxies_web/live/alliance_live.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
defmodule GalaxiesWeb.AllianceLive do
use Phoenix.LiveView

alias Galaxies.Accounts

def mount(_params, _session, socket) do
{:ok,
socket
|> assign(:current_planet, Accounts.get_active_planet(socket.assigns.current_player))}
end

def render(assigns) do
~H"""
Alliance
Expand Down
49 changes: 0 additions & 49 deletions lib/galaxies_web/live/buildings_live.ex

This file was deleted.

7 changes: 0 additions & 7 deletions lib/galaxies_web/live/buildings_live.html.heex

This file was deleted.

Loading

0 comments on commit 51a24a1

Please sign in to comment.