File size: 2,154 Bytes
e229d32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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||'-';