-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.min.js
1 lines (1 loc) · 3.13 KB
/
main.min.js
1
function getRandomColor(){for(var e="0123456789ABCDEF",a="#",o=0;o<6;o++)a+=e[Math.floor(16*Math.random())];return a}function syncUsersFromServer(){return new Promise((e,a)=>{db.collection("users").get().then(a=>{a.forEach(e=>{users.push(e.data()),docIds[e.data().userId]=e.id}),e()}).catch(e=>{a(e)})})}function getUserIP(){return new Promise((e,a)=>{var o=new XMLHttpRequest;o.onreadystatechange=function(){4==this.readyState&&200==this.status?e(this.response):4==this.readyState&&200!=this.status&&a(this.statusText)},o.open("GET","https://api.ipify.org/",!0),o.send()})}function loacalAreaUpdater(){return userLocal=JSON.parse(localStorage.getItem("userLocal")),Date.now()>userLocal.lastLogin+432e5&&(userLocal.balloon.area+=1,userLocal.lastLogin=Date.now(),localStorage.setItem("userLocal",JSON.stringify(userLocal)),document.getElementById(userLocal.userId).remove(),generateBalloon(userLocal),!0)}function localIpsUpdater(e){return userLocal=JSON.parse(localStorage.getItem("userLocal")),!userLocal.ips.includes(e)&&(userLocal.ips.push(e),localStorage.setItem("userLocal",JSON.stringify(userLocal)),!0)}function syncLocalUserWithServer(){return new Promise((e,a)=>{userLocal=JSON.parse(localStorage.getItem("userLocal")),userLocal&&db.collection("users").doc(docIds[userLocal.userId]).set(userLocal).then(()=>{e()}).catch(e=>{a(e)})})}async function userLogin(){try{await firebase.auth().signInAnonymously(),firebase.auth().onAuthStateChanged(async e=>{if(e)try{await syncUsersFromServer(),generateBalloons();let a=await getUserIP();if(userLocal=JSON.parse(localStorage.getItem("userLocal")),userLocal){let e=users.find(e=>e.userId==userLocal.userId);if(e){localStorage.setItem("userLocal",JSON.stringify(e));let o=localIpsUpdater(a),t=loacalAreaUpdater();(o||t)&&await syncLocalUserWithServer()}}else{let o=users.find(e=>e.ips.includes(a));if(o){localStorage.setItem("userLocal",JSON.stringify(o));let e=loacalAreaUpdater();e&&await syncLocalUserWithServer()}else{let o={userId:e.uid,ips:[a],lastLogin:Date.now(),balloon:createNewBalloon()};await db.collection("users").add(o),localStorage.setItem("userLocal",JSON.stringify(o)),generateBalloon(o)}}}catch(e){}})}catch(e){}}function createNewBalloon(){return{area:1,x:81*Math.random()+10,y:81*Math.random()+10,color:getRandomColor()}}function generateBalloons(){users.forEach(e=>{generateBalloon(e)})}function generateBalloon(e){balloon=e.balloon;let a=document.createElement("div");a.classList.add("balloon"),a.style.top=balloon.y+"%",a.style.left=balloon.x+"%";let o=2*Math.sqrt(balloon.area/Math.PI);a.style.width=o+"rem",a.style.height=o+"rem",a.style.backgroundColor=balloon.color,a.id=e.userId,container.appendChild(a)}var firebaseConfig={apiKey:"AIzaSyADcIy_uyXHOR7DakkZklpqNMLHAOBoyyI",authDomain:"aalsozluk1.firebaseapp.com",databaseURL:"https://aalsozluk1.firebaseio.com",projectId:"aalsozluk1",storageBucket:"aalsozluk1.appspot.com",messagingSenderId:"543786190600",appId:"1:543786190600:web:ab6749b481da8af4488ebd",measurementId:"G-MS0VCJS1Z9"};firebase.initializeApp(firebaseConfig);var db=firebase.firestore();let users=[],docIds=[];window.onload=(()=>{userLogin()});const container=document.getElementById("balloon_container");