122 lines
2.9 KiB
PHP
122 lines
2.9 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Services\Game;
|
||
|
|
|
||
|
|
use App\Models\Character;
|
||
|
|
|
||
|
|
class EconomyService
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* 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
|
||
|
|
ActivityLog::logMoneyTransaction($from, 'spent', $amount, "Transfer to {$to->name}: {$reason}");
|
||
|
|
ActivityLog::logMoneyTransaction($to, 'earned', $amount, "Transfer from {$from->name}: {$reason}");
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Add money to a character
|
||
|
|
*/
|
||
|
|
public function addMoney(Character $character, int $amount, string $reason = ''): void
|
||
|
|
{
|
||
|
|
$character->addMoney($amount);
|
||
|
|
|
||
|
|
if ($amount > 0 && $reason) {
|
||
|
|
ActivityLog::logMoneyTransaction($character, 'earned', $amount, $reason);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 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) {
|
||
|
|
ActivityLog::logMoneyTransaction($character, 'spent', $amount, $reason);
|
||
|
|
}
|
||
|
|
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
}
|