File size: 5,114 Bytes
78fdef7
 
 
 
 
be2b9f0
78fdef7
 
 
 
 
 
 
 
 
 
 
 
 
be2b9f0
 
 
 
 
 
 
 
 
 
 
78fdef7
 
 
 
 
 
 
 
be2b9f0
 
 
 
78fdef7
 
 
 
 
 
 
 
 
 
 
 
 
be2b9f0
78fdef7
be2b9f0
 
78fdef7
 
 
be2b9f0
78fdef7
 
 
 
 
 
be2b9f0
 
78fdef7
be2b9f0
78fdef7
be2b9f0
78fdef7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be2b9f0
 
 
78fdef7
 
 
 
 
 
be2b9f0
 
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
/**
 * @file Markets.js
 * @description Handles the parsing of geographic JSON data, placing interactive market dots 
 * onto the 3D globe, and maintaining their color states (Open/Closed/Holiday) via time logic.
 */

import { setMarqueeMarkets } from '../core/TimeManager.js';

export const marketDots = []; // Stores the rendered 3D dot objects for raycasting

let holidaysCache = {}; // Local cache for country-specific holiday dates

/**
 * Converts geographic Latitude and Longitude coordinates to 3D Cartesian coordinates.
 * @param {number} lat - Latitude in degrees
 * @param {number} lon - Longitude in degrees
 * @param {number} radius - The radius of the globe to map onto
 * @returns {THREE.Vector3} - The resulting X,Y,Z coordinate vector
 */
function latLonToVector3(lat, lon, radius) {
    const phi = (90 - lat) * (Math.PI / 180);
    const theta = (lon + 180) * (Math.PI / 180);

    const x = -(radius * Math.sin(phi) * Math.cos(theta));
    const z = (radius * Math.sin(phi) * Math.sin(theta));
    const y = (radius * Math.cos(phi));

    return new THREE.Vector3(x, y, z);
}

// ==========================================
// 1. Market Initialization
// ==========================================

/**
 * Fetches market coordinate/holiday data and renders 3D dots on the globe.
 * @param {THREE.Group} earthGroup - The parent group to attach the dots to.
 */
export function initMarkets(earthGroup) {
    const dotGeometry = new THREE.SphereGeometry(0.012, 16, 16);
    const closedMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
    
    Promise.all([
        fetch('assets/geo/markets.json').then(res => res.json()),
        fetch('assets/geo/holidays.json').then(res => res.json())
    ]).then(([marketsData, holidaysData]) => {
        holidaysCache = holidaysData;
        setMarqueeMarkets(marketsData);
        marketsData.forEach(market => {
            const dot = new THREE.Mesh(dotGeometry, closedMaterial.clone());
            const position = latLonToVector3(market.lat, market.lon, 1.008);
            dot.position.copy(position);
            dot.userData = market;
            earthGroup.add(dot);
            marketDots.push(dot);
        });
    }).catch(err => console.error("Error loading markets or holidays:", err));
}

let lastUpdate = 0;
const formatters = {};

export function updateMarketColors(currentUTCFloat) {
    const now = Date.now();
    if (now - lastUpdate < 1000) return; // Only calculate once per second to save CPU
    lastUpdate = now;
    
    const nowDate = new Date(now); // Use real time as requested
    
    marketDots.forEach(dot => {
        const market = dot.userData;
        const tz = market.trading_hours.timezone;
        let isOpen = false;
        let isHoliday = false;
        
        try {
            if (!formatters[tz]) {
                formatters[tz] = new Intl.DateTimeFormat('en-US', {
                    timeZone: tz,
                    year: 'numeric',
                    month: '2-digit',
                    day: '2-digit',
                    weekday: 'long',
                    hour: 'numeric',
                    minute: 'numeric',
                    hour12: false
                });
            }
            
            const parts = formatters[tz].formatToParts(nowDate);
            const getPart = (type) => parts.find(p => p.type === type)?.value;
            
            const year = getPart('year');
            const month = getPart('month');
            const day = getPart('day');
            const weekday = getPart('weekday');
            let hour = parseInt(getPart('hour'));
            if (hour === 24) hour = 0;
            const minute = parseInt(getPart('minute'));
            
            const localDateStr = `${year}-${month}-${day}`;
            
            const marketHolidays = holidaysCache[market.iso] || [];
            isHoliday = marketHolidays.includes(localDateStr);
            const isWeekend = market.trading_hours.weekend_days.includes(weekday);
            
            if (!isHoliday && !isWeekend) {
                const [openH, openM] = market.trading_hours.open.split(':').map(Number);
                const [closeH, closeM] = market.trading_hours.close.split(':').map(Number);
                
                const currentMins = hour * 60 + minute;
                const openMins = openH * 60 + openM;
                const closeMins = closeH * 60 + closeM;
                
                if (openMins > closeMins) {
                    isOpen = currentMins >= openMins || currentMins <= closeMins;
                } else {
                    isOpen = currentMins >= openMins && currentMins < closeMins;
                }
            }
        } catch (e) {
            console.error(`Error calculating time for ${market.name}:`, e);
        }
        
        market.isOpen = isOpen;
        market.isHoliday = isHoliday;
        if (isHoliday) {
            dot.material.color.setHex(0x58ccff); // Ice Blue
        } else {
            dot.material.color.setHex(isOpen ? 0x00ff00 : 0xff0000); // Green (Open) / Red (Closed)
        }
    });
}