Ad levels

This commit is contained in:
Ruben 2016-09-01 16:33:00 +01:00
parent 81bc082145
commit a656e28157
11 changed files with 514 additions and 23 deletions

View file

@ -1,6 +1,8 @@
<?php
chdir(__DIR__);
define('APPLICATION_ENV', 'development');
require_once __DIR__ . "/../bootstrap.php";
@ -34,17 +36,87 @@ foreach($queries as $sql){ echo "$sql;\n"; }
// updateSchema
$tool->updateSchema($classes);
// ADD EMOTIONS
$emotions = ['anger','contempt','disgust', 'fear', 'joy','sadness','surprise'];
foreach($emotions as $emo) {
$emotions = [];
try {
$emos = ['anger','contempt','disgust', 'fear', 'joy','sadness','surprise'];
foreach($emos as $emo) {
$emotion = $em->getRepository(EmotionHero\Models\Emotion::class)->findOneBy(['name' => $emo]);
if(empty($emotion))
{
$emotion = new EmotionHero\Models\Emotion();
$emotion->setName($emo);
$em->persist($emotion);
}
$emotions[$emo] = $emotion;
}
$em->flush();
} catch (Exception $e) {
echo "\nEmotions Probably already inserted...\n";
}
// TEST USER
if(empty($em->getRepository(EmotionHero\Models\User::class)->findOneBy([]))) {
$user = new EmotionHero\Models\User();
$em->persist($user);
$em->flush();
}
var_dump($user->getId());
$lvl = new EmotionHero\Models\Level();
$lvl->setId(1);
$lvl->setName("I am sooo ANGRY");
$lvl->createTarget($emotions['anger'], 100, 1);
$lvl->createTarget($emotions['anger'], 100, 2);
$lvl->createTarget($emotions['anger'], 10, 3);
$lvl->createTarget($emotions['anger'], 20, 4);
$lvl->createTarget($emotions['anger'], 40, 5);
$lvl->createTarget($emotions['anger'], 70, 6);
$lvl->createTarget($emotions['anger'], 100, 7);
$em->persist($lvl);
$lvl = new EmotionHero\Models\Level();
$lvl->setId(2);
$lvl->setName("Let's be joyfull!");
$lvl->createTarget($emotions['joy'], 100, 1);
$lvl->createTarget($emotions['joy'], 100, 2);
$lvl->createTarget($emotions['joy'], 100, 4);
$lvl->createTarget($emotions['contempt'], 20, 4);
$lvl->createTarget($emotions['anger'], 100, 5);
$lvl->createTarget($emotions['joy'], 100, 7);
$lvl->createTarget($emotions['anger'], 100, 9);
$lvl->createTarget($emotions['joy'], 100, 11);
$lvl->createTarget($emotions['joy'], 70, 12);
$lvl->createTarget($emotions['joy'], 60, 13);
$lvl->createTarget($emotions['joy'], 30, 14);
$lvl->createTarget($emotions['joy'], 10, 14.5);
$lvl->createTarget($emotions['anger'], 100, 16);
$lvl->createTarget($emotions['joy'], 100, 17);
$lvl->createTarget($emotions['joy'], 100, 18);
$lvl->createTarget($emotions['joy'], 100, 19);
$lvl->createTarget($emotions['joy'], 100, 20);
$em->persist($lvl);
$lvl = new EmotionHero\Models\Level();
$lvl->setId(3);
$lvl->setName("What a surprise");
$lvl->createTarget($emotions['surprise'], 20, 1);
$lvl->createTarget($emotions['surprise'], 50, 2);
$lvl->createTarget($emotions['surprise'], 80, 3);
$lvl->createTarget($emotions['surprise'], 100, 4);
$em->persist($lvl);
$lvl = new EmotionHero\Models\Level();
$lvl->setId(4);
$lvl->setName("Please, don't cry...");
$lvl->createTarget($emotions['sadness'], 20, 1);
$lvl->createTarget($emotions['sadness'], 50, 2);
$lvl->createTarget($emotions['sadness'], 80, 3);
$lvl->createTarget($emotions['sadness'], 100, 4);
$em->persist($lvl);
$em->flush();

View file

@ -64,10 +64,10 @@ class Game extends \EmotionHero\Models\Game implements \Doctrine\ORM\Proxy\Proxy
public function __sleep()
{
if ($this->__isInitialized__) {
return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'user', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'level', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'hits', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'createdAt'];
return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'user', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'level', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'hits', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'score', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'createdAt'];
}
return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'user', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'level', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'hits', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'createdAt'];
return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'user', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'level', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'hits', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'score', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'createdAt'];
}
/**
@ -173,4 +173,85 @@ class Game extends \EmotionHero\Models\Game implements \Doctrine\ORM\Proxy\Proxy
}
/**
* {@inheritDoc}
*/
public function getId()
{
if ($this->__isInitialized__ === false) {
return parent::getId();
}
$this->__initializer__ && $this->__initializer__->__invoke($this, 'getId', []);
return parent::getId();
}
/**
* {@inheritDoc}
*/
public function getUser()
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'getUser', []);
return parent::getUser();
}
/**
* {@inheritDoc}
*/
public function getLevel()
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'getLevel', []);
return parent::getLevel();
}
/**
* {@inheritDoc}
*/
public function getHits()
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'getHits', []);
return parent::getHits();
}
/**
* {@inheritDoc}
*/
public function getScore()
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'getScore', []);
return parent::getScore();
}
/**
* {@inheritDoc}
*/
public function getCreatedAt()
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'getCreatedAt', []);
return parent::getCreatedAt();
}
/**
* {@inheritDoc}
*/
public function getPosition()
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'getPosition', []);
return parent::getPosition();
}
}

