hustoj /
hustoj-neo
| 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
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('contest_permission')) { |
||
| 56 | /** |
||
| 57 | * @param Contest $contest |
||
| 58 | * |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | function contest_permission($contest) |
||
| 62 | { |
||
| 63 | if ($contest instanceof Contest) { |
||
|
0 ignored issues
–
show
|
|||
| 64 | $contest = $contest->id; |
||
| 65 | } |
||
| 66 | |||
| 67 | return 'contest.'.$contest; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | if (! function_exists('can_view_code')) { |
||
| 72 | function can_view_code($solution) |
||
| 73 | { |
||
| 74 | /** @var User $user */ |
||
| 75 | $user = auth()->user(); |
||
| 76 | if (! $user) { |
||
|
0 ignored issues
–
show
|
|||
| 77 | return false; |
||
| 78 | } |
||
| 79 | if ($user->hasRole('admin')) { |
||
| 80 | return true; |
||
| 81 | } |
||
| 82 | |||
| 83 | return $solution->user_id == $user->id; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | if (! function_exists('show_ratio')) { |
||
| 88 | function show_ratio($number, $total) |
||
| 89 | { |
||
| 90 | $result = 0; |
||
| 91 | if ($total > 0) { |
||
| 92 | $result = 100.0 * $number / $total; |
||
| 93 | } |
||
| 94 | |||
| 95 | return sprintf('%2.2f%%', $result); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | if (! function_exists('display_penalize_time')) { |
||
| 100 | function display_penalize_time($seconds) |
||
| 101 | { |
||
| 102 | $hour = (int) ($seconds / (Carbon::SECONDS_PER_MINUTE * Carbon::MINUTES_PER_HOUR)); |
||
| 103 | $seconds -= $hour * Carbon::SECONDS_PER_MINUTE * Carbon::MINUTES_PER_HOUR; |
||
| 104 | $minute = (int) ($seconds / Carbon::SECONDS_PER_MINUTE); |
||
| 105 | $leftSeconds = $seconds % Carbon::SECONDS_PER_MINUTE; |
||
| 106 | |||
| 107 | return sprintf('%d:%02d:%02d', $hour, $minute, $leftSeconds); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | if (! function_exists('show_order')) { |
||
| 112 | function show_order($order) |
||
| 113 | { |
||
| 114 | return chr($order + ord('A')); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | if (! function_exists('show_problem_id')) { |
||
| 119 | /** |
||
| 120 | * @param Solution $solution |
||
| 121 | * |
||
| 122 | * @return int|string |
||
| 123 | */ |
||
| 124 | function show_problem_id($solution) |
||
| 125 | { |
||
| 126 | if ($solution->contest_id) { |
||
| 127 | return show_order($solution->order); |
||
| 128 | } |
||
| 129 | |||
| 130 | return $solution->problem_id; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | if (! function_exists('opening_contest')) { |
||
| 135 | function opening_contest() |
||
| 136 | { |
||
| 137 | return (new ContestService())->openingContest(); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | if (! function_exists('original_order')) { |
||
| 142 | function original_order($order) |
||
| 143 | { |
||
| 144 | return ord($order) - ord('A'); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | if (! function_exists('!show_status')) { |
||
| 149 | function show_status($status) |
||
| 150 | { |
||
| 151 | if (array_key_exists($status, Solution::$status)) { |
||
| 152 | return Solution::$status[$status]; |
||
| 153 | } |
||
| 154 | |||
| 155 | return 'not found!'; |
||
| 156 | } |
||
| 157 | } |
||
| 158 |