-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.min.js
1 lines (1 loc) · 9.52 KB
/
script.min.js
1
!function(){function e(){xe=Date.now(),Te=6,Se=!0,Ie=r(),we=!1,t()}function t(){return Te--,Te>0&&Te<=3?(R(Te),void setTimeout(t,500)):Te>0?(R("Get ready!"),void setTimeout(t,500)):N()?(Te=-1,R("Waiting for other snake to go away...",color="Red"),void setTimeout(t,50)):(Le=he,De=0,Te=0,Se=!1,void R("Go!",3e3))}function n(){var e=Date.now(),t=e-xe;if(t>=te){for(var r=t-te,o=Math.max(1,Math.round(r/te)),i=0;i<o;i++)Me=!1,a();0!=Ie.length&&U(Ie),xe=Date.now()}E(),x(),S(),F(),w(),retryClicked&&(retryClicked=!1,k()),setTimeout(n,1)}function a(){0!=Te||(we||Ie.length<=0||s()?we||h():o())}function r(){for(var e=H(4),t=W(),n=[],a=0;a<5;a++){var r=a*he;n[a]={x:e-r,y:t}}return n}function o(){const e={x:Ie[0].x+Le,y:Ie[0].y+De};Ie.unshift(e);const t=u();if(t){var n=A(d())-1;if(n<0)for(var a=0;a>n&&1!=P(Ie);a--)Ie.pop();else He+=n;n++,n>0?R("+"+n,1e3,"LimeGreen"):0==n?R("0",1e3):n<0&&R(n,1e3,"Red")}else He<=0?Ie.pop():He--}function l(e,t,n,a){e?(Le=0,De=-he):t?(Le=-he,De=0):n?(Le=0,De=he):a&&(Le=he,De=0)}function s(){for(var e=4;e<Ie.length;e++)if(Ie[e].x===Ie[0].x&&Ie[e].y===Ie[0].y)return!0;if(N())return!0;const t=Ie[0].x<0,n=Ie[0].x>ye-he,a=Ie[0].y<0,r=Ie[0].y>ve-he;return t||n||a||r}function u(){var e=!1;for(var t in _e){var n=_e[t];if(Ie[0].x===n.x&&Ie[0].y===n.y){e=!0;break}}return e}function c(e,t,n){_e[_e.length]={x:e,y:t,level:n,uuid:crypto.randomUUID()},q()}function f(e,t){var n=[],a=ne;for(var r in i=0,_e){var o=_e[r];foodX=o.x,foodY=o.y,e!=foodX||t!=foodY?(n[i]=o,i++):a=o.level}return _e=n,q(),a}function d(){var e=f(Ie[0].x,Ie[0].y),t=Math.max(1,Math.round(O()/2))*ze;t-=_e.length;for(var n=0;n<t;n++){for(var a,r=!0;r;)for(var o in a=y(),0==_e.length&&(r=!1),_e){var i=_e[o];i.x==a.x&&i.y==a.y||(r=!1)}c(a.x,a.y,C())}return e}function y(){var e=H(),t=W();return Ie.forEach(n=>{const a=n.x==e&&n.y==t;a&&y()}),Ce.forEach(n=>{const a=n.x==e&&n.y==t;a&&y()}),{x:e,y:t}}function v(){if(!(P(Ie)<=5))for(var e in Ie){var t=Ie[e];t.x<0||t.x>ye-he||t.y<0||t.y>ve-he||0==_(0,5)&&c(t.x,t.y,C())}}function h(){we=!0,U([]),v(),Ie=[],R("Game Over!",0,"Red"),document.getElementById("score").style.visibility="visible",document.getElementById("score").innerHTML="Your score: "+Be+'<button id="buttonRetry" class="button retry" onclick="onRetryClick()">Retry (r)</button>'}function p(){g()}function b(e){const t=38,n=87,a=37,r=65,o=40,i=83,s=39,u=68,c=13,f=82;if(Me)return;Me=!0;const d=e.keyCode,y=De===-he,v=De===he,h=Le===he,p=Le===-he;l((d===t||d===n)&&!v,(d===a||d===r)&&!h,(d===o||d===i)&&!y,(d===s||d===u)&&!p),d!==c&&d!==f||k()}function m(e){var t=fe,n=t.getBoundingClientRect(),a=be/ye,r=me/ve;const o=(e.touches[0].clientX-n.left)/a,i=(e.touches[0].clientY-n.top)/r;if(Me)return;Me=!0;const s=De===-he,u=De===he,c=Le===he,f=Le===-he,d=!(!(ve/2>i)||ye/4>o||ye-ye/4<o||u),y=ye/4>o&&!c,v=!(!(ve/2<i)||ye/4>o||ye-ye/4<o||s),h=ye-ye/4<o&&!f;l(d,y,v,h)}function k(){we&&(document.getElementById("score").style.visibility="hidden",e())}function g(){var e=1.5*window.innerWidth*.5,t=1.5*window.innerHeight*.75,n=ye/ve,a=ye/e,r=ve/t;a>r?(be=e,me=e/n):r>a?(be=t*n,me=t):(be=e,me=t),fe.width=be,fe.height=me,de.scale(be/ye,me/ve)}function x(){de.fillStyle=Z,de.fillRect(0,0,ye,ve)}function w(){if(0!=Ie.length){var e=!0;Ie.forEach(t=>{T(pe,t,e),e=!1})}}function T(e,t,n){de.fillStyle=e,de.strokestyle="black",de.fillRect(t.x,t.y,he,he),de.strokeRect(t.x,t.y,he,he),n&&(de.fillStyle="black"===e?"white":"black",de.beginPath(),Le>0?(de.moveTo(t.x+he-1,t.y+he/2),de.lineTo(t.x+.75*he-1,t.y),de.lineTo(t.x+.75*he-1,t.y+he)):Le<0?(de.moveTo(t.x+1,t.y+he/2),de.lineTo(t.x+.25*he+1,t.y),de.lineTo(t.x+.25*he+1,t.y+he)):De>0?(de.moveTo(t.x+he/2,t.y+he-1),de.lineTo(t.x,t.y+.75*he-1),de.lineTo(t.x+he,t.y+.75*he-1)):De<0&&(de.moveTo(t.x+he/2,t.y+1),de.lineTo(t.x,t.y+.25*he+1),de.lineTo(t.x+he,t.y+.25*he+1)),de.closePath(),de.fill())}function S(){for(var e in _e){var t=_e[e];de.fillStyle=L(D(t.level)),de.strokestyle="black",de.fillRect(t.x,t.y,he,he),de.strokeRect(t.x,t.y,he,he)}We++}function E(){var e=[],t="";for(var n in i=0,Ae){var a=P(Ae[n].pos)-5;Be=a>=0&&n==Ee?a:Be,e[i]={score:a,playerName:n},i++}for(var r in e.sort((e,t)=>t.score-e.score),e){var o=e[r],l=(a=o.score,n=o.playerName,Ae[n]);t+='<span id="scoreboard" style="color:'+l.color+';">'+G(n)+"</span>: "+(a<-4?"Spectator":a)+"<br>"}document.getElementById("sidebar").innerHTML="Online: "+z()+"/15<br><br>"+t}function R(e,t=0,n="Black"){var a='<div id="innerStatus" style="color: '+n+';">'+e+"</div>";document.getElementById("status").innerHTML=a,t>0&&setTimeout(()=>{document.getElementById("status").innerHTML===a&&(document.getElementById("status").innerHTML='<div id="innerStatus" style="color: transparent;">42</div')},t)}function I(){do{if(ge>J){for(var e in ke=!0,Ie=[],U([]),Ae)if(otherSnake=Ie[e],e==Ee&&null!=otherSnake&&0!=P(otherSnake))return;ke=!1,alert("Client outdated. Please close the tab, delete all cookies and data from this page and reopen this page.\nThe easiest way is to delete all your browsing data.")}else if(ge<J){for(var e in ke=!0,Ie=[],U([]),Ae)if(otherSnake=Ie[e],e==Ee&&null!=otherSnake&&0!=P(otherSnake))return;ke=!1,alert("Database outdated. Please wait for the database to update, then reopen this tab.")}else ke=!0}while(!ke)}function B(){var e=M(),t=e[_(0,e.length-1)];return t}function M(){var e=[],t=[],n=0;for(var a in Ce)otherSnake=Ce[a],null!=otherSnake&&null!=otherSnake.color&&(e[n]=otherSnake.color,n++);for(var r in n=0,ee)Array.from(e).includes(ee[r])||(t[n]=ee[r],n++);return t}function L(e){50==We&&(We=-50);var t={h:e==ue?(We+50)/99*360:e,s:100,v:e==ue?100:Math.abs(We)+50},n=Color(t);return n.toString()}function D(e){switch(e){case ae:return ie;case re:return le;case oe:return se;case ne:return ue;default:return 0}}function C(){if(Pe>=0)return Pe;var e=_(0,300);return e>=0&&e<100?ae:e>=100&&e<200?re:e>=200&&e<250?oe:ne}function A(e){return 0==e?_(-10,10):e}function G(e){return String(e).replace("&","&").replace("<","<").replace(">",">").replace('"',""")}function _(e,t){return Math.round(Math.random()*(t-e)+e)}function H(e){return e=e||0,Math.round(_(e*he,ye-he)/he)*he}function W(){return Math.round(_(0,ve-he)/he)*he}function P(e){var t=0;for(var n in e)t++;return t}function z(){return P(Ae)}function O(){var e=0;for(var t in Ae){var n=Ae[t];null!=n.pos&&0!=P(n.pos)&&e++}return e}function Y(){for(;z()>=ce;)alert("The game is full (15/15). Click the button, to retry.")}function F(){for(var e in Ce){var t=Ce[e];if(null!=t&&null!=t.pos){var n=!0;t.pos.forEach(e=>{T(null==t.color?"red":t.color,e,n),n=!1})}}}function N(){for(var e in Ce){var t=Ce[e].pos;if(null!=t)for(var n=0;n<t.length;n++)if(t[n].x===Ie[0].x&&t[n].y===Ie[0].y)return!0}return!1}function U(e){Se&&(e=[]),firebase.database().ref("snake/players/"+Ee+"/pos").set(e)}function q(){firebase.database().ref("snake/foods").set(_e)}function X(){firebase.database().ref("snake/foods").on("value",e=>{data=e.val(),_e=null==data?[]:data}),firebase.database().ref("snake/variables/forcedFoodLevel").on("value",e=>{data=e.val(),Pe=null==data?-1:data}),firebase.database().ref("snake/variables/foodFactor").on("value",e=>{data=e.val(),ze=null==data?1:data}),firebase.database().ref("snake/players").on("value",e=>{if(data=e.val(),null!=data){var t=[];for(var n in data)n!=Ee?t[n]=data[n]:pe=data[n].color;Ce=t,Ae=data,Ge=!0}else Ge=!0})}function K(){p(),alert("We use cookies. By using this site, you agree with it.\n\nWe use the Realtime Database of Google Firebase, to serve multiplayer. If you want to know how Google proceeds your data, look on their page: https://firebase.google.com/support/privacy/\n\nIf you don't want to agree, close this site. No data has been saved yet."),firebase.initializeApp(Oe),firebase.database().ref("snake/variables/version").on("value",e=>{data=e.val(),ge=parseInt(data,10),I()}),Q()}function Q(){ke?(document.addEventListener("keydown",b),window.addEventListener("resize",p,!1),fe.addEventListener("touchstart",m),window.addEventListener("keydown",function(e){[32,37,38,39,40].indexOf(e.keyCode)>-1&&e.preventDefault()},!1),p(),X(),$()):setTimeout(Q,50)}function $(){Ge?(Y(),V(),j()):setTimeout($,50)}function j(){Re?(firebase.database().ref("snake/players/"+Ee+"/verifyName").set(Ee),firebase.database().ref("snake/players/"+Ee).onDisconnect().remove(),pe=B(),firebase.database().ref("snake/players/"+Ee+"/color").set(pe),0==_e.length&&c(H(),W(),C()),xe=Date.now(),n(),e()):setTimeout(j,50)}function V(){for(Re=!1,Ee="";""==Ee;){var e=prompt('Choose a player name:\n\n".", "/", "#", "$", "[", or "]" will be replaced by "_".');Ee=null==e?"":e,Ee=Ee.replaceAll(".","_").replaceAll("#","_").replaceAll("$","_").replaceAll("[","_").replaceAll("]","_").replaceAll("/","_"),Ee.length>16?(alert("This name is too long."),Ee=""):firebase.database().ref("snake/players/"+Ee+"/color").get().then(e=>{if(e.exists())return alert("Someone has already chosen this name."),Ee="",void V();Re=!0})}}const J=16,Z="#555555",ee=["Aqua","Yellow","Red","Black","White","DeepPink","LawnGreen","Orange","SaddleBrown","OrangeRed","DarkViolet","Gold","Indigo","Silver","DarkGreen"],te=100,ne=0,ae=1,re=2,oe=3,ie=175,le=110,se=0,ue=-1,ce=ee.length,fe=document.getElementById("snakeboard"),de=fe.getContext("2d"),ye=1280,ve=720,he=16;var pe,be,me,ke=!1,ge=0,xe=0,we=!1,Te=6,Se=!0,Ee="",Re=!1,Ie=[],Be=0,Me=!1,Le=he,De=0,Ce=[],Ae=[],Ge=!1,_e=[],He=0,We=0,Pe=-1,ze=1,Oe={apiKey:"AIzaSyBbRpK_BcltEmRQzLAUCFykMHEq5PQWWz4",authDomain:"psyched-canto-311609.firebaseapp.com",databaseURL:"https://psyched-canto-311609-default-rtdb.europe-west1.firebasedatabase.app",projectId:"psyched-canto-311609",storageBucket:"psyched-canto-311609.appspot.com",messagingSenderId:"556381649934",appId:"1:556381649934:web:af27169882d8297d78d05f"};K()}();