From dea73137dc142a86f6e34cbf1a7dd1effb8d1f53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristian=20St=C3=B6ckel?= Date: Sat, 20 Feb 2021 15:56:23 +0100 Subject: [PATCH] Fixed points calculation --- app/Http/Controllers/StatusController.php | 4 ---- app/Http/Controllers/TransportController.php | 1 - app/Models/User.php | 12 ++++++++-- ...155443_remove_static_points_from_users.php | 24 +++++++++++++++++++ 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 database/migrations/2021_02_20_155443_remove_static_points_from_users.php diff --git a/app/Http/Controllers/StatusController.php b/app/Http/Controllers/StatusController.php index a2e1a1c57..f8948330e 100644 --- a/app/Http/Controllers/StatusController.php +++ b/app/Http/Controllers/StatusController.php @@ -164,10 +164,6 @@ public static function DeleteStatus($user, $statusId): ?bool { $user->train_distance -= $trainCheckin->distance; $user->train_duration -= $trainCheckin->duration; - //Don't subtract points, if status outside of current point calculation - if ($trainCheckin->departure->isAfter(Carbon::parse('last thursday 3:14am'))) { - $user->points -= $trainCheckin->points; - } $user->update(); $status->delete(); return true; diff --git a/app/Http/Controllers/TransportController.php b/app/Http/Controllers/TransportController.php index d436e7392..ec2cf52ed 100644 --- a/app/Http/Controllers/TransportController.php +++ b/app/Http/Controllers/TransportController.php @@ -380,7 +380,6 @@ public static function TrainCheckin($tripId, $user->train_distance += $trainCheckin->distance; $user->train_duration += $trainCheckin->duration; - $user->points += $trainCheckin->points; $user->update(); diff --git a/app/Models/User.php b/app/Models/User.php index fde8220dc..c33ab3878 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -2,6 +2,7 @@ namespace App\Models; +use Carbon\Carbon; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsToMany; @@ -25,10 +26,10 @@ class User extends Authenticatable implements MustVerifyEmail ]; protected $casts = [ 'email_verified_at' => 'datetime', - 'private_profile' => 'boolean' + 'private_profile' => 'boolean' ]; protected $appends = [ - 'averageSpeed' + 'averageSpeed', 'points' ]; public function getAverageSpeedAttribute(): float { @@ -67,4 +68,11 @@ public function icsTokens(): HasMany { return $this->hasMany(IcsToken::class, 'user_id', 'id'); } + public function getPointsAttribute(): int { + return TrainCheckin::whereIn('status_id', $this->statuses()->select('id')) + ->where('departure', '>=', Carbon::now()->subDays(7)->toIso8601String()) + ->select('points') + ->sum('points'); + } + } diff --git a/database/migrations/2021_02_20_155443_remove_static_points_from_users.php b/database/migrations/2021_02_20_155443_remove_static_points_from_users.php new file mode 100644 index 000000000..f55d77aa2 --- /dev/null +++ b/database/migrations/2021_02_20_155443_remove_static_points_from_users.php @@ -0,0 +1,24 @@ +dropColumn('points'); + }); + } + + public function down(): void { + Schema::table('users', function(Blueprint $table) { + $table->integer('points') + ->unsigned() + ->default(0) + ->after('train_duration'); + }); + } +}