Skip to content

Commit

Permalink
Merge pull request #56 from floptheworld/dev
Browse files Browse the repository at this point in the history
Merging Dev into Master
  • Loading branch information
zsarnett authored Jul 2, 2019
2 parents 19b869c + cc6eca9 commit 14ecf2d
Show file tree
Hide file tree
Showing 207 changed files with 404 additions and 77 deletions.
54 changes: 37 additions & 17 deletions src/common/game/game-play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ import { solveHands } from "./solve-hands";
import { updatePots } from "./update-pots";

export class GamePlay implements GamePlayType {
public gameID: string;
public cardBack: string;
public bigBlind: number;
public littleBlind: number;
public players: PlayerType[] = [];
public board: string[] = [];
public round: number = 0;
public pot: number = 0;
public currentBet: number = 0;
public currentPot: number = 0;
public currentPlayerID: string = "";
public winDesc: string = "";
public pots: number[] = [];
public deck: string[] = [];
public isStarted: boolean = false;
public currentPlayerID: string = "";
public winDesc: string = "";
public cardBack: string;
public gameID: string;
public currentBet: number = 0;
public currentPot: number = 0;
public round: number = 0;
public pot: number = 0;
public littleBlind: number;
public bigBlind: number;
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 @@ -104,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 All @@ -129,7 +139,6 @@ export class GamePlay implements GamePlayType {
}

player.status = action;

this.nextTurn();
}

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 All @@ -189,6 +204,7 @@ export class GamePlay implements GamePlayType {
this.pot = 0;
this.winDesc = "";
this.isGameOver = false;
this.isOpen = false;
}

private clearPlayers(active: boolean = false): void {
Expand Down Expand Up @@ -297,4 +313,8 @@ export class GamePlay implements GamePlayType {
)
.map((player) => (player.isSittingOut = false));
}

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

Expand All @@ -43,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 All @@ -53,6 +57,13 @@ export class Game extends GamePlay {
this.players.push(player);
}

public removePlayer(removePlayer: PlayerType): void {
this.players.splice(
this.players.findIndex((player) => player === removePlayer),
1
);
}

public findPlayerByID(userID: string): PlayerType | undefined {
return this.players.find((player) => player.playerID === userID);
}
Expand All @@ -66,4 +77,104 @@ export class Game extends GamePlay {

this.actionPlayed(player, action, dataNum);
}

public OpenGame(callback: () => void): void {
this.updateRound();
callback();

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

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

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
);
}
}
8 changes: 8 additions & 0 deletions src/common/game/next-turn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ export function nextTurn(game: GamePlayType): void {

game.players[game.playerTurnIndex].isTurn = false;

// all players in the pot are all-in, or one player is playing alone against opponents who are all all-in
if (
game.activePlayers.filter((player) => player.stackAmount > 0).length <= 1
) {
game.isOpen = true;
return;
}

// While next player Doesnt Exist, isn't Active, has no stack or is sitting out
while (
!game.players[nextPlayerIndex] ||
Expand Down
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;
}
2 changes: 1 addition & 1 deletion src/common/game/update-pots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function updatePots(game: GamePlayType) {
game.pots.push(0);
}

game.pots[index] += roundToPrecision(
game.pots[index] = roundToPrecision(
roundToPrecision(minInvested, 0.01) * investedPlayers.length,
0.01
);
Expand Down
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
4 changes: 4 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 @@ -42,6 +44,8 @@ export interface GameState {
pots: number[];
isStarted: boolean;
isGameOver: boolean;
isOpen: boolean;
timer?: number;
readonly sittingInPlayers: PlayerType[];
}

Expand Down
Loading

0 comments on commit 14ecf2d

Please sign in to comment.