Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/issues.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
[
{
"title": "logic to check if the item is durable, because an item that has quantity > 1 cannot be stackable",
"description": "An item that has quantity > 1 cannot be stackable",
"labels": ["bug"]
}
]
5 changes: 2 additions & 3 deletions src/Application/UseCase/Dice/RollDice/RollDiceUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Eco\Result;
use RPGPlayground\Application\UseCase\Dice\RollDice\RollDiceInput;
use RPGPlayground\Application\UseCase\Dice\RollDice\RollDiceOutput;
use RPGPlayground\Domain\Actions\Dice\RollDiceAction;
use RPGPlayground\Domain\Enums\Roll\RollAttribute;

final class RollDiceUseCase
Expand Down Expand Up @@ -46,11 +45,11 @@ private static function roll(RollDiceInput $input): array
$rolls = [];

for ($i = 0; $i < $input->multiplier; $i++) {
$rolls[] = RollDiceAction::roll($input->dice);
$rolls[] = $input->dice->roll();
}

if ($input->attribute !== null) {
$rolls[] = RollDiceAction::roll($input->dice);
$rolls[] = $input->dice->roll();
}

return $rolls;
Expand Down
2 changes: 2 additions & 0 deletions src/Core/Handler/LogHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

// TODO: Move LogHandler to infrastructure layer

declare(strict_types=1);

namespace RPGPlayground\Core\Handler;
Expand Down
21 changes: 0 additions & 21 deletions src/Domain/Actions/Dice/RollDiceAction.php

This file was deleted.

72 changes: 72 additions & 0 deletions src/Domain/Entities/Character.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace RPGPlayground\Domain\Entities;

use RPGPlayground\Core\Handler\StrHandler;
use RPGPlayground\Domain\Entities\Inventory;
use RPGPlayground\Domain\ValueObjects\Attributes;
use RPGPlayground\Domain\ValueObjects\Character\Identity;
use RPGPlayground\Domain\ValueObjects\Character\Statistics;

class Character
{
private Inventory $inventory;

/**
* @param string $name
* @param string $description
* @param Identity $identity
* @param Statistics $statistics
* @param Attributes $attributes
*/
private function __construct(
public readonly string $name,
public readonly string $description,
public readonly Identity $identity,
public readonly Statistics $statistics,
public readonly Attributes $attributes,
) {}

/**
* @param string $name
* @param string $description
* @param Identity $identity
* @param Statistics $statistics
* @param Attributes $attributes
* @return self
* @throws \InvalidArgumentException
*/
public static function create(
string $name,
string $description,
Identity $identity,
Statistics $statistics,
Attributes $attributes,
): self {
if (empty($name)) {
throw new \InvalidArgumentException('Name cannot be empty.');
}

$name = StrHandler::sanitize($name);

if (empty($description)) {
throw new \InvalidArgumentException('Description cannot be empty.');
}

$description = StrHandler::sanitize($description);

return new self($name, $description, $identity, $statistics, $attributes);
}

public function setInventory(Inventory $inventory): void
{
$this->inventory = $inventory;
}

public function getInventory(): Inventory
{
return $this->inventory;
}
}
4 changes: 4 additions & 0 deletions src/Domain/Entities/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace RPGPlayground\Domain\Entities;

use RPGPlayground\Core\Handler\StrHandler;

