Completed
Push — master ( 395fb2...b0764c )
by he
10:48 queued 10s
created

new_judge_code()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
use App\Entities\Contest;
4
use App\Entities\Option;
5
use App\Entities\Solution;
6
use App\Entities\User;
7
use App\Services\ContestService;
8
use Carbon\Carbon;
9
use Illuminate\Database\Eloquent\Collection;
10
use Ramsey\Uuid\Uuid;
11
12
if (!function_exists('new_judge_code')) {
13
    function new_judge_code()
14
    {
15
        return Uuid::getFactory()->uuid4();
16
    }
17
}
18
19
if (!function_exists('get_option')) {
20
    function get_option($name, $default = null)
21
    {
22
        /** @var Collection|Option[] $options */
23
        $options = Option::all();
24
        foreach ($options as $option) {
25
            if ($option->key === $name && $option->value) {
26
                return $option->value;
27
            }
28
        }
29
30
        return $default;
31
    }
32
}
33
34
if (!function_exists('can_attend')) {
35
    /**
36
     * @param Contest $contest
37
     *
38
     * @return bool
39
     */
40
    function can_attend($contest)
41
    {
42
        /** @var User $user */
43
        $user = auth()->user();
44
        if (!$user) {
0 ignored issues
show
introduced by
$user is of type App\Entities\User, thus it always evaluated to true.
Loading history...
45
            return false;
46
        }
47
        if ($user->hasRole('admin')) {
48
            return true;
49
        }
50
51
        return $contest->users()->wherePivot('user_id', '=', $user->id)->get()->count();
52
    }
53
}
54
55
if (!function_exists('show_ratio')) {
56
    function show_ratio($number, $total)
57
    {
58
        $result = 0;
59
        if ($total > 0) {
60
            $result = 100.0 * $number / $total;
61
        }
62
63
        return sprintf('%2.2f%%', $result);
64
    }
65
}
66
67
if (!function_exists('display_penalize_time')) {
68
    function display_penalize_time($seconds)
69
    {
70
        $hour = (int) ($seconds / (Carbon::SECONDS_PER_MINUTE * Carbon::MINUTES_PER_HOUR));
71
        $seconds -= $hour * Carbon::SECONDS_PER_MINUTE * Carbon::MINUTES_PER_HOUR;
72
        $minute = (int) ($seconds / Carbon::SECONDS_PER_MINUTE);
73
        $leftSeconds = $seconds % Carbon::SECONDS_PER_MINUTE;
74
75
        return sprintf('%d:%02d:%02d', $hour, $minute, $leftSeconds);
76
    }
77
}
78
79
if (!function_exists('show_order')) {
80
    function show_order($order)
81
    {
82
        return chr($order + ord('A'));
83
    }
84
}
85
86
if (!function_exists('show_problem_id')) {
87
    /**
88
     * @param Solution $solution
89
     *
90
     * @return int|string
91
     */
92
    function show_problem_id($solution)
93
    {
94
        if ($solution->contest_id) {
95
            return show_order($solution->order);
96
        }
97
98
        return $solution->problem_id;
99
    }
100
}
101
102
if (!function_exists('opening_contest')) {
103
    function opening_contest()
104
    {
105
        return (new ContestService())->openingContest();
106
    }
107
}
108
109
if (!function_exists('original_order')) {
110
    function original_order($order)
111
    {
112
        return ord($order) - ord('A');
113
    }
114
}
115
116
if (!function_exists('!show_status')) {
117
    function show_status($status)
118
    {
119
        if (array_key_exists($status, Solution::$status)) {
120
            return Solution::$status[$status];
121
        }
122
123
        return 'not found!';
124
    }
125
}
126