Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 95 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,80 @@ function AppContent() {
const pendingAbility = (gameState as any).pendingAbilitySelect?.type;
const corruptionActive = pendingAbility === 'corruption_steal';
const maulwurfActive = pendingAbility === 'maulwurf_steal';
const pendingAbilityRef = useRef<any>(null);
const corruptionTargetUidRef = useRef<number | null>(null);
const awaitingCorruptionRollRef = useRef(false);
const awaitingMaulwurfRollRef = useRef(false);

useEffect(() => {
pendingAbilityRef.current = (gameState as any).pendingAbilitySelect ?? null;
}, [gameState]);

useEffect(() => {
const handleTargetSelected = (event: Event) => {
const detail = (event as CustomEvent).detail as { targetUid?: number };
corruptionTargetUidRef.current = detail?.targetUid ?? null;
awaitingCorruptionRollRef.current = Boolean(detail?.targetUid);
};

const handleMaulwurfSelect = (event: Event) => {
const detail = (event as CustomEvent).detail as { targetUid?: number };
awaitingMaulwurfRollRef.current = Boolean(detail?.targetUid);
};

const handleClearSelection = () => {
corruptionTargetUidRef.current = null;
awaitingCorruptionRollRef.current = false;
awaitingMaulwurfRollRef.current = false;
};

window.addEventListener('pc:corruption_target_selected', handleTargetSelected as EventListener);
window.addEventListener('pc:maulwurf_select_target', handleMaulwurfSelect as EventListener);
window.addEventListener('pc:corruption_resolved', handleClearSelection as EventListener);
window.addEventListener('pc:clear_pending_selection', handleClearSelection as EventListener);
return () => {
window.removeEventListener('pc:corruption_target_selected', handleTargetSelected as EventListener);
window.removeEventListener('pc:maulwurf_select_target', handleMaulwurfSelect as EventListener);
window.removeEventListener('pc:corruption_resolved', handleClearSelection as EventListener);
window.removeEventListener('pc:clear_pending_selection', handleClearSelection as EventListener);
};
}, []);

useEffect(() => {
if (maulwurfActive) {
awaitingMaulwurfRollRef.current = true;
}
}, [maulwurfActive]);

useEffect(() => {
const handleDiceResult = () => {
const pending = pendingAbilityRef.current as any;
if (!pending?.type) return;

if (pending.type === 'corruption_steal') {
const targetUid = corruptionTargetUidRef.current;
if (!awaitingCorruptionRollRef.current || !targetUid) return;
awaitingCorruptionRollRef.current = false;
window.dispatchEvent(new CustomEvent('pc:corruption_request_roll', {
detail: { player: pending.actorPlayer ?? gameState.current, targetUid },
}));
}

if (pending.type === 'maulwurf_steal') {
const targetUid = pending.targetUid as number | undefined;
if (!awaitingMaulwurfRollRef.current || !targetUid) return;
awaitingMaulwurfRollRef.current = false;
window.dispatchEvent(new CustomEvent('pc:maulwurf_request_roll', {
detail: { player: pending.actorPlayer ?? gameState.current, targetUid },
}));
}
};

window.addEventListener('pc:dice_result', handleDiceResult as EventListener);
return () => {
window.removeEventListener('pc:dice_result', handleDiceResult as EventListener);
};
}, [gameState.current]);

useEffect(() => {
const handleCorruptionResolved = (event: Event) => {
Expand Down Expand Up @@ -134,7 +208,7 @@ function AppContent() {
title: 'Handkarte auswählen',
body: 'Wähle eine Karte aus deiner Hand, um eine Aktion zu starten.',
};
}, [deckBuilderOpen, corruptionActive, selectedHandIndex]);
}, [deckBuilderOpen, corruptionActive, maulwurfActive, selectedHandIndex]);

// No global image preloading required

Expand Down Expand Up @@ -215,6 +289,25 @@ function AppContent() {
// Handle game control buttons


if (data.type === 'board_card' && corruptionActive) {
const opponent = gameState.current === 1 ? 2 : 1;
if (data.player === opponent && data.lane === 'aussen') {
const targetUid = data.card?.uid ?? data.card?.id;
if (targetUid) {
log(`🎯 Corruption: Ziel gewählt (${data.card.name}).`);
corruptionTargetUidRef.current = targetUid;
awaitingCorruptionRollRef.current = true;
window.dispatchEvent(new CustomEvent('pc:corruption_pick_target', {
detail: { player: gameState.current, targetUid },
}));
window.dispatchEvent(new CustomEvent('pc:corruption_target_selected', {
detail: { player: gameState.current, targetUid },
}));
}
return;
}
}

if (data.type === 'button_pass_turn') {
const currentPlayer = gameState.current;
logger.info(`🔧 DEBUG: button_pass_turn clicked - currentPlayer: ${currentPlayer}`);
Expand Down Expand Up @@ -407,7 +500,7 @@ function AppContent() {
} catch (e) {}
return;
}
}, [gameState, selectedHandIndex, playCard, selectHandCard, passTurn, nextTurn, log]);
}, [gameState, selectedHandIndex, playCard, selectHandCard, passTurn, nextTurn, log, corruptionActive]);

