Adds possibility to add parameters to actions.

This commit is contained in:
Vassyli
2017-06-12 14:58:40 +02:00
parent bbc960fd3d
commit 29e474b9c1
4 changed files with 105 additions and 2 deletions
+23 -2
View File
@@ -11,15 +11,18 @@ class Action
{
protected $id;
protected $destinationSceneId;
protected $parameters = [];
/**
* Construct a new action with the specified Scene as its destination.
* @param int $destinationSceneId
* @param array $parameters
*/
public function __construct(int $destinationSceneId)
public function __construct(int $destinationSceneId, array $parameters = [])
{
$this->id = bin2hex(random_bytes(8));
$this->destinationSceneId = $destinationSceneId;
$this->parameters = $parameters;
}
/**
@@ -35,10 +38,28 @@ class Action
/**
* Return the database ID of the destination scene, where the user will
* go if they take this action.
* @return string
* @return int
*/
public function getDestinationSceneId(): int
{
return $this->destinationSceneId;
}
/**
* Returns all parameters for this action
* @return array
*/
public function getParameters(): array
{
return $this->parameters;
}
/**
* Sets all parameters for this action
* @param array $parameters
*/
public function setParameters(array $parameters): void
{
$this->parameters = $parameters;
}
}
+6
View File
@@ -333,6 +333,7 @@ class Game
if ($action === null) {
throw new ActionNotFoundException("Invalid actionId={$actionId} for current viewpoint.");
}
$actionParameters = $action->getParameters();
$sceneId = $action->getDestinationSceneId();
$scene = $this->getEntityManager()->getRepository(Scene::class)->find([
@@ -341,7 +342,12 @@ class Game
if ($scene == null) {
throw new SceneNotFoundException("Cannot find sceneId={$sceneId} specified by actionId={$actionId}.");
}
// action parameters overwrite other parameters since the former cannot be changed by the user
$parameters = array_merge($parameters, $actionParameters);
$this->navigateToScene($scene, $parameters);
$v->save($this->getEntityManager());
}
}
+71
View File
@@ -52,6 +52,19 @@ class DefaultSceneProvider implements EventHandler
$v->setAttachments(self::$attachments);
$v->setData(self::$data);
break;
case 'h/lotgd/core/navigate-to/lotgd/tests/paramaters':
/* @var Viewpoint $v //*/
$v = $context->getDataField('viewpoint');
/* @var array //*/
$p = $context->getDataField('parameters');
if ($p["foo"] === "baz") {
$v->setDescription("Parameter is baz.");
} else {
$v->setDescription("Parameter is NOT baz.");
}
break;
}
return $context;
@@ -188,6 +201,64 @@ class GameTest extends CoreModelTestCase
$this->assertSame($s->getTemplate(), $v->getTemplate());
}
public function testIfActionParametersAreRelayedToEvent()
{
/* @var $c Character */
$c = $this->getEntityManager()->getRepository(Character::class)->find(2);
$this->g->setCharacter($c);
// subscribe event
$this->g->getEventManager()->subscribe('#h/lotgd/core/navigate-to/lotgd/tests/paramaters#', DefaultSceneProvider::class, 'lotgd/core/tests');
$action = new Action(7, ["foo" => "baz"]);
$actionId = $action->getId();
$ag = new ActionGroup("group1", "Group 1", 5);
$ag->addAction($action);
$v = $c->getViewpoint();
$v->setDescription("Test");
$v->setActionGroups([$ag]);
$c->setViewpoint($v);
$this->g->takeAction($actionId);
$v = $c->getViewpoint();
$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');
}
public function testIfActionParametersTakePriorityToOtherParameters()
{
/* @var $c Character */
$c = $this->getEntityManager()->getRepository(Character::class)->find(2);
$this->g->setCharacter($c);
// subscribe event
$this->g->getEventManager()->subscribe('#h/lotgd/core/navigate-to/lotgd/tests/paramaters#', DefaultSceneProvider::class, 'lotgd/core/tests');
$action = new Action(7, ["foo" => "baz"]);
$actionId = $action->getId();
$ag = new ActionGroup("group1", "Group 1", 5);
$ag->addAction($action);
$v = $c->getViewpoint();
$v->setDescription("Test");
$v->setActionGroups([$ag]);
$c->setViewpoint($v);
$this->g->takeAction($actionId, ["foo" => "nobaz"]);
$v = $c->getViewpoint();
$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');
}
public function testIfActionsAreAddedAsExpected()
{
$viewpointToArray = function(Viewpoint $v) {
+5
View File
@@ -51,6 +51,11 @@ scenes:
title: "Child Scene 2"
description: "This is a parent scene that connects to two children."
template: "lotgd/tests/none"
-
id: 7
title: "Parameter test"
description: "This is a parameter test"
template: "lotgd/tests/paramaters"
scene_connection_groups:
-
scene: 4