+6
-8
@@ -46,13 +46,15 @@ class TimeKeeper
|
||||
/**
|
||||
* Returns whether a user who is interating with the game now and last
|
||||
* interacted at $lastInteractionTime should experience a New Day event.
|
||||
* @param DateTime $lastInteractionTime
|
||||
* @param DateTime|null $lastInteractionTime
|
||||
* @return bool
|
||||
*/
|
||||
public function isNewDay(DateTime $lastInteractionTime): bool
|
||||
public function isNewDay(?DateTime $lastInteractionTime): bool
|
||||
{
|
||||
if ($lastInteractionTime == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$t1 = $this->gameTime();
|
||||
$t2 = $this->convertToGameTime($lastInteractionTime);
|
||||
$d1 = $t1->format("Y-m-d");
|
||||
@@ -75,9 +77,7 @@ class TimeKeeper
|
||||
* @param DateTime $time Game time to convert.
|
||||
* @return DateTime Real time corresponding to game time $time.
|
||||
*/
|
||||
public function convertFromGameTime(
|
||||
DateTime $time
|
||||
): DateTime {
|
||||
public function convertFromGameTime(DateTime $time): DateTime {
|
||||
// Game dates are in the distant past, better not use getTimestamp().
|
||||
$i = $this->theBeginning->diff($time);
|
||||
|
||||
@@ -96,9 +96,7 @@ class TimeKeeper
|
||||
* @param DateTime $time Real time to convert.
|
||||
* @return DateTime Game time corresponding to real time $time.
|
||||
*/
|
||||
public function convertToGameTime(
|
||||
DateTime $time
|
||||
): DateTime {
|
||||
public function convertToGameTime(DateTime $time): DateTime {
|
||||
$timeUnix = $time->getTimestamp();
|
||||
$epochUnix = $this->adjustedEpoch->getTimestamp();
|
||||
|
||||
|
||||
@@ -63,7 +63,25 @@ class TimeKeeperTests extends \PHPUnit_Framework_TestCase {
|
||||
$this->assertEquals('0000-01-02', $converted->format('Y-m-d'));
|
||||
}
|
||||
|
||||
public function testDetectsNewDay() {
|
||||
public function testIfIsNewDayReturnsTrueWithNullAsLastInteractionTime() {
|
||||
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, 4);
|
||||
|
||||
$this->assertTrue($keeper->isNewDay(null));
|
||||
}
|
||||
|
||||
public function testIfIsNewDayReturnsFalseIfLastInteractionTimeWasJustRecently()
|
||||
{
|
||||
// game days per day: 4, each day 6h.
|
||||
$keeper = new TimeKeeper($this->gameEpoch, $this->gameOffsetSeconds, 4);
|
||||
|
||||
$now = new \DateTime();
|
||||
$nowMinus1Second = $now->sub(new \DateInterval("PT1S"));
|
||||
$newMinus1Minute = $now->sub(new \DateInterval("PT1M"));
|
||||
$newMinus1Hour = $now->sub(new \DateInterval("PT1H"));
|
||||
$oldPLus1H = $now->sub(new \DateInterval("PT5H59M59S"));
|
||||
|
||||
$this->assertFalse($keeper->isNewDay($now));
|
||||
$this->assertFalse($keeper->isNewDay($nowMinus1Second));
|
||||
}
|
||||
|
||||
public function testConvertToRespectsGameOffset() {
|
||||
|
||||
Reference in New Issue
Block a user