Skip to content

Commit

Permalink
Reads directly with Eloquent (#51)
Browse files Browse the repository at this point in the history
* No Domain reads: Character display through Eloquent.

* No Domain reads: Using Eloquent model for Character read operations.

* No Domain reads: Removing ContainsModel trait.

* Refactoring battle turn creation.

# Conflicts:
#	app/Modules/Battle/Domain/Entities/BattleRound.php

* Refactoring Battle generation.

# Conflicts:
#	app/Modules/Battle/Domain/Entities/Battle.php

* Refactoring Battle generation.

# Conflicts:
#	app/Modules/Battle/Domain/Entities/Battle.php
  • Loading branch information
mchekin authored Feb 14, 2020
1 parent 3998b41 commit c54928b
Show file tree
Hide file tree
Showing 34 changed files with 191 additions and 339 deletions.
73 changes: 16 additions & 57 deletions app/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,98 +40,57 @@ class Character extends Model

protected $guarded = [];

/**
* Get the user of the character
*
* @return BelongsTo
*/
public function user()
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

/**
* Get the user of the character
*
* @return BelongsTo
*/
public function race()
public function race(): BelongsTo
{
return $this->belongsTo(Race::class);
}

/**
* Get the current location of the character
*
* @return BelongsTo
*/
public function location()
public function location(): BelongsTo
{
return $this->belongsTo(Location::class);
}

/**
* Get the user of the character
*
* @return BelongsTo
*/
public function profilePicture()
public function profilePicture(): BelongsTo
{
return $this->belongsTo(Image::class, 'profile_picture_id');
}

/**
* @return HasMany
*/
public function images()
public function images(): HasMany
{
return $this->hasMany(Image::class);
}

/**
* @return HasMany
*/
public function items()
public function items(): HasMany
{
return $this->hasMany(Item::class, 'owner_character_id');
}

/**
* @return HasMany
*/
public function receivedMessages()
public function receivedMessages(): HasMany
{
return $this->hasMany(Message::class, 'to_id');
}

/**
* @return HasMany
*/
public function sentMessages()
public function sentMessages(): HasMany
{
return $this->hasMany(Message::class, 'from_id');
}

/**
* @return HasMany
*/
public function attacks()
public function attacks(): HasMany
{
return $this->hasMany(Battle::class, 'attacker_id');
}

/**
* @return HasMany
*/
public function defends()
public function defends(): HasMany
{
return $this->hasMany(Battle::class, 'defender_id');
}

/**
* @return HasMany
*/
public function battles()
public function battles(): HasMany
{
return $this->hasMany(Battle::class, 'defender_id');
}
Expand All @@ -153,12 +112,12 @@ public function isYou(): bool

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

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

public function hasProfilePicture(): bool
Expand All @@ -175,7 +134,7 @@ public function isOnline(): bool
return $this->user->isOnline();
}

public function getProfilePicture()
public function getProfilePicture(): Image
{
return $this->profilePicture;
}
Expand Down Expand Up @@ -206,7 +165,7 @@ public function getProfilePictureSmall(): string
return 'svg/avatar.svg';
}

public function getProfilePictureId()
public function getProfilePictureId(): ?string
{
return $this->profile_picture_id;
}
Expand All @@ -226,7 +185,7 @@ public function getLocationName():string
return $this->location->getName();
}

public function getId()
public function getId(): string
{
return $this->id;
}
Expand Down
9 changes: 4 additions & 5 deletions app/Http/Controllers/CharacterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace App\Http\Controllers;

use App\Character;
use App\Modules\Character\Domain\Services\CharacterService;
use App\Modules\Character\Presentation\Http\CommandMappers\AttackCharacterCommandMapper;
use App\Modules\Character\Presentation\Http\CommandMappers\CreateCharacterCommandMapper;
use App\Http\Requests\CreateCharacterRequest;
use App\Http\Requests\UpdateCharacterAttributeRequest;
use App\Modules\Character\Presentation\Http\CommandMappers\IncreaseAttributeCommandMapper;
use App\Modules\Character\Presentation\Http\CommandMappers\MoveCharacterCommandMapper;
use App\Modules\Level\Domain\Services\LevelService;
use App\Race;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -60,12 +60,11 @@ public function store(
return redirect()->route('character.show', ['character' => $character->getId()]);
}

public function show(string $characterId, LevelService $levelService): View
public function show(string $characterId): View
{
$character = $this->characterService->getOne($characterId);
$level = $levelService->getLevel($character->getLevelNumber());
$character = Character::query()->findOrFail($characterId);

return view('character.show', ['character' => $character->getModel(), 'level' => $level]);
return view('character.show', compact('character'));
}

