try game hints and minor fixes
This commit is contained in:
parent
60db54a00f
commit
4bfc649dec
6 changed files with 182 additions and 11 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,7 +1,7 @@
|
|||
vendor
|
||||
composer.lock
|
||||
cache/db.sqlite
|
||||
cache/proxies/__CG__*
|
||||
cache/proxy/__CG__*
|
||||
config/*
|
||||
!config/_default.php
|
||||
.phpintel
|
||||
|
|
|
@ -60,7 +60,6 @@ class ScoreControllerProvider implements ControllerProviderInterface
|
|||
* Expects a full game + hits in the request
|
||||
*/
|
||||
$controllers->post('/games', function (Request $request, Application $app) {
|
||||
error_reporting(E_ALL);ini_set('display_errors',1);
|
||||
|
||||
$data = json_decode($request->getContent(), true);
|
||||
$token = $app['security.token_storage']->getToken();
|
||||
|
|
|
@ -134,15 +134,7 @@ class Game
|
|||
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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the value of user.
|
||||
|
@ -185,4 +177,46 @@ class Game
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @todo Em should not be fetched here. Quick fix for now...
|
||||
*
|
||||
* @JMS\VirtualProperty
|
||||
* @return Position
|
||||
*/
|
||||
public function getHint() {
|
||||
$lowest_scoring_hit = null;
|
||||
foreach($this->hits as $hit) {
|
||||
if(!$lowest_scoring_hit || $lowest_scoring_hit->getScore() > $hit->getScore()) {
|
||||
$lowest_scoring_hit = $hit;
|
||||
}
|
||||
}
|
||||
|
||||
if(!$lowest_scoring_hit)
|
||||
return "";
|
||||
|
||||
$target = $lowest_scoring_hit->getTarget();
|
||||
$emotion = $target->getEmotion();
|
||||
|
||||
$text = "When you had to feel "
|
||||
. $target->getScore() ."% " . $emotion->getName()
|
||||
." you showed " . $hit->getEmotions()->getEmotionScore($emotion) ."%. To show you empathy, ...";
|
||||
return $text;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ use Doctrine\Common\Collections\ArrayCollection;
|
|||
*
|
||||
* @ORM\Table(name="hits")
|
||||
* @ORM\Entity
|
||||
* @ORM\Entity(repositoryClass="EmotionHero\Models\HitRepository")
|
||||
*/
|
||||
class Hit
|
||||
{
|
||||
|
@ -388,6 +389,26 @@ class Emotions{
|
|||
return $this->surprise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the score of given emotion
|
||||
* @param string|Emotion $name name of the emotion
|
||||
* @return float The score
|
||||
*/
|
||||
public function getEmotionScore($emotion) {
|
||||
if(!is_string($emotion)) {
|
||||
if(is_a($emotion, Emotion::class)) {
|
||||
$emotion = $emotion->getName();
|
||||
} else {
|
||||
throw new RuntimeException("Invalid emotion", 1);
|
||||
}
|
||||
}
|
||||
$emotions = static::$EMOTIONS;
|
||||
if(in_array($emotion, $emotions)) {
|
||||
return $this->$emotion;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
public function setEmotion(string $emotion, float $value) {
|
||||
$emotions = static::$EMOTIONS;
|
||||
|
|
47
src/Models/HitRepository.php
Normal file
47
src/Models/HitRepository.php
Normal file
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
namespace EmotionHero\Models;
|
||||
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use EmotionHero\Tools\Position;
|
||||
|
||||
class HitRepository 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->getCountForLevel($game->getLevel());
|
||||
$highscore = $this->getHighscoreForLevel($game->getLevel());
|
||||
|
||||
return new Position($position, $total, $game->getScore(), $highscore);
|
||||
}
|
||||
|
||||
public function getCountForLevel(Level $level) {
|
||||
|
||||
$query = $this->_em->createQuery(
|
||||
"SELECT COUNT(g.id) FROM ".Game::class." g WHERE g.level = :level"
|
||||
)
|
||||
->setParameters([
|
||||
'level' => $level,
|
||||
]);
|
||||
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' => $level,
|
||||
]);
|
||||
return $query->getSingleScalarResult();
|
||||
}
|
||||
}
|
|
@ -77,4 +77,74 @@ class Target
|
|||
public function addHit(Hit $hit){
|
||||
$this->hits->add($hit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of id.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Incremental position in level.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getPosition()
|
||||
{
|
||||
return $this->position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of level.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLevel()
|
||||
{
|
||||
return $this->level;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of time.
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function getTime()
|
||||
{
|
||||
return $this->time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of emotion.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getEmotion()
|
||||
{
|
||||
return $this->emotion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Required score.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getScore()
|
||||
{
|
||||
return $this->score;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of hits.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getHits()
|
||||
{
|
||||
return $this->hits;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue