Adds the possibility to serialize a battle state for saving it
This commit is contained in:
+34
-1
@@ -61,13 +61,46 @@ class Battle
|
||||
* @param FighterInterface $player
|
||||
* @param FighterInterface $monster
|
||||
*/
|
||||
public function __construct(Game $game, FighterInterface $player, FighterInterface $monster)
|
||||
public function __construct(Game $game, FighterInterface $player, ?FighterInterface $monster)
|
||||
{
|
||||
$this->game = $game;
|
||||
$this->player = $player;
|
||||
$this->monster = $monster;
|
||||
$this->events = new ArrayCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string which contains the important fields that must be serialized.
|
||||
* @return string
|
||||
*/
|
||||
public function serialize(): string
|
||||
{
|
||||
return serialize([
|
||||
"monster" => $this->monster,
|
||||
"result" => $this->result,
|
||||
"round" => $this->round,
|
||||
"configuration" => $this->configuration,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Game $game
|
||||
* @param FighterInterface $player
|
||||
* @param string $serialized
|
||||
* @return Battle
|
||||
*/
|
||||
public static function unserialize(Game $game, FighterInterface $player, string $serialized): self
|
||||
{
|
||||
$battle = new self($game, $player, null);
|
||||
$unserialized = unserialize($serialized);
|
||||
|
||||
$battle->monster = $unserialized["monster"];
|
||||
$battle->result = $unserialized["result"];
|
||||
$battle->round = $unserialized["round"];
|
||||
$battle->configuration = $unserialized["configuration"];
|
||||
|
||||
return $battle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ToDo Returns at some point battle actions
|
||||
|
||||
@@ -91,6 +91,35 @@ class BattleTest extends CoreModelTestCase
|
||||
$this->assertTrue($character->isAlive() xor $monster->isAlive());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a fight can happen if it is serialized between each round.
|
||||
*/
|
||||
public function testFairBattleWithSerializationBetweenRounds()
|
||||
{
|
||||
$em = $this->getEntityManager();
|
||||
|
||||
$character = $em->getRepository(Character::class)->find(1);
|
||||
$monster = $em->getRepository(Monster::class)->find(1);
|
||||
|
||||
$battle = new Battle($this->getMockGame($character), $character, $monster);
|
||||
$battle = $battle->serialize();
|
||||
|
||||
for ($n = 0; $n < 99; $n++) {
|
||||
$battle = Battle::unserialize($this->getMockGame($character), $character, $battle);
|
||||
|
||||
$battle->fightNRounds(1);
|
||||
|
||||
if ($battle->isOver()) {
|
||||
break;
|
||||
}
|
||||
|
||||
$battle = $battle->serialize();
|
||||
}
|
||||
|
||||
$this->assertTrue($battle->isOver());
|
||||
$this->assertTrue($battle->getPlayer()->isAlive() xor $battle->getMonster()->isAlive());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a fight which the player has to win (lvl 100 vs lvl 1)
|
||||
*/
|
||||
|
||||
@@ -58,7 +58,7 @@ class DiceBagTests extends \PHPUnit_Framework_TestCase
|
||||
$value = $db->dice(1, 6);
|
||||
$this->assertGreaterThanOrEqual(1, $value);
|
||||
$this->assertLessThanOrEqual(6, $value);
|
||||
|
||||
|
||||
$value = $db->dice(5, 5);
|
||||
$this->assertSame(5, $value);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user