-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.php
28 lines (20 loc) · 861 Bytes
/
server.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
<?php
require_once __DIR__ . '/bootstrap.php';
$intervalInSeconds = 5.0;
$lastExecutedTime = time();
function writeSystemLog(string $msg) {
file_put_contents(__DIR__ . '/log.txt', $msg . PHP_EOL, FILE_APPEND);
}
$timer = \React\EventLoop\Loop::addPeriodicTimer($intervalInSeconds, function () use (&$lastExecutedTime) {
$resultLogs = DI::getService(Game\Server::class)->performTasks();
foreach ($resultLogs as $log) {
echo $log . PHP_EOL;
}
writeSystemLog(implode(PHP_EOL, $resultLogs));
$lastExecutedTime = time();
});
echo 'Server is running' . PHP_EOL;
\React\EventLoop\Loop::addPeriodicTimer(0.1, function () use ($timer, &$lastExecutedTime) {
$nextExecutionIn = (float)($lastExecutedTime + $timer->getInterval()) - (float) time();
printf("Next execution in %0.1f seconds\r", $nextExecutionIn) . PHP_EOL;
});