Skip to content

Commit

Permalink
gathering more metrics (#1417)
Browse files Browse the repository at this point in the history
  • Loading branch information
cajames authored Jan 25, 2024
1 parent be43068 commit a784cce
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 33 deletions.
2 changes: 1 addition & 1 deletion packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class ImmutableConfiguration {
constructor(options: { environment: Environment }) {
this.environment = options.environment;
setEnvironment(options.environment);
track('config', 'load_imtbl_config');
track('config', 'created_imtbl_config');
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/game-bridge/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": ["../../.eslintrc"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json",
"tsconfigRootDir": "."
}
}
3 changes: 3 additions & 0 deletions packages/game-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
"version": "0.0.0",
"dependencies": {
"@imtbl/config": "0.0.0",
"@imtbl/metrics": "0.0.0",
"@imtbl/passport": "0.0.0",
"@imtbl/version-check": "0.0.0",
"@imtbl/x-client": "0.0.0",
"@imtbl/x-provider": "0.0.0"
},
"devDependencies": {
"eslint": "^8.40.0",
"parcel": "^2.8.3"
},
"scripts": {
"build": "yarn build:sdk && parcel build --no-cache --no-scope-hoist && yarn updateSdkVersion",
"build:sdk": "cd ../.. && yarn build",
"lint": "eslint ./src --ext .ts,.jsx,.tsx --max-warnings=0",
"start": "parcel",
"updateSdkVersion": "./scripts/updateSdkVersion.sh"
},
Expand Down
86 changes: 72 additions & 14 deletions packages/game-bridge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as passport from '@imtbl/passport';
import * as config from '@imtbl/config';
import * as provider from '@imtbl/x-provider';
import { track, identify } from '@imtbl/metrics';
import { gameBridgeVersionCheck } from '@imtbl/version-check';

/* eslint-disable no-undef */
Expand All @@ -13,6 +14,8 @@ const keyFunctionName = 'fxName';
const keyRequestId = 'requestId';
const keyData = 'data';

const moduleName = 'game_bridge';

// version check placeholders
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const sdkVersionTag = '__SDK_VERSION__';
Expand Down Expand Up @@ -58,10 +61,10 @@ let zkEvmProviderInstance: passport.Provider | null;

declare global {
interface Window {
callFunction: (jsonData: string) => void,
ue: any,
callFunction: (jsonData: string) => void;
ue: any;
// eslint-disable-next-line @typescript-eslint/naming-convention
Unity: any,
Unity: any;
}
}

Expand All @@ -87,11 +90,15 @@ const callbackToGame = (data: object) => {
} else if (window.Unity !== 'undefined') {
window.Unity.call(message);
} else {
console.error('No available game callbacks to call from ImmutableSDK game-bridge');
console.error(
'No available game callbacks to call from ImmutableSDK game-bridge',
);
}
};

