2026-06-27 14:38:18 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Game;
|
|
|
|
|
|
|
|
|
|
use App\Models\Character;
|
|
|
|
|
|
|
|
|
|
class EconomyService
|
|
|
|
|
{
|
2026-07-04 17:09:55 +03:00
|
|
|
protected ActivityLogService $activityLogService;
|
|
|
|
|
|
|
|
|
|
public function __construct(ActivityLogService $activityLogService)
|
|
|
|
|
{
|
|
|
|
|
$this->activityLogService = $activityLogService;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-27 14:38:18 +03:00
|
|
|
/**
|
|
|
|
|
* Transfer money between characters
|
|
|
|
|
*/
|
|
|
|
|
public function transfer(Character $from, Character $to, int $amount, string $reason = ''): bool
|
|
|
|
|
{
|
|
|
|
|
if (!$from->hasMoney($amount)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($from->id === $to->id) {
|
|
|
|
|
return false; // Can't transfer to self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$from->removeMoney($amount);
|
|
|
|
|
$to->addMoney($amount);
|
|
|
|
|
|
|
|
|
|
// Log the transaction
|
2026-07-04 17:09:55 +03:00
|
|
|
$this->activityLogService->logMoneyTransaction($from, 'spent', $amount, "Transfer to {$to->name}: {$reason}");
|
|
|
|
|
$this->activityLogService->logMoneyTransaction($to, 'earned', $amount, "Transfer from {$from->name}: {$reason}");
|
2026-06-27 14:38:18 +03:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add money to a character
|
|
|
|
|
*/
|
|
|
|
|
public function addMoney(Character $character, int $amount, string $reason = ''): void
|
|
|
|
|
{
|
|
|
|
|
$character->addMoney($amount);
|
|
|
|
|
|
|
|
|
|
if ($amount > 0 && $reason) {
|
2026-07-04 17:09:55 +03:00
|
|
|
$this->activityLogService->logMoneyTransaction($character, 'earned', $amount, $reason);
|
2026-06-27 14:38:18 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove money from a character
|
|
|
|
|
*/
|
|
|
|
|
public function removeMoney(Character $character, int $amount, string $reason = ''): bool
|
|
|
|
|
{
|
|
|
|
|
if (!$character->hasMoney($amount)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$character->removeMoney($amount);
|
|
|
|
|
|
|
|
|
|
if ($amount > 0 && $reason) {
|
2026-07-04 17:09:55 +03:00
|
|
|
$this->activityLogService->logMoneyTransaction($character, 'spent', $amount, $reason);
|
2026-06-27 14:38:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Deposit money to bank
|
|
|
|
|
*/
|
|
|
|
|
public function depositToBank(Character $character, int $amount): bool
|
|
|
|
|
{
|
|
|
|
|
return $character->depositToBank($amount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Withdraw money from bank
|
|
|
|
|
*/
|
|
|
|
|
public function withdrawFromBank(Character $character, int $amount): bool
|
|
|
|
|
{
|
|
|
|
|
return $character->withdrawFromBank($amount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Apply daily interest to all characters
|
|
|
|
|
*/
|
|
|
|
|
public function applyDailyInterest(): void
|
|
|
|
|
{
|
|
|
|
|
$characters = Character::all();
|
|
|
|
|
|
|
|
|
|
foreach ($characters as $character) {
|
|
|
|
|
$character->applyBankInterest();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the starting money for new characters
|
|
|
|
|
*/
|
|
|
|
|
public function getStartingMoney(): int
|
|
|
|
|
{
|
|
|
|
|
return config('game.economy.start_money', 5000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the starting bank balance
|
|
|
|
|
*/
|
|
|
|
|
public function getStartingBank(): int
|
|
|
|
|
{
|
|
|
|
|
return config('game.economy.start_bank', 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the maximum bank balance
|
|
|
|
|
*/
|
|
|
|
|
public function getMaxBank(): int
|
|
|
|
|
{
|
|
|
|
|
return config('game.economy.max_bank', 10000000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the bank interest rate
|
|
|
|
|
*/
|
|
|
|
|
public function getBankInterestRate(): float
|
|
|
|
|
{
|
|
|
|
|
return config('game.economy.bank_interest', 0.05);
|
|
|
|
|
}
|
|
|
|
|
}
|