Skip to content

Commit

Permalink
Merge pull request #85 from mchekin/vendor-npcs
Browse files Browse the repository at this point in the history
Adding merchant npcs which with buy & sell store.
  • Loading branch information
mchekin authored Nov 4, 2022
2 parents 6743d18 + c45333b commit 5e4b129
Show file tree
Hide file tree
Showing 30 changed files with 255 additions and 148 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/TradeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function sellItem(Request $request, SellItemCommandMapper $commandMapper)
} catch (Exception $exception) {

return response()->json([
'message' => 'Error buying item: ' . $exception->getMessage()
'message' => 'Error selling item: ' . $exception->getMessage()
], 500);
}

Expand Down
7 changes: 6 additions & 1 deletion app/Http/Controllers/LocationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ public function __construct()

public function show(string $locationId)
{
$location = Location::query()->findOrFail($locationId);
/** @var Location $location */
$location = Location::query()
->with('characters.race')
->with('characters.user')
->with('adjacentLocations')
->findOrFail($locationId);

return view('location.show', compact('location'));
}
Expand Down
26 changes: 24 additions & 2 deletions app/Models/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Modules\Character\Domain\CharacterType;
use App\Modules\Equipment\Domain\ItemStatus;
use App\Modules\Equipment\Domain\ItemType;
use App\Traits\UsesStringId;
Expand All @@ -27,6 +28,7 @@
* @property string location_id
* @property Race race
* @property string gender
* @property string type
* @property int total_hit_points
* @property int victor_xp_gained
* @property Image profilePicture
Expand Down Expand Up @@ -118,14 +120,29 @@ public function isYou(): bool
return $this->isPlayerCharacter() && $this->user->isCurrentAuthenticatedUser();
}

public function isOwnMerchant(): bool
{
return $this->isMerchant() && $this->user->isCurrentAuthenticatedUser();
}

public function isPlayerCharacter(): bool
{
return $this->user !== null;
return $this->type === CharacterType::PLAYER;
}

public function isMerchant(): bool
{
return $this->type === CharacterType::MERCHANT;
}

public function isMonster(): bool
{
return $this->type === CharacterType::MONSTER;
}

public function isNPC(): bool
{
return $this->user === null;
return !$this->isPlayerCharacter();
}

public function hasProfilePicture(): bool
Expand Down Expand Up @@ -258,6 +275,11 @@ public function getGender(): string
return $this->gender;
}

public function getType(): string
{
return $this->type;
}

public function getRaceId(): int
{
return $this->race->getId();
Expand Down
6 changes: 6 additions & 0 deletions app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Modules\Equipment\Domain\ItemStatus;
use App\Traits\UsesStringId;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
Expand Down Expand Up @@ -38,6 +39,11 @@ public function inventory(): BelongsToMany
return $this->belongsToMany(Inventory::class);
}

public function prototype(): BelongsTo
{
return $this->belongsTo(ItemPrototype::class);
}

