Team   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Importance

Changes 5
Bugs 3 Features 0
Metric Value
eloc 55
c 5
b 3
f 0
dl 0
loc 172
rs 10
wmc 29

14 Methods

Rating   Name   Duplication   Size   Complexity  
A addFailedCount() 0 8 3
A addSolution() 0 13 4
A user() 0 3 1
A markProblemAccept() 0 9 4
A setUser() 0 3 1
A __construct() 0 9 2
A isProblemAccept() 0 3 1
A getProblemAcceptTime() 0 9 2
A shouldMarkAsPenalty() 0 10 4
A totalPenalty() 0 14 3
A numberOfAccept() 0 3 1
A getProblemWACount() 0 3 1
A getPenaltyOfProblem() 0 3 1
A getNumberOfSubmit() 0 3 1
1
<?php
2
3
namespace App\Entities;
4
5
use Carbon\Carbon;
6
7
class Team
8
{
9
    protected $waCounts = [];
10
    /** @var Carbon[] */
11
    protected $timePassed = [];
12
13
    /** @var \App\Entities\User */
14
    protected $user;
15
16
    protected $accepts = 0;
17
    protected $submits = 0;
18
19
    /** @var Contest */
20
    protected $contest;
21
    private $problemCount;
22
23
    public function __construct(Contest $contest)
24
    {
25
        $this->contest = $contest;
26
27
        $this->problemCount = $contest->problems->count();
28
29
        for ($i = 0; $i < $this->problemCount; $i++) {
30
            $this->waCounts[$i] = 0;
31
            $this->timePassed[$i] = null;
32
        }
33
    }
34
35
    public function setUser(User $user)
36
    {
37
        $this->user = $user;
38
    }
39
40
    public function user(): User
41
    {
42
        return $this->user;
43
    }
44
45
    /**
46
     * 添加新的solution,统计数据.
47
     *
48
     * @param Solution $solution
49
     */
50
    public function addSolution(Solution $solution)
51
    {
52
        if ($this->problemCount === 0) {
53
            return;
54
        }
55
        $this->submits++;
56
57
        if ($solution->isAccepted()) {
58
            $this->markProblemAccept($solution);
59
        }
60
61
        if ($solution->isFailed()) {
62
            $this->addFailedCount($solution);
63
        }
64
    }
65
66
    /**
67
     * @param Solution $solution
68
     */
69
    private function markProblemAccept($solution)
70
    {
71
        $pid = $solution->order;
72
        if (! array_key_exists($pid, $this->timePassed) || $this->timePassed[$pid] === null) {
73
            $this->timePassed[$pid] = $solution->created_at;
74
            $this->accepts++;
75
        } else {
76
            if ($this->timePassed[$pid]->gt($solution->created_at)) {
77
                $this->timePassed[$pid] = $solution->created_at;
78
            }
79
        }
80
    }
81
82
    private function addFailedCount(Solution $solution)
83
    {
84
        if (! array_key_exists($solution->order, $this->waCounts)) {
85
            $this->waCounts[$solution->order] = 0;
86
        }
87
88
        if ($this->shouldMarkAsPenalty($solution)) {
89
            $this->waCounts[$solution->order]++;
90
        }
91
    }
92
93
    private function shouldMarkAsPenalty(Solution $solution): bool
94
    {
95
        if (array_key_exists($solution->order, $this->timePassed)) {
96
            if ($this->timePassed[$solution->order]
97
                && $this->timePassed[$solution->order]->gt($solution->created_at)) {
98
                return false;
99
            }
100
        }
101
102
        return true;
103
    }
104
105
    /**
106
     * 获取通过的题目数.
107
     *
108
     * @return int
109
     */
110
    public function numberOfAccept()
111
    {
112
        return $this->accepts;
113
    }
114
115
    public function getNumberOfSubmit()
116
    {
117
        return $this->submits;
118
    }
119
120
    public function isProblemAccept($pid)
121
    {
122
        return null !== $this->timePassed[$pid];
123
    }
124
125
    /**
126
     * 获取总时间.
127
     *
128
     * @return int|mixed
129
     */
130
    public function totalPenalty()
131
    {
132
        $totalTime = 0;
133
        $number_of_problem = $this->contest->problems->count();
134
        for ($i = 0; $i < $number_of_problem; $i++) {
135
            $accepted_at = $this->getProblemAcceptTime($i);
136
            if ($accepted_at > 0) {
137
                // 只有通过的题目才需要计算时间和罚时
138
                $totalTime += $accepted_at;
139
                $totalTime += $this->getPenaltyOfProblem($i);
140
            }
141
        }
142
143
        return $totalTime;
144
    }
145
146
    public function getProblemAcceptTime($pid)
147
    {
148
        if (null === $this->timePassed[$pid]) {
149
            return 0;
150
        }
151
152
        $start = new Carbon($this->contest->start_time);
153
154
        return $this->timePassed[$pid]->diffInSeconds($start);
155
    }
156
157
    /**
158
     * 获取单个题目的罚时.
159
     *
160
     * @param $pid
161
     *
162
     * @return mixed
163
     */
164
    public function getPenaltyOfProblem($pid)
165
    {
166
        return $this->getProblemWACount($pid) * 20 * 60;
167
    }
168
169
    /**
170
     * 获取题目的尝试次数.
171
     *
172
     * @param $pid
173
     *
174
     * @return mixed
175
     */
176
    public function getProblemWACount($pid)
177
    {
178
        return $this->waCounts[$pid];
179
    }
180
}
181