Player.php 1.52 KB
<?php

namespace FootyRoom\Core\Predictor;

use FootyRoom\Core\CoreException;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
 * @ODM\Document(collection="predictor.players")
 */
class Player
{
    /** @var int Amount of points new player gets. */
    public const STARTING_POINTS = 1000;

    /**
     * @ODM\Id
     *
     * @var string
     */
    protected $id;

    /**
     * @ODM\Field(type="int")
     *
     * @var int
     */
    protected $userId;

    /**
     * @ODM\Field(type="string")
     *
     * @var string
     */
    protected $username;

    /**
     * @ODM\Field(type="int", strategy="increment")
     *
     * @var int
     */
    protected $points;

    public function __construct(int $userId, string $username)
    {
        $this->userId = $userId;
        $this->username = $username;
        $this->points = self::STARTING_POINTS;
    }

    public function getUserId(): int
    {
        return $this->userId;
    }

    public function getUsername(): string
    {
        return $this->username;
    }

    public function getPoints(): int
    {
        return $this->points;
    }

    public function bet(int $stake = 0)
    {
        if ($stake > $this->points) {
            throw new CoreException('You don\'t have enough points to make this bet.');
        }

        $this->points -= $stake;
    }

    public function awardPoints(int $points)
    {
        if ($points < 0) {
            throw new CoreException('You can\'t award less than 0 points.');
        }

        $this->points += $points;
    }
}