@telazer/zilana_client_sdk
Advanced tools
Comparing version 0.9.4 to 0.9.5
@@ -299,2 +299,3 @@ "use strict"; | ||
1000), | ||
isActive: session.isActive, | ||
gameEventFires: (_a = session.gameEventFires) === null || _a === void 0 ? void 0 : _a.map((gef) => { | ||
@@ -301,0 +302,0 @@ return { |
@@ -1,2 +0,2 @@ | ||
"use strict";(()=>{var c=(g,t,s)=>new Promise((i,n)=>{var o=r=>{try{e(s.next(r))}catch(d){n(d)}},a=r=>{try{e(s.throw(r))}catch(d){n(d)}},e=r=>r.done?i(r.value):Promise.resolve(r.value).then(o,a);e((s=s.apply(g,t)).next())});var h={dataKey:"com.telazer.zilana.dev-data",apiBase:"https://api.dev.zilana.io"},l=class{static initialize(n){return c(this,arguments,function*({config:t,eventMap:s,debug:i=!1}){var o;try{if(this.initialized){this.log("SDK is already initialized.","warn");return}this.debug=i,this.eventMap=s,this.log("Initializing SDK...","info"),this.config=t,this.data=this.getLocalData()||{},(o=this.data.sessions)!=null&&o.length?this.data.sessions.forEach(a=>{a.isActive&&(a.isActive=!1)}):this.data.sessions=[],this.data.playerId?(!this.data.playerCustomId&&this.config.playerCustomId&&(yield this.refreshPlayer(this.config.playerCustomId)),this.data.playerCustomId&&!this.config.playerCustomId&&(yield this.refreshPlayer(null)),this.data.playerCustomId&&this.config.playerCustomId&&this.data.playerCustomId!==this.config.playerCustomId&&(yield this.refreshPlayer(this.config.playerCustomId))):yield this.assignPlayerId(),this.setLocalData(),this.startHealthChecker(),this.initialized=!0,this.startSession(),this.log("SDK initialized successfully.","info")}catch(a){this.log("SDK initialized failed. Contact support!","error"),console.error(a),this.config=null,this.data=null,this.initialized=!1}})}static recordEvent(t){var s,i,n,o;try{this.ensureInitialized();let a=(s=this.eventMap)==null?void 0:s[t];if(!a){this.log(`Event "${t}" is not registered.`,"warn");return}this.log(`Recording event: ${t} -> ${a}`,"info");let e=(n=(i=this.data)==null?void 0:i.sessions)==null?void 0:n.find(r=>r.isActive);if(!e){this.log("No active session found. Event not recorded.","warn");return}e.gameEventFires=e.gameEventFires||[],e.gameEventFires.push({gameEventId:a,createdAt:new Date}),(o=this.setLocalData)==null||o.call(this),this.log(`Event "${t}" recorded successfully.`,"info")}catch(a){let e=a;this.log(`Error recording event: ${e.message}`,"error")}}static refreshPlayer(t){return c(this,null,function*(){var s,i,n,o;try{this.ensureInitialized(!0);try{this.log("Try sync existing sessions","info"),yield(s=this.syncSessions)==null?void 0:s.call(this,!0)}catch(a){let e=a;this.log(`Error syncing sessions: ${e.message}`,"error")}try{this.log(`Refreshing player with customId: ${t}`,"info"),this.config&&(this.config.playerCustomId=t),yield(i=this.assignPlayerId)==null?void 0:i.call(this),this.log(`Player refreshed successfully with customId: ${t}`,"info")}catch(a){let e=a;this.log(`Error refreshing player: ${e.message}`,"error")}(n=this.setLocalData)==null||n.call(this),(o=this.startSession)==null||o.call(this)}catch(a){let e=a;this.log(`Critical error in refreshPlayer: ${e.message}`,"error")}})}static log(t,s="info"){let i="[Zilana SDK]";s==="info"&&this.debug?console.log(`${i} ${t}`):s==="warn"?console.warn(`${i} ${t}`):s==="error"&&console.error(`${i} ${t}`)}static getLocalData(){try{let t=localStorage.getItem(h.dataKey);return t?JSON.parse(t):null}catch(t){return this.log(`Failed to retrieve data: ${t}`,"error"),null}}static setLocalData(){try{localStorage.setItem(h.dataKey,JSON.stringify(this.data))}catch(t){this.log(`Failed to save data: ${t}`,"error")}}static assignPlayerId(){return c(this,null,function*(){this.ensureInitialized(!0),this.log("Assigning player ID...","info");let t={userId:this.config.userId,gameId:this.config.gameId,customId:this.config.playerCustomId},s=yield this.apiPost("/api/sdk/players",t);this.data.playerId=s.data.playerId,this.data.playerCustomId=this.config.playerCustomId,this.setLocalData(),this.log("Player ID assigned successfully.","info")})}static apiPost(t,s){return c(this,null,function*(){if(!this.config)throw this.log("SDK config is not set. Call initialize() first.","error"),new Error("Zilana SDK config not set. Call initialize() first.");let i=yield fetch(`${h.apiBase}${t}`,{method:"POST",headers:{"Content-Type":"application/json","zilana-user-id":this.config.userId,"zilana-game-id":this.config.gameId},body:JSON.stringify(s)});if(!i.ok){let n=yield i.json();throw this.log(`API Error ${i.status}: ${n.message}`,"error"),new Error(`API Error ${i.status}: ${n.message}`)}return this.log(`API call to ${t} successful.`,"info"),i.json()})}static startSession(){var t,s;this.ensureInitialized(),this.log("Starting new session...","info"),(t=this.data.sessions)==null||t.forEach(i=>i.isActive=!1),(s=this.data.sessions)==null||s.push({tempId:`${Date.now()}`,isActive:!0,createdAt:new Date,updatedAt:new Date,gameEventFires:[]}),this.setLocalData(),this.scheduleSync(),this.log("Session started successfully.","info")}static ensureInitialized(t=!1){if(!t&&!this.initialized||!this.data||!this.config)throw this.log("SDK is not properly initialized. Call initialize() first.","error"),new Error("Zilana SDK not properly initialized. Call initialize() first.")}static syncSessions(t){return c(this,null,function*(){var s,i;try{if(!this.data.playerId||!((s=this.data.sessions)!=null&&s.length))return;t&&((i=this.data.sessions)==null||i.forEach(e=>e.isActive=!1)),this.log("Syncing sessions with server...","info");let n=new Date,o={userId:this.config.userId,gameId:this.config.gameId,playerId:this.data.playerId,sessions:this.data.sessions.map(e=>{var r;return{id:e.id,tempId:e.id?void 0:e.tempId,durationSec:Math.round(((e.isActive?n.getTime():new Date(e.updatedAt).getTime())-new Date(e.createdAt).getTime())/1e3),gameEventFires:(r=e.gameEventFires)==null?void 0:r.map(d=>({gameEventId:d.gameEventId}))}})},a=yield this.apiPost("/api/sdk/sessions",o);this.data.sessions=this.data.sessions.map(e=>(e.isActive&&(e.updatedAt=n,e.gameEventFires&&(e.gameEventFires=e.gameEventFires.filter(r=>new Date(r.createdAt)>n))),e)).filter(e=>e.isActive),a.data.sessions.forEach(e=>{var d;let r=(d=this.data.sessions)==null?void 0:d.find(f=>f.tempId===e.tempId);r&&(r.id=e.id,delete r.tempId)}),this.setLocalData(),this.log("Sessions synced successfully.","info")}catch(n){throw n}finally{t||this.scheduleSync()}})}static scheduleSync(){this.syncTimeout&&clearTimeout(this.syncTimeout),this.syncTimeout=setTimeout(()=>this.syncSessions(),9e3),this.log("Sync scheduled in 9 seconds.","info")}static startHealthChecker(){setInterval(()=>c(this,null,function*(){var t;try{this.log("Running health check...","info"),(t=this.data)!=null&&t.playerId||(yield this.assignPlayerId()),this.syncTimeout||this.scheduleSync(),this.log("Health check passed.","info")}catch(s){this.log(`Health check failed: ${s}`,"error")}}),3e4)}};l.initialized=!1,l.config=null,l.data=null,l.syncTimeout=null,l.debug=!1,l.eventMap={};typeof window!="undefined"&&(window.Zilana=l);})(); | ||
"use strict";(()=>{var c=(g,t,i)=>new Promise((s,n)=>{var o=r=>{try{e(i.next(r))}catch(d){n(d)}},a=r=>{try{e(i.throw(r))}catch(d){n(d)}},e=r=>r.done?s(r.value):Promise.resolve(r.value).then(o,a);e((i=i.apply(g,t)).next())});var h={dataKey:"com.telazer.zilana.dev-data",apiBase:"https://api.dev.zilana.io"},l=class{static initialize(n){return c(this,arguments,function*({config:t,eventMap:i,debug:s=!1}){var o;try{if(this.initialized){this.log("SDK is already initialized.","warn");return}this.debug=s,this.eventMap=i,this.log("Initializing SDK...","info"),this.config=t,this.data=this.getLocalData()||{},(o=this.data.sessions)!=null&&o.length?this.data.sessions.forEach(a=>{a.isActive&&(a.isActive=!1)}):this.data.sessions=[],this.data.playerId?(!this.data.playerCustomId&&this.config.playerCustomId&&(yield this.refreshPlayer(this.config.playerCustomId)),this.data.playerCustomId&&!this.config.playerCustomId&&(yield this.refreshPlayer(null)),this.data.playerCustomId&&this.config.playerCustomId&&this.data.playerCustomId!==this.config.playerCustomId&&(yield this.refreshPlayer(this.config.playerCustomId))):yield this.assignPlayerId(),this.setLocalData(),this.startHealthChecker(),this.initialized=!0,this.startSession(),this.log("SDK initialized successfully.","info")}catch(a){this.log("SDK initialized failed. Contact support!","error"),console.error(a),this.config=null,this.data=null,this.initialized=!1}})}static recordEvent(t){var i,s,n,o;try{this.ensureInitialized();let a=(i=this.eventMap)==null?void 0:i[t];if(!a){this.log(`Event "${t}" is not registered.`,"warn");return}this.log(`Recording event: ${t} -> ${a}`,"info");let e=(n=(s=this.data)==null?void 0:s.sessions)==null?void 0:n.find(r=>r.isActive);if(!e){this.log("No active session found. Event not recorded.","warn");return}e.gameEventFires=e.gameEventFires||[],e.gameEventFires.push({gameEventId:a,createdAt:new Date}),(o=this.setLocalData)==null||o.call(this),this.log(`Event "${t}" recorded successfully.`,"info")}catch(a){let e=a;this.log(`Error recording event: ${e.message}`,"error")}}static refreshPlayer(t){return c(this,null,function*(){var i,s,n,o;try{this.ensureInitialized(!0);try{this.log("Try sync existing sessions","info"),yield(i=this.syncSessions)==null?void 0:i.call(this,!0)}catch(a){let e=a;this.log(`Error syncing sessions: ${e.message}`,"error")}try{this.log(`Refreshing player with customId: ${t}`,"info"),this.config&&(this.config.playerCustomId=t),yield(s=this.assignPlayerId)==null?void 0:s.call(this),this.log(`Player refreshed successfully with customId: ${t}`,"info")}catch(a){let e=a;this.log(`Error refreshing player: ${e.message}`,"error")}(n=this.setLocalData)==null||n.call(this),(o=this.startSession)==null||o.call(this)}catch(a){let e=a;this.log(`Critical error in refreshPlayer: ${e.message}`,"error")}})}static log(t,i="info"){let s="[Zilana SDK]";i==="info"&&this.debug?console.log(`${s} ${t}`):i==="warn"?console.warn(`${s} ${t}`):i==="error"&&console.error(`${s} ${t}`)}static getLocalData(){try{let t=localStorage.getItem(h.dataKey);return t?JSON.parse(t):null}catch(t){return this.log(`Failed to retrieve data: ${t}`,"error"),null}}static setLocalData(){try{localStorage.setItem(h.dataKey,JSON.stringify(this.data))}catch(t){this.log(`Failed to save data: ${t}`,"error")}}static assignPlayerId(){return c(this,null,function*(){this.ensureInitialized(!0),this.log("Assigning player ID...","info");let t={userId:this.config.userId,gameId:this.config.gameId,customId:this.config.playerCustomId},i=yield this.apiPost("/api/sdk/players",t);this.data.playerId=i.data.playerId,this.data.playerCustomId=this.config.playerCustomId,this.setLocalData(),this.log("Player ID assigned successfully.","info")})}static apiPost(t,i){return c(this,null,function*(){if(!this.config)throw this.log("SDK config is not set. Call initialize() first.","error"),new Error("Zilana SDK config not set. Call initialize() first.");let s=yield fetch(`${h.apiBase}${t}`,{method:"POST",headers:{"Content-Type":"application/json","zilana-user-id":this.config.userId,"zilana-game-id":this.config.gameId},body:JSON.stringify(i)});if(!s.ok){let n=yield s.json();throw this.log(`API Error ${s.status}: ${n.message}`,"error"),new Error(`API Error ${s.status}: ${n.message}`)}return this.log(`API call to ${t} successful.`,"info"),s.json()})}static startSession(){var t,i;this.ensureInitialized(),this.log("Starting new session...","info"),(t=this.data.sessions)==null||t.forEach(s=>s.isActive=!1),(i=this.data.sessions)==null||i.push({tempId:`${Date.now()}`,isActive:!0,createdAt:new Date,updatedAt:new Date,gameEventFires:[]}),this.setLocalData(),this.scheduleSync(),this.log("Session started successfully.","info")}static ensureInitialized(t=!1){if(!t&&!this.initialized||!this.data||!this.config)throw this.log("SDK is not properly initialized. Call initialize() first.","error"),new Error("Zilana SDK not properly initialized. Call initialize() first.")}static syncSessions(t){return c(this,null,function*(){var i,s;try{if(!this.data.playerId||!((i=this.data.sessions)!=null&&i.length))return;t&&((s=this.data.sessions)==null||s.forEach(e=>e.isActive=!1)),this.log("Syncing sessions with server...","info");let n=new Date,o={userId:this.config.userId,gameId:this.config.gameId,playerId:this.data.playerId,sessions:this.data.sessions.map(e=>{var r;return{id:e.id,tempId:e.id?void 0:e.tempId,durationSec:Math.round(((e.isActive?n.getTime():new Date(e.updatedAt).getTime())-new Date(e.createdAt).getTime())/1e3),isActive:e.isActive,gameEventFires:(r=e.gameEventFires)==null?void 0:r.map(d=>({gameEventId:d.gameEventId}))}})},a=yield this.apiPost("/api/sdk/sessions",o);this.data.sessions=this.data.sessions.map(e=>(e.isActive&&(e.updatedAt=n,e.gameEventFires&&(e.gameEventFires=e.gameEventFires.filter(r=>new Date(r.createdAt)>n))),e)).filter(e=>e.isActive),a.data.sessions.forEach(e=>{var d;let r=(d=this.data.sessions)==null?void 0:d.find(f=>f.tempId===e.tempId);r&&(r.id=e.id,delete r.tempId)}),this.setLocalData(),this.log("Sessions synced successfully.","info")}catch(n){throw n}finally{t||this.scheduleSync()}})}static scheduleSync(){this.syncTimeout&&clearTimeout(this.syncTimeout),this.syncTimeout=setTimeout(()=>this.syncSessions(),9e3),this.log("Sync scheduled in 9 seconds.","info")}static startHealthChecker(){setInterval(()=>c(this,null,function*(){var t;try{this.log("Running health check...","info"),(t=this.data)!=null&&t.playerId||(yield this.assignPlayerId()),this.syncTimeout||this.scheduleSync(),this.log("Health check passed.","info")}catch(i){this.log(`Health check failed: ${i}`,"error")}}),3e4)}};l.initialized=!1,l.config=null,l.data=null,l.syncTimeout=null,l.debug=!1,l.eventMap={};typeof window!="undefined"&&(window.Zilana=l);})(); | ||
//# sourceMappingURL=sdk.min.js.map |
@@ -37,2 +37,3 @@ export declare enum ZilanaHeaders { | ||
tempId?: string; | ||
isActive?: boolean; | ||
durationSec?: number; | ||
@@ -39,0 +40,0 @@ gameEventFires?: { |
{ | ||
"name": "@telazer/zilana_client_sdk", | ||
"version": "0.9.4", | ||
"version": "0.9.5", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61195
559