@@ -7,55 +7,28 @@ class DartsGame_Service_Session extends Zend_Session_Namespace implements DartsG
{
const SESSION_NAMESPACE = "darts-game";

protected $gamesTable;

/**
* Constructor.
*/
public function __construct($gamesTable)
public function __construct()
{
parent::__construct(self::SESSION_NAMESPACE);
$this->gamesTable = $gamesTable;
}

/**
* Returns the current game.
*
* @return DartsGame_Model_Game
* {@inheritdoc}
*/
public function getGame()
{
if (!$this->game->isConnected()) {
$this->game->setTable($this->gamesTable);
}

return $this->game;
}

/**
* Sets a new game in session.
*
* @param DartsGame_Model_Game $game
*/
public function setGame(DartsGame_Model_Game $game = null)
{
$this->game = $game;
}

/**
* {@inheritdoc}
*/
public function getLastPlayer()
{
return $this->lastPlayer;
}

/**
* {@inheritdoc}
*/
public function setLastPlayer(DartsGame_Model_Player $player = null)
public function setGame(DartsGame_Model_Game $game)
{
$this->lastPlayer = $player;
$this->game = $game;
}

/**
@@ -17,21 +17,7 @@ public function getGame();
*
* @param DartsGame_Model_Game $game
*/
public function setGame(DartsGame_Model_Game $game = null);

/**
* Returns the last player who played in the current game.
*
* @return DartsGame_Model_Player
*/
public function getLastPlayer();

/**
* Sets the last player who played in the current game.
*
* @param DartsGame_Model_Player $player
*/
public function setLastPlayer(DartsGame_Model_Player $player = null);
public function setGame(DartsGame_Model_Game $game);

/**
* Resets the current session state.

This file was deleted.

This file was deleted.

@@ -3,70 +3,81 @@
/**
* Represents a single dart game.
*/
class DartsGame_Model_Game extends Zend_Db_Table_Row_Abstract
class DartsGame_Model_Game
{
/**
* {@inheritdoc}
* @var array
*/
protected $_tableClass = 'DartsGame_Model_Table_Games';
private $turnsByPlayerID = array();

/**
* @var DartsGame_Model_Player[]
* @var int
*/
private $players;
private $currentTurnNumber = 0;

/**
* @var DartsGame_Model_Player
*/
private $currentPlayer;

/**
* {@inheritdoc}
* @param DartsGame_Model_Turn $turn
*/
public function init()
public function addTurn(DartsGame_Model_Turn $turn)
{
parent::init();
$this->date = Zend_Date::now();
if (!array_key_exists($turn->getPlayer()->getID(), $this->turnsByPlayerID)) {
$this->turnsByPlayerID[$turn->getPlayer()->getID()] = array();
}
$this->turnsByPlayerID[$turn->getPlayer()->getID()][] = $turn;
}

/**
* Returns the game ID.
* Returns the current turn number.
*
* @return int
*/
public function getID()
public function getCurrentTurnNumber()
{
return $this->currentTurnNumber;
}

/**
* Increments the current turn number.
*/
public function incrementCurrentTurnNumber()
{
return $this->id;
$this->currentTurnNumber++;
}

/**
* Returns the date and time when the game took place.
* Returns the player who is playing in the current game.
*
* @return Zend_Date
* @return DartsGame_Model_Player
*/
public function getDate()
public function getCurrentPlayer()
{
return $this->date;
return $this->currentPlayer;
}

/**
* Returns all players who played this game.
* Sets the player who is playing in the current game.
*
* @return DartsGame_Model_Player[]
* @param DartsGame_Model_Player $player
*/
public function getPlayers()
public function setCurrentPlayer(DartsGame_Model_Player $player)
{
return $this->findDependentRowset('DartsGame_Model_Table_Players')->toArray();
$this->currentPlayer = $player;
}

/**
* Returns all turns for the given player.
* Returns all the turns for a given player.
*
* @param DartsGame_Model_Player $player
* @return DartsGame_Model_Turn[]
*/
public function getTurnsForPlayer(DartsGame_Model_Player $player)
{
return $this->findDependentRowset(
'DartsGame_Model_Table_Turns',
null,
$this->select()->where('player_id = ?', $player->getID())
)->toArray();
// TODO: implement
return array();
}
}
@@ -40,18 +40,13 @@ public function getEmail()
return $this->email;
}

/**
* Returns all the turns for the given game.
*
* @param DartsGame_Model_Game $game
* @return DartsGame_Model_Turn[]
*/
public function getTurnsInGame(DartsGame_Model_Game $game)
public function incrementGamesWon()
{
$this->games_won++;
}

