Skip to content

Commit

Permalink
Merge pull request #55 from zsarnett/showdown-func
Browse files Browse the repository at this point in the history
Adds call clock Functionality and showdown
  • Loading branch information
zsarnett authored Jul 2, 2019
2 parents 5833ed2 + 11d8694 commit cc6eca9
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 53 deletions.
29 changes: 24 additions & 5 deletions src/common/game/game-play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class GamePlay implements GamePlayType {
public isGameOver: boolean = false;
public isStarted: boolean = false;
public isOpen: boolean = false;
public timer?: number;

get dealerIndex(): number {
return this.players.findIndex((player) => player.dealer === true);
Expand Down Expand Up @@ -105,16 +106,24 @@ export class GamePlay implements GamePlayType {
case "check":
break;
case "call":
player.subtractBet(this.currentBet);
this.currentPot += this.currentBet - (parseFloat(player.bet) || 0);
player.bet = this.currentBet.toFixed(2);
const bet =
this.currentBet > player.stackAmount
? player.stackAmount
: this.currentBet;
player.subtractBet(bet);
this.currentPot += bet - (parseFloat(player.bet) || 0);
player.bet = bet.toFixed(2);
break;
case "bet":
case "raise":
this.currentBet = data;
player.subtractBet(this.currentBet);

this.currentPot += this.currentBet - (parseFloat(player.bet) || 0);
player.bet = data.toFixed(2);

this.clearLastAggresor();
player.isLastAggressor = true;
break;
case "rebuy":
player.stackAmount += data || 0;
Expand Down Expand Up @@ -158,13 +167,19 @@ export class GamePlay implements GamePlayType {
this.board.push(this.deck!.pop()!);
break;
default:
this.solveHands();
return;
this.isGameOver = true;
break;
}

updatePots(this);
}

public updatePlayerStacks(): void {
this.activePlayers.map(
(activePlayer, i) => (activePlayer.stackAmount += activePlayer.result)
);
}

private nextTurn(): void {
if (this.activePlayers.length === 1) {
this.activePlayers.map((player) => {
Expand Down Expand Up @@ -298,4 +313,8 @@ export class GamePlay implements GamePlayType {
)
.map((player) => (player.isSittingOut = false));
}

private clearLastAggresor(): void {
this.players.map((player) => (player.isLastAggressor = false));
}
}
97 changes: 92 additions & 5 deletions src/common/game/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ export class Game extends GamePlay {
return {
board: this.board,
gameID: this.gameID,
players:
this.isGameOver || this.isOpen
? this.players
: this.getGameStatePlayers(currentPlayerID),
players: this.isOpen
? this.players
: this.getGameStatePlayers(currentPlayerID),
pot: this.pot,
round: this.round,
bigBlind: this.bigBlind,
Expand All @@ -33,6 +32,7 @@ export class Game extends GamePlay {
isStarted: this.isStarted,
isOpen: this.isOpen,
sittingInPlayers: this.sittingInPlayers,
timer: this.timer,
};
}

Expand All @@ -45,7 +45,9 @@ export class Game extends GamePlay {
player.playerID !== currentPlayerID && player.cards.length > 0
)
.map((player) => {
player.cards = [this.cardBack, this.cardBack];
player.cards = player.showCards
? player.cards
: [this.cardBack, this.cardBack];
});

return copyPlayers;
Expand Down Expand Up @@ -81,6 +83,9 @@ export class Game extends GamePlay {
callback();

if (this.isGameOver) {
this.solveHands();
this.updatePlayerStacks();

setTimeout(() => {
this.start();
callback();
Expand All @@ -90,4 +95,86 @@ export class Game extends GamePlay {

setTimeout(() => this.OpenGame(callback), 3000);
}

public showdown(callback: () => void): void {
this.solveHands();

const winnerFound: boolean = false;
const lastAggressorIndex = this.activePlayers.findIndex(
(player) => player.isLastAggressor === true
);
const resultTemp: number[] = [];
this.activePlayers.map((activePlayer) =>
resultTemp.push(activePlayer.result)
);

setTimeout(
() =>
this.showdownHelper(lastAggressorIndex, winnerFound, resultTemp, () =>
callback()
),
1500
);
}

private showdownHelper(
lastAggressorIndex: number,
winnerFound: boolean,
resultTemp: number[],
callback: () => void
): void {
if (
this.activePlayers.filter((activePlayer) => activePlayer.result > 0)
.length === 0
) {
this.activePlayers.map(
(activePlayer, i) => (activePlayer.result = resultTemp[i])
);

this.updatePlayerStacks();
callback();

setTimeout(() => {
this.start();
callback();
}, 5000);

return;
}

const player = this.activePlayers[lastAggressorIndex];

if (!player) {
lastAggressorIndex = 0;

this.showdownHelper(lastAggressorIndex, winnerFound, resultTemp, () =>
callback()
);
return;
}

lastAggressorIndex++;

if (winnerFound && player.result === 0) {
this.showdownHelper(lastAggressorIndex, winnerFound, resultTemp, () =>
callback()
);
return;
}

if (player.result > 0) {
winnerFound = true;
player.result = 0;
}

player.showCards = true;
callback();
setTimeout(
() =>
this.showdownHelper(lastAggressorIndex, winnerFound, resultTemp, () =>
callback()
),
1500
);
}
}
6 changes: 0 additions & 6 deletions src/common/game/solve-hands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,4 @@ export function solveHands(game: GamePlayType): void {
winners = [];
boardWinner = true;
}

game.players
.filter((player) => player.result > 0)
.map((player) => (player.stackAmount += player.result));

game.isGameOver = true;
}
4 changes: 4 additions & 0 deletions src/common/player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export class Player implements PlayerType {
public result: number = 0;
public pendingSitOut: boolean = false;
public isSittingOut: boolean = false;
public isLastAggressor: boolean = false;
public showCards: boolean = false;

get isActive(): boolean {
return this.cards.length === 2 && this.status !== "fold";
Expand Down Expand Up @@ -62,6 +64,8 @@ export class Player implements PlayerType {
this.isTurn = false;
this.isBigBlind = false;
this.isLittleBlind = false;
this.isLastAggressor = false;
this.showCards = false;
this.invested = 0;
this.result = 0;
}
Expand Down
3 changes: 3 additions & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface PlayerState {
solvedHand?: Hand;
isSittingOut: boolean;
pendingSitOut: boolean;
isLastAggressor: boolean;
showCards: boolean;
readonly isActive: boolean;
readonly isCheck: boolean;
readonly numBet: number;
Expand Down Expand Up @@ -43,6 +45,7 @@ export interface GameState {
isStarted: boolean;
isGameOver: boolean;
isOpen: boolean;
timer?: number;
readonly sittingInPlayers: PlayerType[];
}

Expand Down
96 changes: 64 additions & 32 deletions src/components/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {
import { classMap } from "lit-html/directives/class-map";
import { GameState, PlayerState } from "../common/types";
import { roundToPrecision } from "../common/round-to-precision";
import { sendPlayerAction, startGame, leaveGame } from "../data/connection";
import {
sendPlayerAction,
startGame,
leaveGame,
callClock,
} from "../data/connection";

interface BetTarget extends EventTarget {
multiplier: number;
Expand Down Expand Up @@ -45,8 +50,8 @@ export class Action extends LitElement {

if (this.player.bet === "" && this.player.isTurn && !this.game.isGameOver) {
this.player.bet = (this.game.currentBet !== 0
? this.game.currentBet
: this.game.bigBlind
? this.game.currentBet + this.game.bigBlind
: this.game.bigBlind + this.game.littleBlind
).toFixed(2);
}

Expand Down Expand Up @@ -124,35 +129,38 @@ export class Action extends LitElement {
type="number"
/>
</div>
<div class="bet-actions">
<button
.multiplier=${0.25}
@click=${this._setBet}
class="button-small dark-blue-button"
>
1/4
</button>
<button
.multiplier=${0.5}
@click=${this._setBet}
class="button-small dark-blue-button"
>
1/2
</button>
<button
.multiplier=${0.75}
@click=${this._setBet}
class="button-small dark-blue-button"
>
3/4
</button>
<button
.multiplier=${1}
@click=${this._setBet}
class="button-small dark-blue-button"
>
Full
</button>
<div class="bottom-main-box">
<div class="bet-actions">
<button
.multiplier=${0.25}
@click=${this._setBet}
class="button-small dark-blue-button"
>
1/4
</button>
<button
.multiplier=${0.5}
@click=${this._setBet}
class="button-small dark-blue-button"
>
1/2
</button>
<button
.multiplier=${0.75}
@click=${this._setBet}
class="button-small dark-blue-button"
>
3/4
</button>
<button
.multiplier=${1}
@click=${this._setBet}
class="button-small dark-blue-button"
>
Full
</button>
</div>
<div class="timer">${this.game.timer || ""}</div>
</div>
</div>
<div class="action-box sm-box">
Expand Down Expand Up @@ -215,6 +223,17 @@ export class Action extends LitElement {
</button>
</div>
`}
${this.game.timer !== undefined ||
!this.game.isStarted ||
this.player.isSittingOut ||
this.game.isGameOver ||
this.game.isOpen
? ""
: html`
<button @click=${this._callClock} class="button dark-blue-button">
Call Clock
</button>
`}
</div>
`;
}
Expand Down Expand Up @@ -317,6 +336,15 @@ export class Action extends LitElement {
.rebuy-button {
font-size: 12px;
}
.bottom-main-box {
display: flex;
justify-content: space-between;
align-items: center;
}
.timer {
color: white;
padding-right: 4px;
}
`;
}

Expand Down Expand Up @@ -373,4 +401,8 @@ export class Action extends LitElement {
private _leaveGame(): void {
leaveGame(this.socket, this.game.gameID);
}

private _callClock(): void {
callClock(this.socket, this.game.gameID);
}
}
4 changes: 4 additions & 0 deletions src/data/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ export function leaveGame(socket: SocketIOClient.Socket, gameID: string) {
localStorage.clear();
document.location.reload();
}

export function callClock(socket: SocketIOClient.Socket, gameID: string) {
socket.emit("callClock", gameID);
}
Loading

0 comments on commit cc6eca9

Please sign in to comment.