-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
144 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { SDKPlatform } from '~type/sdk'; | ||
|
||
export const SDK_PLATFORMS: Record<SDKPlatform, string> = { | ||
[SDKPlatform.CRAZY_GAMES]: 'https://sdk.crazygames.com/crazygames-sdk-v2.js', | ||
[SDKPlatform.POKI]: 'https://game-cdn.poki.com/scripts/v2/poki-sdk.js', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { SDK_PLATFORMS } from '~const/sdk'; | ||
import { GameAdType } from '~type/game'; | ||
import { ISDK, SDKPlatform } from '~type/sdk'; | ||
|
||
import { registerScript } from './utils'; | ||
|
||
export class SDK implements ISDK { | ||
private platform: SDKPlatform; | ||
|
||
constructor(platform: SDKPlatform) { | ||
try { | ||
registerScript(SDK_PLATFORMS[platform]).then(() => { | ||
this.platform = platform; | ||
|
||
switch (this.platform) { | ||
case SDKPlatform.POKI: { | ||
window.PokiSDK?.init(); | ||
} | ||
} | ||
}); | ||
} catch (error) { | ||
console.error('SDK initialization error:', error); | ||
} | ||
} | ||
|
||
public showAdv( | ||
type: GameAdType, | ||
callbackBeg: () => void, | ||
callbackEnd: (success: boolean) => void, | ||
) { | ||
try { | ||
switch (this.platform) { | ||
case SDKPlatform.CRAZY_GAMES: { | ||
window.CrazyGames?.SDK?.ad?.requestAd(type, { | ||
adStarted: callbackBeg, | ||
adFinished: () => callbackEnd(true), | ||
}); | ||
break; | ||
} | ||
case SDKPlatform.POKI: { | ||
const method = type === GameAdType.REWARDED ? 'rewardedBreak' : 'commercialBreak'; | ||
|
||
window.PokiSDK?.[method](callbackBeg).then((success: boolean) => { | ||
callbackEnd(success); | ||
}); | ||
break; | ||
} | ||
} | ||
} catch (error) { | ||
console.error('SDK Show adv error:', error); | ||
} | ||
} | ||
|
||
// public toggleLoadState(state: boolean) { | ||
// switch (this.platform) { | ||
// case SDKPlatform.CRAZY_GAMES: { | ||
// if (state) { | ||
// window.CrazyGames?.SDK?.game?.sdkGameLoadingStart(); | ||
// } else { | ||
// window.CrazyGames?.SDK?.game?.sdkGameLoadingStop(); | ||
// } | ||
// break; | ||
// } | ||
// case SDKPlatform.POKI: { | ||
// if (!state) { | ||
// window.PokiSDK?.gameLoadingFinished(); | ||
// } | ||
// break; | ||
// } | ||
// } | ||
// } | ||
|
||
// public togglePlayState(state: boolean) { | ||
// switch (this.platform) { | ||
// case SDKPlatform.POKI: { | ||
// if (state) { | ||
// window.PokiSDK.gameplayStart(); | ||
// } else { | ||
// window.PokiSDK.gameplayStop(); | ||
// } | ||
// } | ||
// } | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { GameAdType } from './game'; | ||
|
||
export interface ISDK { | ||
/** | ||
* Show advertising. | ||
* @param type - Ad type | ||
* @param callbackBeg - Start callback | ||
* @param callbackEnd - Complete callback | ||
*/ | ||
showAdv( | ||
type: GameAdType, | ||
callbackBeg: () => void, | ||
callbackEnd: (success: boolean) => void | ||
): void | ||
} | ||
|
||
export enum SDKPlatform { | ||
CRAZY_GAMES = 'CRAZY_GAMES', | ||
POKI = 'POKI', | ||
} | ||
|
||
declare global { | ||
interface Window { | ||
PokiSDK?: any | ||
CrazyGames?: any | ||
} | ||
} |