const handleCardHover = useCallback((data: any) => {
setHoveredCard(data);
Expand Down
103 changes: 71 additions & 32 deletions src/components/GameBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const GameBoard: React.FC<GameBoardProps> = ({
const [corruptionSuccessUids, setCorruptionSuccessUids] = useState<Set<number>>(new Set());
const [corruptionFailUids, setCorruptionFailUids] = useState<Set<number>>(new Set());
const corruptionResultTimers = useRef<Map<number, number>>(new Map());
const [corruptionTargetUid, setCorruptionTargetUid] = useState<number | null>(null);

useEffect(() => {
const currentUids = new Set<number>();
Expand Down Expand Up @@ -111,6 +112,37 @@ const GameBoard: React.FC<GameBoardProps> = ({
}
), []);

useEffect(() => {
const handleTargetSelected = (event: Event) => {
const detail = (event as CustomEvent).detail as { targetUid?: number };
setCorruptionTargetUid(detail?.targetUid ?? null);
};

const handleMaulwurfTarget = (event: Event) => {
const detail = (event as CustomEvent).detail as { targetUid?: number };
setCorruptionTargetUid(detail?.targetUid ?? null);
};

const clearTarget = () => setCorruptionTargetUid(null);

window.addEventListener('pc:corruption_target_selected', handleTargetSelected as EventListener);
window.addEventListener('pc:maulwurf_select_target', handleMaulwurfTarget as EventListener);
window.addEventListener('pc:corruption_resolved', clearTarget as EventListener);
window.addEventListener('pc:clear_pending_selection', clearTarget as EventListener);
return () => {
window.removeEventListener('pc:corruption_target_selected', handleTargetSelected as EventListener);
window.removeEventListener('pc:maulwurf_select_target', handleMaulwurfTarget as EventListener);
window.removeEventListener('pc:corruption_resolved', clearTarget as EventListener);
window.removeEventListener('pc:clear_pending_selection', clearTarget as EventListener);
};
}, []);

useEffect(() => {
if (!corruptionActive && !maulwurfTargetUid) {
setCorruptionTargetUid(null);
}
}, [corruptionActive, maulwurfTargetUid]);

useEffect(() => {
const handleCorruptionRoll = (event: Event) => {
const detail = (event as CustomEvent).detail as { victim?: 1 | 2 };
Expand Down Expand Up @@ -184,31 +216,35 @@ const GameBoard: React.FC<GameBoardProps> = ({
style: React.CSSProperties,
data: any,
options?: { selected?: boolean; showActivate?: boolean; onActivate?: () => void; highlight?: boolean }
) => (
<div
key={card.uid}
className={`game-board__card${options?.selected ? ' game-board__card--selected' : ''}${options?.highlight ? ' game-board__card--corruption' : ''}`}
style={style}
onClick={() => onCardClick(data)}
onMouseEnter={(event) => handleHover(card, event)}
onMouseMove={(event) => handleHover(card, event)}
onMouseLeave={() => handleHover(null)}
>
<img src={getCardImagePath(card, 'ui')} alt={card.name} />
{options?.showActivate && options.onActivate && (
<button
type="button"
className="game-board__activate"
onClick={(event) => {
event.stopPropagation();
options.onActivate?.();
}}
>
Aktivieren
</button>
)}
</div>
);
) => {
const isSuccess = corruptionSuccessUids.has(card.uid);
const isFail = corruptionFailUids.has(card.uid);
return (
<div
key={card.uid}
className={`game-board__card${options?.selected ? ' game-board__card--selected' : ''}${options?.highlight ? ' game-board__card--corruption' : ''}${isSuccess ? ' game-board__card--corruption-success' : ''}${isFail ? ' game-board__card--corruption-fail' : ''}`}
style={style}
onClick={() => onCardClick(data)}
onMouseEnter={(event) => handleHover(card, event)}
onMouseMove={(event) => handleHover(card, event)}
onMouseLeave={() => handleHover(null)}
>
<img src={getCardImagePath(card, 'ui')} alt={card.name} />
{options?.showActivate && options.onActivate && (
<button
type="button"
className="game-board__activate"
onClick={(event) => {
event.stopPropagation();
options.onActivate?.();
}}
>
Aktivieren
</button>
)}
</div>
);
};

const renderSlot = (
key: string,
Expand All @@ -234,13 +270,15 @@ const GameBoard: React.FC<GameBoardProps> = ({
lane: 'aussen' | 'innen',
label: string,
) => {
const shouldHighlightCorruption = (
const shouldHighlightCorruption = Boolean(
lane === 'aussen'
&& (
(corruptionActive && player === corruptionTargetPlayer)
|| (corruptionHold.player && player === corruptionHold.player)
)
&& (
(corruptionActive && player === corruptionTargetPlayer)
|| (corruptionHold.player && player === corruptionHold.player)
),
);
const targetUid = maulwurfTargetUid ?? corruptionTargetUid;
const canSelectTarget = corruptionActive && player === corruptionTargetPlayer && lane === 'aussen' && !targetUid;
const rects = lane === 'aussen'
? getGovernmentRects(player === 1 ? 'player' : 'opponent')
: getPublicRects(player === 1 ? 'player' : 'opponent');
Expand All @@ -249,20 +287,21 @@ const GameBoard: React.FC<GameBoardProps> = ({
return rects.map((rect, index) => {
const style = { left: rect.x, top: rect.y, width: rect.w, height: rect.h } as React.CSSProperties;
const card = cards[index];
const isTargetCard = Boolean(targetUid && card?.uid === targetUid);
if (!card) {
return renderSlot(
`${player}-${lane}-${index}`,
style,
label,
() => onCardClick({ type: 'row_slot', player, lane, index }),
isCorruptionTarget,
shouldHighlightCorruption || canSelectTarget,
);
}
return renderCard(
card,
style,
{ type: 'board_card', player, lane, index, card },
{ highlight: isCorruptionTarget },
{ highlight: shouldHighlightCorruption || canSelectTarget || isTargetCard },
);
});
};
Expand Down