Add time keeper to Game.

This commit is contained in:
Austen McDonald
2016-07-27 21:25:23 +00:00
parent 0e70b7f0b1
commit 06794f2ec2
2 changed files with 25 additions and 8 deletions
+15
View File
@@ -115,4 +115,19 @@ class Game
{
return $this->logger;
}
/**
* Returns the time keeper.
* @return TimeKeeper
*/
public function getTimeKeeper(): TimeKeeper
{
if ($this->timeKeeper == null) {
$gameEpoch = $this->getConfiguration()->getGameEpoch();
$gameOffsetSeconds = $this->getConfiguration()->getGameOffsetSeconds();
$gameDaysPerDay = $this->getConfiguration()->getGameDaysPerDay();
$this->timeKeeper = new TimeKeeper($gameEpoch, $gameOffsetSeconds, $gameDaysPerDay);
}
return $this->timeKeeper;
}
}
+10 -8
View File
@@ -3,6 +3,8 @@ declare(strict_types=1);
namespace LotGD\Core;
use DateTime;
class TimeKeeper
{
private $adjustedEpoch;
@@ -17,13 +19,13 @@ class TimeKeeper
private $secondsPerGameMinute;
private $secondsPerGameSecond;
public function __construct(\DateTime $gameEpoch, int $gameOffsetSeconds, int $gameDaysPerDay)
public function __construct(DateTime $gameEpoch, int $gameOffsetSeconds, int $gameDaysPerDay)
{
$gameEpochCopy = clone($gameEpoch);
$this->adjustedEpoch = $gameEpochCopy->add(
$this->interval(0, 0, 0, 0, $gameOffsetSeconds)
);
$this->theBeginning = new \DateTime("0000-01-01 UTC");
$this->theBeginning = new DateTime("0000-01-01 UTC");
$this->secondsPerGameDay = (float) $this->secondsPerDay / $gameDaysPerDay;
$this->secondsPerGameYear = $this->secondsPerGameDay * 365;
@@ -45,14 +47,14 @@ class TimeKeeper
return $d1 != $d2;
}
public function gameTime(): \DateTime
public function gameTime(): DateTime
{
return $this->convertToGameTime(new \DateTime());
return $this->convertToGameTime(new DateTime());
}
public function convertFromGameTime(
\DateTime $time
): \DateTime {
DateTime $time
): DateTime {
// Game dates are in the distant past, better not use getTimestamp().
$i = $this->theBeginning->diff($time);
@@ -67,8 +69,8 @@ class TimeKeeper
}
public function convertToGameTime(
\DateTime $time
): \DateTime {
DateTime $time
): DateTime {
$timeUnix = $time->getTimestamp();
$epochUnix = $this->adjustedEpoch->getTimestamp();