Implemented Character->getCharacterModel()
This commit is contained in:
@@ -5,6 +5,7 @@ namespace LotGD\Core\Models;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use LotGD\Core\Tools\Model\Creator;
|
||||
use LotGD\Core\Tools\Model\Deletor;
|
||||
@@ -26,7 +27,7 @@ class Character
|
||||
private $id;
|
||||
/** @Column(type="string", length=50, unique=true); */
|
||||
private $name;
|
||||
/** @Column(type="text", unique=true); */
|
||||
/** @Column(type="text"); */
|
||||
private $displayName;
|
||||
/** @Column(type="integer", options={"default" = 10}) */
|
||||
private $maxHealth = 10;
|
||||
@@ -34,7 +35,7 @@ class Character
|
||||
private $health = 10;
|
||||
/** @OneToMany(targetEntity="CharacterProperty", mappedBy="owner", cascade={"persist"}) */
|
||||
private $properties;
|
||||
/** @OneToOne(targetEntity="CharacterScene", mappedBy="owner", cascade={"persist"}) */
|
||||
/** @OneToMany(targetEntity="CharacterScene", mappedBy="owner", cascade={"persist"}) */
|
||||
private $characterScene;
|
||||
|
||||
/** @var array */
|
||||
@@ -55,6 +56,7 @@ class Character
|
||||
|
||||
public function __construct() {
|
||||
$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
|
||||
* @return \LotGD\Core\Models\CharacterScene
|
||||
*/
|
||||
public function getCharacterScene(): CharacterScene
|
||||
public function getCharacterScene(EntityManagerInterface $em): CharacterScene
|
||||
{
|
||||
if ($this->characterScene === null) {
|
||||
$this->characterScene = CharacterScene::create(["owner" => $this]);
|
||||
if (count($this->characterScene) === 0) {
|
||||
$characterScene = CharacterScene::Create(["owner" => $this]);
|
||||
$this->characterScene->add($characterScene);
|
||||
}
|
||||
|
||||
return $this->characterScene;
|
||||
return $this->characterScene->first();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
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
|
||||
* all changes from modules included.
|
||||
* @Entity
|
||||
* @Table(name="character_scene")
|
||||
* @Table(name="character_scenes")
|
||||
*/
|
||||
class CharacterScene {
|
||||
class CharacterScene
|
||||
{
|
||||
use Creator;
|
||||
use SceneBasics;
|
||||
|
||||
/** @Id @OneToOne(targetEntity="Character") */
|
||||
/** @Id @ManyToOne(targetEntity="Character", inversedBy="id", cascade="persist") */
|
||||
private $owner;
|
||||
/** @Column(type="array") */
|
||||
private $actions = [];
|
||||
/** @Column(type="array") */
|
||||
private $attachements = [];
|
||||
/** @Column(type="array") */
|
||||
private $data = [];
|
||||
|
||||
/** @var array */
|
||||
private static $fillable = [
|
||||
@@ -45,8 +53,65 @@ class CharacterScene {
|
||||
* Copies the static data from a scene to a characterScene entity
|
||||
* @param \LotGD\Core\Models\Scene $scene
|
||||
*/
|
||||
public function changeFromScene(Scene $scene) {
|
||||
public function changeFromScene(Scene $scene)
|
||||
{
|
||||
$this->setTitle($scene->getTitle());
|
||||
$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;
|
||||
|
||||
/**
|
||||
* Description of CharacterModelTest
|
||||
*
|
||||
* @author Basilius Sauter
|
||||
* Tests the management of Characters
|
||||
*/
|
||||
class CharacterModelTest extends ModelTestCase {
|
||||
/** @var string default data set */
|
||||
@@ -65,6 +63,8 @@ class CharacterModelTest extends ModelTestCase {
|
||||
$characterEntity->save($em);
|
||||
|
||||
$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;
|
||||
|
||||
/**
|
||||
* Description of CharacterModelTest
|
||||
* Tests for creating scenes and moving them around.
|
||||
*/
|
||||
class SceneModelTest extends ModelTestCase
|
||||
{
|
||||
@@ -27,6 +27,8 @@ class SceneModelTest extends ModelTestCase
|
||||
$this->assertInstanceOf(Scene::class, $scene->getParent());
|
||||
$this->assertEquals(true, $scene->hasParent());
|
||||
$this->assertEquals(false, $scene->hasChildren());
|
||||
|
||||
$em->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,6 +43,8 @@ class SceneModelTest extends ModelTestCase
|
||||
|
||||
$this->assertEquals($parentScene, $childScene->getParent());
|
||||
$this->assertContains($childScene, $parentScene->getChildren());
|
||||
|
||||
$em->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,5 +85,7 @@ class SceneModelTest extends ModelTestCase
|
||||
$this->assertEquals(false, $orphanScene->hasParent());
|
||||
$this->assertNotContains($orphanScene, $parentScene1->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