const setProvider = (passportProvider: provider.IMXProvider | null): boolean => {
const setProvider = (
passportProvider: provider.IMXProvider | null,
): boolean => {
if (passportProvider !== null && passportProvider !== undefined) {
providerInstance = passportProvider;
console.log('IMX provider set');
Expand Down Expand Up @@ -125,7 +132,12 @@ const getZkEvmProvider = (): passport.Provider => {
return zkEvmProviderInstance;
};

window.callFunction = async (jsonData: string) => { // eslint-disable-line no-unused-vars
track(moduleName, 'load_game_bridge', {
sdkVersionTag,
});

window.callFunction = async (jsonData: string) => {
// eslint-disable-line no-unused-vars
console.log(`Call function ${jsonData}`);

let fxName = null;
Expand All @@ -149,11 +161,12 @@ window.callFunction = async (jsonData: string) => { // eslint-disable-line no-un
clientId: request.clientId,
audience,
scope,
redirectUri: (redirect ?? redirectUri),
redirectUri: redirect ?? redirectUri,
logoutRedirectUri: request?.logoutRedirectUri,
crossSdkBridgeEnabled: true,
};
passportClient = new passport.Passport(passportConfig);
track(moduleName, 'init_inititalise_passport');
}
callbackToGame({
responseFor: fxName,
Expand All @@ -173,6 +186,7 @@ window.callFunction = async (jsonData: string) => { // eslint-disable-line no-un
};
console.log(`Version check: ${JSON.stringify(versionCheckParams)}`);

track(moduleName, 'complete_init_game_bridge', versionCheckParams);
gameBridgeVersionCheck(versionCheckParams);
break;
}
Expand All @@ -190,7 +204,16 @@ window.callFunction = async (jsonData: string) => { // eslint-disable-line no-un
break;
}
case PASSPORT_FUNCTIONS.relogin: {
const userInfo = await passportClient?.login({ useCachedSession: true });
const userInfo = await passportClient?.login({
useCachedSession: true,
});
const succeeded = userInfo !== null;
if (succeeded) {
identify({ passportId: userInfo?.sub });
}
track(moduleName, 'performed_relogin', {
succeeded,
});
callbackToGame({
responseFor: fxName,
requestId,
Expand All @@ -201,11 +224,17 @@ window.callFunction = async (jsonData: string) => { // eslint-disable-line no-un
}
case PASSPORT_FUNCTIONS.reconnect: {
let providerSet = false;
const userInfo = await passportClient?.login({ useCachedSession: true });
const userInfo = await passportClient?.login({
useCachedSession: true,
});
if (userInfo) {
const passportProvider = await passportClient?.connectImx();
providerSet = setProvider(passportProvider);
identify({ passportId: userInfo?.sub });
}
track(moduleName, 'performed_reconnect', {
succeeded: userInfo !== null,
});
callbackToGame({
responseFor: fxName,
requestId,
Expand All @@ -226,7 +255,12 @@ window.callFunction = async (jsonData: string) => { // eslint-disable-line no-un
}
case PASSPORT_FUNCTIONS.loginPKCE: {
const request = JSON.parse(data);
await passportClient?.loginWithPKCEFlowCallback(request.authorizationCode, request.state);
const profile = await passportClient?.loginWithPKCEFlowCallback(
request.authorizationCode,
request.state,
);
identify({ passportId: profile.sub });
track(moduleName, 'performed_login_pkce');
callbackToGame({
responseFor: fxName,
requestId,
Expand All @@ -236,9 +270,18 @@ window.callFunction = async (jsonData: string) => { // eslint-disable-line no-un
}
case PASSPORT_FUNCTIONS.connectPKCE: {
const request = JSON.parse(data);
await passportClient?.loginWithPKCEFlowCallback(request.authorizationCode, request.state);
const profile = await passportClient?.loginWithPKCEFlowCallback(
request.authorizationCode,
request.state,
);
const passportProvider = await passportClient?.connectImx();
const providerSet = setProvider(passportProvider);
if (providerSet) {
identify({ passportId: profile.sub });
}
track(moduleName, 'performed_connect_pkce', {
succeeded: providerSet,
});
callbackToGame({
responseFor: fxName,
requestId,
Expand All @@ -249,11 +292,15 @@ window.callFunction = async (jsonData: string) => { // eslint-disable-line no-un
}
case PASSPORT_FUNCTIONS.loginConfirmCode: {
const request = JSON.parse(data);
await passportClient?.loginWithDeviceFlowCallback(
const profile = await passportClient?.loginWithDeviceFlowCallback(
request.deviceCode,
request.interval,
request.timeoutMs ?? null,
);

identify({ passportId: profile.sub });
track(moduleName, 'performed_login_confirm_code');

callbackToGame({
responseFor: fxName,
requestId,
Expand All @@ -263,13 +310,22 @@ window.callFunction = async (jsonData: string) => { // eslint-disable-line no-un
}
case PASSPORT_FUNCTIONS.connectConfirmCode: {
const request = JSON.parse(data);
await passportClient?.loginWithDeviceFlowCallback(
const profile = await passportClient?.loginWithDeviceFlowCallback(
request.deviceCode,
request.interval,
request.timeoutMs ?? null,
);

const passportProvider = await passportClient?.connectImx();
const providerSet = setProvider(passportProvider);

if (providerSet) {
identify({ passportId: profile.sub });
}
track(moduleName, 'performed_connect_confirm_code', {
succeeded: providerSet,
});

callbackToGame({
responseFor: fxName,
requestId,
Expand Down Expand Up @@ -378,7 +434,9 @@ window.callFunction = async (jsonData: string) => { // eslint-disable-line no-un
}
case PASSPORT_FUNCTIONS.imx.batchNftTransfer: {
const nftTransferDetails = JSON.parse(data);
const response = await getProvider().batchNftTransfer(nftTransferDetails);
const response = await getProvider().batchNftTransfer(
nftTransferDetails,
);
callbackToGame({
...{
responseFor: fxName,
Expand Down
Loading

0 comments on commit a784cce

Please sign in to comment.