1
|
|
|
<?php |
2
|
|
|
namespace PhpDraft\Domain\Services; |
3
|
|
|
|
4
|
|
|
use Silex\Application; |
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use PhpDraft\Domain\Entities\Draft; |
7
|
|
|
use PhpDraft\Domain\Entities\RoundTime; |
8
|
|
|
use PhpDraft\Domain\Models\PhpDraftResponse; |
9
|
|
|
use PhpDraft\Domain\Models\RoundTimeCreateModel; |
10
|
|
|
|
11
|
|
|
class RoundTimeService { |
12
|
|
|
private $app; |
13
|
|
|
|
14
|
|
|
public function __construct(Application $app) { |
15
|
|
|
$this->app = $app; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function SaveRoundTimes(Draft $draft, RoundTimeCreateModel $model) { |
19
|
|
|
$response = new PhpDraftResponse(); |
20
|
|
|
|
21
|
|
|
try { |
22
|
|
|
$this->app['phpdraft.RoundTimeRepository']->DeleteAll($draft->draft_id); |
23
|
|
|
$roundTimes = $this->app['phpdraft.RoundTimeRepository']->Save($model); |
24
|
|
|
|
25
|
|
|
$response->success = true; |
26
|
|
|
$response->roundTimes = $roundTimes; |
|
|
|
|
27
|
|
|
} catch (\Exception $e) { |
28
|
|
|
$response->success = false; |
29
|
|
|
$response->errors = array($e->getMessage()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $response; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function GetCurrentPickTimeRemaining(Draft $draft) { |
36
|
|
|
$response = new PhpDraftResponse(); |
37
|
|
|
|
38
|
|
|
try { |
39
|
|
|
$current_pick = $this->app['phpdraft.PickRepository']->GetCurrentPick($draft); |
|
|
|
|
40
|
|
|
$last_pick = $this->app['phpdraft.PickRepository']->GetPreviousPick($draft); |
41
|
|
|
$current_round_picktime = $this->app['phpdraft.RoundTimeRepository']->LoadByRound($draft); |
42
|
|
|
} catch (\Exception $e) { |
43
|
|
|
$response->success = false; |
44
|
|
|
$response->errors = array($e->getMessage()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$response->success = true; |
48
|
|
|
$response->timer_enabled = $current_round_picktime != null; |
|
|
|
|
49
|
|
|
|
50
|
|
|
if (!$response->timer_enabled) { |
51
|
|
|
return $response; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
//Take last pick's picktime and add our timer seconds to it |
55
|
|
|
$last_pick_time = $last_pick != null |
|
|
|
|
56
|
|
|
? $last_pick->pick_time |
57
|
|
|
: $draft->draft_start_time; |
58
|
|
|
|
59
|
|
|
//Because strtotime uses the server timezone, we can create a DateTime object, specify UTC, then get the Unix timestamp that way: |
60
|
|
|
$last_pick_datetime = new \DateTime($last_pick_time, new \DateTimeZone("UTC")); |
61
|
|
|
$last_pick_timestamp = $last_pick_datetime->getTimestamp(); |
62
|
|
|
|
63
|
|
|
$timer_ends_at = $last_pick_timestamp + $current_round_picktime->round_time_seconds; |
64
|
|
|
|
65
|
|
|
//then subtract the NOW timestamp to get seconds left and return that for timer to count down. |
66
|
|
|
$now_utc = new \DateTime(null, new \DateTimeZone("UTC")); |
67
|
|
|
$right_now = $now_utc->getTimestamp(); |
68
|
|
|
$seconds_remaining = $timer_ends_at - $right_now; |
69
|
|
|
|
70
|
|
|
//Return non-negative seconds figure, 0 meaning TIME IS UP! |
71
|
|
|
$response->seconds_remaining = max($seconds_remaining, 0); |
|
|
|
|
72
|
|
|
|
73
|
|
|
return $response; |
74
|
|
|
} |
75
|
|
|
} |