public function getGamesWon()
{
return $this->findDependentRowset(
'DartsGame_Model_Table_Turns',
null,
$this->select()->where('game_id = ?', $game->getID())
)->toArray();
return $this->games_won;
}
}

This file was deleted.

@@ -15,11 +15,6 @@ class DartsGame_Model_Table_Players extends Zend_Db_Table_Abstract
*/
protected $_rowClass = 'DartsGame_Model_Player';

/**
* @var array
*/
protected $_dependentTables = array('DartsGame_Model_Table_Turns');

/**
* Returns an array of players.
*

This file was deleted.

@@ -1,33 +1,38 @@
<?php

/**
* Represents a single dart game.
* Represents a single turn of a dart game, for a single player.
*/
class DartsGame_Model_Turn extends Zend_Db_Table_Row_Abstract
class DartsGame_Model_Turn
{
/**
* {@inheritdoc}
* @var DartsGame_Model_Player
*/
protected $_tableClass = 'DartsGame_Model_Table_Turn';
private $player;

/**
* Returns the turn ID.
*
* @return int
* @var int
*/
public function getID()
{
return $this->id;
}
private $number;

/**
* Returns the player related to this turn.
*
* @return DartsGame_Model_Player
* @var int[]
*/
public function getGame()
private $baseScores = array();

/**
* @var int[]
*/
private $multipliers = array();

/**
* @param DartsGame_Model_Player $player
* @param $number
*/
public function __construct(DartsGame_Model_Player $player, $number)
{
return $this->findParentRow('DartsGame_Model_Table_Games');
$this->player = $player;
$this->number = $number;
}

/**
@@ -37,7 +42,7 @@ public function getGame()
*/
public function getPlayer()
{
return $this->findParentRow('DartsGame_Model_Table_Player');
return $this->player;
}

/**
@@ -62,8 +67,8 @@ public function setScoreForShot($shot, $baseScore, $multiplier)
throw new InvalidArgumentException("Multiplier can only be 1, 2 or 3.");
}

$this->{"score_" . $shot} = $baseScore;
$this->{"multiplier_" . $shot} = $multiplier;
$this->baseScores[$shot - 1] = $baseScore;
$this->multipliers[$shot - 1] = $multiplier;
}

/**
@@ -88,9 +93,8 @@ private function validateShotNumber($shot)
* @param int $currentScore
* @return bool
*/
public function isBusted($currentScore)
public function isBust($currentScore)
{

$nextScore = $currentScore - $this->getTotalScore();
// If the next score would be negative, the player busted.
if ($nextScore < 0) {
@@ -1,7 +1,12 @@
<?php
/** @var DartsGame_Model_Game $game */
$game = $this->game;
?>
<div id="turn-header">
<h2><?= $this->escape($this->currentPlayerFullName) ?> - Round # <?= $this->escape($this->currentTurnNumber) ?></h2>
<h2><?= $this->escape($game->getCurrentPlayer()->getFullName()) ?> - Round
# <?= $this->escape($game->getCurrentTurnNumber()) ?></h2>

<h1>Points: <?= $this->escape($this->currentPlayerScore) ?></h1>
<h1>Points: <?= $this->escape($this->scoreBoard->getScoreForPlayer($game->getCurrentPlayer())) ?></h1>
</div>

<form id="<?= $this->form->getName() ?>" action="#" method="POST">
@@ -39,6 +44,7 @@
<th>Player</th>
<th>Score</th>
</tr>
<?php /** @var DartsGame_Model_Player $player */ ?>
<?php foreach ($this->players as $player) { ?>
<tr>
<td><?= $this->escape($player->getFullName()) ?></td>
@@ -52,5 +58,3 @@
onclick="document.getElementById('turn').submit(); return false;"
href="#">next</a>
</div>


@@ -7,17 +7,19 @@ $this->headTitle($this->title);
<table class="list">
<tr>
<th>Full name</th>
<th>E-Mail</th>
<th>Games won</th>
</tr>
<?php foreach ($this->players as $player) { ?>
<tr>
<?php /** @var $player DartsGame_Model_Player */ ?>
<td><?= $this->escape($player->getFullName()); ?></td>
<td><?= $this->escape($player->getEmail()); ?></td>
<td><?= $this->escape($player->getGamesWon()); ?></td>
</tr>
<?php } ?>
</table>

<div class="navigation-container">
<a id="start-new-game" class="button" href="<?= $this->url(array('controller' => 'game', 'action' => 'start')) ?>">Start
new game</a>
<a id="start-new-game" class="button" href="<?= $this->url(array('controller' => 'game', 'action' => 'start')) ?>">
Start new game
</a>
</div>