Skip to content

Commit

Permalink
Refined how MessageBar displays system events
Browse files Browse the repository at this point in the history
  • Loading branch information
predatorray committed Dec 10, 2024
1 parent e86e35c commit 60fc7f7
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 20 deletions.
21 changes: 19 additions & 2 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,11 @@
justify-content: flex-end;
}

.message-bar .message .private-message {
margin-left: 2px;
color: #666;
}

.message-bar .message .card-char {
font-size: 30px;
color: black;
Expand All @@ -524,8 +529,20 @@
margin-bottom: 5px;
}

.message-bar .message .message-text {
margin-left: 4px;
.message-bar .message.system-notification {
justify-content: center;
font-size: 10px;
color: #666;
}

.message-bar .message.system-notification::before {
content: '(';
margin-right: auto;
}

.message-bar .message.system-notification::after {
content: ')';
margin-left: auto;
}

.message-bar .message-input {
Expand Down
63 changes: 46 additions & 17 deletions src/components/MessageBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ function EventOrMessage(props: {
eventOrMessage: EventLog | Message;
}) {
const em = props.eventOrMessage;

const AvatarOrMe = (subProps: { whose: string }) => {
return subProps.whose === props.myPlayerId
? <b>Me:&nbsp;</b>
: <><PlayerAvatar playerId={subProps.whose}/>:&nbsp;</>
}

switch (em.type) {
case 'message':
return (
<div className={em.whose === props.myPlayerId ? "message mime" : "message"}>
{
em.whose === props.myPlayerId
? <b>Me:</b>
: <><PlayerAvatar playerId={em.whose}/>:</>
}
<AvatarOrMe whose={em.whose}/>
<div className="message-text">{em.text}</div>
</div>
);
case 'board':
return (
<div className="message">
<div className="message system-notification">
<b>Board:</b> {
em.board
.map(card => {
Expand All @@ -35,18 +38,44 @@ function EventOrMessage(props: {
);
case 'hole':
return (
<div className="message">
<PlayerAvatar playerId={em.playerId} />: {
em.hole
.map(card => {
const cardUnicode = convertToUnicode(card);
return <span className={`card-char ${card.suit.toLowerCase()}`}>{cardUnicode}</span>
})
}
<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>
);
case 'raise':
return (
<div className={em.playerId === props.myPlayerId ? "message mime system-notification" : "message system-notification"}>
<AvatarOrMe whose={em.playerId}/>raised / called ${em.raisedAmount}
{
em.allin && <b>&nbsp;ALL-IN</b>
}
</div>
);
case 'fold':
return (
<div className={em.playerId === props.myPlayerId ? "message mime system-notification" : "message system-notification"}>
<AvatarOrMe whose={em.playerId}/>fold
</div>
);
case 'check':
return (
<div className={em.playerId === props.myPlayerId ? "message mime system-notification" : "message system-notification"}>
<AvatarOrMe whose={em.playerId}/>checked
</div>
);
case "newRound":
return <></>; // TODO
}
}

Expand Down Expand Up @@ -99,7 +128,7 @@ export default function MessageBar(props: {
return;
}
messagesDiv.scrollTo(0, messagesDiv.scrollHeight);
}, [messages]);
}, [messages, eventLogs]);

return (
<div className={collapsed ? "message-bar collapsed" : "message-bar"}>
Expand Down
64 changes: 63 additions & 1 deletion src/lib/texas-holdem/useEventLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,34 @@ export interface BoardEventLog {
timestamp: number;
}

export interface CheckEventLog {
type: 'check';
playerId: string;
timestamp: number;
}

export interface RaiseEventLog {
type: 'raise';
playerId: string;
raisedAmount: number;
allin: boolean;
timestamp: number;
}

export interface FoldEventLog {
type: 'fold';
playerId: string;
timestamp: number;
}

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

export type EventLogs = EventLog[];

Expand Down Expand Up @@ -80,5 +104,43 @@ export default function useEventLogs(): EventLogs {
};
}, [appendLog]);

useEffect(() => {
const betListener: TexasHoldemGameRoomEvents['bet'] = (round, amount, who, allin) => {
if (amount === 0) {
appendLog({
type: 'check',
playerId: who,
timestamp: Date.now(),
});
} else {
appendLog({
type: 'raise',
raisedAmount: amount,
playerId: who,
allin,
timestamp: Date.now(),
});
}
};
TexasHoldem.listener.on('bet', betListener);
return () => {
TexasHoldem.listener.off('bet', betListener);
};
}, [appendLog]);

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

return logs;
}

0 comments on commit 60fc7f7

Please sign in to comment.