<?php

namespace App\Http;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Notifications\PlayerPerformanceDrop;


class Helpers {
    public static function findClubsByOwnerId($owner_id){
        return DB::table('clubs')->where('owner_id', $owner_id)->get();
    }

    public static function findPercentile($player, $total_players) {
        $rating = $player->rating;

        if($rating){
            $better = DB::table('players')->where('rating', '>', $rating)->count();

            $worse = DB::table('players')->where('rating', '<', $rating)->count();

            $same = $total_players - ($better + $worse);

            if ($worse == 0) {
                $percentile = 0;
            } else {
                $percentile = ceil((($worse + $same) / ($worse + $better + 1)) * 100);
            }

            return $percentile;
        }

        return "unknown";
    }
}
