-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from ImNotAVirus/feature/item-basics
Feature/item basics
- Loading branch information
Showing
15 changed files
with
740 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule ElvenData.Enums.ItemEnums do | ||
@moduledoc """ | ||
TODO: Documentation | ||
""" | ||
|
||
import SimpleEnum, only: [defenum: 2] | ||
|
||
## Enums | ||
|
||
defenum :inventory_type, | ||
# equipped: not present in the game, for DB purpose | ||
equipped: -1, | ||
equipment: 0, | ||
main: 1, | ||
etc: 2, | ||
miniland: 3, | ||
specialist: 6, | ||
costume: 7 | ||
|
||
defenum :slot_type, [ | ||
:main_weapon, | ||
:armor, | ||
:hat, | ||
:gloves, | ||
:boots, | ||
:secondary_weapon, | ||
:necklace, | ||
:ring, | ||
:bracelet, | ||
:mask, | ||
:fairy, | ||
:amulet, | ||
:sp, | ||
:costume_suit, | ||
:costume_hat, | ||
:weapon_skin, | ||
:wings | ||
] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Config | ||
|
||
# Import environment specific config. This must remain at the bottom | ||
# of this file so it overrides the configuration defined above. | ||
if File.exists?("#{__DIR__}/runtime.#{config_env()}.exs") do | ||
Code.require_file("runtime.#{config_env()}.exs", __DIR__) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Config | ||
|
||
config :elven_database, ElvenDatabase.Repo, | ||
database: "elvengard_test", | ||
username: "postgres", | ||
password: "postgres", | ||
hostname: "localhost", | ||
pool: Ecto.Adapters.SQL.Sandbox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
defmodule ElvenDatabase.Players.Item do | ||
@moduledoc false | ||
|
||
use Ecto.Schema | ||
|
||
import Ecto.Changeset | ||
|
||
require ElvenData.Enums.ItemEnums, as: ItemEnums | ||
|
||
alias __MODULE__ | ||
alias ElvenDatabase.Players.Character | ||
|
||
@type id :: non_neg_integer() | ||
@type t :: %Item{ | ||
id: id(), | ||
owner_id: non_neg_integer(), | ||
inventory_type: ItemEnums.inventory_type_keys(), | ||
slot: ItemEnums.slot_type() | non_neg_integer(), | ||
vnum: non_neg_integer(), | ||
quantity: non_neg_integer(), | ||
# Ecto fields | ||
__meta__: Ecto.Schema.Metadata.t(), | ||
inserted_at: any(), | ||
updated_at: any() | ||
} | ||
|
||
## Schema | ||
|
||
schema "items" do | ||
belongs_to :owner, ElvenDatabase.Players.Character | ||
|
||
field :inventory_type, Ecto.Enum, values: ItemEnums.inventory_type(:__keys__) | ||
field :slot, :integer | ||
field :vnum, :integer | ||
field :quantity, :integer | ||
|
||
timestamps() | ||
end | ||
|
||
## Public API | ||
|
||
@fields [ | ||
:owner_id, | ||
:inventory_type, | ||
:slot, | ||
:vnum, | ||
:quantity | ||
] | ||
|
||
@spec changeset(Item.t(), map()) :: Ecto.Changeset.t() | ||
def changeset(%Item{} = item, attrs) do | ||
attrs = | ||
case attrs do | ||
%{slot: slot} when is_atom(slot) -> Map.put(attrs, :slot, ItemEnums.slot_type(slot)) | ||
attrs -> attrs | ||
end | ||
|
||
attrs = | ||
case attrs do | ||
%{owner: %Character{} = owner} -> Map.put(attrs, :owner_id, owner.id) | ||
attrs -> attrs | ||
end | ||
|
||
item | ||
|> cast(attrs, @fields) | ||
|> foreign_key_constraint(:owner_id) | ||
|> validate_required(@fields) | ||
|> unique_constraint(:slot, name: :owner_inventory_slot) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
defmodule ElvenDatabase.Players.Items do | ||
@moduledoc """ | ||
TODO: Documentation | ||
""" | ||
|
||
import Ecto.Query, only: [from: 2] | ||
|
||
alias ElvenDatabase.Players.Item | ||
alias ElvenDatabase.Repo | ||
|
||
# Dyalizer doesn't like `Item.changeset(%Item{}, attrs)` | ||
# because fields on Item struct can't be nil | ||
@dialyzer [ | ||
{:no_return, create: 1, create!: 1}, | ||
{:no_fail_call, create: 1, create!: 1} | ||
] | ||
|
||
## Public API | ||
|
||
@spec create(map()) :: {:ok, Item.t()} | {:error, Ecto.Changeset.t()} | ||
def create(attrs) do | ||
%Item{} | ||
|> Item.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@spec create!(map()) :: Item.t() | ||
def create!(attrs) do | ||
%Item{} | ||
|> Item.changeset(attrs) | ||
|> Repo.insert!() | ||
end | ||
|
||
@spec get(Item.id()) :: {:ok, Item.t()} | {:error, :not_found} | ||
def get(id) do | ||
case Repo.get(Item, id) do | ||
nil -> {:error, :not_found} | ||
item -> {:ok, item} | ||
end | ||
end | ||
|
||
@spec get!(Item.id()) :: Item.t() | ||
def get!(id) do | ||
Repo.get!(Item, id) | ||
end | ||
|
||
@spec list_by_owner(non_neg_integer()) :: [Item.t()] | ||
def list_by_owner(character_id) do | ||
from(c in Item, where: c.owner_id == ^character_id) | ||
|> Repo.all() | ||
end | ||
|
||
@spec update(Item.t(), map()) :: {:ok, Item.t()} | {:error, Ecto.Changeset.t()} | ||
def update(%Item{} = item, attrs) do | ||
item | ||
|> Item.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@spec update!(Item.t(), map()) :: {:ok, Item.t()} | {:error, Ecto.Changeset.t()} | ||
def update!(%Item{} = item, attrs) do | ||
item | ||
|> Item.changeset(attrs) | ||
|> Repo.update!() | ||
end | ||
|
||
@spec delete(Item.t()) :: {:ok, Item.t()} | {:error, Ecto.Changeset.t()} | ||
def delete(%Item{} = item) do | ||
Repo.delete(item) | ||
end | ||
|
||
@spec delete!(Item.t()) :: {:ok, Item.t()} | {:error, Ecto.Changeset.t()} | ||
def delete!(%Item{} = item) do | ||
Repo.delete!(item) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 3 additions & 6 deletions
9
apps/elven_database/priv/repo/migrations/20210414112303_create_accounts.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 3 additions & 6 deletions
9
apps/elven_database/priv/repo/migrations/20210515152135_create_characters.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
apps/elven_database/priv/repo/migrations/20240820084419_create_items.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule ElvenDatabase.Repo.Migrations.CreateItems do | ||
use Ecto.Migration | ||
|
||
require ElvenDatabase.EctoEnumHelpers, as: EctoEnumHelpers | ||
require ElvenData.Enums.ItemEnums, as: ItemEnums | ||
|
||
def change() do | ||
execute( | ||
EctoEnumHelpers.create_query(ItemEnums, :inventory_type), | ||
EctoEnumHelpers.drop_query(:inventory_type) | ||
) | ||
|
||
create table(:items) do | ||
add :owner_id, references(:characters), null: false | ||
add :inventory_type, :inventory_type_enum, null: false | ||
add :slot, :int2, null: false | ||
add :vnum, :int2, null: false | ||
add :quantity, :int2, null: false | ||
|
||
timestamps() | ||
end | ||
|
||
create unique_index(:items, [:owner_id, :inventory_type, :slot], name: :owner_inventory_slot) | ||
end | ||
end |
Oops, something went wrong.