|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Services; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entities\Contest; |
|
6
|
|
|
use App\Entities\Permission; |
|
7
|
|
|
use App\Entities\Problem; |
|
8
|
|
|
use App\Repositories\ContestRepository; |
|
9
|
|
|
use App\Repositories\Criteria\Where; |
|
10
|
|
|
use App\Repositories\PermissionRepository; |
|
11
|
|
|
use Carbon\Carbon; |
|
12
|
|
|
|
|
13
|
|
|
class ContestService |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var ContestRepository */ |
|
16
|
|
|
private $repository; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* ContestService constructor. |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->repository = app(ContestRepository::class); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param $id |
|
28
|
|
|
* |
|
29
|
|
|
* @return Contest |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getContest($id) |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->repository->findOrFail($id); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param $contest |
|
38
|
|
|
* |
|
39
|
|
|
* @return Permission |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getContestPermission($contest) |
|
42
|
|
|
{ |
|
43
|
|
|
$name = self::permissionOfContest($contest); |
|
44
|
|
|
/** @var PermissionRepository $repository */ |
|
45
|
|
|
$repository = app(PermissionRepository::class); |
|
46
|
|
|
$perms = $repository->findBy('name', $name); |
|
47
|
|
|
if ($perms->count()) { |
|
48
|
|
|
return $perms->first(); |
|
49
|
|
|
} |
|
50
|
|
|
$perm = new Permission(); |
|
51
|
|
|
$perm->name = $name; |
|
52
|
|
|
$perm->display_name = 'Contest .'.$contest->id; |
|
53
|
|
|
$perm->description = 'Privilege For Contest .'.$contest->id; |
|
54
|
|
|
$perm->save(); |
|
55
|
|
|
|
|
56
|
|
|
return $perm; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function permissionOfContest($contest) |
|
60
|
|
|
{ |
|
61
|
|
|
return 'contest.'.$contest->id; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param Contest $contest |
|
66
|
|
|
* @param $order |
|
67
|
|
|
* |
|
68
|
|
|
* @return Problem |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getProblemByOrder($contest, $order) |
|
71
|
|
|
{ |
|
72
|
|
|
return $contest->problems() |
|
73
|
|
|
->wherePivot('order', '=', original_order($order)) |
|
74
|
|
|
->first(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function openingContest() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->repository->clearCriteria(); |
|
80
|
|
|
|
|
81
|
|
|
// current is opening |
|
82
|
|
|
$now = Carbon::now(); |
|
83
|
|
|
$this->repository->pushCriteria(new Where('start_time', $now, '<')); |
|
84
|
|
|
$this->repository->pushCriteria(new Where('end_time', $now, '>')); |
|
85
|
|
|
|
|
86
|
|
|
// should be public |
|
87
|
|
|
$this->repository->pushCriteria(new Where('private', Contest::PUBLIC)); |
|
88
|
|
|
|
|
89
|
|
|
// should be normal |
|
90
|
|
|
$this->repository->pushCriteria(new Where('status', Contest::ST_NORMAL)); |
|
91
|
|
|
|
|
92
|
|
|
return $this->repository->all(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|