Integration
How a game runs in your app
Each game is one HTML package with everything inside. Your app opens it in a WebView, tells it where to sit and what currency to show, and listens for its events. Your client panel holds the settings and gives your app an API.
1. Open the game with its placement
The launch URL from the API already carries your panel's settings. You can also set them yourself:
| Parameter | Values | What it does |
|---|---|---|
mode | room, game | Voice room: the game sits in the lower part of the screen. Game: full screen. |
safeHeight | 75%, 633px, or design units such as 968 | The room-mode height. The game lays its whole UI out inside it. |
currency | coins, diamonds, beans, points | Currency name and icons everywhere in the game. |
sound, music | 1, 0 | Starting state of sound effects and music. |
balance | whole number | Balance to show at start. Display only; your server stays in charge. |
quality | low | Lighter rendering for slow phones. |
Apps already built for BAISHUN games can keep their logic: game_mode 2 is the voice room, 3 is full screen, safe_height is in the same design units, and walletUpdate() is answered too.
2. Load it in your WebView
Android (Kotlin)
webView.settings.javaScriptEnabled = true
webView.settings.domStorageEnabled = true
webView.settings.mediaPlaybackRequiresUserGesture = false
webView.setBackgroundColor(Color.TRANSPARENT) // voice room: your UI shows above the game
webView.addJavascriptInterface(object {
@JavascriptInterface
fun postMessage(json: String) { /* ready, balance, spinResult, topUpRequest */ }
}, "AVGameHost")
webView.loadUrl(game.launchUrl) // from GET /api/v1/games
// later, from your app
webView.evaluateJavascript("window.AVGame && AVGame.setBalance(50000)", null)iOS (Swift)
let config = WKWebViewConfiguration()
config.mediaTypesRequiringUserActionForPlayback = []
config.userContentController.add(handler, name: "avgame") // events arrive as dictionaries
let webView = WKWebView(frame: .zero, configuration: config)
webView.isOpaque = false
webView.backgroundColor = .clear
webView.load(URLRequest(url: URL(string: launchUrl)!))
webView.evaluateJavaScript("window.AVGame && AVGame.setBalance(50000)")React Native
<WebView
ref={web}
source={{ uri: launchUrl }}
style={{ backgroundColor: "transparent" }}
mediaPlaybackRequiresUserAction={false}
onMessage={e => handle(JSON.parse(e.nativeEvent.data))}
/>
web.current.injectJavaScript("window.AVGame && AVGame.setBalance(50000); true;")Web
<iframe src="LAUNCH_URL" allow="autoplay" style="border:0;width:100%;height:100%"></iframe>
<script>
addEventListener("message", e => {
if (e.data && e.data.source === "avgame") console.log(e.data.type, e.data)
})
</script>3. Talk to the game
| Your app calls | Effect |
|---|---|
AVGame.setBalance(n) | Shows the balance from your server |
AVGame.setCurrency(key) | Switches currency name and icons |
AVGame.setSound(on) | Follows your app's mute switch |
AVGame.setMusic(on) | Music on or off |
AVGame.setMode(m) | Moves between room and full screen |
AVGame.setSafeHeight(v) | Re-lays out when your room UI changes height |
AVGame.getState() | Balance, bet, level, currency, mode |
| The game sends | When |
|---|---|
ready | Loaded, with version and currency |
balance | The balance changed |
spinResult | A round finished: bet, win, balance |
topUpRequest | The player tapped + or ran out: open your store |
close | The player closed the game |
error | Something failed |
4. Get your games from the API
Your client panel shows your key. GET /api/v1/games lists the games your app has switched on, and GET /api/v1/games/{game_id} returns one. A wrong key answers code 1003; a game you have no licence for answers code 1012.
curl https://games.appverselabs.com/api/v1/games \
-H "Authorization: Bearer avk_your_key"
{
"code": 0,
"msg": "success",
"app_id": "av_3f9c2a1b7d",
"data": [
{
"game_id": "lucky777",
"name": "Lucky 777",
"game_version": "0.8.0",
"preview_url": "https://games.appverselabs.com/uploads/games/lucky777-cover.webp",
"download_url": "https://games.appverselabs.com/hosted/…/package.zip",
"launch_url": "https://games.appverselabs.com/hosted/…/index.html?mode=room&safeHeight=75%25¤cy=diamonds&sound=1&music=1",
"game_mode": [2, 3],
"default_mode": 2,
"game_orientation": 1,
"safe_height": 968,
"safe_height_percent": 75,
"currency": "diamonds",
"sound": true,
"music": true,
"enabled": true
}
]
}