Skip to content

Commit 146ce90

Browse files
style: improve appearance of material count
1 parent cdaf284 commit 146ce90

1 file changed

Lines changed: 47 additions & 34 deletions

File tree

src/components/Common/PlayerInfo.tsx

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,21 @@ export const PlayerInfo: React.FC<PlayerInfoProps> = ({
130130
const myCapturedPieces =
131131
color === 'white' ? capturedPieces.black : capturedPieces.white
132132

133-
// Calculate score advantage for this player
134-
const myAdvantage =
135-
materialAdvantage[color as 'white' | 'black'] -
136-
materialAdvantage[color === 'white' ? 'black' : 'white']
133+
// Calculate net material advantage (white total - black total)
134+
const netAdvantage = materialAdvantage.white - materialAdvantage.black
135+
136+
// Only show advantage for the side that actually has more material
137+
const myAdvantage = useMemo(() => {
138+
if (netAdvantage === 0) return 0
139+
140+
if (netAdvantage > 0) {
141+
// White has advantage
142+
return color === 'white' ? netAdvantage : 0
143+
} else {
144+
// Black has advantage
145+
return color === 'black' ? Math.abs(netAdvantage) : 0
146+
}
147+
}, [netAdvantage, color])
137148

138149
// Map chess pieces to Material UI icons
139150
const getPieceIcon = (piece: string): string => {
@@ -149,30 +160,39 @@ export const PlayerInfo: React.FC<PlayerInfoProps> = ({
149160

150161
// Render captured pieces
151162
const renderCapturedPieces = () => {
152-
const pieces: React.JSX.Element[] = []
163+
const pieceGroups: React.JSX.Element[] = []
153164

154165
// Order pieces by value (lowest to highest)
155166
const orderedPieces = ['p', 'n', 'b', 'r', 'q']
156167

157168
orderedPieces.forEach((piece) => {
158169
const count = myCapturedPieces[piece] || 0
159-
for (let i = 0; i < count; i++) {
160-
pieces.push(
161-
<span
162-
key={`${piece}-${i}`}
163-
className="material-symbols-outlined text-sm text-secondary"
164-
title={`${piece === 'p' ? 'pawn' : piece === 'n' ? 'knight' : piece === 'b' ? 'bishop' : piece === 'r' ? 'rook' : 'queen'}`}
165-
>
166-
{getPieceIcon(piece)}
167-
</span>,
170+
if (count > 0) {
171+
const piecesOfType: React.JSX.Element[] = []
172+
173+
for (let i = 0; i < count; i++) {
174+
piecesOfType.push(
175+
<span
176+
key={`${piece}-${i}`}
177+
className={`material-symbols-outlined material-symbols-filled text-sm text-secondary ${i > 0 ? '-ml-1.5' : ''}`}
178+
title={`${piece === 'p' ? 'pawn' : piece === 'n' ? 'knight' : piece === 'b' ? 'bishop' : piece === 'r' ? 'rook' : 'queen'}`}
179+
>
180+
{getPieceIcon(piece)}
181+
</span>,
182+
)
183+
}
184+
185+
pieceGroups.push(
186+
<div key={piece} className="flex items-center gap-0">
187+
{piecesOfType}
188+
</div>,
168189
)
169190
}
170191
})
171192

172-
return pieces
193+
return pieceGroups
173194
}
174195

175-
// Update clock countdown every second if this clock is active
176196
useEffect(() => {
177197
if (!clock || !clock.isActive) return
178198

@@ -181,7 +201,7 @@ export const PlayerInfo: React.FC<PlayerInfoProps> = ({
181201
const elapsedSinceUpdate = (now - clock.lastUpdateTime) / 1000
182202
const newTime = Math.max(0, clock.timeInSeconds - elapsedSinceUpdate)
183203
setCurrentTime(newTime)
184-
}, 100) // Update every 100ms for smooth countdown
204+
}, 100)
185205

186206
return () => clearInterval(interval)
187207
}, [clock])
@@ -212,6 +232,16 @@ export const PlayerInfo: React.FC<PlayerInfoProps> = ({
212232
{rating ? `(${rating})` : null}
213233
</span>
214234
</p>
235+
{currentFen && (
236+
<div className="ml-1 flex select-none items-center gap-1">
237+
<div className="flex items-center">{renderCapturedPieces()}</div>
238+
{myAdvantage > 0 && (
239+
<span className="text-xxs font-medium text-secondary">
240+
+{myAdvantage}
241+
</span>
242+
)}
243+
</div>
244+
)}
215245
</div>
216246
<div className="flex items-center gap-4">
217247
{showArrowLegend && (
@@ -231,23 +261,6 @@ export const PlayerInfo: React.FC<PlayerInfoProps> = ({
231261
</div>
232262
)}
233263

234-
{/* Captured pieces and material advantage */}
235-
{currentFen && (
236-
<div className="flex items-center gap-2">
237-
{/* Captured pieces icons */}
238-
<div className="flex items-center gap-0.5">
239-
{renderCapturedPieces()}
240-
</div>
241-
242-
{/* Material advantage score */}
243-
{myAdvantage !== 0 && (
244-
<span className="text-xs font-medium text-primary">
245-
+{Math.abs(myAdvantage)}
246-
</span>
247-
)}
248-
</div>
249-
)}
250-
251264
{clock && (
252265
<div
253266
className={`flex items-center px-2 py-1 ${

0 commit comments

Comments
 (0)