Files
moon-gaming/games/New folder/Extensions/PlayerAuthentication/playerauthenticationtools.js
T
2023-11-23 20:37:33 +00:00

3 lines
8.3 KiB
JavaScript

var gdjs;(function(d){const a=new d.Logger("Player Authentication"),c=d.playerAuthenticationComponents;let S;(function(i){let f=null,T=null,p=null,M=!1,w=!1,G=null,b=null,E=null,C=null,D=null,h=null,l=null,x=null,W=null,u=null,_=null,g=null;d.registerRuntimeScenePostEventsCallback(()=>{M=!1}),d.registerFirstRuntimeSceneLoadedCallback(e=>{B(e)==="web"&&(L(),u=t=>{U(e,t,{checkOrigin:!0})},window.addEventListener("message",u,!0),a.info("Notifying parent window that player authentication is ready."),window.parent.postMessage({id:"playerAuthReady"},"*"),x=setTimeout(()=>{a.info("Removing initial authentication listener."),L()},3e3))});const O=e=>`${e}_authenticatedUser`,j=({runtimeGame:e,gameId:t,connectionId:n})=>`https://gd.games/auth?gameId=${t}${n?`&connectionId=${n}`:""}${e.isUsingGDevelopDevelopmentEnvironment()?"&dev=true":""}`,B=e=>e.getGame().getRenderer().getElectron()?"electron":typeof cordova!="undefined"?"cordova":"web",q=e=>{const t=e.getGame().getAdditionalOptions();return t&&t.isPreview&&t.allowAuthenticationUsingIframeForPreview};i.isAuthenticated=()=>(w||R(),p!==null),i.hasLoggedIn=()=>M,i.getUsername=()=>(w||R(),f||""),i.getUserToken=()=>(w||R(),p||null),i.getUserId=()=>(w||R(),T||null);const P=(e,t,n=0)=>{const r=`${e.isUsingGDevelopDevelopmentEnvironment()?"https://api-dev.gdevelop.io":"https://api.gdevelop.io"}/game/public-game/${t}`;return fetch(r,{method:"HEAD"}).then(s=>s.status!==200?(a.warn(`Error while fetching the game: ${s.status} ${s.statusText}`),s.status===404||n>2?!1:P(e,t,n+1)):!0,s=>(a.error("Error while fetching game:",s),!1))};i.logout=e=>{f=null,p=null,T=null;const t=d.projectData.properties.projectUuid;if(!t){a.error("Missing game id in project properties.");return}window.localStorage.removeItem(O(t)),k(e),i.removeAuthenticationBanner(e);const n=e.getGame().getRenderer().getDomElementContainer();if(!n){m(e,"The div element covering the game couldn't be found, the authentication banner cannot be displayed.");return}c.displayLoggedOutNotification(n)};const R=()=>{const e=d.projectData.properties.projectUuid;if(!e){a.error("Missing game id in project properties.");return}try{const t=window.localStorage.getItem(O(e));if(!t){w=!0;return}const n=JSON.parse(t);f=n.username,T=n.userId,p=n.userToken,w=!0}catch(t){a.warn("Unable to read authentication details from localStorage. Player authentication will not be available.",t)}},k=e=>{i.removeAuthenticationContainer(e),J(),g&&(g.close(),g=null),G&&(G.close(),G=null),b&&(b.close(),b=null)},F=({username:e,userId:t,userToken:n})=>{e||a.warn("The authenticated player does not have a username"),f=e,T=t,p=n,M=!0;const o=d.projectData.properties.projectUuid;if(!o){a.error("Missing game id in project properties.");return}try{window.localStorage.setItem(O(o),JSON.stringify({username:f,userId:T,userToken:p}))}catch(r){a.warn("Unable to save the authentication details to localStorage. Player authentication will not be available.",r)}},H=function(e,t,n,o){F({userId:t,username:n,userToken:o}),k(e),i.removeAuthenticationBanner(e);const r=e.getGame().getRenderer().getDomElementContainer();if(!r){m(e,"The div element covering the game couldn't be found, the authentication banner cannot be displayed.");return}c.displayLoggedInNotification(r,f||"Anonymous"),$(e)},U=function(e,t,{checkOrigin:n}){if(!(n&&!["https://liluo.io","https://gd.games"].includes(t.origin))){if(!t.data.id)throw new Error("Malformed message");switch(t.data.id){case"authenticationResult":{if(!(t.data.body&&t.data.body.token))throw new Error("Malformed message.");H(e,t.data.body.userId,t.data.body.username,t.data.body.token);break}case"alreadyAuthenticated":{if(!(t.data.body&&t.data.body.token))throw new Error("Malformed message.");F({userId:t.data.body.userId,username:t.data.body.username,userToken:t.data.body.token}),L(),V(e);break}}}},m=function(e,t){a.error(t),k(e);const n=e.getGame().getRenderer().getDomElementContainer();if(!n){m(e,"The div element covering the game couldn't be found, the authentication banner cannot be displayed.");return}c.displayErrorNotification(n),$(e)},z=e=>{J();const t=12*60*1e3;W=setTimeout(()=>{a.info("Authentication window did not send message in time. Closing it."),k(e),$(e)},t)},J=()=>{x&&clearTimeout(x),W&&clearTimeout(W)},K=function(e){const t=()=>{i.removeAuthenticationBanner(e)},n=()=>{i.openAuthenticationWindow(e)};return p?c.computeAuthenticatedBanner(n,t,f):c.computeNotAuthenticatedBanner(n,t)};i.displayAuthenticationBanner=function(e){if(l){l.style.opacity="1";return}w||R();const t=e.getGame().getRenderer().getDomElementContainer();if(!t){m(e,"The div element covering the game couldn't be found, the authentication banner cannot be displayed.");return}l=K(e),t.appendChild(l)};const V=function(e){if(!l)return;const t=e.getGame().getRenderer().getDomElementContainer();if(!t){m(e,"The div element covering the game couldn't be found, the authentication banner cannot be displayed.");return}const n=l;l=K(e),t.replaceChild(l,n)},X=(e,t)=>{const n=e.getGame().isUsingGDevelopDevelopmentEnvironment()?"wss://api-ws-dev.gdevelop.io/play":"wss://api-ws.gdevelop.io/play";g=new WebSocket(n),g.onopen=()=>{g&&g.send(JSON.stringify({action:"getConnectionId"}))},g.onerror=()=>{m(e,"Error while connecting to the authentication server.")},g.onmessage=o=>{if(o.data){const r=JSON.parse(o.data);switch(r.type){case"authenticationResult":{const s=r.data;H(e,s.userId,s.username,s.token);break}case"connectionId":{const v=r.data.connectionId;if(!v){a.error("No connectionId received");return}const y=j({runtimeGame:e.getGame(),gameId:t,connectionId:v}),I=e.getGame().getRenderer().getElectron(),A=()=>I.shell.openExternal(y);A(),h&&c.addAuthenticationUrlToTextsContainer(A,h);break}}}}},Y=(e,t)=>{const n=j({runtimeGame:e.getGame(),gameId:t});b=cordova.InAppBrowser.open(n,"authentication","location=yes"),b&&(_=o=>{U(e,o,{checkOrigin:!1})},b.addEventListener("message",_,!0))},Z=(e,t)=>{const n=j({runtimeGame:e.getGame(),gameId:t});u=y=>{U(e,y,{checkOrigin:!0})},window.addEventListener("message",u,!0);const o=screen.width/2-500/2,r=screen.height/2-600/2,s=`left=${o},top=${r},width=500,height=600`,v=()=>window.open(n,"authentication",s);G=v(),h&&c.addAuthenticationUrlToTextsContainer(v,h)},ee=(e,t)=>{if(!D||!C||!h){console.error("Can't open an authentication iframe - no iframe container, loader container or text container was opened for it.");return}const n=j({runtimeGame:e.getGame(),gameId:t});u=o=>{U(e,o,{checkOrigin:!0})},window.addEventListener("message",u,!0),c.displayIframeInsideAuthenticationContainer(D,C,h,n)};i.openAuthenticationWindow=function(e){const t=e.getGame().getRenderer().getDomElementContainer();if(!t){m(e,"The div element covering the game couldn't be found, the authentication window cannot be displayed.");return}const n=()=>{k(e),i.displayAuthenticationBanner(e)},o=d.projectData.properties.projectUuid;if(!o){m(e,"The game ID is missing, the authentication window cannot be opened.");return}l&&(l.style.opacity="0");const r=B(e),{rootContainer:s,loaderContainer:v,iframeContainer:y}=c.computeAuthenticationContainer(n);E=s,C=v,D=y,t.appendChild(E),P(e.getGame(),o).then(I=>{if(C){const A=e.getGame().getRenderer().getElectron(),oe=A?()=>A.shell.openExternal("https://wiki.gdevelop.io/gdevelop5/publishing/web"):null;h=c.addAuthenticationTextsToLoadingContainer(C,r,I,oe)}if(I)switch(z(e),r){case"electron":X(e,o);break;case"cordova":Y(e,o);break;case"web":default:q(e)?ee(e,o):Z(e,o);break}}).catch(I=>{m(e,"Error while checking if the game is registered."),a.error(I)})},i.isAuthenticationWindowOpen=function(){return!!E},i.removeAuthenticationContainer=function(e){L();const t=e.getGame().getRenderer().getDomElementContainer();if(!t){a.info("The div element covering the game couldn't be found, the authentication must be already closed.");return}E&&t.removeChild(E),E=null,C=null,D=null,h=null};const L=function(){u&&(window.removeEventListener("message",u,!0),u=null,_=null)};i.removeAuthenticationBanner=function(e){if(!l){a.info("The authentication banner couldn't be found, the authentication banner must be already closed.");return}const t=e.getGame().getRenderer().getDomElementContainer();if(!t){a.info("The div element covering the game couldn't be found, the authentication must be already closed.");return}t.removeChild(l),l=null};const $=function(e){const t=e.getGame().getRenderer().getCanvas();t&&t.focus()}})(S=d.playerAuthentication||(d.playerAuthentication={}))})(gdjs||(gdjs={}));
//# sourceMappingURL=playerauthenticationtools.js.map