Skip to content

Commit a0db8fa

Browse files
committed
Support standalone platform capability detection
- Detect standalone via `platform_id` only - Respect Playgama host feature flags for auth, cloud save, and payments - Update bridge tests for standalone optional services
1 parent 8608a0a commit a0db8fa

8 files changed

Lines changed: 129 additions & 50 deletions

File tree

src/PlaygamaBridge.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,6 @@ class PlaygamaBridge {
269269
platformId = this.#getPlatformId(configFileModule.options.forciblySetPlatformId.toLowerCase())
270270
} else if (url.searchParams.has('platform_id')) {
271271
platformId = this.#getPlatformId(url.searchParams.get('platform_id').toLowerCase())
272-
} else if (__INCLUDE_STANDALONE__ && url.searchParams.get('platform') === PLATFORM_ID.STANDALONE) {
273-
platformId = PLATFORM_ID.STANDALONE
274272
} else if (__INCLUDE_YANDEX__ && (url.hostname.includes(['y', 'a', 'n', 'd', 'e', 'x', '.', 'n', 'e', 't'].join('')) || url.hash.includes('yandex'))) {
275273
platformId = PLATFORM_ID.YANDEX
276274
} else if (__INCLUDE_CRAZY_GAMES__ && (url.hostname.includes('crazygames.') || url.hostname.includes('1001juegos.com'))) {

src/platform-bridges/PlaygamaPlatformBridge.js

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
6666

6767
// player
6868
get isPlayerAuthorizationSupported() {
69-
return true
69+
return this.#isPlayerAuthorizationSupported
7070
}
7171

7272
// payments
@@ -76,8 +76,12 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
7676

7777
_isAdvancedBannersSupported = true
7878

79+
#isCloudSaveSupported = true
80+
7981
#isPaymentsSupported = true
8082

83+
#isPlayerAuthorizationSupported = true
84+
8185
initialize() {
8286
if (this._isInitialized) {
8387
return Promise.resolve()
@@ -142,20 +146,21 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
142146
}
143147
})
144148

145-
Promise.all([
146-
this.#getPlayer(),
147-
this._platformSdk.platformService?.isReady ?? Promise.resolve(),
148-
]).then(() => {
149-
if (this._platformSdk.platformService?.getIsPaymentsSupported) {
150-
this.#isPaymentsSupported = this._platformSdk.platformService.getIsPaymentsSupported()
151-
}
152-
this._isInitialized = true
153-
this._resolvePromiseDecorator(ACTION_NAME.INITIALIZE)
154-
})
149+
const platformReadyPromise = this._platformSdk.platformService?.isReady ?? Promise.resolve()
150+
platformReadyPromise
151+
.then(() => {
152+
this.#resolveSupportedFeatures()
155153

156-
if (this._platformSdk.platformService?.getAdditionalParams) {
157-
this._additionalData = this._platformSdk.platformService.getAdditionalParams() || {}
158-
}
154+
if (this._platformSdk.platformService?.getAdditionalParams) {
155+
this._additionalData = this._platformSdk.platformService.getAdditionalParams() || {}
156+
}
157+
158+
return this.#getPlayer()
159+
})
160+
.then(() => {
161+
this._isInitialized = true
162+
this._resolvePromiseDecorator(ACTION_NAME.INITIALIZE)
163+
})
159164
})
160165
})
161166
}
@@ -179,22 +184,26 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
179184
// storage
180185
isStorageSupported(storageType) {
181186
if (storageType === STORAGE_TYPE.PLATFORM_INTERNAL) {
182-
return true
187+
return this.#isCloudSaveSupported
183188
}
184189

185190
return super.isStorageSupported(storageType)
186191
}
187192

188193
isStorageAvailable(storageType) {
189194
if (storageType === STORAGE_TYPE.PLATFORM_INTERNAL) {
190-
return this._isPlayerAuthorized
195+
return this.#isCloudSaveSupported && this._isPlayerAuthorized
191196
}
192197

193198
return super.isStorageAvailable(storageType)
194199
}
195200

