import { CXF } from '../typings/cxf'; import { IDictionary, DIReader } from './common'; import { timeout } from './utils'; import { ArgumentNullError } from './ArgumentNullError'; import { inject } from 'readuz'; export function formatQueryString(obj: IDictionary) { return Object .entries(obj) .map(([key, value]) => `${key}=${encodeURIComponent(value)}`) .join('&'); } export const getPingAndLanguage = (cxf: CXF) => { return { zoneId: cxf.zoneId, playerId: cxf.playerId, instanceId: cxf.instanceId, networkId: cxf.networkId, gameId: cxf.gameId, language: cxf.language, locale: cxf.language, }; }; export interface IProvider { get: () => T | undefined; set: (value: T) => void; } export const entityProvider = (initial?: T): IProvider => { let element = initial; return { get: () => element, set: value => { element = value; }, }; }; export type TryCatch = { (fn: () => R): () => R; (fn: (p1: P1) => R): (p1: P1) => R; (fn: (p1: P1, p2: P2) => R): (p1: P1, p2: P2) => R; (fn: (p1: P1, p2: P2, p3: P3) => R): (p1: P1, p2: P2, p3: P3) => R; }; export const tryCatch: DIReader = inject( env => env.logError, () => (fn: any) => (...args: any[]) => { try { fn(...args); } catch (e) { if (isArgumentNullError(e)) { logError(e); } } }, ); export const isArgumentNullError = (e: Error): e is ArgumentNullError => e.name === 'ArgumentNullError'; export type LoadCxf = (...args: any[]) => Promise; // tslint:disable-next-line:no-var-requires export const loadCxf = timeout(() => require('@goodgamestudios/cxf-ready') as Promise, 10000); export type Log = (...args: any[]) => void; export const log: Log = (...args: any[]) => // tslint:disable-next-line:no-console console.log('%c TRACKING-CXF -> ', 'background: #222; color: #bada55', ...args); export type LogError = (...args: any[]) => void; export const logError: LogError = (...args: any[]) => // tslint:disable-next-line:no-console console.error('%c TRACKING-CXF -> ', 'background: #ff0000; color: #fff', ...args);