neuronalstarx / src /main.js
salomonsky's picture
Upload 10 files
e229d32 verified
import {initFirebase, getAuthState, ensureProfile, signOut, getProfile} from './firebase.js';
import {initScene, clearScene, usernameSphere, addNeuronMesh, drawMinimap, focusOnUser, teleportToUser, userCenter} from './scene.js';
import {initUI, handleSeed} from './ui.js';
import {subscribeNeurons} from './db.js';
import {appId} from './config.js';
import {hslFromString} from './utils.js';
const THREE=window.THREE;
await initFirebase();
initScene();
initUI();
document.getElementById('seedBtn').addEventListener('click',handleSeed);
document.getElementById('logoutBtn').addEventListener('click',async()=>{await signOut(); location.reload();});
let profiles={};
subscribeNeurons(appId, async snap=>{
clearScene();
const perUser={};
snap.forEach(d=>{const v=d.data(); if(!v.userId||!v.position||!v.label) return; if(!perUser[v.userId]) perUser[v.userId]=[]; perUser[v.userId].push(v); if(v.username && !profiles[v.userId]) profiles[v.userId]={username:v.username};});
const uids=Object.keys(perUser);
const userList=document.getElementById('userList');
if (userList) userList.innerHTML='';
for(const uid of uids){
if(!profiles[uid]){ try{ const p=await ensureProfile(uid); profiles[uid]=p; }catch{} }
const uname=(profiles[uid]?.username)||`Usuario ${uid.slice(0,4)}`;
usernameSphere(uid,uname);
const arr=perUser[uid].sort((a,b)=>(a.createdAt?.seconds||0)-(b.createdAt?.seconds||0));
for(const n of arr){
const col=hslFromString(n.label).color;
const pos=new THREE.Vector3(n.position.x,n.position.y,n.position.z);
addNeuronMesh(uid,n.label,n.level||1,pos,col);
}
if (userList){
const item=document.createElement('div');
item.className='p-2 mb-1 rounded-md hover:bg-gray-700 cursor-pointer';
item.textContent=uname;
item.addEventListener('click',()=> teleportToUser(uid));
userList.appendChild(item);
}
}
const me=getAuthState().userId;
drawMinimap(uids,profiles,me);
focusOnUser(me);
});
window.addEventListener('teleport',e=>{teleportToUser(e.detail.uid)});
document.getElementById('usernameLabel').textContent=getAuthState().username||'-';