forked from corowne/lorekeeper
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
small module for NPC interactions for use with dialogues / general use
- Loading branch information
1 parent
cb7afdc
commit 9db1460
Showing
11 changed files
with
332 additions
and
1 deletion.
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
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,43 @@ | ||
<?php | ||
|
||
namespace App\Models\Character; | ||
|
||
use App\Models\Model; | ||
|
||
class CharacterNpcInformation extends Model { | ||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'character_id', 'biography', 'default_affection', | ||
]; | ||
|
||
/** | ||
* The table associated with the model. | ||
* | ||
* @var string | ||
*/ | ||
protected $table = 'character_npc_information'; | ||
|
||
/** | ||
* The primary key of the model. | ||
* | ||
* @var string | ||
*/ | ||
public $primaryKey = 'character_id'; | ||
|
||
/********************************************************************************************** | ||
RELATIONS | ||
**********************************************************************************************/ | ||
|
||
/** | ||
* Get the character this profile belongs to. | ||
*/ | ||
public function character() { | ||
return $this->belongsTo(Character::class, 'character_id'); | ||
} | ||
} |
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,52 @@ | ||
<?php | ||
|
||
namespace App\Models\User; | ||
|
||
use App\Models\Model; | ||
use App\Models\Character\Character; | ||
|
||
class UserNpcAffection extends Model { | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'character_id', 'user_id', 'affection', | ||
]; | ||
|
||
/** | ||
* The table associated with the model. | ||
* | ||
* @var string | ||
*/ | ||
protected $table = 'user_npc_affection'; | ||
|
||
/** | ||
* The primary key of the model. | ||
* | ||
* @var string | ||
*/ | ||
public $primaryKey = 'user_id'; | ||
|
||
/********************************************************************************************** | ||
RELATIONS | ||
**********************************************************************************************/ | ||
|
||
/** | ||
* Get the character this belongs to. | ||
*/ | ||
public function character() { | ||
return $this->belongsTo(Character::class); | ||
} | ||
|
||
/** | ||
* Get the user this belongs to. | ||
*/ | ||
public function user() { | ||
return $this->belongsTo(User::class); | ||
} | ||
} |
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,91 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\User\User; | ||
use App\Models\User\UserNpcAffection; | ||
use App\Models\Character\Character; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class NpcManager extends Service { | ||
|
||
/********************************************************************************************** | ||
* | ||
* CHARACTER METHODS | ||
* | ||
**********************************************************************************************/ | ||
|
||
/** | ||
* Updates a character's NPC status. | ||
* | ||
* @param Character $character | ||
* @param array $data | ||
*/ | ||
public function editCharacterNpcInformation(Character $character, array $data) { | ||
DB::beginTransaction(); | ||
|
||
try { | ||
|
||
if (!isset($data['is_npc'])) { | ||
$character->npcInformation()->delete(); | ||
$character->is_npc = false; | ||
$character->save(); | ||
} else { | ||
$character->is_npc = true; | ||
$character->save(); | ||
|
||
$npcInformation = $character->npcInformation; | ||
if ($npcInformation) { | ||
$npcInformation->update($data); | ||
} else { | ||
$npcInformation = $character->npcInformation()->create($data); | ||
} | ||
} | ||
|
||
return $this->commitReturn(true); | ||
} catch (\Exception $e) { | ||
$this->setError('error', $e->getMessage()); | ||
} | ||
|
||
return $this->rollbackReturn(false); | ||
} | ||
|
||
/********************************************************************************************** | ||
* | ||
* USER METHODS | ||
* | ||
**********************************************************************************************/ | ||
|
||
/** | ||
* Updates a NPC's affection for a user. | ||
* | ||
* @param Character $character | ||
* @param User $user | ||
* @param int $quantity | ||
*/ | ||
public function updateAffection(Character $character, User $user, int $quantity) { | ||
DB::beginTransaction(); | ||
|
||
try { | ||
|
||
$affection = $user->npcAffection()->where('character_id', $character->id)->first(); | ||
|
||
if ($affection) { | ||
$affection->affection += $quantity; | ||
$affection->save(); | ||
} else { | ||
$user->npcAffection()->create([ | ||
'character_id' => $character->id, | ||
'affection' => $quantity, | ||
]); | ||
} | ||
|
||
return $this->commitReturn(true); | ||
} catch (\Exception $e) { | ||
$this->setError('error', $e->getMessage()); | ||
} | ||
|
||
return $this->rollbackReturn(false); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
database/migrations/2024_02_09_171812_create_npc_character_table.php
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,45 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateNpcCharacterTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::table('characters', function (Blueprint $table) { | ||
$table->boolean('is_npc')->default(false); | ||
}); | ||
|
||
Schema::create('character_npc_information', function (Blueprint $table) { | ||
$table->id(); | ||
$table->integer('character_id')->unsigned(); | ||
$table->integer('default_affection')->unsigned()->default(0); | ||
$table->text('biography')->nullable()->default(null); | ||
$table->integer('biography_affection_requirement')->unsigned()->default(50); | ||
}); | ||
|
||
Schema::create('user_npc_affection', function (Blueprint $table) { | ||
$table->id(); | ||
$table->integer('character_id')->unsigned(); | ||
$table->integer('user_id')->unsigned(); | ||
$table->integer('affection')->unsigned()->default(0); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::table('characters', function (Blueprint $table) { | ||
$table->dropColumn('is_npc'); | ||
}); | ||
Schema::dropIfExists('character_npc_information'); | ||
Schema::dropIfExists('user_npc_affection'); | ||
} | ||
}; |
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,16 @@ | ||
@php | ||
$affection = $user->npcAffections->where('npc_id', $character->id)->first(); | ||
if (!$affection) { | ||
$affection = $user->npcAffections()->create([ | ||
'character_id' => $character->id, | ||
'affection' => $character->npcInformation->default_affection ?? 0, | ||
]); | ||
} | ||
@endphp | ||
|
||
{{-- progress bar --}} | ||
<div class="progress mb-3"> | ||
<div class="progress-bar bg-success progress-bar-striped progress-bar-animated" role="progressbar" style="width: {{ $affection->affection }}%" aria-valuenow="{{ $affection->affection }}" aria-valuemin="0" aria-valuemax="100"> | ||
{{ $affection->affection }}% Affection | ||
</div> | ||
</div> |
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