update
This commit is contained in:
@@ -11,7 +11,8 @@ defmodule Odinsea.Database.Context do
|
||||
import Ecto.Query
|
||||
|
||||
alias Odinsea.Repo
|
||||
alias Odinsea.Database.Schema.{Account, Character}
|
||||
alias Odinsea.Database.Schema.{Account, Character, InventoryItem}
|
||||
alias Odinsea.Game.InventoryType
|
||||
alias Odinsea.Net.Cipher.LoginCrypto
|
||||
|
||||
# ==================================================================================================
|
||||
@@ -481,4 +482,113 @@ defmodule Odinsea.Database.Context do
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
# ==================================================================================================
|
||||
# Inventory Operations
|
||||
# ==================================================================================================
|
||||
|
||||
@doc """
|
||||
Loads all inventory items for a character.
|
||||
Returns a map of inventory types to lists of items.
|
||||
|
||||
Ported from ItemLoader.java
|
||||
"""
|
||||
def load_character_inventory(character_id) do
|
||||
items =
|
||||
InventoryItem
|
||||
|> where([i], i.characterid == ^character_id)
|
||||
|> Repo.all()
|
||||
|
||||
# Group by inventory type
|
||||
items
|
||||
|> Enum.map(&InventoryItem.to_game_item/1)
|
||||
|> Enum.group_by(fn item ->
|
||||
db_item = Enum.find(items, fn db -> db.inventoryitemid == item.id end)
|
||||
InventoryType.from_type(db_item.inventorytype)
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets items for a specific inventory type.
|
||||
"""
|
||||
def get_inventory_items(character_id, inv_type) do
|
||||
type_value = InventoryType.type_value(inv_type)
|
||||
|
||||
InventoryItem
|
||||
|> where([i], i.characterid == ^character_id and i.inventorytype == ^type_value)
|
||||
|> Repo.all()
|
||||
|> Enum.map(&InventoryItem.to_game_item/1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single inventory item by ID.
|
||||
"""
|
||||
def get_inventory_item(item_id) do
|
||||
case Repo.get(InventoryItem, item_id) do
|
||||
nil -> nil
|
||||
db_item -> InventoryItem.to_game_item(db_item)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a new inventory item.
|
||||
"""
|
||||
def create_inventory_item(character_id, inv_type, item) do
|
||||
attrs = InventoryItem.from_game_item(item, character_id, inv_type)
|
||||
|
||||
%InventoryItem{}
|
||||
|> InventoryItem.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates an existing inventory item.
|
||||
"""
|
||||
def update_inventory_item(item_id, updates) do
|
||||
case Repo.get(InventoryItem, item_id) do
|
||||
nil -> {:error, :not_found}
|
||||
db_item ->
|
||||
db_item
|
||||
|> InventoryItem.changeset(updates)
|
||||
|> Repo.update()
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes an inventory item.
|
||||
"""
|
||||
def delete_inventory_item(item_id) do
|
||||
Repo.delete_all(from(i in InventoryItem, where: i.inventoryitemid == ^item_id))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Saves all inventory items for a character.
|
||||
Used during character save.
|
||||
"""
|
||||
def save_character_inventory(character_id, inventories) do
|
||||
Enum.each(inventories, fn {inv_type, inventory} ->
|
||||
Enum.each(inventory.items, fn {_pos, item} ->
|
||||
attrs = InventoryItem.from_game_item(item, character_id, inv_type)
|
||||
|
||||
if item.id do
|
||||
# Update existing
|
||||
update_inventory_item(item.id, attrs)
|
||||
else
|
||||
# Insert new
|
||||
create_inventory_item(character_id, inv_type, item)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the count of items for a character.
|
||||
"""
|
||||
def count_inventory_items(character_id) do
|
||||
InventoryItem
|
||||
|> where([i], i.characterid == ^character_id)
|
||||
|> Repo.aggregate(:count, :inventoryitemid)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user