final class Check
{
private function __construct(
Expand All @@ -23,6 +25,8 @@ public static function create(string $title, int $threshold): self
throw new \InvalidArgumentException('Title cannot be empty.');
}

$title = StrHandler::sanitize($title);

if ($threshold < 0) {
throw new \InvalidArgumentException('Threshold must be greater than or equal to 0.');
}
Expand Down
146 changes: 146 additions & 0 deletions src/Domain/Entities/Inventory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

declare(strict_types=1);

namespace RPGPlayground\Domain\Entities;

use RPGPlayground\Core\Utils\Identifier;
use RPGPlayground\Domain\Entities\Item\InventoryItem;

class Inventory
{
/**
* @param InventoryItem[] $items
* @param int $slots
* @param float $maxWeight
* @return void
* @throws \InvalidArgumentException
*/
public function __construct(
private array $items,
public readonly int $slots,
public readonly float $maxWeight,
) {
if ($slots < 1) {
throw new \InvalidArgumentException('Slots must be greater than 0');
}

if ($maxWeight < 0) {
throw new \InvalidArgumentException('Max weight must be greater than or equal to 0');
}

$this->checkItemsWeight($items);
}

/**
* @param InventoryItem[] $items
* @return static
* @throws \InvalidArgumentException
*/
private function checkItemsWeight(array $items): void
{
$totalWeight = 0;

foreach ($items as $item) {
$totalWeight += $item->getItem()->getWeight();
}

if ($totalWeight > $this->maxWeight) {
throw new \InvalidArgumentException('Total weight of items must be less than or equal to max weight');
}
}

public function add(InventoryItem $item): void
{
if ($this->has($item->getItem()->getId())) {
throw new \InvalidArgumentException('Item already exists in inventory');
}

if ((count($this->items) + $item->getQuantity()) > $this->slots) {
throw new \InvalidArgumentException('Inventory is full');
}

// TODO: checks if the inventory will fit the items if the new item is added
$this->checkItemsWeight([...$this->items, $item]);

$this->items[$item->getItem()->getId()->value] = $item;
}

/**
* @param Identifier $id
* @return void
*/
public function remove(Identifier $id): void
{
if (!$this->has($id)) {
throw new \InvalidArgumentException('Item not found in inventory');
}

unset($this->items[$id->value]);
}

/**
* @param Identifier $id
* @param int $quantity
* @return self
* @throws \InvalidArgumentException
*/
public function drop(Identifier $id, int $quantity): self
{
if (!$this->has($id)) {
throw new \InvalidArgumentException('Item not found in inventory');
}

$item = $this->get($id);

if ($item->getQuantity() == $quantity) {
$this->remove($id);
return $this;
}

if ($quantity > $item->getQuantity()) {
throw new \InvalidArgumentException("Not enough quantity of {$item->getItem()->getName()} to drop");
}

$item->setQuantity($item->getQuantity() - $quantity);

$this->items[$id->value] = $item;

return $this;
}

/**
* @param Identifier $id
* @return InventoryItem
* @throws \InvalidArgumentException
*/
public function get(Identifier $id): InventoryItem
{
if (!$this->has($id)) {
throw new \InvalidArgumentException('Item not found in inventory');
}

return $this->items[$id->value];
}

/**
* @param Identifier $id
* @return bool
*/
public function has(Identifier $id): bool
{
if (empty($id)) {
throw new \InvalidArgumentException('Id cannot be empty');
}

return array_key_exists($id->value, $this->items);
}

/**
* @return InventoryItem[]
*/
public function show(): array
{
return $this->items;
}
}
81 changes: 81 additions & 0 deletions src/Domain/Entities/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace RPGPlayground\Domain\Entities;

use RPGPlayground\Core\Handler\StrHandler;
use RPGPlayground\Core\Utils\Identifier;
use RPGPlayground\Domain\ValueObjects\Attributes;

class Item
{
/**
* @param Identifier $id
* @param string $name
* @param string $description
* @param float $weight
* @param Attributes $attributes
*/
public function __construct(
private Identifier $id,
private string $name,
private string $description,
private float $weight,
private Attributes $attributes,
) {
if (empty($name)) {
throw new \InvalidArgumentException('Name cannot be empty');
}

if (empty($description)) {
throw new \InvalidArgumentException('Description cannot be empty');
}

$name = StrHandler::sanitize($name);
$description = StrHandler::sanitize($description);

$this->name = $name;
$this->description = $description;
}

/**
* @return Identifier
*/
public function getId(): Identifier
{
return $this->id;
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* @return string
*/
public function getDescription(): string
{
return $this->description;
}

/**
* @return float
*/
public function getWeight(): float
{
return $this->weight;
}

/**
* @return Attributes
*/
public function getAttributes(): Attributes
{
return $this->attributes;
}
}
Loading
Loading