1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Smr\Pages\Player\Bar; |
4
|
|
|
|
5
|
|
|
use AbstractSmrPlayer; |
6
|
|
|
use Smr\Blackjack\Result; |
7
|
|
|
use Smr\Blackjack\Table; |
8
|
|
|
use Smr\Page\PlayerPageProcessor; |
9
|
|
|
use Smr\Request; |
10
|
|
|
|
11
|
|
|
class PlayBlackjackProcessor extends PlayerPageProcessor { |
12
|
|
|
|
13
|
|
|
public function __construct( |
14
|
|
|
private readonly int $locationID, |
15
|
|
|
private readonly string $action, |
16
|
|
|
private readonly ?Table $table = null, |
17
|
|
|
private readonly ?int $bet = null |
18
|
|
|
) {} |
19
|
|
|
|
20
|
|
|
public function build(AbstractSmrPlayer $player): never { |
21
|
|
|
$table = $this->table ?? new Table(); |
22
|
|
|
$bet = $this->bet ?? Request::getInt('bet'); |
23
|
|
|
$do = $this->action; |
24
|
|
|
|
25
|
|
|
if ($do == 'new game') { |
26
|
|
|
if ($player->getCredits() < $bet) { |
27
|
|
|
create_error('Not even enough to play BlackJack...you need to trade!'); |
28
|
|
|
} |
29
|
|
|
if ($bet == 0) { |
30
|
|
|
create_error('We don\'t want you here if you don\'t want to play with cash!'); |
31
|
|
|
} |
32
|
|
|
if ($bet > 100 && $player->getNewbieTurns() > 0) { |
33
|
|
|
create_error('Sorry. According to Galactic Laws we can only play with up to 100 credits while under newbie protection.'); |
34
|
|
|
} |
35
|
|
|
if ($bet > 10000) { |
36
|
|
|
create_error('Sorry. According to Galactic Laws we can only play with up to 10,000 credits'); |
37
|
|
|
} |
38
|
|
|
if ($bet < 0) { |
39
|
|
|
create_error('Yeah...we are gonna give you money to play us! GREAT IDEA!!'); |
40
|
|
|
} |
41
|
|
|
$player->decreaseCredits($bet); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Add cards to the player's hand |
45
|
|
|
if ($do == 'HIT') { |
46
|
|
|
$table->playerHits(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Check if the game has ended |
50
|
|
|
$gameEnded = ($do == 'STAY' || $table->gameOver()); |
51
|
|
|
|
52
|
|
|
$winningsMsg = ''; |
53
|
|
|
if ($gameEnded) { |
54
|
|
|
// Add cards to the dealer's hand (if necessary) |
55
|
|
|
$table->dealerHitsUntil(17); |
56
|
|
|
|
57
|
|
|
// Process winnings and HoF stats |
58
|
|
|
$result = $table->getPlayerResult(); |
59
|
|
|
if ($result == Result::Win || $result == Result::Blackjack) { |
60
|
|
|
$multiplier = $result == Result::Blackjack ? 2.5 : 2; |
61
|
|
|
$winnings = IFloor($bet * $multiplier); |
|
|
|
|
62
|
|
|
$player->increaseCredits($winnings); |
63
|
|
|
$stat = $winnings - $bet; |
64
|
|
|
$player->increaseHOF($stat, ['Blackjack', 'Money', 'Won'], HOF_PUBLIC); |
65
|
|
|
$player->increaseHOF(1, ['Blackjack', 'Results', 'Won'], HOF_PUBLIC); |
66
|
|
|
$winningsMsg = 'You have won $' . number_format($winnings) . ' credits!'; |
67
|
|
|
} elseif ($result == Result::Tie) { |
68
|
|
|
$player->increaseCredits($bet); |
69
|
|
|
$player->increaseHOF(1, ['Blackjack', 'Results', 'Draw'], HOF_PUBLIC); |
70
|
|
|
$winningsMsg = 'You have won back your $' . number_format($bet) . ' credits.'; |
71
|
|
|
} else { |
72
|
|
|
$player->increaseHOF($bet, ['Blackjack', 'Money', 'Lost'], HOF_PUBLIC); |
73
|
|
|
$player->increaseHOF(1, ['Blackjack', 'Results', 'Lost'], HOF_PUBLIC); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$container = new PlayBlackjack( |
78
|
|
|
locationID: $this->locationID, |
79
|
|
|
table: $table, |
80
|
|
|
gameEnded: $gameEnded, |
81
|
|
|
bet: $bet, |
|
|
|
|
82
|
|
|
winningsMsg: $winningsMsg |
83
|
|
|
); |
84
|
|
|
$container->go(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|