Moved TimeKeeper.now to constructor.

Breaks BC intentionally.
This commit is contained in:
Vassyli
2017-04-06 14:52:03 +02:00
parent a33473d435
commit 70d29f67b8
3 changed files with 26 additions and 30 deletions
+2 -1
View File
@@ -3,6 +3,7 @@ declare (strict_types=1);
namespace LotGD\Core; namespace LotGD\Core;
use DateTime;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Monolog\Logger; use Monolog\Logger;
@@ -152,7 +153,7 @@ class Game
$gameEpoch = $this->getConfiguration()->getGameEpoch(); $gameEpoch = $this->getConfiguration()->getGameEpoch();
$gameOffsetSeconds = $this->getConfiguration()->getGameOffsetSeconds(); $gameOffsetSeconds = $this->getConfiguration()->getGameOffsetSeconds();
$gameDaysPerDay = $this->getConfiguration()->getGameDaysPerDay(); $gameDaysPerDay = $this->getConfiguration()->getGameDaysPerDay();
$this->timeKeeper = new TimeKeeper($gameEpoch, $gameOffsetSeconds, $gameDaysPerDay); $this->timeKeeper = new TimeKeeper($gameEpoch, new DateTime(), $gameOffsetSeconds, $gameDaysPerDay);
} }
return $this->timeKeeper; return $this->timeKeeper;
} }
+8 -12
View File
@@ -28,11 +28,16 @@ class TimeKeeper
/** /**
* Construct a TimeKeeper with required configuration. * Construct a TimeKeeper with required configuration.
* @param DateTime $gameEpoch When in real time is game day 0. * @param DateTime $gameEpoch When in real time is game day 0.
* @param DateTime $now The current time.
* @param int $gameOffsetSeconds How many seconds from midnight on the epoch should the first game day start. * @param int $gameOffsetSeconds How many seconds from midnight on the epoch should the first game day start.
* @param int $gameDaysPerDay How many game days are in one real day. * @param int $gameDaysPerDay How many game days are in one real day.
*/ */
public function __construct(DateTime $gameEpoch, int $gameOffsetSeconds, int $gameDaysPerDay) public function __construct(
{ DateTime $gameEpoch,
DateTime $now,
int $gameOffsetSeconds,
int $gameDaysPerDay
) {
$gameEpochCopy = clone($gameEpoch); $gameEpochCopy = clone($gameEpoch);
if ($gameOffsetSeconds < 0) { if ($gameOffsetSeconds < 0) {
@@ -53,16 +58,7 @@ class TimeKeeper
$this->secondsPerGameMinute = $this->secondsPerGameHour / 60; $this->secondsPerGameMinute = $this->secondsPerGameHour / 60;
$this->secondsPerGameSecond = $this->secondsPerGameMinute / 60; $this->secondsPerGameSecond = $this->secondsPerGameMinute / 60;
$this->now = new DateTime(); $this->now = $now;
}
/**
* Changes the "now" state of the TimeKeeper.
* @param DateTime $dateTime
*/
public function changeNow(DateTime $dateTime)
{
$this->now = $dateTime;
} }
/** /**
+16 -17
View File
@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace LotGD\Core\Tests; namespace LotGD\Core\Tests;
use DateTime;
use LotGD\Core\TimeKeeper; use LotGD\Core\TimeKeeper;
/** /**
@@ -22,7 +23,7 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
public function testConvertToBasicConversion() { public function testConvertToBasicConversion() {
$this->gameDaysPerDay = 1; $this->gameDaysPerDay = 1;
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, $this->gameDaysPerDay); $keeper = new TimeKeeper($this->gameEpoch, new DateTime(), $this->gameOffsetSeconds, $this->gameDaysPerDay);
$date = new \DateTime('2015-07-27 23:59:59 PDT'); $date = new \DateTime('2015-07-27 23:59:59 PDT');
$converted = $keeper->convertToGameTime($date); $converted = $keeper->convertToGameTime($date);
@@ -37,7 +38,7 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
$date = new \DateTime('2015-07-27 05:59:59 PDT'); $date = new \DateTime('2015-07-27 05:59:59 PDT');
$this->gameDaysPerDay = 4; $this->gameDaysPerDay = 4;
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, $this->gameDaysPerDay); $keeper = new TimeKeeper($this->gameEpoch, new DateTime(), $this->gameOffsetSeconds, $this->gameDaysPerDay);
$converted = $keeper->convertToGameTime($date); $converted = $keeper->convertToGameTime($date);
$this->assertEquals('0000-01-01', $converted->format('Y-m-d')); $this->assertEquals('0000-01-01', $converted->format('Y-m-d'));
@@ -47,7 +48,7 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
$date = new \DateTime('2015-07-27 06:00:00 PDT'); $date = new \DateTime('2015-07-27 06:00:00 PDT');
$this->gameDaysPerDay = 4; $this->gameDaysPerDay = 4;
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, $this->gameDaysPerDay); $keeper = new TimeKeeper($this->gameEpoch, new DateTime(), $this->gameOffsetSeconds, $this->gameDaysPerDay);
$converted = $keeper->convertToGameTime($date); $converted = $keeper->convertToGameTime($date);
$this->assertEquals('0000-01-02', $converted->format('Y-m-d')); $this->assertEquals('0000-01-02', $converted->format('Y-m-d'));
@@ -57,14 +58,14 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
$date = new \DateTime('2015-07-27 11:59:59 PDT'); $date = new \DateTime('2015-07-27 11:59:59 PDT');
$this->gameDaysPerDay = 4; $this->gameDaysPerDay = 4;
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, $this->gameDaysPerDay); $keeper = new TimeKeeper($this->gameEpoch, new DateTime(), $this->gameOffsetSeconds, $this->gameDaysPerDay);
$converted = $keeper->convertToGameTime($date); $converted = $keeper->convertToGameTime($date);
$this->assertEquals('0000-01-02', $converted->format('Y-m-d')); $this->assertEquals('0000-01-02', $converted->format('Y-m-d'));
} }
public function testIfIsNewDayReturnsTrueWithNullAsLastInteractionTime() { public function testIfIsNewDayReturnsTrueWithNullAsLastInteractionTime() {
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, 4); $keeper = new TimeKeeper($this->gameEpoch, new DateTime(), $this->gameOffsetSeconds, 4);
$this->assertTrue($keeper->isNewDay(null)); $this->assertTrue($keeper->isNewDay(null));
} }
@@ -72,8 +73,7 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
public function testIfIsNewDayReturnsFalseIfLastInteractionTimeWasJustRecently() public function testIfIsNewDayReturnsFalseIfLastInteractionTimeWasJustRecently()
{ {
// game days per day: 4, each day 6h. // game days per day: 4, each day 6h.
$keeper = new TimeKeeper($this->gameEpoch, 3600*5, 4); $keeper = new TimeKeeper($this->gameEpoch, new DateTime("2017-01-02 11:59:59"), 3600*5, 4);
$keeper->changeNow(new \DateTime("2017-01-02 11:59:59"));
$time1 = new \DateTime("2017-01-02 06:00:00"); $time1 = new \DateTime("2017-01-02 06:00:00");
$time2 = new \DateTime("2017-01-02 11:59:59"); $time2 = new \DateTime("2017-01-02 11:59:59");
@@ -90,8 +90,7 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
{ {
// game days per day: 4, each day 6h. // game days per day: 4, each day 6h.
// interestingly, it looks like new game day starts 01:00:00? // interestingly, it looks like new game day starts 01:00:00?
$keeper = new TimeKeeper($this->gameEpoch, 3600*5, 4); $keeper = new TimeKeeper($this->gameEpoch, new DateTime("2017-01-02 12:00:00"), 3600*5, 4);
$keeper->changeNow(new \DateTime("2017-01-02 12:00:00")); // it is a new day
$time1 = new \DateTime("2017-01-02 06:00:00"); $time1 = new \DateTime("2017-01-02 06:00:00");
$time2 = new \DateTime("2017-01-02 11:59:59"); $time2 = new \DateTime("2017-01-02 11:59:59");
@@ -109,7 +108,7 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
$this->gameOffsetSeconds = 60*60; $this->gameOffsetSeconds = 60*60;
$this->gameDaysPerDay = 1; $this->gameDaysPerDay = 1;
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, $this->gameDaysPerDay); $keeper = new TimeKeeper($this->gameEpoch, new DateTime(), $this->gameOffsetSeconds, $this->gameDaysPerDay);
$converted = $keeper->convertToGameTime($date); $converted = $keeper->convertToGameTime($date);
$this->assertEquals('0000-01-01 00:01:15', $converted->format('Y-m-d H:i:s')); $this->assertEquals('0000-01-01 00:01:15', $converted->format('Y-m-d H:i:s'));
@@ -120,7 +119,7 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
$this->gameOffsetSeconds = 60*60; $this->gameOffsetSeconds = 60*60;
$this->gameDaysPerDay = 1; $this->gameDaysPerDay = 1;
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, $this->gameDaysPerDay); $keeper = new TimeKeeper($this->gameEpoch, new DateTime(), $this->gameOffsetSeconds, $this->gameDaysPerDay);
$converted = $keeper->convertToGameTime($date); $converted = $keeper->convertToGameTime($date);
$this->assertEquals('0000-01-01 23:59:59', $converted->format('Y-m-d H:i:s')); $this->assertEquals('0000-01-01 23:59:59', $converted->format('Y-m-d H:i:s'));
@@ -131,7 +130,7 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
$this->gameOffsetSeconds = 60*60; $this->gameOffsetSeconds = 60*60;
$this->gameDaysPerDay = 1; $this->gameDaysPerDay = 1;
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, $this->gameDaysPerDay); $keeper = new TimeKeeper($this->gameEpoch, new DateTime(), $this->gameOffsetSeconds, $this->gameDaysPerDay);
$converted = $keeper->convertToGameTime($date); $converted = $keeper->convertToGameTime($date);
$this->assertEquals('0000-01-02', $converted->format('Y-m-d')); $this->assertEquals('0000-01-02', $converted->format('Y-m-d'));
@@ -142,7 +141,7 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
$this->gameOffsetSeconds = 60*60; $this->gameOffsetSeconds = 60*60;
$this->gameDaysPerDay = 1; $this->gameDaysPerDay = 1;
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, $this->gameDaysPerDay); $keeper = new TimeKeeper($this->gameEpoch, new DateTime(), $this->gameOffsetSeconds, $this->gameDaysPerDay);
$converted = $keeper->convertToGameTime($date); $converted = $keeper->convertToGameTime($date);
$this->assertEquals('0000-01-02', $converted->format('Y-m-d')); $this->assertEquals('0000-01-02', $converted->format('Y-m-d'));
@@ -152,7 +151,7 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
$date = new \DateTime('0000-01-02 00:00:00 UTC'); $date = new \DateTime('0000-01-02 00:00:00 UTC');
$this->gameDaysPerDay = 1; $this->gameDaysPerDay = 1;
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, $this->gameDaysPerDay); $keeper = new TimeKeeper($this->gameEpoch, new DateTime(), $this->gameOffsetSeconds, $this->gameDaysPerDay);
$converted = $keeper->convertFromGameTime($date); $converted = $keeper->convertFromGameTime($date);
$this->assertEquals("2015-07-28", $converted->format('Y-m-d')); $this->assertEquals("2015-07-28", $converted->format('Y-m-d'));
@@ -165,7 +164,7 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
$this->gameEpoch = $epoch; $this->gameEpoch = $epoch;
$this->gameOffsetSeconds = 60*60; $this->gameOffsetSeconds = 60*60;
$this->gameDaysPerDay = 1; $this->gameDaysPerDay = 1;
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, $this->gameDaysPerDay); $keeper = new TimeKeeper($this->gameEpoch, new DateTime(), $this->gameOffsetSeconds, $this->gameDaysPerDay);
$converted = $keeper->convertFromGameTime($date); $converted = $keeper->convertFromGameTime($date);
$this->assertEquals("2015-07-29 00:59:59", $converted->format('Y-m-d H:i:s')); $this->assertEquals("2015-07-29 00:59:59", $converted->format('Y-m-d H:i:s'));
@@ -177,14 +176,14 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
$this->gameEpoch = $epoch; $this->gameEpoch = $epoch;
$this->gameDaysPerDay = 4; $this->gameDaysPerDay = 4;
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, $this->gameDaysPerDay); $keeper = new TimeKeeper($this->gameEpoch, new DateTime(), $this->gameOffsetSeconds, $this->gameDaysPerDay);
$converted = $keeper->convertFromGameTime($date); $converted = $keeper->convertFromGameTime($date);
$this->assertEquals("2015-07-27 11:59:59", $converted->format('Y-m-d H:i:s')); $this->assertEquals("2015-07-27 11:59:59", $converted->format('Y-m-d H:i:s'));
} }
public function testGameTimeSanity() { public function testGameTimeSanity() {
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, $this->gameDaysPerDay); $keeper = new TimeKeeper($this->gameEpoch, new DateTime(), $this->gameOffsetSeconds, $this->gameDaysPerDay);
$this->assertNotNull($keeper->getGameTime()); $this->assertNotNull($keeper->getGameTime());
} }
} }