@@ -3,6 +3,22 @@ declare global {
33 CrazyGames ?: {
44 SDK : {
55 init : ( ) => Promise < void > ;
6+ user : {
7+ getUser ( ) : Promise < {
8+ username : string ;
9+ profilePictureUrl : string ;
10+ } | null > ;
11+ } ;
12+ ad : {
13+ requestAd : (
14+ adType : string ,
15+ callbacks : {
16+ adStarted : ( ) => void ;
17+ adFinished : ( ) => void ;
18+ adError : ( error : any ) => void ;
19+ } ,
20+ ) => void ;
21+ } ;
622 game : {
723 gameplayStart : ( ) => Promise < void > ;
824 gameplayStop : ( ) => Promise < void > ;
@@ -15,6 +31,7 @@ declare global {
1531 } ) => string ;
1632 hideInviteButton : ( ) => void ;
1733 getInviteParam : ( paramName : string ) => string | null ;
34+ isInstantMultiplayer ?: boolean ;
1835 } ;
1936 } ;
2037 } ;
@@ -24,6 +41,24 @@ declare global {
2441export class CrazyGamesSDK {
2542 private initialized = false ;
2643 private isGameplayActive = false ;
44+ private readyPromise : Promise < void > ;
45+ private resolveReady ! : ( ) => void ;
46+
47+ constructor ( ) {
48+ this . readyPromise = new Promise ( ( resolve ) => {
49+ this . resolveReady = resolve ;
50+ } ) ;
51+ }
52+
53+ async ready ( ) : Promise < boolean > {
54+ const timeout = new Promise < boolean > ( ( resolve ) => {
55+ setTimeout ( ( ) => resolve ( false ) , 3000 ) ;
56+ } ) ;
57+
58+ const ready = this . readyPromise . then ( ( ) => true ) ;
59+
60+ return Promise . race ( [ ready , timeout ] ) ;
61+ }
2762
2863 isOnCrazyGames ( ) : boolean {
2964 try {
@@ -70,12 +105,29 @@ export class CrazyGamesSDK {
70105 try {
71106 await window . CrazyGames . SDK . init ( ) ;
72107 this . initialized = true ;
108+ this . resolveReady ( ) ;
73109 console . log ( "CrazyGames SDK initialized" ) ;
74110 } catch ( error ) {
75111 console . error ( "Failed to initialize CrazyGames SDK:" , error ) ;
76112 }
77113 }
78114
115+ async getUsername ( ) : Promise < string | null > {
116+ const isReady = await this . ready ( ) ;
117+ if ( ! isReady ) {
118+ return null ;
119+ }
120+ return ( await window . CrazyGames ! . SDK . user . getUser ( ) ) ?. username ?? null ;
121+ }
122+
123+ async isInstantMultiplayer ( ) : Promise < boolean > {
124+ const isReady = await this . ready ( ) ;
125+ if ( ! isReady ) {
126+ return false ;
127+ }
128+ return window . CrazyGames ! . SDK . game . isInstantMultiplayer ?? false ;
129+ }
130+
79131 async gameplayStart ( ) : Promise < void > {
80132 if ( ! this . isReady ( ) ) {
81133 return ;
@@ -200,6 +252,33 @@ export class CrazyGamesSDK {
200252 return null ;
201253 }
202254 }
255+
256+ requestMidgameAd ( ) : Promise < void > {
257+ return new Promise ( ( resolve ) => {
258+ if ( ! this . isReady ( ) ) {
259+ resolve ( ) ;
260+ return ;
261+ }
262+
263+ try {
264+ const callbacks = {
265+ adFinished : ( ) => {
266+ console . log ( "End midgame ad" ) ;
267+ resolve ( ) ;
268+ } ,
269+ adError : ( error : any ) => {
270+ console . log ( "Error midgame ad" , error ) ;
271+ resolve ( ) ;
272+ } ,
273+ adStarted : ( ) => console . log ( "Start midgame ad" ) ,
274+ } ;
275+ window . CrazyGames ! . SDK . ad . requestAd ( "midgame" , callbacks ) ;
276+ } catch ( error ) {
277+ console . error ( "Failed to request midgame ad:" , error ) ;
278+ resolve ( ) ;
279+ }
280+ } ) ;
281+ }
203282}
204283
205284export const crazyGamesSDK = new CrazyGamesSDK ( ) ;
0 commit comments