@@ -15,22 +15,14 @@ class DartsGame_Model_Game extends Zend_Db_Table_Row_Abstract
*/
private $players;


/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
$this->date = new Zend_Date(new \DateTime());
}

/**
* TODO
* @return array
*/
public function __sleep()
{
return array('id');
$this->date = Zend_Date::now();
}

/**
@@ -46,7 +38,7 @@ public function getID()
/**
* Returns the date and time when the game took place.
*
* @return DateTime
* @return Zend_Date
*/
public function getDate()
{
@@ -34,4 +34,7 @@ public function fetchAllAsArray()
return $result;
}

public function findByID($id){
return $this->find($id)->current();
}
}
@@ -34,13 +34,20 @@ class DartsGame_Model_Table_Turns extends Zend_Db_Table_Abstract
/**
* Factory method to create a new turn.
*
* @param DartsGame_Model_Game $game
* @param DartsGame_Model_Player $player
* @param $number
* @param int $number
* @return DartsGame_Model_Turn
*/
public function create(DartsGame_Model_Player $player, $number)
public function create(DartsGame_Model_Game $game, DartsGame_Model_Player $player, $number)
{
return new DartsGame_Model_Turn($player, $number);
return $this->createRow(
array(
'game_id' => $game->getID(),
'player_id' => $player->getID(),
'number' => $number
)
);
}

/**
@@ -51,9 +58,40 @@ public function create(DartsGame_Model_Player $player, $number)
*/
public function findCurrentTurnNumber(DartsGame_Model_Game $game)
{
return (int) $this->_db->query(
$this->select('max(number)')
->where('game_id = ?'), array($game->getID()))
->fetchColumn(0);
$select = new Zend_Db_Table_Select($this);
$select->from($this->_name, array(
'id',
new Zend_Db_Expr('MAX(number) as max_turn')))
->where('game_id = ?', $game->getID());
$playedTurns = $this->_db->query($select)->fetchAll();
if (count($playedTurns) == 0) {
return 1;
}
$turn = $playedTurns[0]['max_turn'];
if (count($game->getPlayers()) == count($playedTurns) || $turn == 0) {
$turn++;
}
return $turn;
}

/**
* Finds all the turns that a player has played in a given game.
*
* @param DartsGame_Model_Player $player
* @param DartsGame_Model_Game $game
* @return DartsGame_Model_Turn[]
*/
public function findTurnsForPlayer(DartsGame_Model_Player $player, DartsGame_Model_Game $game)
{
$result = array();
$turns = $this->fetchAll(
$this->select()
->where('game_id = ?', $game->getID())
->where('player_id = ?', $player->getID())
);
foreach ($turns as $turn) {
$result[] = $turn;
}
return $result;
}
}
@@ -10,17 +10,7 @@ class DartsGame_Model_Turn extends Zend_Db_Table_Row_Abstract
*/
protected $_tableClass = 'DartsGame_Model_Table_Turn';

/**
* @param DartsGame_Model_Player $player
* @param int $number
*/
public function __construct(DartsGame_Model_Player $player, $number)
{
$this->player_id = $player->getID();
$this->number = $number;
}

/**
/**
* Returns the turn ID.
*
* @return int
@@ -129,4 +119,37 @@ public function setScoreForShot($shot, $baseScore, $multiplier)
$this->{"score_" . $shot} = $baseScore;
$this->{"multiplier_" . $shot} = $multiplier;
}

/**
* Returns true if the player busted the current turn.
* A turn is considered busted if either:
* - subtracting the current turn value would make the player's score negative, or
* - the player would reach zero with this turn, but he did not score a double multiplier with its last shot,
*
* @param int $currentScore
* @return bool
*/
public function isBusted($currentScore) {

$nextScore = $currentScore - $this->getTotalScore();
// If the next score would be negative, the player busted.
if ($nextScore < 0) {
return true;
}
// If the next score would be zero, then we need to find the last shot
else if ($nextScore == 0) {
$lastShot = -1;
// Search the last non-zero shot.
for ($shotNumber = 0; $shotNumber < 3; $shotNumber++) {
if ($this->getBaseScoreForShot($shotNumber) > 0) {
$lastShot = $shotNumber;
}
}
// If the last non-zero shot does not have a double multiplier, than the player busted.
if ($this->getMultiplierForShot($lastShot) != 2 || $this->getBaseScoreForShot($lastShot) == 50) {
return true;
}
}
return false;
}
}
@@ -1,5 +1,5 @@
<div id="turn-header">
<h2><?= $this->escape($this->currentPlayerFullName) ?> - Round # <?= $this->escape($this->currentTurnNumber + 1) ?></h2>
<h2><?= $this->escape($this->currentPlayerFullName) ?> - Round # <?= $this->escape($this->currentTurnNumber) ?></h2>
<h1>Points: <?= $this->escape($this->currentPlayerScore) ?></h1>
</div>