BettingManager::bankerWinsStake()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
ccs 5
cts 5
cp 1
crap 1
1
<?php
2
3
namespace App\Game;
4
5
use ValueError;
6
7
class BettingManager
8
{
9
    /**
10
     * @var int  $playerCoins Player's remaining coins.
11
     * @var int  $bankerCoins Banker's remaining coins.
12
     * @var int  $stake       Coins currently at stake.
13
     * @var int  $step        Minimum bet step.
14
     * @var bool $betting     As true if betting in on, otherwise false.
15
     * @var bool $hasBet      As true if there is an active bet, otherwise false.
16
     * @var bool $gameOver    As true if either the player or banker are out of coins, otherwise false.
17
     */
18
    private int $playerCoins = 0;
19
    private int $bankerCoins = 0;
20
    private int $stake = 0;
21
    private int $step = 0;
22
    private bool $betting = false;
23
    private bool $hasBet = false;
24
    private bool $gameOver = false;
25
26
    /**
27
     * Create new betting manager object.
28
     *
29
     * @param int $max  Coins go give player and banker to start with.
30
     * @param int $step Minimum step when betting.
31
     */
32 17
    public function __construct(int $max = 100, int $step = 5)
33
    {
34 17
        if ($step > $max) {
35 1
            throw new ValueError("Step can not be bigger than max bet");
36
        }
37
38 16
        if ($step < 1) {
39 1
            throw new ValueError("Minimum step is 1");
40
        }
41
42 15
        if ($max % $step !== 0) {
43 1
            throw new ValueError("Max value must be divisible by step");
44
        }
45
46 14
        $this->playerCoins = $this->bankerCoins = $max;
47 14
        $this->step = $step;
48
    }
49
50
    /** @param bool $value As true if betting is on, otherwise false. */
51 1
    public function setBetting(bool $value): void
52
    {
53 1
        $this->betting = $value;
54
    }
55
56
    /** @return bool As true if betting is on, otherwise false. */
57 2
    public function isBetting(): bool
58
    {
59 2
        return $this->betting;
60
    }
61
62
    /**
63
     * Player places a bet and banker tries to match it.
64
     *
65
     * @param int $bet Amount of coins to bet. This becomes the stake.
66
     *
67
     * @return bool As true if bet was successfully made, otherwise false.
68
     */
69 10
    public function placeBet(int $bet): bool
70
    {
71 10
        if (($bet > $this->playerCoins) or ($bet % $this->step !== 0)) {
72 3
            $this->hasBet = false;
73
74 3
            return $this->hasBet;
75
        }
76
77 7
        $this->hasBet = true;
78
79 7
        $this->playerCoins -= $bet;
80 7
        $this->stake += $bet;
81
82 7
        if ($this->bankerCoins - $bet < 0) {
83 1
            $this->stake += $this->bankerCoins;
84 1
            $this->bankerCoins = 0;
85
86 1
            return $this->hasBet;
87
        }
88
89 7
        $this->stake += $bet;
90 7
        $this->bankerCoins -= $bet;
91
92 7
        return $this->hasBet;
93
    }
94
95
    /** Give player the stake */
96 3
    public function playerWinsStake(): void
97
    {
98 3
        $this->playerCoins += $this->stake;
99 3
        $this->stake = 0;
100 3
        $this->hasBet = false;
101 3
        $this->gameOver = $this->bankerCoins <= 0;
102
    }
103
104
    /** Give banker the stake */
105 2
    public function bankerWinsStake(): void
106
    {
107 2
        $this->bankerCoins += $this->stake;
108 2
        $this->stake = 0;
109 2
        $this->hasBet = false;
110 2
        $this->gameOver = $this->playerCoins <= 0;
111
    }
112
113
    /**
114
     * Create and array of values to use as tick labels for range input.
115
     *
116
     * @return int[] As values for the different betting steps.
117
     */
118 12
    private function getInputRangeTicks(): array
119
    {
120 12
        return range($this->step, $this->playerCoins, $this->step);
121
    }
122
123
    /** @return mixed[] $sate As the current betting state. */
124 12
    public function getState(): array
125
    {
126 12
        $state = [
127 12
            'playerCoins' => $this->playerCoins,
128 12
            'bankerCoins' => $this->bankerCoins,
129 12
            'stake'       => $this->stake,
130 12
            'step'        => $this->step,
131 12
            'hasBet'      => $this->hasBet,
132 12
            'ticks'       => $this->getInputRangeTicks(),
133 12
            'betting'     => $this->betting,
134 12
            'gameOver'    => $this->gameOver,
135 12
        ];
136
137 12
        return $state;
138
    }
139
}
140