File diff suppressed because one or more lines are too long

View file

@ -64,10 +64,10 @@ class Level extends \EmotionHero\Models\Level implements \Doctrine\ORM\Proxy\Pro
public function __sleep()
{
if ($this->__isInitialized__) {
return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'targets', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'games'];
return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'name', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'targets', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'games'];
}
return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'targets', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'games'];
return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'name', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'targets', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'games'];
}
/**
@ -173,4 +173,37 @@ class Level extends \EmotionHero\Models\Level implements \Doctrine\ORM\Proxy\Pro
}
/**
* {@inheritDoc}
*/
public function createTarget(\EmotionHero\Models\Emotion $emotion, float $score, float $time)
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'createTarget', [$emotion, $score, $time]);
return parent::createTarget($emotion, $score, $time);
}
/**
* {@inheritDoc}
*/
public function setId($id)
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'setId', [$id]);
return parent::setId($id);
}
/**
* {@inheritDoc}
*/
public function setName($name)
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'setName', [$name]);
return parent::setName($name);
}
}

View file

@ -64,10 +64,10 @@ class Target extends \EmotionHero\Models\Target implements \Doctrine\ORM\Proxy\P
public function __sleep()
{
if ($this->__isInitialized__) {
return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'level', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'time', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'emotion', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'hits'];
return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'position', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'level', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'time', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'emotion', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'score', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'hits'];
}
return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'level', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'time', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'emotion', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'hits'];
return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'position', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'level', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'time', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'emotion', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'score', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'hits'];
}
/**

View file

@ -265,4 +265,15 @@ class User extends \EmotionHero\Models\User implements \Doctrine\ORM\Proxy\Proxy
return parent::getId();
}
/**
* {@inheritDoc}
*/
public function getJwt()
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'getJwt', []);
return parent::getJwt();
}
}

View file

@ -6,6 +6,7 @@ use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\Common\Collections\ArrayCollection;
use EmotionHero\Application;
/**
* A Single play of a game: combines User, Level and time
@ -51,4 +52,74 @@ class Game
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* Gets the value of id.
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Gets the value of user.
*
* @return mixed
*/
public function getUser()
{
return $this->user;
}
/**
* Gets the value of level.
*
* @return mixed
*/
public function getLevel()
{
return $this->level;
}
/**
* Gets the value of hits.
*
* @return mixed
*/
public function getHits()
{
return $this->hits;
}
/**
* Gets the value of score.
*
* @return mixed
*/
public function getScore()
{
return $this->score;
}
/**
* Gets the value of createdAt.
*
* @return \DateTime $created
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* @todo Em should not be fetched here. Quick fix for now...
*
* @JMS\VirtualProperty
* @return Position
*/
public function getPosition() {
return Application::getInstance()->getEm()->getRepository(static::class)->getPositionForGame($this);
}
}

View file

