Merge branch 'master' of https://github.com/lotgd/core into php8-compability

This commit is contained in:
Vassyli
2020-12-23 12:44:41 +01:00
19 changed files with 267 additions and 48 deletions
+2
View File
@@ -25,6 +25,8 @@ return PhpCsFixer\Config::create()
'doctrine_annotation_braces' => true,
'doctrine_annotation_indentation' => true,
'doctrine_annotation_spaces' => [
'after_array_assignments_colon' => false,
'after_array_assignments_equals' => false,
'before_argument_assignments' => false,
'before_array_assignments_colon' => false,
'before_array_assignments_equals' => false,
+14 -4
View File
@@ -8,7 +8,6 @@ use Doctrine\ORM\EntityManagerInterface;
use LotGD\Core\Events\NavigateToSceneData;
use LotGD\Core\Events\NewViewpointData;
use LotGD\Core\Exceptions\ActionNotFoundException;
use LotGD\Core\Exceptions\CharacterNotFoundException;
use LotGD\Core\Exceptions\InvalidConfigurationException;
use LotGD\Core\Exceptions\SceneNotFoundException;
@@ -17,6 +16,8 @@ use LotGD\Core\Models\Scene;
use LotGD\Core\Models\SceneConnectable;
use LotGD\Core\Models\SceneConnection;
use LotGD\Core\Models\Viewpoint;
use LotGD\Core\SceneTemplates\BasicSceneTemplate;
use LotGD\Core\SceneTemplates\SceneTemplateInterface;
use Monolog\Logger;
/**
@@ -338,6 +339,13 @@ class Game
$viewpoint->setActionGroups(\array_values($actionGroups));
$sceneTemplate = $scene->getTemplate();
$templateClass = $sceneTemplate ? $sceneTemplate->getClass() : BasicSceneTemplate::class;
if (!\is_a($templateClass, SceneTemplateInterface::class, true)) {
throw new \Exception("Scene template must implement ".SceneTemplateInterface::class);
}
// Let and installed listeners (ie modules) make modifications to the
// new viewpoint, including the ability to redirect the user to
// a different scene, by setting $context['redirect'] to a new scene.
@@ -349,7 +357,7 @@ class Game
'redirect' => null,
]);
$hook = 'h/lotgd/core/navigate-to/' . $scene->getTemplate();
$hook = "h/lotgd/core/navigate-to/".$templateClass::getNavigationEvent();
$contextData = $this->getEventManager()->publish($hook, $contextData);
$scene = $contextData->get('redirect');
@@ -379,13 +387,15 @@ class Game
if ($action === null) {
throw new ActionNotFoundException("Invalid actionId={$actionId} for current viewpoint.");
}
$actionParameters = $action->getParameters();
$actionParameters = $action->getParameters();
$sceneId = $action->getDestinationSceneId();
/** @var Scene $scene */
$scene = $this->getEntityManager()->getRepository(Scene::class)->find([
'id' => $sceneId,
]);
if ($scene == null) {
if ($scene === null) {
throw new SceneNotFoundException("Cannot find sceneId={$sceneId} specified by actionId={$actionId}.");
}
+3 -3
View File
@@ -47,11 +47,11 @@ class Character implements CharacterInterface, CreateableInterface, GameAwareInt
private $name = "";
/** @Column(type="text"); */
private $displayName = "";
/** @Column(type="integer", options={"default"= 10}) */
/** @Column(type="integer", options={"default"=10}) */
private $maxHealth = 10;
/** @Column(type="integer", options={"default"= 10}) */
/** @Column(type="integer", options={"default"=10}) */
private $health = 10;
/** @Column(type="integer", options={"default"= 1})/ */
/** @Column(type="integer", options={"default"=1})/ */
private $level = 1;
/** @OneToMany(targetEntity="CharacterProperty", mappedBy="owner", cascade={"persist", "remove"}) */
private $properties;
+1 -1
View File
@@ -26,7 +26,7 @@ class MessageThread implements SaveableInterface
private $id;
/** @Column(type="string", length=255, unique=true) */
private $threadKey;
/** @Column(type="boolean", options={"default"= false}) */
/** @Column(type="boolean", options={"default"=false}) */
private $readonly = false;
/** @ManyToMany(targetEntity="Character", cascade={"persist"}, mappedBy="messageThreads") */
private $participants;
+5 -5
View File
@@ -27,7 +27,7 @@ class Scene implements CreateableInterface, SceneConnectable
use Deletor;
use SceneBasics;
/** @Id @Column(type="string", length=36, unique=True, name="id", options={"fixed"= true}) */
/** @Id @Column(type="string", length=36, unique=True, name="id", options={"fixed"=true}) */
protected $id;
/**
@@ -143,7 +143,7 @@ class Scene implements CreateableInterface, SceneConnectable
/**
* Removes a connection group from this scene.
* @param \LotGD\Core\Models\SceneConnectionGroup $group
* @param SceneConnectionGroup $group
* @throws ArgumentException
*/
public function dropConnectionGroup(SceneConnectionGroup $group): void
@@ -199,7 +199,7 @@ class Scene implements CreateableInterface, SceneConnectable
/**
* Checks if the given scene is connected to this entity.
* @param \LotGD\Core\Models\Scene $scene
* @param self $scene
* @return bool True if yes.
*/
public function isConnectedTo(self $scene): bool
@@ -228,7 +228,7 @@ class Scene implements CreateableInterface, SceneConnectable
/**
* Adds a connection to the outgoing connections.
* @param \LotGD\Core\Models\SceneConnection $connection
* @param SceneConnection $connection
*/
public function addOutgoingConnection(SceneConnection $connection): void
{
@@ -242,7 +242,7 @@ class Scene implements CreateableInterface, SceneConnectable
/**
* Adds a connection to the incoming connections.
* @param \LotGD\Core\Models\SceneConnection $connection
* @param SceneConnection $connection
*/
public function addIncomingConnection(SceneConnection $connection): void
{
+1 -1
View File
@@ -28,7 +28,7 @@ class SceneConnection
private $incomingScene;
/**
* @Column(type="integer", options={"default"= 0})
* @Column(type="integer", options={"default"=0})
*/
private $directionality = 0;
+65
View File
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Models;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Table;
use LotGD\Core\Exceptions\ArgumentException;
use LotGD\Core\Exceptions\ClassNotFoundException;
use LotGD\Core\SceneTemplates\SceneTemplateInterface;
/**
* Class SceneTemplates.
* @Entity
* @Table("scene_templates")
*/
class SceneTemplate
{
/** @Id @Column(type="string", length=255, unique=True, name="class") */
protected $class;
/** @Column(type="string", length=255, name="module") */
protected $module;
/** @Column(type="boolean", options={"default"=true}) */
protected $userAssignable = true;
/**
* SceneTemplates constructor.
* @param string $class FQCN of the scene handling class.
* @param string $module Module from where the class is from.
* @throws ClassNotFoundException
* @throws ArgumentException
*/
public function __construct(string $class, string $module)
{
if (!\class_exists($class)) {
throw new ClassNotFoundException("The class {$class} cannot be found.");
} elseif (\is_a($class, SceneTemplateInterface::class, true) === false) {
throw new ArgumentException("The given {$class} must implement SceneTemplateInterface");
}
$this->class = $class;
$this->module = $module;
}
/**
* @return string The class name of the template.
*/
public function getClass(): string
{
return $this->class;
}
/**
* Changes whether the template should be able to get manually assigned to a template or not.
* @param bool $flag
*/
public function setUserAssignable(bool $flag = true)
{
$this->userAssignable = $flag;
}
}
+6 -3
View File
@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace LotGD\Core\Models;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
@@ -140,7 +141,7 @@ class Viewpoint implements CreateableInterface
$snapshot = new ViewpointSnapshot(
$this->getTitle(),
$this->getDescription(),
$this->getTemplate(),
\get_class($this->getTemplate()),
$this->getActionGroups(),
$this->getAttachments(),
$this->getData()
@@ -153,11 +154,13 @@ class Viewpoint implements CreateableInterface
* Changes the current viewpoint to the state saved in the given restoration point.
* @param ViewpointSnapshot $snapshot
*/
public function changeFromSnapshot(ViewpointSnapshot $snapshot)
public function changeFromSnapshot(EntityManager $entityManager, ViewpointSnapshot $snapshot)
{
$templateInstance = $entityManager->getRepository(SceneTemplate::class)->find($snapshot->getTemplate());
$this->setTitle($snapshot->getTitle());
$this->setDescription($snapshot->getDescription());
$this->setTemplate($snapshot->getTemplate());
$this->setTemplate($templateInstance);
$this->setActionGroups($snapshot->getActionGroups());
$this->setAttachments($snapshot->getAttachments());
$this->setData($snapshot->getData());
+21
View File
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\SceneTemplates;
/**
* Class BasicSceneTemplate.
*
* Offers a basic scene template. All scenes with no template use this class internally.
*/
class BasicSceneTemplate implements SceneTemplateInterface
{
/**
* {@inheritDoc}
* @return string
*/
public static function getNavigationEvent(): string
{
return "no-template";
}
}
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\SceneTemplates;
interface SceneTemplateInterface
{
/**
* Returns the event string that's attached to the navigation-to hook.
* @return string
*/
public static function getNavigationEvent(): string;
}
+14 -5
View File
@@ -3,6 +3,11 @@ declare(strict_types=1);
namespace LotGD\Core\Tools\Model;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use LotGD\Core\Models\SceneTemplate;
/**
* Provides scene basics.
*/
@@ -13,7 +18,11 @@ trait SceneBasics
/** @Column(type="text") */
private $description = "{No scene set}";
/** @Column(type="string", length=255) */
private $template = "{No template set}";
/**
* @ManyToOne(targetEntity="SceneTemplate", fetch="EAGER")
* @JoinColumn(name="template", referencedColumnName="class", nullable=true)
*/
private $template;
/**
* Sets scene title.
@@ -53,18 +62,18 @@ trait SceneBasics
/**
* Sets scene template.
* @param string $template
* @param SceneTemplate|null $template
*/
public function setTemplate(string $template)
public function setTemplate(?SceneTemplate $template)
{
$this->template = $template;
}
/**
* Returns scene template.
* @return string
* @return SceneTemplate|null
*/
public function getTemplate(): string
public function getTemplate(): ?SceneTemplate
{
return $this->template;
}
+23 -10
View File
@@ -8,9 +8,21 @@ use Doctrine\ORM\EntityManager;
use Monolog\Logger;
use Monolog\Handler\NullHandler;
use LotGD\Core\{
Action, ActionGroup, Bootstrap, Configuration, ComposerManager, DiceBag, EventHandler, EventManager, Events\NewViewpointData, Game, GameBuilder, TimeKeeper, ModuleManager
};
use LotGD\Core\{Action,
ActionGroup,
Bootstrap,
Configuration,
ComposerManager,
DiceBag,
EventHandler,
EventManager,
Events\NewViewpointData,
Game,
GameBuilder,
Tests\SceneTemplates\ParameterTestSceneTemplate,
Tests\SceneTemplates\VillageSceneTemplate,
TimeKeeper,
ModuleManager};
use LotGD\Core\Models\{
Character, Viewpoint, Scene
};
@@ -40,7 +52,7 @@ class DefaultSceneProvider implements EventHandler
$context->setDataField("scene", $g->getEntityManager()->getRepository(Scene::class)
->find("30000000-0000-0000-0000-000000000001"));
break;
case 'h/lotgd/core/navigate-to/lotgd/tests/village':
case "h/lotgd/core/navigate-to/".VillageSceneTemplate::getNavigationEvent();
$v = $context->getDataField('viewpoint');
self::$actionGroups = [new ActionGroup('default', 'Title', 0)];
@@ -54,7 +66,7 @@ class DefaultSceneProvider implements EventHandler
$v->setData(self::$data);
break;
case 'h/lotgd/core/navigate-to/lotgd/tests/paramaters':
case "h/lotgd/core/navigate-to/".ParameterTestSceneTemplate::getNavigationEvent():
/* @var Viewpoint $v //*/
$v = $context->getDataField('viewpoint');
/* @var array //*/
@@ -164,8 +176,9 @@ class GameTest extends CoreModelTestCase
$v = $this->g->getViewpoint();
// Run it twice to make sure no additional DB operations happen.
/** @var Viewpoint $v */
$v = $this->g->getViewpoint();
$this->assertEquals('lotgd/tests/village', $v->getTemplate());
$this->assertSame(VillageSceneTemplate::class, $v->getTemplate()->getClass());
// Validate the changes made by the hook.
$this->assertSame(DefaultSceneProvider::$actionGroups, $v->getActionGroups());
@@ -215,7 +228,7 @@ class GameTest extends CoreModelTestCase
$this->g->setCharacter($c);
// subscribe event
$this->g->getEventManager()->subscribe('#h/lotgd/core/navigate-to/lotgd/tests/paramaters#', DefaultSceneProvider::class, 'lotgd/core/tests');
$this->g->getEventManager()->subscribe('#'.ParameterTestSceneTemplate::getNavigationEvent().'#', DefaultSceneProvider::class, 'lotgd/core/tests');
$this->getEntityManager()->flush();
$action = new Action("30000000-0000-0000-0000-000000000007", null, ["foo" => "baz"]);
@@ -235,7 +248,7 @@ class GameTest extends CoreModelTestCase
$this->assertSame("Parameter is baz.", $v->getDescription());
// unsubscribe event
$this->g->getEventManager()->unsubscribe('#h/lotgd/core/navigate-to/lotgd/tests/paramaters#', DefaultSceneProvider::class, 'lotgd/core/tests');
$this->g->getEventManager()->unsubscribe('#'.ParameterTestSceneTemplate::getNavigationEvent().'#', DefaultSceneProvider::class, 'lotgd/core/tests');
}
public function testIfActionParametersTakePriorityToOtherParameters()
@@ -245,7 +258,7 @@ class GameTest extends CoreModelTestCase
$this->g->setCharacter($c);
// subscribe event
$this->g->getEventManager()->subscribe('#h/lotgd/core/navigate-to/lotgd/tests/paramaters#', DefaultSceneProvider::class, 'lotgd/core/tests');
$this->g->getEventManager()->subscribe('#'.ParameterTestSceneTemplate::getNavigationEvent().'#', DefaultSceneProvider::class, 'lotgd/core/tests');
$this->getEntityManager()->flush();
$action = new Action("30000000-0000-0000-0000-000000000007", null, ["foo" => "baz"]);
@@ -265,7 +278,7 @@ class GameTest extends CoreModelTestCase
$this->assertSame("Parameter is baz.", $v->getDescription());
// unsubscribe event
$this->g->getEventManager()->unsubscribe('#h/lotgd/core/navigate-to/lotgd/tests/paramaters#', DefaultSceneProvider::class, 'lotgd/core/tests');
$this->g->getEventManager()->unsubscribe('#'.ParameterTestSceneTemplate::getNavigationEvent().'#', DefaultSceneProvider::class, 'lotgd/core/tests');
}
public function testIfActionsAreAddedAsExpected()
+3 -2
View File
@@ -4,8 +4,9 @@ declare(strict_types=1);
namespace LotGD\Core\Tests\Models;
use LotGD\Core\Exceptions\ArgumentException;
use LotGD\Core\Models\{Scene, SceneConnection, SceneConnectionGroup};
use LotGD\Core\Models\{Scene, SceneConnection, SceneConnectionGroup, SceneTemplate};
use LotGD\Core\Tests\CoreModelTestCase;
use LotGD\Core\Tests\SceneTemplates\NewSceneSceneTemplate;
/**
* Tests for creating scenes and moving them around.
@@ -38,7 +39,7 @@ class SceneModelTest extends CoreModelTestCase
return [
"title" => "A new scene",
"description" => "This is a new scene",
"template" => "lotgd/test/new-scene"
"template" => $this->getEntityManager()->getRepository(SceneTemplate::class)->find(NewSceneSceneTemplate::class),
];
}
+28 -8
View File
@@ -3,9 +3,13 @@ declare(strict_types=1);
namespace LotGD\Core\Tests\Models;
use Composer\Repository\RepositoryInterface;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\EntityManager;
use LotGD\Core\Action;
use LotGD\Core\ActionGroup;
use LotGD\Core\Models\Scene;
use LotGD\Core\Models\SceneTemplate;
use LotGD\Core\Models\Viewpoint;
use LotGD\Core\Tests\CoreModelTestCase;
@@ -29,10 +33,18 @@ class ViewpointRestorationTest extends CoreModelTestCase
protected function getViewpoint()
{
$sceneTemplateMock = $this->createMock(SceneTemplate::class);
$sceneMock = $this->createMock(Scene::class);
$sceneMock->method("getTitle")->willReturn("Scene Mock Title");
$sceneMock->method("getDescription")->willReturn("Scene Mock Description");
$sceneMock->method("getTemplate")->willReturn("lotgd/scene-mock-template");
$sceneMock->method("getTemplate")->willReturn($sceneTemplateMock);
$repository = $this->createMock(ObjectRepository::class);
$repository->method("find")->willReturn($sceneTemplateMock);
$entityManager = $this->createMock(EntityManager::class);
$entityManager->method("getRepository")->willReturn($repository);
$actionGroup = $this->getActionGroup();
@@ -40,31 +52,39 @@ class ViewpointRestorationTest extends CoreModelTestCase
$viewpoint->changeFromScene($sceneMock);
$viewpoint->setActionGroups([$actionGroup]);
return $viewpoint;
return [$entityManager, $viewpoint];
}
protected function getAlternativeViewpoint()
{
$sceneTemplateMock = $this->createMock(SceneTemplate::class);
$sceneMock = $this->createMock(Scene::class);
$sceneMock->method("getTitle")->willReturn("Another Scene Mock Title");
$sceneMock->method("getDescription")->willReturn("Another Scene Mock Description");
$sceneMock->method("getTemplate")->willReturn("lotgd/scene-mock-template/another");
$sceneMock->method("getTemplate")->willReturn($sceneTemplateMock);
$repository = $this->createMock(ObjectRepository::class);
$repository->method("find")->willReturn($sceneTemplateMock);
$entityManager = $this->createMock(EntityManager::class);
$entityManager->method("getRepository")->willReturn($repository);
$viewpoint = new Viewpoint();
$viewpoint->changeFromScene($sceneMock);
return $viewpoint;
return [$entityManager, $viewpoint];
}
public function testIfViewpointAfterUnserializationIsEqualToBeforeItsSerialization()
{
$viewpoint = $this->getViewpoint();
$viewpointRestoration = $this->getViewpoint()->getSnapshot();
[$entityManager, $viewpoint] = $this->getViewpoint();
$viewpointRestoration = $viewpoint->getSnapshot();
$serialized = serialize($viewpointRestoration);
$viewpointRestored = unserialize($serialized);
$newViewpoint = $this->getAlternativeViewpoint();
$newViewpoint->changeFromSnapshot($viewpointRestored);
[$entityManager2, $newViewpoint] = $this->getAlternativeViewpoint();
$newViewpoint->changeFromSnapshot($entityManager, $viewpointRestored);
$this->assertSame($viewpoint->getTitle(), $newViewpoint->getTitle());
$this->assertSame($viewpoint->getDescription(), $newViewpoint->getDescription());
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests\SceneTemplates;
use LotGD\Core\SceneTemplates\BasicSceneTemplate;
class NewSceneSceneTemplate extends BasicSceneTemplate
{
public static function getNavigationEvent(): string
{
return "tests/new-scene";
}
}
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests\SceneTemplates;
use LotGD\Core\SceneTemplates\BasicSceneTemplate;
class ParameterTestSceneTemplate extends BasicSceneTemplate
{
public static function getNavigationEvent(): string
{
return "tests/parameter";
}
}
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tests\SceneTemplates;
use LotGD\Core\SceneTemplates\BasicSceneTemplate;
class VillageSceneTemplate extends BasicSceneTemplate
{
public static function getNavigationEvent(): string
{
return "tests/village";
}
}
+14 -5
View File
@@ -25,7 +25,7 @@ scenes:
id: "30000000-0000-0000-0000-000000000001"
title: "The Village"
description: "This is the village."
template: "lotgd/tests/village"
template: "LotGD\\Core\\Tests\\SceneTemplates\\VillageSceneTemplate"
-
id: "30000000-0000-0000-0000-000000000002"
title: "The Forest"
@@ -40,22 +40,31 @@ scenes:
id: "30000000-0000-0000-0000-000000000004"
title: "Parent Scene"
description: "This is a parent scene that connects to two children."
template: "lotgd/tests/none"
template:
-
id: "30000000-0000-0000-0000-000000000005"
title: "Child Scene 1"
description: "This is a parent scene that connects to two children."
template: "lotgd/tests/none"
template:
-
id: "30000000-0000-0000-0000-000000000006"
title: "Child Scene 2"
description: "This is a parent scene that connects to two children."
template: "lotgd/tests/none"
template:
-
id: "30000000-0000-0000-0000-000000000007"
title: "Parameter test"
description: "This is a parameter test"
template: "lotgd/tests/paramaters"
template: "LotGD\\Core\\Tests\\SceneTemplates\\ParameterTestSceneTemplate"
scene_templates:
-
class: "LotGD\\Core\\Tests\\SceneTemplates\\VillageSceneTemplate"
module: "lotgd/core"
userAssignable: true
-
class: "LotGD\\Core\\Tests\\SceneTemplates\\ParameterTestSceneTemplate"
module: "lotgd/core"
userAssignable: true
scene_connection_groups:
-
scene: "30000000-0000-0000-0000-000000000004"
+6 -1
View File
@@ -50,4 +50,9 @@ scene_connections:
incomingScene: "30000000-0000-0000-0000-000000000003"
-
outgoingScene: "30000000-0000-0000-0000-000000000001"
incomingScene: "30000000-0000-0000-0000-000000000004"
incomingScene: "30000000-0000-0000-0000-000000000004"
scene_templates:
-
class: "LotGD\\Core\\Tests\\SceneTemplates\\NewSceneSceneTemplate"
module: "lotgd/core"
userAssignable: true