196201
getDataFromStorage(key, storageType, tryParseJson) {
197202
if (storageType === STORAGE_TYPE.PLATFORM_INTERNAL) {
203+
if (!this.#isCloudSaveSupported) {
204+
return Promise.reject(ERROR.STORAGE_NOT_SUPPORTED)
205+
}
206+
198207
if (!this._isPlayerAuthorized) {
199208
return Promise.reject()
200209
}
@@ -208,6 +217,10 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
208217
setDataToStorage(key, value, storageType) {
209218
switch (storageType) {
210219
case STORAGE_TYPE.PLATFORM_INTERNAL: {
220+
if (!this.#isCloudSaveSupported) {
221+
return Promise.reject(ERROR.STORAGE_NOT_SUPPORTED)
222+
}
223+
211224
if (!this._isPlayerAuthorized) {
212225
return Promise.reject()
213226
}
@@ -257,6 +270,10 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
257270
deleteDataFromStorage(key, storageType) {
258271
switch (storageType) {
259272
case STORAGE_TYPE.PLATFORM_INTERNAL: {
273+
if (!this.#isCloudSaveSupported) {
274+
return Promise.reject(ERROR.STORAGE_NOT_SUPPORTED)
275+
}
276+
260277
return new Promise((resolve, reject) => {
261278
const data = this._platformStorageCachedData !== null
262279
? { ...this._platformStorageCachedData }
@@ -321,6 +338,10 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
321338
}
322339

323340
authorizePlayer(options) {
341+
if (!this.#isPlayerAuthorizationSupported) {
342+
return Promise.reject()
343+
}
344+
324345
let promiseDecorator = this._getPromiseDecorator(ACTION_NAME.AUTHORIZE_PLAYER)
325346
if (!promiseDecorator) {
326347
promiseDecorator = this._createPromiseDecorator(ACTION_NAME.AUTHORIZE_PLAYER)
@@ -349,6 +370,10 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
349370

350371
// payments
351372
paymentsPurchase(id, options) {
373+
if (!this.isPaymentsSupported) {
374+
return Promise.reject()
375+
}
376+
352377
const product = this._paymentsGetProductPlatformData(id)
353378
if (!product) {
354379
return Promise.reject()
@@ -469,6 +494,11 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
469494
}
470495

471496
#getPlayer() {
497+
if (!this.#isPlayerAuthorizationSupported) {
498+
this._playerApplyGuestData()
499+
return Promise.resolve()
500+
}
501+
472502
return new Promise((resolve) => {
473503
this._platformSdk.userService.getUser()
474504
.then((player) => {
@@ -478,8 +508,12 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
478508
this._playerName = player.name
479509
this._playerPhotos = player.photos
480510
this._playerExtra = player
481-
this._defaultStorageType = STORAGE_TYPE.PLATFORM_INTERNAL
482-
return this.#getDataFromPlatformStorage([])
511+
if (this.#isCloudSaveSupported) {
512+
this._defaultStorageType = STORAGE_TYPE.PLATFORM_INTERNAL
513+
return this.#getDataFromPlatformStorage([])
514+
}
515+
516+
return Promise.resolve()
483517
}
484518

485519
this._playerApplyGuestData()
@@ -501,6 +535,20 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
501535

502536
return getKeysFromObject(key, this._platformStorageCachedData, tryParseJson)
503537
}
538+
539+
#resolveSupportedFeatures() {
540+
if (this._platformSdk.platformService?.getIsPlayerAuthorizationSupported) {
541+
this.#isPlayerAuthorizationSupported = this._platformSdk.platformService.getIsPlayerAuthorizationSupported()
542+
}
543+
544+
if (this._platformSdk.platformService?.getIsCloudSaveSupported) {
545+
this.#isCloudSaveSupported = this._platformSdk.platformService.getIsCloudSaveSupported()
546+
}
547+
548+
if (this._platformSdk.platformService?.getIsPaymentsSupported) {
549+
this.#isPaymentsSupported = this._platformSdk.platformService.getIsPaymentsSupported()
550+
}
551+
}
504552
}
505553

506554
export default PlaygamaPlatformBridge

src/platform-bridges/StandalonePlatformBridge.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ class StandalonePlatformBridge extends PlaygamaPlatformBridge {
3535
return true
3636
}
3737

38-
get isPaymentsSupported() {
39-
return true
40-
}
41-
4238
_isAdvancedBannersSupported = false
4339
}
4440

tests/common/bridge/bridge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async function createBridge(options: BridgeOptions = {}): Promise<CreateBridgeRe
2929
const stateManager = new StateManager()
3030
const bridge = new PlaygamaBridge()
3131

32-
await createPlaygamaSdk(testGlobal, stateManager)
32+
await createPlaygamaSdk(testGlobal, stateManager, mergedOptions.playgamaCapabilities)
3333
await createQaToolSdk(testGlobal, stateManager)
3434
await AbsoluteGamesSdkEmulator.create(testGlobal)
3535

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import type PlaygamaBridge from '../../../src/PlaygamaBridge'
22
import type { StateManager } from '../stateManager/stateManager'
33

4-
export interface BridgeOptions {
5-
supportedFeatures?: string[]
6-
bridgeOptions?: Record<string, unknown>
7-
internalStoragePolicy?: string
8-
}
4+
export interface BridgeOptions {
5+
supportedFeatures?: string[]
6+
bridgeOptions?: Record<string, unknown>
7+
internalStoragePolicy?: string
8+
playgamaCapabilities?: {
9+
playerAuthorization?: boolean
10+
cloudSave?: boolean
11+
payments?: boolean
12+
}
13+
}
914

1015
export interface CreateBridgeResult {
1116
bridge: PlaygamaBridge
@@ -15,4 +20,4 @@ export interface CreateBridgeResult {
1520
export const defaultOptions: BridgeOptions = {
1621
supportedFeatures: [],
1722
bridgeOptions: {},
18-
}
23+
}

tests/common/playgama/playgama.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ import type { TestGlobalThis } from '../../common/types'
33
import { StateManager } from '../stateManager/stateManager'
44
import type { PlaygamaPlayer, PlaygamaSdk } from './playgama.types'
55

6-
export function createPlaygamaSdk(
7-
testGlobalThis: TestGlobalThis,
8-
stateManager: StateManager
9-
): Promise<void> {
6+
export function createPlaygamaSdk(
7+
testGlobalThis: TestGlobalThis,
8+
stateManager: StateManager,
9+
capabilities: {
10+
playerAuthorization?: boolean
11+
cloudSave?: boolean
12+
payments?: boolean
13+
} = {},
14+
): Promise<void> {
1015

1116
const userService = {
1217
getUser(): Promise<PlaygamaPlayer> {
@@ -42,11 +47,13 @@ export function createPlaygamaSdk(
4247
}
4348

4449

45-
const platformService = {
46-
isReady: Promise.resolve(),
47-
getIsPaymentsSupported: vi.fn().mockReturnValue(false),
48-
getAdditionalParams: vi.fn().mockReturnValue({}),
49-
}
50+
const platformService = {
51+
isReady: Promise.resolve(),
52+
getIsPlayerAuthorizationSupported: vi.fn().mockReturnValue(capabilities.playerAuthorization ?? true),
53+
getIsCloudSaveSupported: vi.fn().mockReturnValue(capabilities.cloudSave ?? true),
54+
getIsPaymentsSupported: vi.fn().mockReturnValue(capabilities.payments ?? false),
55+
getAdditionalParams: vi.fn().mockReturnValue({}),
56+
}
5057

5158
const sdk = {
5259
userService,

tests/common/playgama/playgama.types.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,23 @@ export interface PlaygamaPlayer {
88
extra: Record<string, unknown>
99
}
1010

11-
export interface PlaygamaSdk {
11+
export interface PlaygamaSdk {
1212
userService: {
1313
getUser: () => Promise<PlaygamaPlayer>
1414
authorizeUser: () => Promise<boolean>
1515
}
1616
advService: {
1717
subscribeToAdStateChanges: ReturnType<typeof vi.fn>
1818
}
19-
cloudSaveApi: {
20-
getState: () => Promise<Record<string, unknown>>
21-
setItems: (items: Record<string, unknown>) => Promise<void>
22-
}
23-
}
19+
cloudSaveApi: {
20+
getState: () => Promise<Record<string, unknown>>
21+
setItems: (items: Record<string, unknown>) => Promise<void>
22+
}
23+
platformService: {
24+
isReady: Promise<void>
25+
getIsPlayerAuthorizationSupported: ReturnType<typeof vi.fn>
26+
getIsCloudSaveSupported: ReturnType<typeof vi.fn>
27+
getIsPaymentsSupported: ReturnType<typeof vi.fn>
28+
getAdditionalParams: ReturnType<typeof vi.fn>
29+
}
30+
}

tests/src/bridge/initialize.e2e.spec.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, test, expect } from 'vitest'
2-
import { PLATFORM_ID } from '../../../src/constants'
2+
import { PLATFORM_ID, STORAGE_TYPE } from '../../../src/constants'
33
import { createBridgeByPlatformId, createBridgeByUrl } from '../../common/bridge/bridge'
44

55
describe('initialize (integration, PlaygamaBridge)', () => {
@@ -22,8 +22,26 @@ describe('initialize (integration, PlaygamaBridge)', () => {
2222
expect(bridge.platform.id).toBe(platformId)
2323
})
2424

25-
test('Initialize standalone by platform query parameter', async () => {
26-
const { bridge } = await createBridgeByUrl(`http://localhost/?platform=${PLATFORM_ID.STANDALONE}`)
25+
test('Initialize standalone by platform_id query parameter', async () => {
26+
const { bridge } = await createBridgeByUrl(`http://localhost/?platform_id=${PLATFORM_ID.STANDALONE}`)
2727
expect(bridge.platform.id).toBe(PLATFORM_ID.STANDALONE)
2828
})
29-
})
29+
30+
test('Initialize standalone with optional host services disabled', async () => {
31+
const { bridge } = await createBridgeByUrl(
32+
`http://localhost/?platform_id=${PLATFORM_ID.STANDALONE}`,
33+
{
34+
playgamaCapabilities: {
35+
playerAuthorization: false,
36+
cloudSave: false,
37+
payments: false,
38+
},
39+
},
40+
)
41+
42+
expect(bridge.player.isAuthorizationSupported).toBe(false)
43+
expect(bridge.storage.isSupported(STORAGE_TYPE.PLATFORM_INTERNAL)).toBe(false)
44+
expect(bridge.storage.defaultType).toBe(STORAGE_TYPE.LOCAL_STORAGE)
45+
expect(bridge.payments.isSupported).toBe(false)
46+
})
47+
})

0 commit comments

Comments
 (0)