@ -2,8 +2,46 @@
namespace EmotionHero\Models;
use Doctrine\ORM\EntityRepository;
use EmotionHero\Tools\Position;
class GameRepository extends EntityRepository
{
public function getPositionForGame(Game $game) {
$query = $this->_em->createQuery(
"SELECT COUNT(g.id) FROM ". Game::class ." g WHERE g.score < :score AND g.level = :level"
)
->setParameters([
'score' => $game->getScore(),
'level' => $game->getLevel(),
]);
$position = $query->getSingleScalarResult();
$total = $this->getGameCountForLevel($game->getLevel());
$highscore = $this->getHighscoreForLevel($game->getLevel());
return new Position($position, $total, $game->getScore(), $highscore);
}
public function getGameCountForLevel(Level $level) {
$query = $this->_em->createQuery(
"SELECT COUNT(g.id) FROM ".Game::class." g WHERE g.level = :level"
)
->setParameters([
'level' => $game->getLevel(),
]);
return $query->getSingleScalarResult();
}
/**
* @return float
*/
public function getHighscoreForLevel(Level $level) {
$query = $this->_em->createQuery(
"SELECT MAX(g.score) FROM ".Game::class." g WHERE g.level = :level"
)
->setParameters([
'level' => $game->getLevel(),
]);
return $query->getSingleScalarResult();
}
}

View file

@ -23,9 +23,15 @@ class Level
*/
private $id;
/**
* Name of the level
* @var string
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="Target", mappedBy="level")
* @ORM\OneToMany(targetEntity="Target", mappedBy="level", cascade={"persist"})
*/
private $targets;
@ -40,4 +46,38 @@ class Level
$this->games = new ArrayCollection();
}
public function createTarget(Emotion $emotion, float $score, float $time) {
$pos = $this->targets->count()+1;
$target = new Target($this, $pos, $emotion, $score, $time);
$this->targets->set($pos, $target);
}
/**
* Sets the value of id.
*
* @param integer $id the id
*
* @return self
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Sets the Name of the level.
*
* @param string $name the name
*
* @return self
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
}

View file

@ -9,7 +9,8 @@ use Doctrine\Common\Collections\ArrayCollection;
/**
* Users
*
* @ORM\Table(name="targets")
* @ORM\Table(name="targets",uniqueConstraints={
* })
* @ORM\Entity
*/
class Target
@ -23,9 +24,14 @@ class Target
*/
private $id;
/**
* Incremental position in level
* @var int
*/
private $position;
/**
* @ORM\ManyToOne(targetEntity="Level", inversedBy="targets")
* @ORM\ManyToOne(targetEntity="Level", inversedBy="targets", cascade={"persist"})
* @ORM\JoinColumn(name="level_id", referencedColumnName="id", nullable=false)
*/
private $level;
@ -41,15 +47,24 @@ class Target
*/
private $emotion;
/**
* Required score
* @var int
*/
private $score;
/**
* @ORM\OneToMany(targetEntity="Hit", mappedBy="target", fetch="EXTRA_LAZY")
*/
private $hits;
public function __construct()
public function __construct(Level $level, int $position, Emotion $emotion, int $score, float $time)
{
$this->hits = new ArrayCollection();
$this->level = $level;
$this->emotion = $emotion;
$this->score = $score;
$this->time = $time;
$this->position = $position;
}
}

130
src/Tools/Position.php Normal file
View file

@ -0,0 +1,130 @@
<?php
namespace EmotionHero\Tools;
use JMS\Serializer\Annotation as JMS;
/**
* The position of something in a greater whole
*/
class Position {
/** @var integer */
private $position;
/** @var integer */
private $total;
/** @var float */
private $score;
/** @var float */
private $highscore;
public function __construct($position, $total, $score, $highscore)
{
$this->position = $position;
$this->total = $total;
$this->score = $score;
$this->highscore = $highscore;
}
/**
* Gets the value of position.
*
* @return mixed
*/
public function getPosition()
{
return $this->position;
}
/**
* Sets the value of position.
*
* @param mixed $position the position
*
* @return self
*/
public function setPosition($position)
{
$this->position = $position;
return $this;
}
/**
* Gets the value of total.
*
* @return mixed
*/
public function getTotal()
{
return $this->total;
}
/**
* Sets the value of total.
*
* @param mixed $total the total
*
* @return self
*/
public function setTotal($total)
{
$this->total = $total;
return $this;
}
/**
* Gets the value of score.
*
* @return mixed
*/
public function getScore()
{
return $this->score;
}
/**
* Sets the value of score.
*
* @param mixed $score the score
*
* @return self
*/
public function setScore($score)
{
$this->score = $score;
return $this;
}
/**
* Gets the value of highscore.
*
* @return mixed
*/
public function getHighscore()
{
return $this->highscore;
}
/**
* Sets the value of highscore.
*
* @param mixed $highscore the highscore
*
* @return self
*/
public function setHighscore($highscore)
{
$this->highscore = $highscore;
return $this;
}
/**
* @JMS\VirtualProperty
* @return float
*/
public function getPercentage() {
return ($this->position / $this->total) * 100;
}
}