Passed
Push — master ( a37b13...cfbde9 )
by John
03:42
created

AccountModel   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 61
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A rankList() 0 14 3
A detail() 0 16 1
A generateContestAccount() 0 25 2
A add() 0 12 1
A generatePassword() 0 9 2
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Facades\Hash;
8
use Cache;
9
10
class AccountModel extends Model
11
{
12
    public function generatePassword($length=8)
13
    {
14
        $chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789';
15
16
        $password='';
17
        for ($i=0; $i<$length; $i++) {
18
            $password.=$chars[mt_rand(0, strlen($chars)-1)];
19
        }
20
        return $password;
21
    }
22
23
    public function generateContestAccount($cid, $ccode, $num)
24
    {
25
        $ret=[];
26
        $starting=DB::table("users")->where(["contest_account"=>$cid])->count();
27
        $contestModel=new ContestModel();
28
        for ($i=1; $i<=$num; $i++) {
29
            $pass=$this->generatePassword();
30
            $name=strtoupper($ccode).str_pad($starting+$i, 3, "0", STR_PAD_LEFT);
31
            $uid=$this->add([
32
                'name' => $name,
33
                'email' => "[email protected]",
34
                'email_verified_at' => date("Y-m-d H:i:s"),
35
                'password' => $pass,
36
                'avatar' => "/static/img/avatar/default.png",
37
                'contest_account' => $cid
38
            ]);
39
            $contestModel->grantAccess($uid, $cid, 1);
40
            $ret[]=[
41
                "uid"=>$uid,
42
                "name"=>$name,
43
                "email"=>"[email protected]",
44
                "password"=>$pass
45
            ];
46
        }
47
        return $ret;
48
    }
49
50
    public function add($data)
51
    {
52
        return DB::table("users")->insertGetId([
53
            'name' => $data['name'],
54
            'email' => $data['email'],
55
            'email_verified_at' => $data["email_verified_at"],
56
            'password' => Hash::make($data['password']),
57
            'avatar' => $data["avatar"],
58
            'contest_account' => $data["contest_account"],
59
            'remember_token'=>null,
60
            'created_at'=>date("Y-m-d H:i:s"),
61
            'updated_at'=>date("Y-m-d H:i:s")
62
        ]);
63
    }
64
65
    public function detail($uid)
66
    {
67
        $ret=DB::table("users")->where(["id"=>$uid])->first();
68
        $ret["submissionCount"]=DB::table("submission")->where([
69
            "uid"=>$uid,
70
        ])->whereNotIn('verdict', [
71
            'System Error',
72
            'Submission Error'
73
        ])->count();
74
        $ret["solved"]=DB::table("submission")->where([
75
            "uid"=>$uid,
76
            "verdict"=>"Accepted"
77
        ])->join("problem","problem.pid","=","submission.pid")->select('pcode')->distinct()->get()->all();
78
        $ret["solvedCount"]=count($ret["solved"]);
79
        $ret["rank"]=Cache::tags(['rank'])->get($ret["uid"],"N/A");
80
        return $ret;
81
    }
82
83
    public function rankList()
84
    {
85
        Cache::tags(['rank'])->flush();
86
        $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")->all();
87
        $rankIter=1;
88
        $rankValue=1;
89
        $rankSolved=-1;
90
        foreach($rankList as $rankItem){
91
            if($rankSolved!=$rankItem["solvedCount"]){
92
                $rankValue=$rankIter;
93
                $rankSolved=$rankItem["solvedCount"];
94
            }
95
            Cache::tags(['rank'])->put($rankItem["uid"], $rankValue);
96
            $rankIter++;
97
        }
98
    }
99
}
100