Test Setup Failed
Push — master ( ef125c...60f940 )
by he
08:55
created

show_status()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 7
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('can_view_code')) {
56
    function can_view_code($solution)
57
    {
58
        /** @var User $user */
59
        $user = auth()->user();
60
        if (! $user) {
0 ignored issues
show
introduced by
$user is of type App\Entities\User, thus it always evaluated to true.
Loading history...
61
            return false;
62
        }
63
        if ($user->hasRole('admin')) {
64
            return true;
65
        }
66
67
        return $solution->user_id == $user->id;
68
    }
69
}
70
71
if (! function_exists('show_ratio')) {
72
    function show_ratio($number, $total)
73
    {
74
        $result = 0;
75
        if ($total > 0) {
76
            $result = 100.0 * $number / $total;
77
        }
78
79
        return sprintf('%2.2f%%', $result);
80
    }
81
}
82
83
if (! function_exists('display_penalize_time')) {
84
    function display_penalize_time($seconds)
85
    {
86
        $hour = (int) ($seconds / (Carbon::SECONDS_PER_MINUTE * Carbon::MINUTES_PER_HOUR));
87
        $seconds -= $hour * Carbon::SECONDS_PER_MINUTE * Carbon::MINUTES_PER_HOUR;
88
        $minute = (int) ($seconds / Carbon::SECONDS_PER_MINUTE);
89
        $leftSeconds = $seconds % Carbon::SECONDS_PER_MINUTE;
90
91
        return sprintf('%d:%02d:%02d', $hour, $minute, $leftSeconds);
92
    }
93
}
94
95
if (! function_exists('show_order')) {
96
    function show_order($order)
97
    {
98
        return chr($order + ord('A'));
99
    }
100
}
101
102
if (! function_exists('show_problem_id')) {
103
    /**
104
     * @param Solution $solution
105
     *
106
     * @return int|string
107
     */
108
    function show_problem_id($solution)
109
    {
110
        if ($solution->contest_id) {
111
            return show_order($solution->order);
112
        }
113
114
        return $solution->problem_id;
115
    }
116
}
117
118
if (! function_exists('opening_contest')) {
119
    function opening_contest()
120
    {
121
        return (new ContestService())->openingContest();
122
    }
123
}
124
125
if (! function_exists('original_order')) {
126
    function original_order($order)
127
    {
128
        return ord($order) - ord('A');
129
    }
130
}
131
132
if (! function_exists('!show_status')) {
133
    function show_status($status)
134
    {
135
        if (array_key_exists($status, Solution::$status)) {
136
            return Solution::$status[$status];
137
        }
138
139
        return 'not found!';
140
    }
141
}
142