Implemented Character->getCharacterModel()
This commit is contained in:
@@ -5,6 +5,7 @@ namespace LotGD\Core\Models;
|
|||||||
|
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Doctrine\ORM\Mapping\Entity;
|
use Doctrine\ORM\Mapping\Entity;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
use LotGD\Core\Tools\Model\Creator;
|
use LotGD\Core\Tools\Model\Creator;
|
||||||
use LotGD\Core\Tools\Model\Deletor;
|
use LotGD\Core\Tools\Model\Deletor;
|
||||||
@@ -26,7 +27,7 @@ class Character
|
|||||||
private $id;
|
private $id;
|
||||||
/** @Column(type="string", length=50, unique=true); */
|
/** @Column(type="string", length=50, unique=true); */
|
||||||
private $name;
|
private $name;
|
||||||
/** @Column(type="text", unique=true); */
|
/** @Column(type="text"); */
|
||||||
private $displayName;
|
private $displayName;
|
||||||
/** @Column(type="integer", options={"default" = 10}) */
|
/** @Column(type="integer", options={"default" = 10}) */
|
||||||
private $maxHealth = 10;
|
private $maxHealth = 10;
|
||||||
@@ -34,7 +35,7 @@ class Character
|
|||||||
private $health = 10;
|
private $health = 10;
|
||||||
/** @OneToMany(targetEntity="CharacterProperty", mappedBy="owner", cascade={"persist"}) */
|
/** @OneToMany(targetEntity="CharacterProperty", mappedBy="owner", cascade={"persist"}) */
|
||||||
private $properties;
|
private $properties;
|
||||||
/** @OneToOne(targetEntity="CharacterScene", mappedBy="owner", cascade={"persist"}) */
|
/** @OneToMany(targetEntity="CharacterScene", mappedBy="owner", cascade={"persist"}) */
|
||||||
private $characterScene;
|
private $characterScene;
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
@@ -55,6 +56,7 @@ class Character
|
|||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->properties = new ArrayCollection();
|
$this->properties = new ArrayCollection();
|
||||||
|
$this->characterScene = new ArrayCollection();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -143,12 +145,13 @@ class Character
|
|||||||
* Returns the current character scene and creates one if it is non-existant
|
* Returns the current character scene and creates one if it is non-existant
|
||||||
* @return \LotGD\Core\Models\CharacterScene
|
* @return \LotGD\Core\Models\CharacterScene
|
||||||
*/
|
*/
|
||||||
public function getCharacterScene(): CharacterScene
|
public function getCharacterScene(EntityManagerInterface $em): CharacterScene
|
||||||
{
|
{
|
||||||
if ($this->characterScene === null) {
|
if (count($this->characterScene) === 0) {
|
||||||
$this->characterScene = CharacterScene::create(["owner" => $this]);
|
$characterScene = CharacterScene::Create(["owner" => $this]);
|
||||||
|
$this->characterScene->add($characterScene);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->characterScene;
|
return $this->characterScene->first();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace LotGD\Core\Models;
|
namespace LotGD\Core\Models;
|
||||||
|
|
||||||
@@ -9,14 +10,21 @@ use LotGD\Core\Tools\Model\SceneBasics;
|
|||||||
* A CharacterScene is the current Scene a character is experiencing with
|
* A CharacterScene is the current Scene a character is experiencing with
|
||||||
* all changes from modules included.
|
* all changes from modules included.
|
||||||
* @Entity
|
* @Entity
|
||||||
* @Table(name="character_scene")
|
* @Table(name="character_scenes")
|
||||||
*/
|
*/
|
||||||
class CharacterScene {
|
class CharacterScene
|
||||||
|
{
|
||||||
use Creator;
|
use Creator;
|
||||||
use SceneBasics;
|
use SceneBasics;
|
||||||
|
|
||||||
/** @Id @OneToOne(targetEntity="Character") */
|
/** @Id @ManyToOne(targetEntity="Character", inversedBy="id", cascade="persist") */
|
||||||
private $owner;
|
private $owner;
|
||||||
|
/** @Column(type="array") */
|
||||||
|
private $actions = [];
|
||||||
|
/** @Column(type="array") */
|
||||||
|
private $attachements = [];
|
||||||
|
/** @Column(type="array") */
|
||||||
|
private $data = [];
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
private static $fillable = [
|
private static $fillable = [
|
||||||
@@ -45,8 +53,65 @@ class CharacterScene {
|
|||||||
* Copies the static data from a scene to a characterScene entity
|
* Copies the static data from a scene to a characterScene entity
|
||||||
* @param \LotGD\Core\Models\Scene $scene
|
* @param \LotGD\Core\Models\Scene $scene
|
||||||
*/
|
*/
|
||||||
public function changeFromScene(Scene $scene) {
|
public function changeFromScene(Scene $scene)
|
||||||
|
{
|
||||||
$this->setTitle($scene->getTitle());
|
$this->setTitle($scene->getTitle());
|
||||||
$this->setDescription($scene->getDescription());
|
$this->setDescription($scene->getDescription());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all actions
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getActions(): array
|
||||||
|
{
|
||||||
|
return $this->actions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets actions
|
||||||
|
* @param array $actions
|
||||||
|
*/
|
||||||
|
public function setActions(array $actions)
|
||||||
|
{
|
||||||
|
$this->actions = $actions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all data
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getData(): array
|
||||||
|
{
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets all data
|
||||||
|
* @param array $data
|
||||||
|
*/
|
||||||
|
public function setData(array $data)
|
||||||
|
{
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a single data field
|
||||||
|
* @param string $fieldname Fieldname
|
||||||
|
* @param type $default default value
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getDataField(string $fieldname, $default = null)
|
||||||
|
{
|
||||||
|
return $this->data[$fieldname] ?? $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets a single data field
|
||||||
|
* @param string $fieldname
|
||||||
|
*/
|
||||||
|
public function setDataField(string $fieldname, $value)
|
||||||
|
{
|
||||||
|
$this->data[$fieldname] = $value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,7 @@ use LotGD\Core\Models\CharacterProperty;
|
|||||||
use LotGD\Core\Tests\ModelTestCase;
|
use LotGD\Core\Tests\ModelTestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description of CharacterModelTest
|
* Tests the management of Characters
|
||||||
*
|
|
||||||
* @author Basilius Sauter
|
|
||||||
*/
|
*/
|
||||||
class CharacterModelTest extends ModelTestCase {
|
class CharacterModelTest extends ModelTestCase {
|
||||||
/** @var string default data set */
|
/** @var string default data set */
|
||||||
@@ -65,6 +63,8 @@ class CharacterModelTest extends ModelTestCase {
|
|||||||
$characterEntity->save($em);
|
$characterEntity->save($em);
|
||||||
|
|
||||||
$this->assertInternalType("int", $characterEntity->getId());
|
$this->assertInternalType("int", $characterEntity->getId());
|
||||||
|
|
||||||
|
$em->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace LotGD\Core\Tests\Models;
|
||||||
|
|
||||||
|
use LotGD\Core\Models\Character;
|
||||||
|
use LotGD\Core\Models\CharacterScene;
|
||||||
|
use LotGD\Core\Tests\ModelTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the management of CharacterScenes
|
||||||
|
*/
|
||||||
|
class CharacterSceneModelTest extends ModelTestCase
|
||||||
|
{
|
||||||
|
/** @var string default data set */
|
||||||
|
protected $dataset = "characterScenes";
|
||||||
|
|
||||||
|
public function testGetters() {
|
||||||
|
$em = $this->getEntityManager();
|
||||||
|
|
||||||
|
$testCharacter = $em->getRepository(Character::class)->find(2);
|
||||||
|
$this->assertSame(2, $testCharacter->getId());
|
||||||
|
$characterScene = $testCharacter->getCharacterScene($em);
|
||||||
|
|
||||||
|
/*$this->assertInstanceOf(CharacterScene::class, $characterScene);
|
||||||
|
$this->assertSame("The Village", $characterScene->getTitle());
|
||||||
|
$this->assertSame("This is the village.", $characterScene->getDescription());*/
|
||||||
|
|
||||||
|
$em->flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ use LotGD\Core\Models\Scene;
|
|||||||
use LotGD\Core\Tests\ModelTestCase;
|
use LotGD\Core\Tests\ModelTestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description of CharacterModelTest
|
* Tests for creating scenes and moving them around.
|
||||||
*/
|
*/
|
||||||
class SceneModelTest extends ModelTestCase
|
class SceneModelTest extends ModelTestCase
|
||||||
{
|
{
|
||||||
@@ -27,6 +27,8 @@ class SceneModelTest extends ModelTestCase
|
|||||||
$this->assertInstanceOf(Scene::class, $scene->getParent());
|
$this->assertInstanceOf(Scene::class, $scene->getParent());
|
||||||
$this->assertEquals(true, $scene->hasParent());
|
$this->assertEquals(true, $scene->hasParent());
|
||||||
$this->assertEquals(false, $scene->hasChildren());
|
$this->assertEquals(false, $scene->hasChildren());
|
||||||
|
|
||||||
|
$em->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,6 +43,8 @@ class SceneModelTest extends ModelTestCase
|
|||||||
|
|
||||||
$this->assertEquals($parentScene, $childScene->getParent());
|
$this->assertEquals($parentScene, $childScene->getParent());
|
||||||
$this->assertContains($childScene, $parentScene->getChildren());
|
$this->assertContains($childScene, $parentScene->getChildren());
|
||||||
|
|
||||||
|
$em->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -81,5 +85,7 @@ class SceneModelTest extends ModelTestCase
|
|||||||
$this->assertEquals(false, $orphanScene->hasParent());
|
$this->assertEquals(false, $orphanScene->hasParent());
|
||||||
$this->assertNotContains($orphanScene, $parentScene1->getChildren());
|
$this->assertNotContains($orphanScene, $parentScene1->getChildren());
|
||||||
$this->assertNotContains($orphanScene, $parentScene2->getChildren());
|
$this->assertNotContains($orphanScene, $parentScene2->getChildren());
|
||||||
|
|
||||||
|
$em->flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
characters:
|
||||||
|
-
|
||||||
|
id: 1
|
||||||
|
name: "Char without a Scene"
|
||||||
|
displayName: "A"
|
||||||
|
-
|
||||||
|
id: 2
|
||||||
|
name: "Char with a Scene"
|
||||||
|
displayName: "B"
|
||||||
|
character_scenes:
|
||||||
|
-
|
||||||
|
owner_id: 2
|
||||||
|
title: "The Village"
|
||||||
|
description: "This is the village."
|
||||||
|
data: "a:0:{}"
|
||||||
|
attachements: "a:0:{}"
|
||||||
|
actions: "a:0:{}"
|
||||||
|
scenes:
|
||||||
|
-
|
||||||
|
id: 1
|
||||||
|
title: "The Village"
|
||||||
|
description: "This is the village."
|
||||||
|
parent:
|
||||||
|
-
|
||||||
|
id: 2
|
||||||
|
title: "The Forest"
|
||||||
|
description: "This is a very dangerous and dark forest"
|
||||||
|
parent: 1
|
||||||
|
-
|
||||||
|
id: 3
|
||||||
|
title: "The Weaponry"
|
||||||
|
description: "This is the place where you can buy awesome weapons"
|
||||||
|
parent: 1
|
||||||
Reference in New Issue
Block a user