Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scene templates/interface #145

Merged
merged 3 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ public function setCharacter(Character $c)

/**
* Return the viewpoint for the current user.
* @throws InvalidConfigurationException
* @return Viewpoint
* @throws InvalidConfigurationException|CharacterNotFoundException
*/
public function getViewpoint(): Viewpoint
{
Expand Down Expand Up @@ -263,6 +263,7 @@ public function getViewpoint(): Viewpoint
* by the hook.
* @param Scene $scene
* @param array $parameters
* @throws CharacterNotFoundException
*/
private function navigateToScene(Scene $scene, array $parameters)
{
Expand Down
10 changes: 10 additions & 0 deletions src/Models/Scene.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use LotGD\Core\Exceptions\ArgumentException;
use LotGD\Core\Tools\Model\Creator;
use LotGD\Core\Tools\Model\Deletor;
use LotGD\Core\Tools\Model\PropertyManager;
use LotGD\Core\Tools\Model\SceneBasics;
use Ramsey\Uuid\Uuid;

Expand All @@ -26,6 +27,7 @@ class Scene implements CreateableInterface, SceneConnectable
use Creator;
use Deletor;
use SceneBasics;
use PropertyManager;

/** @Id @Column(type="string", length=36, unique=True, name="id", options={"fixed"=true}) */
protected $id;
Expand All @@ -45,6 +47,14 @@ class Scene implements CreateableInterface, SceneConnectable
*/
private $incomingConnections = null;

/**
* @OneToMany(targetEntity="SceneProperty", mappedBy="owner", cascade={"persist", "remove"})
*/
private $properties;

// required for PropertyManager to now which class the properties belong to.
private $propertyClass = SceneProperty::class;

/**
* @var array
*/
Expand Down
43 changes: 43 additions & 0 deletions src/Models/SceneProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);

namespace LotGD\Core\Models;

use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;

use LotGD\Core\Tools\Model\Properties;
use Ramsey\Uuid\Uuid;

/**
* A place for modules to store per-module private data.
* @Entity
* @Table(name="scene_properties")
*/
class SceneProperty
{
use Properties;

/** @Id @ManyToOne(targetEntity="Scene", inversedBy="properties")
* @JoinColumn(name="owner", referencedColumnName="id")
*/
private Scene $owner;

/**
* Returns the owner.
* @return Scene
*/
public function getOwner(): Scene
{
return $this->owner;
}

/**
* Sets the owner.
* @param Scene $owner
*/
public function setOwner(Scene $owner)
{
$this->owner = $owner;
}
}
85 changes: 85 additions & 0 deletions tests/Models/ScenePropertyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);

namespace LotGD\Core\Tests\Models;

use LotGD\Core\Models\Scene;
use LotGD\Core\Models\SceneProperty;
use LotGD\Core\Tests\CoreModelTestCase;

class ScenePropertyTest extends CoreModelTestCase
{
/** @var string default data set */
protected $dataset = "scene-property";

public function testIfScenePropertyCanBeRetrieved()
{
$em = $this->getEntityManager();

# Retrieve scene
$scene = $em->getRepository(Scene::class)->find("30000000-0000-0000-0000-000000000001");

# Fetch property, with default "false".
# Must return true, as this is whats in our dataset
$value = $scene->getProperty("lotgd/core/tests/property/dataset", false);

# Assert the value
$this->assertTrue($value);
}

public function testIfPropertyCanBeSaved()
{
$em = $this->getEntityManager();

# Retrieve scene
$scene = $em->getRepository(Scene::class)->find("30000000-0000-0000-0000-000000000001");

# Set a property
$scene->setProperty("lotgd/core/tests/newProperty", "test-value");

# Get the property value back
$value = $scene->getProperty("lotgd/core/tests/newProperty", null);

# Assert the value
$this->assertSame("test-value", $value);

# Save and clear entity manager, the fetch again.
# Persisting the property should not be necessary
unset($value);
$em->flush();
$em->clear();

# Retrieve scene (again)
$scene = $em->getRepository(Scene::class)->find("30000000-0000-0000-0000-000000000001");

# Get a property
$value = $scene->getProperty("lotgd/core/tests/newProperty", null);

# Assert the value
$this->assertSame("test-value", $value);
}

public function testIfSceneRemovalRemovesPropertyToo()
{
$em = $this->getEntityManager();

# Retrieve scene
$scene = $em->getRepository(Scene::class)->find("30000000-0000-0000-0000-000000000001");

# Delete scene
$em->remove($scene);
$em->flush();
$em->clear();

# Retrieve properties
$sceneProperties = $em->getRepository(SceneProperty::class)->findAll();

# Get all property names
$listOfPropertyNames = [];
foreach ($sceneProperties as $property) {
$listOfPropertyNames[$property->getName()] = $property->getValue();
}

$this->assertArrayNotHasKey("lotgd/core/tests/property/dataset", $listOfPropertyNames);
}
}
11 changes: 11 additions & 0 deletions tests/datasets/scene-property.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
scenes:
-
id: "30000000-0000-0000-0000-000000000001"
title: "A scene"
description: "This is the village."
template: "lotgd/tests/village"
scene_properties:
-
owner: "30000000-0000-0000-0000-000000000001"
propertyName: "lotgd/core/tests/property/dataset"
propertyValue: "b:1;"