Skip to content

Commit ffb5e37

Browse files
committed
feat: Added standalone platform support
1 parent 31dcf72 commit ffb5e37

8 files changed

Lines changed: 92 additions & 21 deletions

File tree

src/PlaygamaBridge.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ class PlaygamaBridge {
276276
platformId = this.#getPlatformId(configFileModule.options.forciblySetPlatformId.toLowerCase())
277277
} else if (url.searchParams.has('platform_id')) {
278278
platformId = this.#getPlatformId(url.searchParams.get('platform_id').toLowerCase())
279+
} else if (__INCLUDE_STANDALONE__ && url.searchParams.get('platform') === PLATFORM_ID.STANDALONE) {
280+
platformId = PLATFORM_ID.STANDALONE
279281
} else if (__INCLUDE_YANDEX__ && (url.hostname.includes(['y', 'a', 'n', 'd', 'e', 'x', '.', 'n', 'e', 't'].join('')) || url.hash.includes('yandex'))) {
280282
platformId = PLATFORM_ID.YANDEX
281283
} else if (__INCLUDE_CRAZY_GAMES__ && (url.hostname.includes('crazygames.') || url.hostname.includes('1001juegos.com'))) {

src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const PLATFORM_ID = {
2626
ABSOLUTE_GAMES: 'absolute_games',
2727
GAME_DISTRIBUTION: 'game_distribution',
2828
PLAYGAMA: 'playgama',
29+
STANDALONE: 'standalone',
2930
PLAYDECK: 'playdeck',
3031
TELEGRAM: 'telegram',
3132
Y8: 'y8',

src/platform-bridges/PlaygamaPlatformBridge.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,28 @@ import {
2828
PLATFORM_MESSAGE,
2929
} from '../constants'
3030

31-
const SDK_URL = 'https://playgama.com/platform-sdk/v1.js'
32-
3331
class PlaygamaPlatformBridge extends PlatformBridgeBase {
3432
// platform
3533
get platformId() {
3634
return PLATFORM_ID.PLAYGAMA
3735
}
3836

37+
get sdkUrl() {
38+
return 'https://playgama.com/platform-sdk/v1.js'
39+
}
40+
41+
get sdkGlobalName() {
42+
return 'PLAYGAMA_SDK'
43+
}
44+
45+
get platformSdk() {
46+
return window[this.sdkGlobalName]
47+
}
48+
49+
get platformLanguage() {
50+
return this._platformSdk?.platformService?.getLanguage?.() || super.platformLanguage
51+
}
52+
3953
// advertisement
4054
get isInterstitialSupported() {
4155
return true
@@ -60,10 +74,6 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
6074
return this.#isPaymentsSupported
6175
}
6276

63-
get platformLanguage() {
64-
return this._platformSdk.platformService.getLanguage() || super.platformLanguage
65-
}
66-
6777
_isAdvancedBannersSupported = true
6878

6979
#isPaymentsSupported = true
@@ -76,9 +86,9 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
7686
if (!promiseDecorator) {
7787
promiseDecorator = this._createPromiseDecorator(ACTION_NAME.INITIALIZE)
7888

79-
addJavaScript(SDK_URL).then(() => {
80-
waitFor('PLAYGAMA_SDK').then(() => {
81-
this._platformSdk = window.PLAYGAMA_SDK
89+
addJavaScript(this.sdkUrl).then(() => {
90+
waitFor(this.sdkGlobalName).then(() => {
91+
this._platformSdk = this.platformSdk
8292

8393
this._platformSdk.advService.subscribeToAdStateChanges((adType, state) => {
8494
if (adType === 'interstitial') {
@@ -134,7 +144,7 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
134144

135145
Promise.all([
136146
this.#getPlayer(),
137-
this._platformSdk.platformService?.isReady,
147+
this._platformSdk.platformService?.isReady ?? Promise.resolve(),
138148
]).then(() => {
139149
if (this._platformSdk.platformService?.getIsPaymentsSupported) {
140150
this.#isPaymentsSupported = this._platformSdk.platformService.getIsPaymentsSupported()
@@ -143,7 +153,7 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
143153
this._resolvePromiseDecorator(ACTION_NAME.INITIALIZE)
144154
})
145155

146-
if (this._platformSdk.platformService.getAdditionalParams) {
156+
if (this._platformSdk.platformService?.getAdditionalParams) {
147157
this._additionalData = this._platformSdk.platformService.getAdditionalParams() || {}
148158
}
149159
})
@@ -157,7 +167,7 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
157167
sendMessage(message) {
158168
switch (message) {
159169
case PLATFORM_MESSAGE.GAME_READY: {
160-
this._platformSdk.gameService.gameReady()
170+
this._platformSdk.gameService?.gameReady?.()
161171
return Promise.resolve()
162172
}
163173
default: {
@@ -235,7 +245,7 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
235245
data[key] = (typeof value !== 'string') ? JSON.stringify(value) : value
236246
}
237247

238-
this._platformSdk.storageApi.setItems(data)
248+
this._platformSdk.storageApi?.setItems?.(data)
239249
return super.setDataToStorage(key, value, storageType)
240250
}
241251
default: {
@@ -271,7 +281,7 @@ class PlaygamaPlatformBridge extends PlatformBridgeBase {
271281
})
272282
}
273283
case STORAGE_TYPE.LOCAL_STORAGE: {
274-
this._platformSdk.storageApi.deleteItems(Array.isArray(key) ? key : [key])
284+
this._platformSdk.storageApi?.deleteItems?.(Array.isArray(key) ? key : [key])
275285
return super.deleteDataFromStorage(key, storageType)
276286
}
277287
default: {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of Playgama Bridge.
3+
*
4+
* Playgama Bridge is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* any later version.
8+
*
9+
* Playgama Bridge is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with Playgama Bridge. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
import PlaygamaPlatformBridge from './PlaygamaPlatformBridge'
19+
import { PLATFORM_ID } from '../constants'
20+
21+
class StandalonePlatformBridge extends PlaygamaPlatformBridge {
22+
get platformId() {
23+
return PLATFORM_ID.STANDALONE
24+
}
25+
26+
get sdkUrl() {
27+
return 'https://playgama.com/platform-sdk/wrap.v1.js'
28+
}
29+
30+
get sdkGlobalName() {
31+
return 'PLAYGAMA_WRAP'
32+
}
33+
34+
get isExternalLinksAllowed() {
35+
return true
36+
}
37+
38+
get isPaymentsSupported() {
39+
return true
40+
}
41+
42+
_isAdvancedBannersSupported = false
43+
}
44+
45+
export default StandalonePlatformBridge

src/platformImports.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ if (__INCLUDE_GAME_DISTRIBUTION__) {
4141
if (__INCLUDE_PLAYGAMA__) {
4242
platformImports[PLATFORM_ID.PLAYGAMA] = () => import('./platform-bridges/PlaygamaPlatformBridge')
4343
}
44+
if (__INCLUDE_STANDALONE__) {
45+
platformImports[PLATFORM_ID.STANDALONE] = () => import('./platform-bridges/StandalonePlatformBridge')
46+
}
4447
if (__INCLUDE_PLAYDECK__) {
4548
platformImports[PLATFORM_ID.PLAYDECK] = () => import('./platform-bridges/PlayDeckPlatformBridge')
4649
}

tests/common/playgama/playgama.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ export function createPlaygamaSdk(
4848
getAdditionalParams: vi.fn().mockReturnValue({}),
4949
}
5050

51-
testGlobalThis.PLAYGAMA_SDK = {
52-
userService,
53-
advService,
54-
cloudSaveApi,
55-
platformService,
56-
}
51+
const sdk = {
52+
userService,
53+
advService,
54+
cloudSaveApi,
55+
platformService,
56+
}
57+
58+
testGlobalThis.PLAYGAMA_SDK = sdk
59+
testGlobalThis.PLAYGAMA_WRAP = sdk
5760

5861
return Promise.resolve()
5962
}

tests/common/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export type TestGlobalThis = Omit<typeof globalThis, 'addEventListener' | 'remov
66
fetch?: ReturnType<typeof vi.fn>
77
location?: Location
88
PLAYGAMA_SDK?: PlaygamaSdk
9+
PLAYGAMA_WRAP?: PlaygamaSdk
910
AgRuSdkMethods?: {
1011
ShowCampaign: string
1112
}
@@ -22,4 +23,4 @@ export type TestGlobalThis = Omit<typeof globalThis, 'addEventListener' | 'remov
2223
parent?: {
2324
postMessage: (message: unknown, target: string) => void
2425
}
25-
}
26+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ describe('initialize (integration, PlaygamaBridge)', () => {
1515
PLATFORM_ID.MOCK,
1616
PLATFORM_ID.QA_TOOL,
1717
PLATFORM_ID.PLAYGAMA,
18+
PLATFORM_ID.STANDALONE,
1819
PLATFORM_ID.ABSOLUTE_GAMES
1920
])('Initialize by platform_id query parameter %s', async (platformId: string) => {
2021
const { bridge } = await createBridgeByUrl(`http://localhost/?platform_id=${platformId}`)
2122
expect(bridge.platform.id).toBe(platformId)
2223
})
24+
25+
test('Initialize standalone by platform query parameter', async () => {
26+
const { bridge } = await createBridgeByUrl(`http://localhost/?platform=${PLATFORM_ID.STANDALONE}`)
27+
expect(bridge.platform.id).toBe(PLATFORM_ID.STANDALONE)
28+
})
2329
})

0 commit comments

Comments
 (0)