public function getId(): string
{
return $this->id;
Expand Down
8 changes: 6 additions & 2 deletions app/Models/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use App\Traits\UsesStringId;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property string id
* @property string name
* @property Collection adjacentLocations
*/
class Location extends Model
{
Expand Down Expand Up @@ -76,12 +78,14 @@ public function characters()
*/
public function adjacentLocations()
{
return $this->belongsToMany(Location::class, 'adjacent_location', 'location_id', 'adjacent_location_id');
return $this->belongsToMany(Location::class, 'adjacent_location', 'location_id', 'adjacent_location_id')->withPivot('direction');
}

public function adjacent($type)
{
return $this->adjacentLocations()->wherePivot('direction', $type)->first();
return $this->adjacentLocations->filter(function (Location $value, $key) use ($type) {
return $value->getOriginal('pivot_direction') === $type;
})->first();
}

public function addAdjacentLocation(Location $adjacent, $direction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class CreateCharacterCommand
* @var string
*/
private $gender;
/**
* @var string
*/
private $type;

/**
* @var int
*/
Expand All @@ -24,10 +29,11 @@ class CreateCharacterCommand
*/
private $userId;

public function __construct(string $name, string $gender, int $raceId, string $userId)
public function __construct(string $name, string $gender, string $type, int $raceId, string $userId)
{
$this->name = $name;
$this->gender = $gender;
$this->type = $type;
$this->raceId = $raceId;
$this->userId = $userId;
}
Expand All @@ -42,6 +48,11 @@ public function getGender(): string
return $this->gender;
}

public function getCharacterType(): string
{
return $this->type;
}

public function getRaceId(): int
{
return $this->raceId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use App\Modules\Character\Application\Commands\CreateCharacterCommand;
use App\Modules\Character\Domain\CharacterId;
use App\Modules\Character\Domain\CharacterType;
use App\Modules\Character\Domain\Race;
use App\Modules\Equipment\Domain\Inventory;
use App\Modules\Character\Domain\Statistics;
Expand All @@ -26,6 +27,7 @@ public function create(CharacterId $characterId, CreateCharacterCommand $command
$race->getStartingLocationId(),
$command->getName(),
new Gender($command->getGender()),
new CharacterType($command->getCharacterType()),
0,
new Reputation(0),
new Attributes([
Expand Down
19 changes: 17 additions & 2 deletions app/Modules/Character/Domain/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class Character
* @var Gender
*/
private $gender;
/**
* @var CharacterType
*/
private $type;
/**
* @var int
*/
Expand Down Expand Up @@ -78,6 +82,7 @@ public function __construct(
string $locationId,
string $name,
Gender $gender,
CharacterType $type,
int $xp,
Reputation $reputation,
Attributes $attributes,
Expand All @@ -86,11 +91,11 @@ public function __construct(
Inventory $inventory,
int $userId = null,
ImageId $profilePictureId = null
)
{
) {
$this->id = $id;
$this->name = $name;
$this->gender = $gender;
$this->type = $type;
$this->levelId = $levelId;
$this->raceId = $raceId;
$this->locationId = $locationId;
Expand Down Expand Up @@ -239,6 +244,11 @@ public function getGender(): Gender
return $this->gender;
}

public function getType(): CharacterType
{
return $this->type;
}

public function getXp(): int
{
return $this->xp;
Expand Down Expand Up @@ -347,4 +357,9 @@ public function getInventory(): Inventory
{
return $this->inventory;
}

public function isMerchant(): bool
{
return $this->type->isMerchant();
}
}
42 changes: 42 additions & 0 deletions app/Modules/Character/Domain/CharacterType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php


namespace App\Modules\Character\Domain;

use InvalidArgumentException;

class CharacterType
{
public const PLAYER = 'player';
public const MERCHANT = 'merchant';
public const CIVILIAN = 'civilian';
public const MONSTER = 'monster';

public const TYPES = [
self::PLAYER,
self::MERCHANT,
self::CIVILIAN,
self::MONSTER,
];

private $value;

public function __construct(string $value)
{
if (!in_array($value, self::TYPES, true)) {
throw new InvalidArgumentException("$value is not a valid Character Type");
}

$this->value = $value;
}

public function getValue(): string
{
return $this->value;
}

public function isMerchant(): bool
{
return $this->value === self::MERCHANT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace App\Modules\Character\Infrastructure\ReconstitutionFactories;

use App\Modules\Character\Domain\CharacterId;
use App\Modules\Character\Domain\CharacterType;
use App\Modules\Equipment\Infrastructure\ReconstitutionFactories\InventoryReconstitutionFactory;
use App\Modules\Character\Domain\Attributes;
use App\Modules\Character\Domain\Character;
Expand Down Expand Up @@ -40,6 +41,7 @@ public function reconstitute(CharacterModel $characterModel): Character
$characterModel->getLocationId(),
$characterModel->getName(),
new Gender($characterModel->getGender()),
new CharacterType($characterModel->getType()),
$characterModel->getXp(),
new Reputation(0),
new Attributes([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function add(Character $character): void

'name' => $character->getName(),
'gender' => $character->getGender()->getValue(),
'type' => $character->getType()->getValue(),

'xp' => $character->getXp(),
'level_id' => $character->getLevelNumber(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace App\Modules\Character\UI\Http\CommandMappers;

use App\Modules\Character\Application\Commands\CreateCharacterCommand;
use App\Modules\Character\Domain\CharacterType;
use Illuminate\Http\Request;
use App\Models\User as UserModel;

Expand All @@ -17,6 +18,7 @@ public function map(Request $request): CreateCharacterCommand
return new CreateCharacterCommand(
$request->input('name'),
$request->input('gender'),
CharacterType::PLAYER,
$request->input('race_id'),
$userModel->getId()
);
Expand Down
15 changes: 0 additions & 15 deletions app/Modules/Equipment/Domain/InventoryItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,4 @@ public function unEquip(): void
{
$this->status = ItemStatus::inBackpack();
}

public function toBaseItem(): Item
{
return new Item(
$this->getId(),
$this->getName(),
$this->getDescription(),
$this->getImageFilePath(),
$this->getType(),
$this->getEffects(),
$this->getPrice(),
$this->getPrototypeId(),
$this->getCreatorCharacterId()
);
}
}
19 changes: 18 additions & 1 deletion app/Modules/Equipment/Domain/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,25 @@ public function getPrice(): ItemPrice
return $this->price;
}

public function changePrice(ItemPrice $price): void
public function changePrice(ItemPrice $price): self
{
$this->price = $price;

return $this;
}

public function toBaseItem(): Item
{
return new Item(
$this->getId(),
$this->getName(),
$this->getDescription(),
$this->getImageFilePath(),
$this->getType(),
$this->getEffects(),
$this->getPrice(),
$this->getPrototypeId(),
$this->getCreatorCharacterId()
);
}
}
Loading

0 comments on commit 5e4b129

Please sign in to comment.