public function update(
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/CharacterMessageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public function store(

$messageService->send($sendMessageCommand);

return redirect()->route("character.message.index", $characterId);
return redirect()->route('character.message.index', $characterId);
}
}
16 changes: 5 additions & 11 deletions app/Http/Controllers/InventoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Character;
use App\Modules\Character\Domain\Services\CharacterService;
use App\Modules\Equipment\Domain\Services\ItemService;
use App\Modules\Equipment\Presentation\Http\CommandMappers\EquipItemCommandMapper;
Expand All @@ -10,15 +11,10 @@
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use App\User as UserModel;
use Illuminate\Support\Facades\DB;

class InventoryController extends Controller
{
/**
* @var ItemService
*/
private $itemService;
/**
* @var CharacterService
*/
Expand All @@ -28,19 +24,17 @@ public function __construct(ItemService $itemService, CharacterService $characte
{
$this->middleware('auth');

$this->itemService = $itemService;
$this->characterService = $characterService;
}

public function index(Request $request, CharacterService $characterService, LevelService $levelService): View
public function index(Request $request, LevelService $levelService): View
{
/** @var UserModel $userModel */
$characterId = $request->user()->character->getId();
/** @var Character $character */
$character = $request->user()->character;

$character = $characterService->getOne($characterId);
$level = $levelService->getLevel($character->getLevelNumber());

return view('character.inventory.index', ['character' => $character->getModel(), 'level' => $level]);
return view('character.inventory.index', ['character' => $character, 'level' => $level]);
}

public function equipItem(Request $request, EquipItemCommandMapper $commandMapper): RedirectResponse
Expand Down
7 changes: 2 additions & 5 deletions app/Http/Controllers/MessageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Http\Controllers;

use Illuminate\Http\Response;
use Illuminate\View\View;

class MessageController extends Controller
{
Expand All @@ -11,10 +11,7 @@ public function __construct()
$this->middleware(['auth', 'has.character']);
}

/**
* @return Response
*/
public function index()
public function index(): View
{
return view('message.index');
}
Expand Down
39 changes: 39 additions & 0 deletions app/Http/ViewComposers/CharacterGeneralInfoComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Http\ViewComposers;

use App\Character;
use App\Modules\Level\Domain\Services\LevelService;
use Illuminate\Support\Arr;
use Illuminate\View\View;

class CharacterGeneralInfoComposer
{
/**
* @var LevelService
*/
private $levelService;

public function __construct(LevelService $levelService)
{
$this->levelService = $levelService;
}

/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
$data = $view->getData();

/** @var Character $character */
$character = Arr::get($data, 'character');

$level = $this->levelService->getLevel($character->getLevelNumber());

$view->with(compact('character', 'level'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface BattleRepositoryInterface
{
public function add(Battle $battle);
}
public function add(Battle $battle):void;
}
34 changes: 18 additions & 16 deletions app/Modules/Battle/Domain/Entities/Battle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

namespace App\Modules\Battle\Domain\Entities;

use App\Modules\Battle\Domain\Factories\BattleRoundFactory;
use App\Modules\Battle\Domain\Entities\Collections\BattleTurns;
use App\Modules\Battle\Domain\Entities\Collections\BattleRounds;
use App\Modules\Character\Domain\Entities\Character;
use App\Traits\ContainsModel;

use App\Traits\GeneratesUuid;
class Battle
{
// Todo: temporary hack of having reference to the Eloquent model
use ContainsModel;
use GeneratesUuid;

/**
* @var string
Expand All @@ -32,11 +30,6 @@ class Battle
*/
private $defender;

/**
* @var BattleRoundFactory
*/
private $roundFactory;

/**
* @var BattleRounds
*/
Expand All @@ -57,7 +50,6 @@ public function __construct(
string $locationId,
Character $attacker,
Character $defender,
BattleRoundFactory $roundFactory,
BattleRounds $rounds,
int $victorXpGained,
Character $victor = null
Expand All @@ -67,16 +59,15 @@ public function __construct(
$this->locationId = $locationId;
$this->attacker = $attacker;
$this->defender = $defender;
$this->roundFactory = $roundFactory;
$this->rounds = $rounds;
$this->victorXpGained = $victorXpGained;
$this->victor = $victor;
}

public function execute()
public function execute(): void
{
do {
$round = $this->roundFactory->create(
$round = $this->createRound(
$this->getId(),
$this->getAttacker(),
$this->getDefender()
Expand All @@ -94,7 +85,7 @@ public function execute()
$this->victorXpGained = $this->calculateVictorXpGained($loser, $this->victor);
}

protected function calculateVictorXpGained(Character $loser, Character $victor): int
private function calculateVictorXpGained(Character $loser, Character $victor): int
{
return max($loser->getLevelNumber() - $victor->getLevelNumber(), 1) * 3;
}
Expand Down Expand Up @@ -138,4 +129,15 @@ public function getLocationId(): string
{
return $this->locationId;
}
}

private function createRound(string $battleId, Character $attacker, Character $defender): BattleRound
{
return new BattleRound(
$this->generateUuid(),
$battleId,
$attacker,
$defender,
new BattleTurns()
);
}
}
Loading

0 comments on commit c54928b

Please sign in to comment.