gamify reputation added

This commit is contained in:
Benjamin Takats
2023-01-15 17:09:04 +01:00
parent 41cbce2add
commit d1c211fd5a
7 changed files with 154 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Console\Commands\Database;
use App\Gamify\Points\BookCaseOrangePilled;
use App\Models\User;
use Illuminate\Console\Command;
class SetReputation extends Command
{
/**
* The name and signature of the console command.
* @var string
*/
protected $signature = 'reputation:set';
/**
* The console command description.
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
* @return int
*/
public function handle()
{
foreach (User::query()
->with(['orangePills'])
->get() as $item) {
foreach ($item->orangePills as $orangePill) {
$orangePill->user->givePoint(new BookCaseOrangePilled($orangePill));
}
}
return Command::SUCCESS;
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Gamify\Points;
use QCod\Gamify\PointType;
class BookCaseOrangePilled extends PointType
{
public $allowDuplicates = false;
protected $payee = 'user';
/**
* Number of points
* @var int
*/
public $points = 210;
/**
* Point constructor
*
* @param $subject
*/
public function __construct($subject)
{
$this->subject = $subject;
}
/**
* User who will be receive points
* @return mixed
*/
public function payee()
{
return $this->getSubject()->user;
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Gamify\Points;
use QCod\Gamify\PointType;
class LoggedIn extends PointType
{
/**
* Number of points
*
* @var int
*/
public $points = 1;
/**
* Point constructor
*
* @param $subject
*/
public function __construct($subject)
{
$this->subject = $subject;
}
/**
* User who will be receive points
*
* @return mixed
*/
public function payee()
{
return $this->getSubject();
}
}

View File

@@ -2,6 +2,7 @@
namespace App\Http\Livewire\Auth; namespace App\Http\Livewire\Auth;
use App\Gamify\Points\LoggedIn;
use App\Models\LoginKey; use App\Models\LoginKey;
use App\Models\User; use App\Models\User;
use App\Notifications\ModelCreatedNotification; use App\Notifications\ModelCreatedNotification;

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Listeners;
use App\Gamify\Points\LoggedIn;
class AddLoginReputation
{
/**
* Create the event listener.
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param object $event
*
* @return void
*/
public function handle($event)
{
$event->user->givePoint(new LoggedIn($event->user));
}
}

View File

@@ -2,6 +2,7 @@
namespace App\Models; namespace App\Models;
use App\Gamify\Points\BookCaseOrangePilled;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -27,6 +28,13 @@ class OrangePill extends Model
'date' => 'datetime', 'date' => 'datetime',
]; ];
protected static function booted()
{
static::creating(function ($model) {
$model->user->givePoint(new BookCaseOrangePilled($model));
});
}
public function user(): BelongsTo public function user(): BelongsTo
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);

View File

@@ -2,7 +2,9 @@
namespace App\Providers; namespace App\Providers;
use App\Listeners\AddLoginReputation;
use App\Observers\EpisodeObserver; use App\Observers\EpisodeObserver;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@@ -14,6 +16,9 @@ class EventServiceProvider extends ServiceProvider
* @var array<class-string, array<int, class-string>> * @var array<class-string, array<int, class-string>>
*/ */
protected $listen = [ protected $listen = [
Login::class => [
AddLoginReputation::class,
],
Registered::class => [ Registered::class => [
SendEmailVerificationNotification::class, SendEmailVerificationNotification::class,
], ],