-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnnouncement.php
49 lines (43 loc) · 1.31 KB
/
Announcement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php declare(strict_types=1);
namespace Nadybot\User\Modules\ANNOUNCE_MODULE;
use Nadybot\Core\Attributes as NCA;
use Nadybot\Core\DBRow;
class Announcement extends DBRow {
public int $id;
public string $name;
public string $content = "TBD";
public string $created_by;
public bool $active = false;
public int $created_on;
public int $interval_between_channels = 5;
public int $interval_between_announcements = 1800;
public ?int $last_announcement = null;
/**
* @var AnnouncementChannel[]
*/
#[NCA\DB\Ignore]
public array $channels = [];
public function __construct() {
$this->created_on = time();
}
/**
* @return null|array<int>
* @phpstan-return null|array{int,int}
*/
public function getNextAnnouncement(): ?array {
if ($this->last_announcement === null) {
return count($this->channels) ? [time(), 0] : null;
}
$cycleTime = $this->interval_between_announcements
+ max(0, count($this->channels) - 1) * $this->interval_between_channels;
$cycles = (int)floor((time() - $this->last_announcement) / $cycleTime);
$baseTime = $this->last_announcement + $cycles * $cycleTime;
for ($i = 0; $i < count($this->channels); $i++) {
$time = $baseTime + $this->interval_between_channels * ($i);
if (time() <= $time) {
return [$time, $i];
}
}
return [$baseTime + $cycleTime, 0];
}
}