Passed
Push — master ( 242098...eb791e )
by John
05:14
created

RankModel::getColor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use GrahamCampbell\Markdown\Facades\Markdown;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Arr;
9
use Cache;
10
11
class RankModel extends Model
12
{
13
    private static $professionalRankiing=[
0 ignored issues
show
introduced by
The private property $professionalRankiing is not used, and could be removed.
Loading history...
14
        "None"=>"wemd-black-text"
15
    ];
16
17
    public $professionalRankiingPer=[
18
        "None"=>1
19
    ];
20
21
    private static $casualRanking=[
22
        "Legendary Grandmaster"=>"cm-colorful-text",
23
        "International Grandmaster"=>"wemd-pink-text",
24
        "Grandmaster"=>"wemd-red-text",
25
        "International Master"=>"wemd-amber-text",
26
        "Master"=>"wemd-orange-text",
27
        "Candidate Master"=>"wemd-purple-text",
28
        "Expert"=>"wemd-blue-text",
29
        "Specialist"=>"wemd-cyan-text",
30
        "Pupil"=>"wemd-green-text",
31
        "Newbie"=>"wemd-gray-text",
32
    ];
33
34
    public $casualRankingPer=[
35
        "Legendary Grandmaster"=>1,
36
        "International Grandmaster"=>5,
37
        "Grandmaster"=>10,
38
        "International Master"=>10,
39
        "Master"=>50,
40
        "Candidate Master"=>100,
41
        "Expert"=>300,
42
        "Specialist"=>700,
43
        "Pupil"=>1000,
44
        "Newbie"=>400,
45
    ];
46
47
    public static function getColor($rankTitle)
48
    {
49
        if(is_null($rankTitle)) return "";
50
        return self::$casualRanking[$rankTitle];
51
    }
52
53
    public function list()
54
    {
55
        return [];
56
    }
57
58
    public function rankList()
59
    {
60
        Cache::tags(['rank'])->flush();
61
        $totUsers=DB::table("submission")->distinct()->where(["verdict"=>"Accepted"])->count();
62
        if ($totUsers>0) {
63
            $rankList=DB::select("SELECT * FROM (SELECT uid,count(DISTINCT pcode) as solvedCount from submission inner join problem on problem.pid=submission.pid and verdict=\"Accepted\" group by uid) as temp ORDER BY solvedCount desc");
64
            $rankIter=1;
65
            $rankValue=1;
66
            $rankSolved=-1;
67
            $this->procRankingPer();
68
            foreach ($rankList as $rankItem) {
69
                if ($rankSolved!=$rankItem["solvedCount"]) {
70
                    $rankValue=$rankIter;
71
                    $rankSolved=$rankItem["solvedCount"];
72
                }
73
                Cache::tags(['rank'])->put($rankItem["uid"], $rankValue, 86400);
74
                Cache::tags(['rank'])->put($rankItem["uid"]."_title", $this->getRankTitle($rankValue), 86400);
75
                $rankIter++;
76
            }
77
        }
78
    }
79
80
    private function procRankingPer()
81
    {
82
        $totUsers=DB::table("submission")->distinct()->where(["verdict"=>"Accepted"])->count();
83
        if ($totUsers>0) {
84
            $tot=0;
85
            $cur=0;
86
            foreach ($this->casualRankingPer as $c) {
87
                $tot+=$c;
88
            }
89
            foreach ($this->casualRankingPer as &$c) {
90
                $c=round($c*$totUsers/$tot);
91
                $cur+=$c;
92
                $c=$cur;
93
            }
94
            $c=$totUsers;
95
            unset($c);
96
        }
97
    }
98
99
    private function getRankTitle($rankVal)
100
    {
101
        foreach($this->casualRankingPer as $title=>$c){
102
            if($rankVal<=$c) return $title;
103
        }
104
        return Arr::last($this->casualRankingPer);
105
    }
106
}
107