Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const PLATFORM_ID = {
ABSOLUTE_GAMES: 'absolute_games',
GAME_DISTRIBUTION: 'game_distribution',
PLAYGAMA: 'playgama',
STANDALONE: 'standalone',
PLAYDECK: 'playdeck',
TELEGRAM: 'telegram',
Y8: 'y8',
Expand Down
118 changes: 88 additions & 30 deletions src/platform-bridges/PlaygamaPlatformBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,28 @@ import {
PLATFORM_MESSAGE,
} from '../constants'

const SDK_URL = 'https://playgama.com/platform-sdk/v1.js'

class PlaygamaPlatformBridge extends PlatformBridgeBase {
// platform
get platformId() {
return PLATFORM_ID.PLAYGAMA
}

get sdkUrl() {
return 'https://playgama.com/platform-sdk/v1.js'
}

get sdkGlobalName() {
return 'PLAYGAMA_SDK'
}

get platformSdk() {
return window[this.sdkGlobalName]
}

get platformLanguage() {
return this._platformSdk?.platformService?.getLanguage?.() || super.platformLanguage
}

// advertisement
get isInterstitialSupported() {
return true
Expand All @@ -52,22 +66,22 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {

// player
get isPlayerAuthorizationSupported() {
return true
return this.#isPlayerAuthorizationSupported
}

// payments
get isPaymentsSupported() {
return this.#isPaymentsSupported
}

get platformLanguage() {
return this._platformSdk.platformService.getLanguage() || super.platformLanguage
}

_isAdvancedBannersSupported = true

#isCloudSaveSupported = true

#isPaymentsSupported = true

#isPlayerAuthorizationSupported = true

initialize() {
if (this._isInitialized) {
return Promise.resolve()
Expand All @@ -76,9 +90,9 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
if (!promiseDecorator) {
promiseDecorator = this._createPromiseDecorator(ACTION_NAME.INITIALIZE)

addJavaScript(SDK_URL).then(() => {
waitFor('PLAYGAMA_SDK').then(() => {
this._platformSdk = window.PLAYGAMA_SDK
addJavaScript(this.sdkUrl).then(() => {
waitFor(this.sdkGlobalName).then(() => {
this._platformSdk = this.platformSdk

this._platformSdk.advService.subscribeToAdStateChanges((adType, state) => {
if (adType === 'interstitial') {
Expand Down Expand Up @@ -132,20 +146,21 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
}
})

Promise.all([
this.#getPlayer(),
this._platformSdk.platformService?.isReady,
]).then(() => {
if (this._platformSdk.platformService?.getIsPaymentsSupported) {
this.#isPaymentsSupported = this._platformSdk.platformService.getIsPaymentsSupported()
}
this._isInitialized = true
this._resolvePromiseDecorator(ACTION_NAME.INITIALIZE)
})
const platformReadyPromise = this._platformSdk.platformService?.isReady ?? Promise.resolve()
platformReadyPromise
.then(() => {
this.#resolveSupportedFeatures()

if (this._platformSdk.platformService.getAdditionalParams) {
this._additionalData = this._platformSdk.platformService.getAdditionalParams() || {}
}
if (this._platformSdk.platformService?.getAdditionalParams) {
this._additionalData = this._platformSdk.platformService.getAdditionalParams() || {}
}

return this.#getPlayer()
})
.then(() => {
this._isInitialized = true
this._resolvePromiseDecorator(ACTION_NAME.INITIALIZE)
})
})
})
}
Expand All @@ -157,7 +172,7 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
sendMessage(message) {
switch (message) {
case PLATFORM_MESSAGE.GAME_READY: {
this._platformSdk.gameService.gameReady()
this._platformSdk.gameService?.gameReady?.()
return Promise.resolve()
}
default: {
Expand All @@ -169,22 +184,26 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
// storage
isStorageSupported(storageType) {
if (storageType === STORAGE_TYPE.PLATFORM_INTERNAL) {
return true
return this.#isCloudSaveSupported
}

return super.isStorageSupported(storageType)
}

isStorageAvailable(storageType) {
if (storageType === STORAGE_TYPE.PLATFORM_INTERNAL) {
return this._isPlayerAuthorized
return this.#isCloudSaveSupported && this._isPlayerAuthorized
}

return super.isStorageAvailable(storageType)
}

getDataFromStorage(key, storageType, tryParseJson) {
if (storageType === STORAGE_TYPE.PLATFORM_INTERNAL) {
if (!this.#isCloudSaveSupported) {
return Promise.reject(ERROR.STORAGE_NOT_SUPPORTED)
}

if (!this._isPlayerAuthorized) {
return Promise.reject()
}
Expand All @@ -198,6 +217,10 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
setDataToStorage(key, value, storageType) {
switch (storageType) {
case STORAGE_TYPE.PLATFORM_INTERNAL: {
if (!this.#isCloudSaveSupported) {
return Promise.reject(ERROR.STORAGE_NOT_SUPPORTED)
}

if (!this._isPlayerAuthorized) {
return Promise.reject()
}
Expand Down Expand Up @@ -235,7 +258,7 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
data[key] = (typeof value !== 'string') ? JSON.stringify(value) : value
}

this._platformSdk.storageApi.setItems(data)
this._platformSdk.storageApi?.setItems?.(data)
return super.setDataToStorage(key, value, storageType)
}
default: {
Expand All @@ -247,6 +270,10 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
deleteDataFromStorage(key, storageType) {
switch (storageType) {
case STORAGE_TYPE.PLATFORM_INTERNAL: {
if (!this.#isCloudSaveSupported) {
return Promise.reject(ERROR.STORAGE_NOT_SUPPORTED)
}

return new Promise((resolve, reject) => {
const data = this._platformStorageCachedData !== null
? { ...this._platformStorageCachedData }
Expand All @@ -271,7 +298,7 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
})
}
case STORAGE_TYPE.LOCAL_STORAGE: {
this._platformSdk.storageApi.deleteItems(Array.isArray(key) ? key : [key])
this._platformSdk.storageApi?.deleteItems?.(Array.isArray(key) ? key : [key])
return super.deleteDataFromStorage(key, storageType)
}
default: {
Expand Down Expand Up @@ -311,6 +338,10 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
}

authorizePlayer(options) {
if (!this.#isPlayerAuthorizationSupported) {
return Promise.reject()
}

let promiseDecorator = this._getPromiseDecorator(ACTION_NAME.AUTHORIZE_PLAYER)
if (!promiseDecorator) {
promiseDecorator = this._createPromiseDecorator(ACTION_NAME.AUTHORIZE_PLAYER)
Expand Down Expand Up @@ -339,6 +370,10 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {

// payments
paymentsPurchase(id, options) {
if (!this.isPaymentsSupported) {
return Promise.reject()
}

const product = this._paymentsGetProductPlatformData(id)
if (!product) {
return Promise.reject()
Expand Down Expand Up @@ -459,6 +494,11 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
}

#getPlayer() {
if (!this.#isPlayerAuthorizationSupported) {
this._playerApplyGuestData()
return Promise.resolve()
}

return new Promise((resolve) => {
this._platformSdk.userService.getUser()
.then((player) => {
Expand All @@ -468,8 +508,12 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
this._playerName = player.name
this._playerPhotos = player.photos
this._playerExtra = player
this._defaultStorageType = STORAGE_TYPE.PLATFORM_INTERNAL
return this.#getDataFromPlatformStorage([])
if (this.#isCloudSaveSupported) {
this._defaultStorageType = STORAGE_TYPE.PLATFORM_INTERNAL
return this.#getDataFromPlatformStorage([])
}

return Promise.resolve()
}

this._playerApplyGuestData()
Expand All @@ -491,6 +535,20 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {

return getKeysFromObject(key, this._platformStorageCachedData, tryParseJson)
}

#resolveSupportedFeatures() {
if (this._platformSdk.platformService?.getIsPlayerAuthorizationSupported) {
this.#isPlayerAuthorizationSupported = this._platformSdk.platformService.getIsPlayerAuthorizationSupported()
}

if (this._platformSdk.platformService?.getIsCloudSaveSupported) {
this.#isCloudSaveSupported = this._platformSdk.platformService.getIsCloudSaveSupported()
}

if (this._platformSdk.platformService?.getIsPaymentsSupported) {
this.#isPaymentsSupported = this._platformSdk.platformService.getIsPaymentsSupported()
}
}
}

export default PlaygamaPlatformBridge
41 changes: 41 additions & 0 deletions src/platform-bridges/StandalonePlatformBridge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This file is part of Playgama Bridge.
*
* Playgama Bridge is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Playgama Bridge is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Playgama Bridge. If not, see <https://www.gnu.org/licenses/>.
*/

import PlaygamaPlatformBridge from './PlaygamaPlatformBridge'
import { PLATFORM_ID } from '../constants'

class StandalonePlatformBridge extends PlaygamaPlatformBridge {
get platformId() {
return PLATFORM_ID.STANDALONE
}

get sdkUrl() {
return 'https://playgama.com/platform-sdk/wrap.v1.js'
}

get sdkGlobalName() {
return 'PLAYGAMA_WRAP'
}

get isExternalLinksAllowed() {
return true
}

_isAdvancedBannersSupported = false
}

export default StandalonePlatformBridge
3 changes: 3 additions & 0 deletions src/platformImports.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ if (__INCLUDE_GAME_DISTRIBUTION__) {
if (__INCLUDE_PLAYGAMA__) {
platformImports[PLATFORM_ID.PLAYGAMA] = () => import('./platform-bridges/PlaygamaPlatformBridge')
}
if (__INCLUDE_STANDALONE__) {
platformImports[PLATFORM_ID.STANDALONE] = () => import('./platform-bridges/StandalonePlatformBridge')
}
if (__INCLUDE_PLAYDECK__) {
platformImports[PLATFORM_ID.PLAYDECK] = () => import('./platform-bridges/PlayDeckPlatformBridge')
}
Expand Down
2 changes: 1 addition & 1 deletion tests/common/bridge/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function createBridge(options: BridgeOptions = {}): Promise<CreateBridgeRe
const stateManager = new StateManager()
const bridge = new PlaygamaBridge()

await createPlaygamaSdk(testGlobal, stateManager)
await createPlaygamaSdk(testGlobal, stateManager, mergedOptions.playgamaCapabilities)
await createQaToolSdk(testGlobal, stateManager)
await AbsoluteGamesSdkEmulator.create(testGlobal)

Expand Down
17 changes: 11 additions & 6 deletions tests/common/bridge/bridge.types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import type PlaygamaBridge from '../../../src/PlaygamaBridge'
import type { StateManager } from '../stateManager/stateManager'

export interface BridgeOptions {
supportedFeatures?: string[]
bridgeOptions?: Record<string, unknown>
internalStoragePolicy?: string
}
export interface BridgeOptions {
supportedFeatures?: string[]
bridgeOptions?: Record<string, unknown>
internalStoragePolicy?: string
playgamaCapabilities?: {
playerAuthorization?: boolean
cloudSave?: boolean
payments?: boolean
}
}

export interface CreateBridgeResult {
bridge: PlaygamaBridge
Expand All @@ -15,4 +20,4 @@ export interface CreateBridgeResult {
export const defaultOptions: BridgeOptions = {
supportedFeatures: [],
bridgeOptions: {},
}
}
Loading
Loading