Skip to content

Commit

Permalink
Changed the system logs shown on message bar
Browse files Browse the repository at this point in the history
  • Loading branch information
predatorray committed Dec 11, 2024
1 parent c39002f commit 44a479d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 80 deletions.
57 changes: 29 additions & 28 deletions src/components/MessageBar.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {EventLog, EventLogs} from "../lib/texas-holdem/useEventLogs";
import {Message, Messages} from "../lib/useChatRoom";
import React, {useCallback, useEffect, useMemo, useRef, useState} from "react";
import {convertToUnicode} from "../lib/rules";
import PlayerAvatar from "./PlayerAvatar";
import {rankDescription} from "phe";

function EventOrMessage(props: {
myPlayerId: string;
Expand All @@ -24,33 +24,6 @@ function EventOrMessage(props: {
<div className="message-text">{em.text}</div>
</div>
);
case 'board':
return (
<div className="message system-notification">
<b>Board:</b> {
em.board
.map(card => {
const cardUnicode = convertToUnicode(card);
return <span className={`card-char ${card.suit.toLowerCase()}`}>{cardUnicode}</span>
})
}
</div>
);
case 'hole':
return (
<div className={em.playerId === props.myPlayerId ? "message mime system-notification" : "message system-notification"}>
<AvatarOrMe whose={em.playerId}/> {
em.hole
.map(card => {
const cardUnicode = convertToUnicode(card);
return <span className={`card-char ${card.suit.toLowerCase()}`}>{cardUnicode}</span>
})
}
{
props.myPlayerId === em.playerId && <i className="private-message">(private)</i>
}
</div>
);
case 'newRound':
return (
<div className="message system-notification">Round {em.round} started</div>
Expand All @@ -76,6 +49,34 @@ function EventOrMessage(props: {
<AvatarOrMe whose={em.playerId}/>checked
</div>
);
case 'winner':
return (
<div className="message system-notification">
{
em.result.how === 'LastOneWins' ? (
<>
<PlayerAvatar playerId={em.result.winner}/>:&nbsp;won.
</>
) : (
<>
{
em.result.showdown[0].players.map(p => <PlayerAvatar playerId={p}/>)
}
:&nbsp;won ({rankDescription[em.result.showdown[0].handValue]}).
</>
)
}
</div>
);
case 'fund':
return (
<div className={em.playerId === props.myPlayerId ? "message mime system-notification" : "message system-notification"}>
<PlayerAvatar playerId={em.playerId}/>'s fund updated: ${em.currentAmount}&nbsp;
{em.previousAmount && <>
({(em.currentAmount - em.previousAmount >= 0) ? '+' : '-'}${Math.abs(em.currentAmount - em.previousAmount)})
</>}
</div>
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/texas-holdem/TexasHoldemGameRoom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ describe('TexasHoldemGameRoom', () => {
});
});
const fundOfAPromise = new Promise<number>(resolve => {
texasHoldemGameRoom.listener.on('fund', (fund, whose) => {
texasHoldemGameRoom.listener.on('fund', (fund, previousFund, whose) => {
if (whose === 'A') {
resolve(fund);
}
});
});
const fundOfBPromise = new Promise<number>(resolve => {
texasHoldemGameRoom.listener.on('fund', (fund, whose) => {
texasHoldemGameRoom.listener.on('fund', (fund, previousFund, whose) => {
if (whose === 'B') {
resolve(fund);
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/texas-holdem/TexasHoldemGameRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface TexasHoldemGameRoomEvents {

whoseTurn: (round: number, whose: string | null, actionMeta?: {callAmount: number}) => void;
allSet: (round: number) => void;
fund: (fund: number, whose: string) => void;
fund: (fund: number, previousFund: number | undefined, whose: string) => void;
winner: (result: WinningResult) => void;
}

Expand Down Expand Up @@ -499,8 +499,9 @@ export class TexasHoldemGameRoom {
}

private updateFundOfPlayer(whose: string, amount: number) {
const previousAmount = this.funds.get(whose);
this.funds.set(whose, amount);
this.emitter.emit('fund', amount, whose);
this.emitter.emit('fund', amount, previousAmount, whose);
}

private async continueUnlessAllSet(round: number, roundData: TexasHoldemRound, whosePreviousTurn: string) {
Expand Down
93 changes: 47 additions & 46 deletions src/lib/texas-holdem/useEventLogs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Board, Hole} from "../rules";
import {useCallback, useEffect, useState} from "react";
import {TexasHoldem} from "../setup";
import {TexasHoldemGameRoomEvents} from "./TexasHoldemGameRoom";
import {TexasHoldemGameRoomEvents, WinningResult} from "./TexasHoldemGameRoom";

export interface NewRoundEventLog {
type: 'newRound'
Expand All @@ -10,19 +9,6 @@ export interface NewRoundEventLog {
timestamp: number;
}

export interface HoleEventLog {
type: 'hole';
playerId: string;
hole: Hole;
timestamp: number;
}

export interface BoardEventLog {
type: 'board'
board: Board;
timestamp: number;
}

export interface CheckEventLog {
type: 'check';
playerId: string;
Expand All @@ -43,13 +29,27 @@ export interface FoldEventLog {
timestamp: number;
}

export interface WinnerEventLog {
type: 'winner';
result: WinningResult;
timestamp: number;
}

export interface FundEventLog {
type: 'fund';
playerId: string;
previousAmount?: number;
currentAmount: number;
timestamp: number;
}

export type EventLog =
| NewRoundEventLog
| HoleEventLog
| BoardEventLog
| CheckEventLog
| RaiseEventLog
| FoldEventLog
| WinnerEventLog
| FundEventLog
;

export type EventLogs = EventLog[];
Expand All @@ -75,35 +75,6 @@ export default function useEventLogs(): EventLogs {
};
}, [appendLog]);

useEffect(() => {
const holeListener: TexasHoldemGameRoomEvents['hole'] = (round, whose, hole) => {
appendLog({
type: 'hole',
playerId: whose,
hole,
timestamp: Date.now(),
});
};
TexasHoldem.listener.on('hole', holeListener);
return () => {
TexasHoldem.listener.off('hole', holeListener);
};
}, [appendLog]);

useEffect(() => {
const boardListener: TexasHoldemGameRoomEvents['board'] = (round, board) => {
appendLog({
type: 'board',
board,
timestamp: Date.now(),
});
};
TexasHoldem.listener.on('board', boardListener);
return () => {
TexasHoldem.listener.off('board', boardListener);
};
}, [appendLog]);

useEffect(() => {
const betListener: TexasHoldemGameRoomEvents['bet'] = (round, amount, who, allin) => {
if (amount === 0) {
Expand Down Expand Up @@ -142,5 +113,35 @@ export default function useEventLogs(): EventLogs {
};
}, [appendLog]);

useEffect(() => {
const winnerListener: TexasHoldemGameRoomEvents['winner'] = (result) => {
appendLog({
type: 'winner',
result,
timestamp: Date.now(),
});
};
TexasHoldem.listener.on('winner', winnerListener);
return () => {
TexasHoldem.listener.off('winner', winnerListener);
};
}, [appendLog]);

useEffect(() => {
const fundListener: TexasHoldemGameRoomEvents['fund'] = (currentAmount, previousAmount, playerId) => {
appendLog({
type: 'fund',
playerId,
currentAmount,
previousAmount,
timestamp: Date.now(),
});
};
TexasHoldem.listener.on('fund', fundListener);
return () => {
TexasHoldem.listener.off('fund', fundListener);
};
}, [appendLog]);

return logs;
}
4 changes: 2 additions & 2 deletions src/lib/texas-holdem/useTexasHoldem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {useCallback, useEffect, useMemo, useState} from "react";
import {GameRoomStatus} from "../GameRoom";
import {TexasHoldem} from "../setup";
import {Board, Hole} from "../rules";
import {WinningResult} from "./TexasHoldemGameRoom";
import {TexasHoldemGameRoomEvents, WinningResult} from "./TexasHoldemGameRoom";
import { v4 as uuidv4 } from 'uuid';

function useMyPlayerId() {
Expand Down Expand Up @@ -73,7 +73,7 @@ function useGameSetup() {
function useBankrolls() {
const [bankrolls, setBankrolls] = useState<Map<string, number>>(new Map());
useEffect(() => {
const fundListener = (fund: number, whose: string) => {
const fundListener: TexasHoldemGameRoomEvents['fund'] = (fund, previousFund, whose) => {
setBankrolls(prev => {
const newBankrolls = new Map(prev);
newBankrolls.set(whose, fund);
Expand Down

0 comments on commit 44a479d

Please sign in to comment.