192 lines
4.8 KiB
PHP
192 lines
4.8 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Services\Game;
|
||
|
|
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Models\Notification;
|
||
|
|
use App\Models\Character;
|
||
|
|
use App\Models\Crime;
|
||
|
|
|
||
|
|
class NotificationService
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Send a notification to a user
|
||
|
|
*/
|
||
|
|
public function send(User $user, string $type, array $data = [], bool $saveActivity = true): Notification
|
||
|
|
{
|
||
|
|
$notification = Notification::createForUser($user, $type, $data);
|
||
|
|
|
||
|
|
return $notification;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send a system notification
|
||
|
|
*/
|
||
|
|
public function sendSystem(User $user, string $message): Notification
|
||
|
|
{
|
||
|
|
return Notification::sendSystem($user, $message);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send a crime success notification
|
||
|
|
*/
|
||
|
|
public function sendCrimeSuccess(User $user, Crime $crime, int $reward): Notification
|
||
|
|
{
|
||
|
|
return Notification::sendCrimeSuccess($user, $crime, $reward);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send a crime failure notification
|
||
|
|
*/
|
||
|
|
public function sendCrimeFailure(User $user, Crime $crime): Notification
|
||
|
|
{
|
||
|
|
return Notification::sendCrimeFailure($user, $crime);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send a level up notification
|
||
|
|
*/
|
||
|
|
public function sendLevelUp(User $user, Character $character, int $newLevel): Notification
|
||
|
|
{
|
||
|
|
return $this->send($user, 'level_up', [
|
||
|
|
'character_name' => $character->name,
|
||
|
|
'level' => $newLevel,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send a money earned notification
|
||
|
|
*/
|
||
|
|
public function sendMoneyEarned(User $user, int $amount, string $reason): Notification
|
||
|
|
{
|
||
|
|
return $this->send($user, 'money_earned', [
|
||
|
|
'amount' => $amount,
|
||
|
|
'reason' => $reason,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send a money spent notification
|
||
|
|
*/
|
||
|
|
public function sendMoneySpent(User $user, int $amount, string $reason): Notification
|
||
|
|
{
|
||
|
|
return $this->send($user, 'money_spent', [
|
||
|
|
'amount' => $amount,
|
||
|
|
'reason' => $reason,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send an energy low notification
|
||
|
|
*/
|
||
|
|
public function sendEnergyLow(User $user, Character $character): Notification
|
||
|
|
{
|
||
|
|
return $this->send($user, 'energy_low', [
|
||
|
|
'character_name' => $character->name,
|
||
|
|
'energy' => $character->energy,
|
||
|
|
'max_energy' => $character->max_energy,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send a health low notification
|
||
|
|
*/
|
||
|
|
public function sendHealthLow(User $user, Character $character): Notification
|
||
|
|
{
|
||
|
|
return $this->send($user, 'health_low', [
|
||
|
|
'character_name' => $character->name,
|
||
|
|
'health' => $character->health,
|
||
|
|
'max_health' => $character->max_health,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send a character dead notification
|
||
|
|
*/
|
||
|
|
public function sendCharacterDead(User $user, Character $character): Notification
|
||
|
|
{
|
||
|
|
return $this->send($user, 'character_dead', [
|
||
|
|
'character_name' => $character->name,
|
||
|
|
'revive_cost' => config('game.health.hospital_cost', 1000),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Send a character revived notification
|
||
|
|
*/
|
||
|
|
public function sendCharacterRevived(User $user, Character $character): Notification
|
||
|
|
{
|
||
|
|
return $this->send($user, 'character_revived', [
|
||
|
|
'character_name' => $character->name,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get unread notifications for a user
|
||
|
|
*/
|
||
|
|
public function getUnread(User $user): \Illuminate\Database\Eloquent\Collection
|
||
|
|
{
|
||
|
|
return $user->notifications()
|
||
|
|
->whereNull('read_at')
|
||
|
|
->orderBy('created_at', 'desc')
|
||
|
|
->get();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get all notifications for a user
|
||
|
|
*/
|
||
|
|
public function getAll(User $user, int $limit = 20): \Illuminate\Database\Eloquent\Collection
|
||
|
|
{
|
||
|
|
return $user->notifications()
|
||
|
|
->orderBy('created_at', 'desc')
|
||
|
|
->take($limit)
|
||
|
|
->get();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Mark a notification as read
|
||
|
|
*/
|
||
|
|
public function markAsRead(Notification $notification): void
|
||
|
|
{
|
||
|
|
$notification->markAsRead();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Mark all notifications as read for a user
|
||
|
|
*/
|
||
|
|
public function markAllAsRead(User $user): int
|
||
|
|
{
|
||
|
|
return $user->notifications()
|
||
|
|
->whereNull('read_at')
|
||
|
|
->update(['read_at' => now()]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Delete a notification
|
||
|
|
*/
|
||
|
|
public function delete(Notification $notification): bool
|
||
|
|
{
|
||
|
|
return $notification->delete();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Delete all read notifications for a user
|
||
|
|
*/
|
||
|
|
public function deleteAllRead(User $user): int
|
||
|
|
{
|
||
|
|
return $user->notifications()
|
||
|
|
->whereNotNull('read_at')
|
||
|
|
->delete();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the count of unread notifications for a user
|
||
|
|
*/
|
||
|
|
public function getUnreadCount(User $user): int
|
||
|
|
{
|
||
|
|
return $user->notifications()
|
||
|
|
->whereNull('read_at')
|
||
|
|
->count();
|
||
|
|
}
|
||
|
|
}
|