Spaces:
Paused
Paused
File size: 5,635 Bytes
f568a4f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | /**
* TradeBeamSystem - Energy beams that fire from Queen to Exchange Gateways on trade execution
* Curved tube geometry with animated progress and particle trails
*/
import { useRef, useState, useMemo, useEffect } from 'react';
import { useFrame } from '@react-three/fiber';
import * as THREE from 'three';
interface TradeData {
time: string;
side: string;
symbol: string;
quantity: number;
pnl: number;
exchange?: string;
}
interface TradeBeamSystemProps {
recentTrades: TradeData[];
}
// Gateway positions (matching ExchangeGateways.tsx)
const GATEWAY_POSITIONS: Record<string, THREE.Vector3> = {
kraken: new THREE.Vector3(-10.4, 0, -6),
binance: new THREE.Vector3(10.4, 0, -6),
capital: new THREE.Vector3(0, 0, 12),
};
const ORIGIN = new THREE.Vector3(0, 0, 0);
interface ActiveBeam {
id: string;
progress: number;
curve: THREE.CatmullRomCurve3;
color: THREE.Color;
opacity: number;
thickness: number;
particlePositions: THREE.Vector3[];
}
function BeamMesh({ beam }: { beam: ActiveBeam }) {
const meshRef = useRef<THREE.Mesh>(null);
const particlesRef = useRef<THREE.Points>(null);
const tubeGeometry = useMemo(() => {
const visibleCurve = new THREE.CatmullRomCurve3(
beam.curve.getPoints(50).slice(0, Math.floor(beam.progress * 50) + 1)
);
if (visibleCurve.points.length < 2) return null;
return new THREE.TubeGeometry(visibleCurve, 32, beam.thickness, 8, false);
}, [beam.curve, beam.progress, beam.thickness]);
// Particle trail at beam head
const particleGeometry = useMemo(() => {
const geo = new THREE.BufferGeometry();
const positions: number[] = [];
const headPoint = beam.curve.getPoint(Math.min(beam.progress, 1));
for (let i = 0; i < 15; i++) {
positions.push(
headPoint.x + (Math.random() - 0.5) * 0.8,
headPoint.y + (Math.random() - 0.5) * 0.8,
headPoint.z + (Math.random() - 0.5) * 0.8
);
}
geo.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
return geo;
}, [beam.curve, beam.progress]);
if (!tubeGeometry) return null;
return (
<group>
<mesh ref={meshRef} geometry={tubeGeometry}>
<meshBasicMaterial
color={beam.color}
transparent
opacity={beam.opacity}
depthWrite={false}
/>
</mesh>
<points ref={particlesRef} geometry={particleGeometry}>
<pointsMaterial
color={beam.color}
size={0.15}
transparent
opacity={beam.opacity * 0.8}
depthWrite={false}
sizeAttenuation
/>
</points>
</group>
);
}
export function TradeBeamSystem({ recentTrades = [] }: TradeBeamSystemProps) {
const [activeBeams, setActiveBeams] = useState<ActiveBeam[]>([]);
const processedTradesRef = useRef<Set<string>>(new Set());
// Detect new trades and spawn beams
useEffect(() => {
if (recentTrades.length === 0) return;
const newBeams: ActiveBeam[] = [];
for (const trade of recentTrades.slice(0, 5)) {
const tradeId = `${trade.time}-${trade.symbol}-${trade.side}`;
if (processedTradesRef.current.has(tradeId)) continue;
processedTradesRef.current.add(tradeId);
// Keep set from growing indefinitely
if (processedTradesRef.current.size > 100) {
const entries = Array.from(processedTradesRef.current);
processedTradesRef.current = new Set(entries.slice(-50));
}
// Determine target gateway
const exchange = (trade.exchange || 'kraken').toLowerCase();
const target = GATEWAY_POSITIONS[exchange] || GATEWAY_POSITIONS.kraken;
// Create curved path from Queen to Gateway
const midPoint = new THREE.Vector3(
(ORIGIN.x + target.x) / 2 + (Math.random() - 0.5) * 4,
3 + Math.random() * 3,
(ORIGIN.z + target.z) / 2 + (Math.random() - 0.5) * 4
);
const curve = new THREE.CatmullRomCurve3([
ORIGIN.clone(),
midPoint,
target.clone(),
]);
const isBuy = trade.side.toUpperCase().includes('BUY') || trade.side.toUpperCase().includes('LONG');
const color = isBuy
? new THREE.Color(0.0, 1.0, 0.5)
: new THREE.Color(1.0, 0.2, 0.4);
// Larger trades = thicker beams
const notional = Math.abs(trade.quantity * (trade.pnl || 1));
const thickness = Math.min(0.02 + notional * 0.001, 0.12);
newBeams.push({
id: tradeId,
progress: 0,
curve,
color,
opacity: 0.9,
thickness,
particlePositions: [],
});
}
if (newBeams.length > 0) {
setActiveBeams(prev => [...prev, ...newBeams].slice(-8)); // Max 8 active beams
}
}, [recentTrades]);
// Animate beams
useFrame((_, delta) => {
setActiveBeams(prev => {
let changed = false;
const updated = prev.map(beam => {
if (beam.progress < 1) {
changed = true;
return { ...beam, progress: Math.min(beam.progress + delta * 0.8, 1) };
} else if (beam.opacity > 0) {
changed = true;
return { ...beam, opacity: Math.max(beam.opacity - delta * 0.4, 0) };
}
return beam;
}).filter(beam => beam.opacity > 0.01);
return changed ? updated : prev;
});
});
return (
<group>
{activeBeams.map(beam => (
<BeamMesh key={beam.id} beam={beam} />
))}
</group>
);
}
|